1 / 20

Algorithms

The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang. Algorithms. April-May 2013. Dr. Youn-Hee Han. Greedy Method. Origin Charles Dickens’ classic character ‘Ebenezer Scrooge’ He is the most greedy person ever

floria
Télécharger la présentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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.

  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 counterexample shows that the greedy algorithm does not provide an optimal solution. • Sometimes, we adopt the global solution without a proof

  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

  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 ); • }

  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?

  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)returnchange elsedelete coin-size return “failed to compute change”

  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 $0.25 Add quarter($0.25) to change Remaining : $0.11 – $0.10 = $0.01 Add dime($0.10) to change Delete dime($0.10) Delete nickel($0.05) Add penny($0.01) to change Remaining: $0.01 – $0.01 = $0.00

  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 Add new coin($0.12) to change $0.25 Delete dime($0.10) Delete nickel($0.05) Add four fenny($0.01) to change Remaining: $0.04 – $0.04 = $0.00

  10. Single Source Shortest Path Problem • Remind: Problem in the previous lecture • Finding the shortest pathsfrom each vertex to othervertices • We developed the O(n3) dynamic programming algorithm • Floyd algorithm • The problem in this lecture • Finding the shortest paths from one particular vertex to other vertices • Say, the source: v1 • We will use the greedy approach to develop a O(n2) algorithm for this problem 1 v2 v1 9 3 5 1 2 v5 3 A given weighted directed graph 3 2 v3 v4 4

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

  12. Shortest Path in a Graph • Greedy Strategy – Dijkstraalgorithm(example 1) Y := {v1} F := {} Y := {v1, v5} F := {(v1, v5)} Y := {v1, v5, v4} F := {(v1, v5), (v5, v4)} Y := {v1, v5, v4, v3} F := {(v1, v5), (v5, v4), (v1, v3)} Y := {v1, v5, v4, v3, v2} F := {(v1, v5), (v5, v4), (v1, v3), (v4, v2)}

  13. Shortest Path in a Graph • Greedy Strategy – Dijkstraalgorithm(example 2) Y := {v1} F := {} Y := {v1, v2, v3} F := {(v1, v2), (v1, v3)} Y := {v1, v2, v3, v5} F := {(v1, v2), (v1, v3), (v3, v5)} Y := {v1, v2, v3, v5, v4} F := {(v1, v2), (v1, v3), (v3, v5), (v3, v4)}

  14. Shortest Path in a Graph • Data structure for Dijkstraalgorithm • F := 0; Y := {v1}; • Array touch: touch[i]=x • x is the index of vertex vx in Ysuch that the edge <vx, vi> is the last edge on the current shortest path from v1 to viusing only vertices in Yas intermediates. • Array length: length[i]=y • y is the length of the current shortest path from v1 to viusing only vertices in Yas intermediates. touch length

  15. Shortest Path in a Graph • Dijkstra algorithm set_of_edgesdijkstra(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 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(n2)

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

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

  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)

  20. [Programming Practice 5] • Dijkstra Algorithm • The output you should get 1 v2 v1 9 3 5 1 2 v5 3 3 2 v3 v4 A given weighted directed graph 4

More Related