Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.

Similar presentations


Presentation on theme: "Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang."— Presentation transcript:

1 Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang

2 Greedy Method Origin – Charles Dickens’ classic character ‘Ebenezer Scrooge’ He is the most greedy person ever He never considered the past or future He greedily grab as much as gold as he could “Ghost of Christmas Past” reminded him of the past “Ghost of Christmas Future” warned him of the future Finally, he changed his greedy ways Greedy method for algorithm design – It determines something, each time taking the one that is deemed “best” according to some criterion, without regard for the choices it has made before or will in the future. 2/20

3 Greedy Method Conceptually, “greedy method” is opposite to “dynamic programming (DP)” – DP requires ‘strict planned dynamic property’ to solve the given problem Greedy method arrives at a solution by making a sequence of choices, each of which simply looks the best at the moment. – So, each choice is locally optimal – Global (ultimate) solution is not always the optimal It require us to prove the global solution to be optimal – A counter example shows that the greedy algorithm does not provide an optimal solution. Sometimes, we adopt the global solution without a proof 3/20

4 Greedy Method It starts with an empty solution set and adds items to the set in sequence until the set represents a global solution to the problem Three steps of greedy method – Selection procedure Choose the next item to add to the set Selected according to a greedy criterion that satisfies some locally optimal consideration at the time – Feasibility check Determines if the new set is feasible by checking whether its inclusion is feasible to solve the problem – Solution check Determine if the new set constitutes a global solution to the problem 4/20

5 Greedy Method Greedy Algorithm Template // Algorithm takes as input an array a of n elements algorithm greedy ( a, n ) { solution = {}; // Initially empty do { for ( i = 0; i < n; i++ ) { // Select an input from a and remove it // from further consideration x = select ( a ); if ( feasible ( solution, x ) ) solution = solution + x; // Union } } while ( !check ( solution ) ); return ( solution ); } 5/20

6 Coin changing problem Problem – 1) Giving a correct change for a purchase – 2) Giving the change with as few coins as possible Greedy choice – Select the coin with the highest coin value – Solution may be optimal. Is it optimal for all sets of coin sizes? Is there a solution for all sets of coin sizes? 6/20

7 Coin changing problem Greedy solution Input: 1) Set of coins of different denominations, 2) amount-owed change = {} while (more coin-sizes && valueof(change) < amount-owed) Choose the largest remaining coin-size // Selection // feasibility check if (adding the coin does not make the valueof(change) exceed the amount-owed ) then add coin to change // solution check if ( valueof(change) equals amount-owed) return change else delete coin-size return “failed to compute change” 7/20

8 Coin changing problem Greedy solution – Example 1 – Set of coins of different denominations Quarter ($0.25) – 1, Dime ($0.10) – 2, Nickel ($0.05) – 1, Penny ($0.01) – 2 – amount-owed: $0.36 Remaining: $0.36 – $0.25 = $0.11 Remaining : $0.11 – $0.10 = $0.01 Remaining: $0.01 – $0.01 = $0.00 $0.25 Add quarter($0.25) to change Add dime($0.10) to change Delete dime($0.10) Delete nickel($0.05) Add penny($0.01) to change 8/20

9 Coin changing problem Greedy solution – Example 2 – Set of coins of different denominations New coin ($0.12) – 1, Dime ($0.10) – 1, Nickel ($0.05) – 1, Penny ($0.01) – 4 – amount-owed: $0.16 – Is it optimal? No.{One dime($0.10), One Nickel($0.05), One Penny($0.01)} is optimal solution Remaining: $0.16 – $0.12 = $0.04 Remaining: $0.04 – $0.04 = $0.00 $0.25 Add new coin($0.12) to change Delete dime($0.10) Delete nickel($0.05) Add four fenny($0.01) to change 9/20

10 Single Source Shortest Path Problem Remind: Problem in the previous lecture – Finding the shortest paths from each vertex to other vertices – We developed the O(n 3 ) dynamic programming algorithm Floyd algorithm The problem in this lecture – Finding the shortest paths from one particular vertex to other vertices Say, the source: v 1 – We will use the greedy approach to develop a O(n 2 ) algorithm for this problem v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 A given weighted directed graph 10/20

11 Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm 11/20 F := 0; // the set of edges whose shortest path is to be determined Y := { v 1 }; // the set of nodes whose shortest path is to be determined

12 Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm (example 1) 12/20 Y := {v 1 } F := {} Y := {v 1, v 5 } F := {(v 1, v 5 )} Y := {v 1, v 5, v 4 } F := {(v 1, v 5 ), (v 5, v 4 )} Y := {v 1, v 5, v 4, v 3 } F := {(v 1, v 5 ), (v 5, v 4 ), (v 1, v 3 )} Y := {v 1, v 5, v 4, v 3, v 2 } F := {(v 1, v 5 ), (v 5, v 4 ), (v 1, v 3 ), (v 4, v 2 )}

13 Shortest Path in a Graph Greedy Strategy – Dijkstra algorithm (example 2) Y := {v 1 } F := {} Y := {v 1, v 2, v 3 } F := {(v 1, v 2 ), (v 1, v 3 )} Y := {v 1, v 2, v 3, v 5 } F := {(v 1, v 2 ), (v 1, v 3 ), (v 3, v 5 )} Y := {v 1, v 2, v 3, v 5, v 4 } F := {(v 1, v 2 ), (v 1, v 3 ), (v 3, v 5 ), (v 3, v 4 )}

14 Shortest Path in a Graph 14/20 touch length

15 Shortest Path in a Graph Dijkstra algorithm 15/20 set_of_edges dijkstra(int n, number[][] W) { index i, vnear; number min; set_of_edges F; index[] touch = new index[2..n]; number[] length = new number[2..n]; F = empty_set; for(i=2; i <= n; i++) { // initialization of touch and length touch[i] = 1; // because Y include only v1 length[i] = W[1][i]; // initialed with(v1,vi)’s weight } // see the next page

16 Shortest Path in a Graph Dijkstra algorithm 16/20 for (dest=2; dest <= n; dest++); { // insert n-1 vertex into Y min = infinite; for(i=2; i <= n; i++) { if (0 < length[i] < min) { min = length[i]; vnear = i; // vnear is the index of vertex closest to Y } e = edge from vertex indexed by touch[vnear] to vertex indexed by vnear; add e to F; for(i=2; i <= n; i++) if (length[vnear] + W[vnear][i] < length[i]) { length[i] = length[vnear]+W[vnear][i]; touch[i] = vnear; } length[vnear] = -1; } // end of for return F; } // end of main [Complexity] T(n) = (n-1) 2  O(n 2 )

17 Shortest Path in a Graph Dijkstra algorithm – The process (1/2) 17/20 vnear=2 vnear=3 vnear=5 touch length 7 5 7

18 Shortest Path in a Graph Dijkstra algorithm – The process (2/2) 18/20 vnear=4 7

19 [Programming Practice 5] Dijkstra Algorithm – Visit http://link.koreatech.ac.kr/courses/2013_1/AP-KOICA/AP- KOICA20131.html – Download “Dijkstra.java” and run it – Analyze the source codes – Complete the source codes while insert right codes within the two functions public static Edge[] dijkstra(int n) 19/20

20 [Programming Practice 5] Dijkstra Algorithm – The output you should get v5v5 v1v1 v4v4 v2v2 v3v3 3 5 1 9 1 2 2 3 3 4 A given weighted directed graph


Download ppt "Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang."

Similar presentations


Ads by Google