งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

O “take what you can get now” o make a decision that appears to be good (close to optimal solution) o proceed by searching a sequence of choices iteratively.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "O “take what you can get now” o make a decision that appears to be good (close to optimal solution) o proceed by searching a sequence of choices iteratively."— ใบสำเนางานนำเสนอ:

1 o “take what you can get now” o make a decision that appears to be good (close to optimal solution) o proceed by searching a sequence of choices iteratively to decide the (seem to be) best solution 1/93 Greedy Algorithms หมายถึง เป็นอัลกอริทึมที่จะหาคาตอบโดยการเลือกทางออก ที่ดีที่สุดที่พบได้ในขณะนั้นเพื่อให้ได้คาตอบที่ดีที่สุด แต่ในบางครั้ง Greedy Algorithms อาจจะไม่สามารถหาคาตอบของปัญหาที่ดีที่สุดได้เสมอไป

2 ตัวอย่างวิธีคิดแบบ Greedy o Coin Changing o Fractional Knapsack o Bin Packing o Task Scheduling 2/93

3 ตัวอย่างวิธีคิดแบบ Greedy o Prim’s o Kruskal’s o Dijkstra’s o Huffman Code 3/93 graph tree

4 Coin Changing สมมุติว่าเรามีเหรียญขนาดดังต่อไปนี้เหรียญ 10 บาท, เหรียญ 5 บาท, และเหรียญ 1 บาทและสมมุติว่าเราต้องการแลกเงิน 89 บาท เราจะได้เงินเหรียญดังนี้ 10 บาท 8 เหรียญ, 5 บาท 1 เหรียญ, 1 บาท 4 เหรียญ จะเห็นว่าอัลกอริทึมที่เราใช้ก็คือ เราเลือก เหรียญที่มีค่ามากที่สุด แต่ไม่มากกว่า 89 บาท ออกมาก่อน ( เหรียญ 10 บาท 8 เหรียญ ) จากนั้นลบค่านี้ออกจาก 89 บาท ก็ จะเหลือ 9 บาท หลังจากนั้นเราเลือกเหรียญ ที่มีค่ามากที่สุดแต่ไม่เกิน 9 บาท นั่นก็คือได้ ( เหรียญ 5 บาท 1 เหรียญ ) แล้วลบค่านี้ออก จาก 9 บาท จะเหลืออยู่อีก 4 บาท และใน ที่สุดเราก็จะได้ ( เหรียญ 1 บาทอีก 4 เหรียญ ) 4/93

5 Fractional Knapsack o มีสิ่งของ n ประเภทซึ่งแต่ละประเภท (i) กำหนดให้มีจำนวน x i ชิ้น มีค่า ความสำคัญ b i และมีน้ำหนัก w i o ต้องการหาจำนวนสิ่งของแต่ละ ประเภทที่บรรจุลงในเป้ที่รับน้ำหนัก ได้ไม่เกิน W กิโลกรัม o ให้เลือกหยิบสิ่งของทีละชิ้นที่มีค่า ดัชนี (v i =b i /w i ) สูงสุดและทำให้ น้ำหนักรวมไม่เกิน W ก่อน 5/93

6 o ตัวอย่างเช่น มีของ 4 ประเภทคือ o หนังสือ 4 เล่ม มี b 1 =10 และ w 1 =0.6 (v 1 =16.7) o ขนม 2 กล่อง มี b 2 =7 และ w 2 =0.4 (v 2 =17.5) o น้ำ 2 ขวด มี b 3 =5 และ w 3 =0.5 (v 3 =10) o ซีดีเพลง 8 แผ่น มี b 4 =3 และ w 4 =0.2 (v 4 =15) o เป้รับน้ำหนักได้ไม่เกิน 4 กิโลกรัม o เลือก ขนม 2 กล่อง หนังสือ 4 เล่ม และ ซีดีเพลง 4 แผ่น o ได้ค่าความสำคัญสูงสุดและน้ำหนักไม่เกิน 4 กิโลกรัม 6/93

7 o Bin Packing o given N items of sizes s 1, s 2, …, s N ; o while 0 < s i < 1 o find a solution to pack these items in the fewest number of bins o 2 algor. versions : o on-line : an item must be placed in a bin before the next item is read o off-line : all item list are read in a bin 7/93

8 Optimal Bin Packing solution given an item list with sizes : 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 8/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee 0.8 0.2 0.3 0.7 0.5 0.4 0.1 Bin 1 Bin 2 Bin 3

9 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] 9/93 0.5 0.2 empty 0.4 empty 0.7 0.1 Bin 5 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5 empty 0.3 empty 0.8 344-211 Algorithmic Process and Programming, created by Dararat Saelee

10 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] 10/93 0.5 0.2 0.3 0.4 empty 0.7 empty Bin 1 Bin 2 Bin 3 Bin 4 empty 0.8 empty 0.1 344-211 Algorithmic Process and Programming, created by Dararat Saelee

11 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] 11/93 0.3 0.7 Bin 1 Bin 2 Bin 3 Bin 4 0.5 0.2 empty 0.1 empty 0.4 empty 0.8 344-211 Algorithmic Process and Programming, created by Dararat Saelee

12 Task Scheduling o กำหนดงาน n งานด้วย ( เวลาเริ่ม, เวลาเสร็จ ) โดยให้ทำงานทีละงาน บนเครื่อง และจะเอางานใดมาทำบน เครื่องเดียวกันได้ถ้าเวลางานไม่ทับ ซ้อนกัน ทั้งนี้ต้องใช้จำนวนเครื่องใน การทำงานทั้งหมดน้อยที่สุด o ตัวอย่างงาน (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) 12/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

13 Job Scheduling (Uniprocessor) 13/93 Job Time j 1 15 j 2 8 j 3 3 j 4 10 j1j1 j2j2 j3j3 j4j4 0 15 23 26 36 j3j3 j2j2 j4j4 j1j1 0 3 11 21 36 First-come-First-serve : avg. completion time = 25 avg. waiting time = 16 Shortest Job First : avg. completion time = 17.75 avg. waiting time = 8.75 344-211 Algorithmic Process and Programming, created by Dararat Saelee

14 Job Scheduling (Multiprocessor) 14/93 Job Time j 1 3 j 2 5 j 3 6 j 4 10 j 5 11 j 6 14 j 7 15 j 8 18 j 9 20 j1j1 j2j2 j3j3 j4j4 j5j5 j6j6 j7j7 j8j8 j9j9 0 5 16 34 FCFS 0 6 20 40 0 3 13 28 344-211 Algorithmic Process and Programming, created by Dararat Saelee

15 o divide : break a given problem into subproblems o recur : try to solve each in recursive way o conquer : derive the final solution from all solutions 15/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

16 16/93 Problem of size n Subproblem 1 of size n/m Subproblem 2 of size n/m 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

17 o Merge sort o Quick sort o Binary Tree Traversal o Closest-Pair and Convex-Hall o Selection problem 17/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

18 o Factorial o Fibonacci o Binary search o Strassen’s Matrix Multiplication o Big Integer Multiplication 18/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

19 Factorial o n! = n * (n-1)! o (n-1)! = (n-1) * (n-2)! o … o 1! = 1 o 0! = 1 19/93 ตัวอย่าง 4! = ? 4! = 4 * 3! 4! = 4 * 3! 3! = 3 * 2! 3! = 3 * 2! 2! = 2 * 1! 2! = 2 * 1! 1! = 1 * 0! 1! = 1 * 0! 0! = 1 0! = 1 Describe with Tree structure 344-211 Algorithmic Process and Programming, created by Dararat Saelee

20 Fibonacci num.: 1,1,2,3,5,8,13,… o fibo (n) = fibo (n-1) + fibo (n- 2) o fibo (n-1) = fibo (n-2) + fibo (n-3) o fibo (n-2) = fibo (n-3) + fibo (n-4) o … o fibo (3) = fibo (2) + fibo (1) o fibo (2) = 1 o fibo (1) = 1 20/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

21 Binary search 21/93 12 15 18 23 26 37 39 41 43 48 mid Search = 37 344-211 Algorithmic Process and Programming, created by Dararat Saelee

22 Strassen’s Matrix Multiplication o Z = X * Y ; matrix n x n o o I = A x E + B x G, J = A x F + B x H o K = C x E + D x G, L = C x F + D x H 22/93 I J K L A B C D E F G H = x 344-211 Algorithmic Process and Programming, created by Dararat Saelee

23 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 23/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

24 o Binomial Coefficient o Warshall’s & Floyd’s  Directed Graph o Optimal Binary Search tree  Tree o Ordering Matrix Multiplication or Matrix Chain-Product (ABCD  A(BC)D, (AB)(CD)) o All-pair Shortest Path  Directed Graph 24/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

25 o Solving each of smaller subproblems only once and recording the results in a table from which can obtain a solution to the original problem o Using a table instead of recursion o Factorial o Fibonacci numbers 25/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

26 0-1 Knapsack problem o 0-1 : reject or accept o given n items of weights w 1, w 2,…, w n and values v 1, v 2,…, v n and a knapsack of capacity W o find the best solution that gives maximum weight and value 26/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

27 o use table to fill in by applying formulas B[k-1,w], if w k > w B [k,w] = max{B[k- 1,w],B[k-1,w-w k ]+b k }, else o e.g. let W = 5 and data : (item,weight,value)  (1,2,12) (2,1,10) (3,3,20) (4,2,15) 27/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

28 capacity j (W=5) weight, value i 1 2 3 4 5 w 1 =2, v 1 =12 1 0 12 12 12 12 w 2 =1, v 2 =10 2 10 12 22 22 22 w 3 =3, v 3 =20 3 10 12 22 30 32 w 4 =2, v 4 =15 4 10 15 25 30 37 28/93 Select 1, 2, 4 344-211 Algorithmic Process and Programming, created by Dararat Saelee

29 Ordering Matrix Multiplication or Matrix Chain-Product o find the best solution that gives minimum times of multiplication o e.g. ABCD, A(BC)D, (AB)(CD) 29/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

30 30/91

31 o a random number is used to make a decision in some situation o e.g. giving a quiz by using a coin o a good randomized algor. has no bad inputs and no relative to the particular input, e.g. data testing 31/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

32 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 x i+1 = Ax i % M, given seed (x 0 ) value 32/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

33 Skip Lists (in searching & insertion) o e.g. use random number generator to determine which node to be traversed within the expected time Primality Testing o 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 33/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

34 o there are many possibilities to try, if not succeed then step back to try another way o the elimination of a large group of possibilities in one step is known as pruning o e.g. arranging furniture in a new house, never place sofa, bed & closet in the kitchen 34/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

35 o n-Queens o Hamiltonian Circuit o Subset-Sum o Goal Seeking o Turnpike Reconstruction 35/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

36 o Games : chess n-Queens checker Tic-Tac-Toe o Minimax strategy 36/93 344-211 Algorithmic Process and Programming, created by Dararat Saelee

37 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. 37/91

38 Fractional Knapsack : W= 5 หนังสือ 4 เล่ม มี b 1 =10 และ w 1 =0.5 ขนม 3 กล่อง มี b 2 =5 และ w 2 =0.4 น้ำ 2 ขวด มี b 3 =7 และ w 3 =0.5 โน้ตบุ้ค 1 เครื่อง มี b 4 =15 และ w 4 =1.8 Bin Packing : 0.25, 0.5, 1.0, 0.75, 0.125, 0.25, 0.5 38/93


ดาวน์โหลด ppt O “take what you can get now” o make a decision that appears to be good (close to optimal solution) o proceed by searching a sequence of choices iteratively.

งานนำเสนอที่คล้ายกัน


Ads by Google