9. การออกแบบขั้นตอนวิธี Algorithm Design Algorithm Design Techniques Practice Problems 344-211 Algorithmic Process and Programming , created by Dararat Saelee
ลักษณะของขั้นตอนวิธีที่ดี แก้ปัญหาได้อย่างมีประสิทธิภาพ ให้คำตอบที่ถูกต้อง ใช้เวลาในการปฏิบัติการน้อย ชัดเจนและกะทัดรัด
Algorithm Design definition requirement strategy techniques
Algorithm Design Definition Algorithm Definition : a method to solve a problem described as a sequence of steps to be performed in a specific logical order Several algorithms for solving the same problem may exist , based on very different ideas , performance (efficiency , cost and speed) Algorithm หมายถึง กรรมวิธีที่มีขั้นตอนแบบทีละขั้นอย่างชัดเจนในการแก้ปัญหาใดปัญหาหนึ่ง เพื่อให้ได้คำตอบที่ถูกต้องสำหรับทุกรูปแบบข้อมูล ภายในเวลาและทรัพยากรณ์ที่จำกัด
Algorithm Design Requirement Satisfied Requirements : unambiguousness generality correctness finiteness From Uckan p. 27
Satisfied Requirements unambiguousness easier to understand and to program , so contain fewer bugs. sometimes simpler algorithm are more efficient than more complicated algorithm.
Satisfied Requirements generality easier to design an algorithm in more general terms. handle a range of input that is natural for the problem.
Satisfied Requirements correctness must be proved by 1) Mathematical Induction. 2) Testing with sample input data that possibly prove the algorithm failure or give wrong answer.
Satisfied Requirements finiteness–must concern about 1) execution in finite steps. 2) termination in finite time.
Algorithm Design Strategy Computational Device Solving Decision Data Structure Efficiency
Algorithm Design Strategy Computational Device Von Neumann sequential algorithm Multiprocessor parallel algorithm network algorithm distributed algorithm
Algorithm Design Strategy Solving Decision choose between solving the problem with approximation or exact algorithm approximation algorithm - square roots, integrals - shortest path
Algorithm Design Strategy Solving Decision (cont.) choose to solve the problem with non-recursive or recursive algorithm recursion is easy to program, but uses a large number of function calls that affect to execution efficiency.
Algorithm Design Strategy Data Structure to solve problems easier, we need to use appropriate data structure. - student id : int or string ? - matrix 10x5 : array 2D or 50 var.? - graph, tree : array or linked list ?
Algorithm Design Strategy Efficiency time : how fast the algor. runs space : how much extra memory the algor. needs worst / best / average case - sequential search : n / 1 / n/2
Algorithm Design Strategy Efficiency (cont.) Order of Growth for Input size - when input size is large, how is the run time ? - order of growth : O (big oh) - input size : n
Algorithm Design Strategy Efficiency (cont.) - O(n2) : n = 10 running time = 100 n = 100 running time = 10,000 - O(2n) : n = 10 running time = 1,024 - O(log2n) : n = 10 running time = 3.3 n = 100 running time = 6.6
Algorithm Design Techniques To provide guidance for designing algorithms for new problems To make it possible to classify algorithms according to design idea
Algorithm Design Techniques No any general technique can solve all problems e.g. Unsorted data cannot use with Binary search algorithm
Algorithm Design Techniques An algorithm design technique (or strategy or paradigm) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.
Algorithm Design Techniques Greedy Method Divide and Conquer Decrease and Conquer / Prune- and-Search Transform and Conquer From Goodrich
Algorithm Design Techniques Dynamic Programming Randomized Algorithms Backtracking Algorithms
Motto Today เพราะแสวงหา มิใช่เพราะรอคอย เพราะเชี่ยวชาญ มิใช่เพราะโอกาส เพราะแสวงหา มิใช่เพราะรอคอย เพราะเชี่ยวชาญ มิใช่เพราะโอกาส เพราะสามารถ มิใช่เพราะโชคช่วย 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Greedy Method “take what you can get now” make a decision that appears to be good (close to optimal solution) proceed by searching a sequence of choices iteratively to decide the (seem to be) best solution Greedy Algorithms หมายถึง เป็นอัลกอริทึมที่จะหาคาตอบโดยการเลือกทางออกที่ดีที่สุดที่พบได้ในขณะนั้นเพื่อให้ได้คาตอบที่ดีที่สุด แต่ในบางครั้ง Greedy Algorithms อาจจะไม่สามารถหาคาตอบของปัญหาที่ดีที่สุดได้เสมอไป
Greedy Method Coin Changing Fractional Knapsack Bin Packing Task Scheduling
Greedy Method Prim’s Kruskal’s Dijkstra’s Huffman Code graph tree
Greedy Method Coin Changing สมมุติว่าเรามีเหรียญขนาดดังต่อไปนี้เหรียญ 10 บาท, เหรียญ 5 บาท, และเหรียญ 1 บาทและสมมุติว่าเราต้องการแลกเงิน 89 บาท เราจะได้เงินเหรียญดังนี้ 10 บาท 8 เหรียญ, 5 บาท 1 เหรียญ, 1 บาท 4 เหรียญจะเห็นว่าอัลกอริทึมที่เราใช้ก็คือ เรา เลือกเหรียญที่มีค่ามากที่สุด แต่ไม่มากกว่า 89 บาท ออกมาก่อน (เหรียญ 10 บาท 8 เหรียญ ) จากนั้นลบค่านี้ออกจาก 89 บาท ก็ จะเหลือ 9 บาท หลังจากนั้นเราเลือกเหรียญที่มีค่ามากที่สุดแต่ไม่ เกิน 9 บาท นั่นก็คือได้(เหรียญ 5 บาท 1 เหรียญ) แล้วลบค่านี้ ออกจาก 9 บาท จะเหลืออยู่อีก 4 บาท และในที่สุดเราก็จะได้ (เหรียญ 1 บาทอีก 4 เหรียญ)
Greedy Method Fractional Knapsack มีสิ่งของ n ประเภทซึ่งแต่ละประเภท(i) กำหนดให้มี จำนวน xi ชิ้น มีค่าความสำคัญ bi และมีน้ำหนัก wi ต้องการหาจำนวนสิ่งของแต่ละประเภทที่บรรจุลงใน เป้ที่รับน้ำหนักได้ไม่เกิน W กิโลกรัม ให้เลือกหยิบสิ่งของทีละชิ้นที่มีค่าดัชนี (vi=bi/wi) สูงสุดและทำให้น้ำหนักรวมไม่เกิน W ก่อน
Greedy Method ตัวอย่างเช่น มีของ 4 ประเภทคือ หนังสือ 4 เล่ม มี b1=10 และ w1=0.6 (v1=16.7) ขนม 2 กล่อง มี b2=7 และ w2=0.4 (v2=17.5) น้ำ 2 ขวด มี b3=5 และ w3=0.5 (v3=10) ซีดีเพลง 8 แผ่น มี b4=3 และ w4=0.2 (v4=15) เป้รับน้ำหนักได้ไม่เกิน 4 กิโลกรัม เลือก ขนม 2 กล่อง หนังสือ 4 เล่ม และซีดีเพลง 4 แผ่น ได้ค่าความสำคัญสูงสุดและน้ำหนักไม่เกิน 4 กิโลกรัม
Greedy Method Bin Packing given N items of sizes s1 , s2 , …, sN; while 0 < si < 1 find a solution to pack these items in the fewest number of bins 2 algor. versions : on-line : an item must be placed in a bin before the next item is read off-line : all item list are read in a bin
Optimal Bin Packing solution given an item list with sizes : 0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8 0.8 0.3 0.5 0.7 0.1 0.4 0.2 Bin 1 Bin 2 Bin 3 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy Next fit : fill items in a bin until the next item can’t fit , then insert a new bin (never look back) [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty empty empty empty 0.1 0.8 0.5 0.7 0.4 0.3 0.2 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy First fit : fill items in a bin , but if any first previous bin can fit the next item then we can fill in until no any bin can fit , then insert a new bin [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty empty empty 0.1 0.8 0.3 0.5 0.7 0.4 0.2 Bin 1 Bin 2 Bin 3 Bin 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy Best fit : fill items in a bin by trying to place the new item in the bin that left the smallest space [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty 0.3 empty 0.1 0.8 0.5 0.7 0.4 0.2 Bin 1 Bin 2 Bin 3 Bin 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Class Exercise ร้านขนมเค้กแห่งหนึ่งรับทำขนมเค้กตามคำสั่งของลูกค้า โดยการสั่งขนมเค้กอาจเป็น 1 ก้อน ½ ก้อน ¼ ก้อน และขนมเค้กที่ลูกค้าได้รับต้องเป็นขนาดก้อนที่ไม่มีรอย แบ่งภายในก้อน ทั้งนี้ในการทำขนมเค้ก ทางร้านไม่ต้องการให้เหลือขนมเค้กมากจึง ต้องคำนึงถึงจำนวนก้อนขนมเค้กที่น้อยที่สุดที่ต้องทำ ตัวอย่าง ถ้าหากลูกค้า 5 คน มีคำสั่ง 1 ½ ¼ ½ ¼ ดังนั้นทางร้าน ต้องทำเค้ก 3 ก้อน จงเขียนโปรแกรมเพื่ออ่านข้อมูลคำสั่งของลูกค้าจากแฟ้มและหาว่าต้องทำเค้กอย่าง น้อยที่สุดกี่ก้อน 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Task Scheduling Greedy Method ตัวอย่างงาน (1,3) , (1,4) , (2,5) , (3,7) , (4,7) , (6,9) , (7,8) จะได้ผลดังนี้ เครื่องที่ 1 : (1,3) , (3,7) , (7,8) เครื่องที่ 2 : (1,4) , (4,7) และเครื่องที่ 3 : (2,5) , (6,9) 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Uniprocessor) Greedy Method Job Scheduling (Uniprocessor) j1 j2 j3 j4 Job Time j1 15 j2 8 j3 3 j4 10 0 15 23 26 36 First-come-First-serve : avg. completion time = 25 avg. waiting time = 16 j3 j2 j4 j1 0 3 11 21 36 Shortest Job First : avg. completion time = 17.75 avg. waiting time = 8.75 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Multiprocessor) Greedy Method Job Scheduling (Multiprocessor) FCFS Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j1 j4 j7 0 3 13 28 j2 j5 j8 0 5 16 34 j3 j6 j9 0 6 20 40 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Multiprocessor) Greedy Method Job Scheduling (Multiprocessor) Optimal #1 Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j1 j6 j7 0 3 17 32 j2 j5 j8 0 5 16 34 j3 j4 j9 0 6 16 36 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Multiprocessor) Greedy Method Job Scheduling (Multiprocessor) Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j2 j5 j8 0 5 16 34 j6 j9 0 14 34 j1 j3 j4 j7 0 3 9 19 34 Optimal #2 – minimize completion time 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Divide and Conquer divide : break a given problem into subproblems recur : try to solve each in recursive way conquer : derive the final solution from all solutions 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Divide and Conquer … Problem of size n Subproblem m of size n/m Solution to Subproblem 1 Solution to Subproblem 2 Solution to Subproblem m Solution to the original problem 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Divide and Conquer Quick sort Binary Tree Traversal Merge sort Quick sort Binary Tree Traversal Closest-Pair and Convex-Hall Selection problem 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Divide and Conquer Fibonacci Binary search Factorial Fibonacci Binary search Strassen’s Matrix Multiplication Big Integer Multiplication From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Factorial Divide and Conquer n! = n * (n-1)! (n-1)! = (n-1) * (n-2)! … 1! = 1 0! = 1 ตัวอย่าง 4! = ? 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1 From Goodrich Describe with Tree structure 344-211 Algorithmic Process and Programming , created by Dararat Saelee
fibo (n) = fibo (n-1) + fibo (n-2) Divide and Conquer Fibonacci num.: 1,1,2,3,5,8,13,… fibo (n) = fibo (n-1) + fibo (n-2) fibo (n-1) = fibo (n-2) + fibo (n-3) fibo (n-2) = fibo (n-3) + fibo (n-4) … fibo (3) = fibo (2) + fibo (1) fibo (2) = 1 fibo (1) = 1 From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Divide and Conquer Binary search 12 15 18 23 26 37 39 41 43 48 12 15 18 23 26 37 39 41 43 48 mid mid From Goodrich mid 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Strassen’s Matrix Multiplication Divide and Conquer Strassen’s Matrix Multiplication Z = X * Y ; matrix n x n I = AxE + BxG , J = AxF + BxH K = CxE + DxG , L = CxF + DxH I J K L A B C D E F G H = x From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Big Integer Multiplication Divide and Conquer Big Integer Multiplication multiply 2 N-digit numbers : X , Y XY = XLYL10N + (XLYR + XRYL)10N/2 + XRYR XLYR+XRYL = (XL-XR)(YR-YL) + XLYL + XRYR require : 2 subtraction , 3 multiplication D1 = XL-XR , D2 = YR-YL XLYL , XRYR , D1D2 D3 = D1D2 + XLYL + XRYR From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Big Integer Multiplication Divide and Conquer Big Integer Multiplication X = 61,438,521 & Y = 94,736,407 XL = 6143 , XR = 8521 YL = 9473 , YR = 6407 D1 = XL-XR = -2378, D2 = YR-YL = -3066 XLYL = 58192639 , XRYR = 54594047 D1D2 = 7290948 D3 = D1D2 + XLYL + XRYR = 120077634 XY = XLYL108 + D3104 + XRYR From Goodrich XY = 5820464730934047 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Motto Today ในการอยู่ร่วมกันกับผู้อื่น ควร ในการอยู่ร่วมกันกับผู้อื่น ควร มองโลกในแง่ดี และ คิดในทางสร้างสรร มีน้ำใจ เอาใจเขามาใส่ใจเรา และรู้จักให้อภัย 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease and Conquer based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem. it can be exploited either top down (recursively) or bottom up (without a recursion) . 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease and Conquer 3 major variations : decrease-by-a-constant decrease-by-a-constant-factor variable-size-decrease From Anany 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease and Conquer decrease-by-a-constant the size of instance is reduced by the same constant on each iteration decrease-by-a-constant-factor the size of instance is reduced by the same constant factor on each iteration
Decrease-by-a-constant Insertion sort Depth-First search and Breadth-First search (graph) Topological sorting (graph) Generating Combinatorial Objects 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease-by-a-constant Insertion sort use the decrease-by-one sorted-side unsorted-side the size of unsorted data is reduced by 1 on each loop From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Insertion Sort 43 22 80 17 36 16 29 22 43 80 17 36 16 29 17 22 43 80 36 16 29 17 22 36 43 80 16 29 16 17 22 36 43 80 29 16 17 22 29 36 43 80 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease-by-a-constant-factor Jasephus Problem Fake-Coin problem Multiplication à la Russe From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease-by-a-constant-factor Josephus problem to determine the survivor by eliminating every second person (stand in circle) until only one survivor is left e.g. J(6) = 5 , J(7) = 7 , J(9) = 3 use the decrease-by-half (=2) 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Josephus problem use the decrease-by-half (=2) can consider J(n) if n is even (n=2k) , J(2k) = 2 J(k) -1 if n is odd (n=2k+1) , J(2k+1) = 2 J(k) +1 J(6) = 2 J(3) -1 , J(3) = 2 J(1) + 1 , J(1) = 1 J(3) = 3 J(6) = 5 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Josephus problem use the decrease-by-half (=2) can be obtained by rotate left 1 bit J(6) = J(1102) 1012 = 5 J(7) = J(1112) 1112 = 7 J(9) = J(10012) 00112 = 3 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Josephus problem use the decrease-by-3 (=3) eliminate every 3 person 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease and Conquer 3 major variations : decrease-by-a-constant decrease-by-a-constant-factor variable-size-decrease a size reduction pattern varies from one iteration to another 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Variable-size-decrease Euclid algor. Computing a median and the selection problem Interpolation search Binary search tree 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Decrease and Conquer Euclidean algor.: finding gcd(a,b) recursively a ,if b=0 gcd(a,b) = b ,if a=0 gcd (b,a%b) ,otherwise e.g. gcd(124,40) = gcd(40,4) = 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer Problem’s instance solution Change to Another problem instance Simpler instance Change representation solution 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer Horner’s Rule Presorting Gaussian Elimination Balanced Search tree Heap sort (tree) Problem Reduction 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer Horner’s rules p(x) = anxn + an-1xn-1 + … + a1x1 + a0 use the representation change technique p(x) = (…(anx + an-1)x +…)x + a e.g. p(x) = 2x4 – x3 + 3x2 + x – 5 = x(x(x(2x–1) +3) +1) – 5 From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer Problem Reduction reduce the problem to another problem that solving algor. is known Problem1 Problem2 Solution Problem : counting paths in a graph , linear programming , the Least Common Multiple (lcm) From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer 24 = 2 2 2 3 60 = 2 2 3 5 lcm(11,5) = 55 the Least Common Multiple (lcm) lcm(24,60) = 2 2 3 2 5 = 120 24 = 2 2 2 3 60 = 2 2 3 5 lcm(11,5) = 55 From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Transform and Conquer the Least Common Multiple (lcm) lcm (m,n) = lcm(24,60) = 120 gcd(24,60) = 12 lcm(11,5) = 55 gcd(11,5) = 1 From Goodrich 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Dynamic Programming Characterizing subproblems using a small set of integer indices – to allow an optimal solution to a subproblem to be defined by the combination of solutions to even smaller subproblems 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Dynamic Programming Binomial Coefficient Warshall’s & Floyd’s Directed Graph Optimal Binary Search tree Tree Ordering Matrix Multiplication or Matrix Chain- Product (ABCD A(BC)D , (AB)(CD)) All-pair Shortest Path Directed Graph ABCD, all-pair from Mark p. 386 Matrix chain from goodrich p.274, anany p.294 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Dynamic Programming Solving each of smaller subproblems only once and recording the results in a table from which can obtain a solution to the original problem Using a table instead of recursion Factorial Fibonacci numbers 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Dynamic Programming 0-1 Knapsack problem 0-1 : reject or accept given n items of weights w1 , w2 ,…, wn and values v1 , v2 ,…, vn and a knapsack of capacity W find the best solution that gives maximum weight and value p.278 from goodrich , p. 295 from anany 344-211 Algorithmic Process and Programming , created by Dararat Saelee
0-1 Knapsack problem use table to fill in by applying formulas B[k-1,w] , if wk > w B [k,w] = max{B[k-1,w],B[k-1,w-wk]+bk}, else e.g. let W = 5 and data : (item,weight,value) (1,2,12) (2,1,10) (3,3,20) (4,2,15) p.278 from goodrich , p. 295 from anany 344-211 Algorithmic Process and Programming , created by Dararat Saelee
0-1 Knapsack problem Select 1, 2, 4 capacity j (W=5) weight, value i 1 2 3 4 5 w1=2, v1=12 1 0 12 12 12 12 w2=1, v2=10 2 10 12 22 22 22 w3=3, v3=20 3 10 12 22 30 32 w4=2, v4=15 4 10 15 25 30 37 p.278 from goodrich , p. 295 from anany Select 1, 2, 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Dynamic Programming Ordering Matrix Multiplication or Matrix Chain-Product find the best solution that gives minimum times of multiplication e.g. ABCD , A(BC)D , (AB)(CD) p.278 from goodrich , p. 295 from anany 344-211 Algorithmic Process and Programming , created by Dararat Saelee
No ego Help each other then Everyone will succeed
Randomized Algorithms a random number is used to make a decision in some situation e.g. giving a quiz by using a coin a good randomized algor. has no bad inputs and no relative to the particular input , e.g. data testing 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Randomized Algorithms Random Number Generator a method to generate true randomness is impossible to do on computer since numbers , called pseudorandom numbers, will depend on the algorithm e.g. linear congruential generator xi+1 = Axi % M , given seed (x0) value 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Randomized Algorithms Skip Lists (in searching & insertion) e.g. use random number generator to determine which node to be traversed within the expected time Primality Testing e.g. to determine a large N-digit number is prime or not by factoring into N/2-digit primes, then a random generator is needed p. 470 test prime 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Backtracking Algorithms there are many possibilities to try, if not succeed then step back to try another way the elimination of a large group of possibilities in one step is known as pruning e.g. arranging furniture in a new house , never place sofa, bed & closet in the kitchen 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Backtracking Algorithms n-Queens Hamiltonian Circuit Subset-Sum Goal Seeking Turnpike Reconstruction Goal seeking from gilberg 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Backtracking Algorithms Games : chess n-Queens checker Tic-Tac-Toe Minimax strategy 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Practice Problems When confronted with a problem, it is worthwhile to see if any method of algor. design can apply properly together with data structure that will lead to efficient solution.
Practice Problems Bin packing Task scheduling Knapsack problem Bin packing Task scheduling Strassen’s matrix multiplication Big Integer multiplication Josephus problem an Greedy Divide &conquer Decrease&conquer 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Practice Problems Non-recursive : an, fibonacci Recursive exponentiation : an , Horner’s Rule LCM & GCD – Euclid Algor. 0-1 Knapsack Problem Dynamic programming transform 344-211 Algorithmic Process and Programming , created by Dararat Saelee
ทำเดี๋ยวนี้ คือหนทางสู่ความสำเร็จ คิดแต่ไม่ทำ เท่ากับไม่ได้คิด Motto Today ทำเดี๋ยวนี้ คือหนทางสู่ความสำเร็จ คิดแต่ไม่ทำ เท่ากับไม่ได้คิด 344-211 Algorithmic Process and Programming , created by Dararat Saelee
Class Exercises Fractional Knapsack : W= 5 หนังสือ 4 เล่ม มี b1=10 และ w1=0.5 ขนม 3 กล่อง มี b2=5 และ w2=0.4 น้ำ 2 ขวด มี b3=7 และ w3=0.5 โน้ตบุ้ค 1 เครื่อง มี b4=15 และ w4=1.8 Bin Packing : 0.25 , 0.5 , 1.0 , 0.75 , 0.125 , 0.25 , 0.5
Class Exercises Task Scheduling : act# start finish a1 1 4 , a2 2 5 , a3 1 5 , a4 5 8 a5 4 6 , a6 6 10 , a7 7 9 Big Integer Multiplication X = 4,123,450,732 Y = 8,159,324,570 an 1321 (decrease-by-2) Josephus Problem : J(82) decrease-by-3
Class Exercises Euclidean : gcd (2039,113) , gcd (1548,204) Horner’s Rule : p(x) = 7x9 + 4x6 - 3x5 - x4 + 5x2 + 9 Least Common Multiple : lcm (2039,113) , lcm (1548,204) 0-1 knapsack Problem : W = 6 , (1,2,12) (2,1,10) (3,4,20) (4,3,15) (5,2,14) (2039,113) gcd =1 , lcm = 2039*113 = 230407 (1548,204) gcd =12 , lcm = 1548*204/12 = 26316 0-1 select 2,4,5 (v=39)