1 / 31

Greedy Algorithms

Pasi Fränti. Greedy Algorithms. 8.10.2013. Greedy algorithm. Coin problem Minimum spanning tree Generalized knapsack problem Traveling salesman problem. Coin problem. Task: Given a coin set, pay the required amount (36 snt) using least number of coins. 25. 10. 1.

denis
Télécharger la présentation

Greedy 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. Pasi Fränti Greedy Algorithms 8.10.2013

  2. Greedy algorithm • Coin problem • Minimum spanning tree • Generalized knapsack problem • Traveling salesman problem

  3. Coin problem Task: Given a coin set, pay the required amount (36 snt) using least number of coins.

  4. 25 10 1 Coin problemAnother coin set Amount to be paid: 30 Greedy: Optimal:

  5. Blank space for notes

  6. Minimum spanning treeWhen greedy works Needs problem definition! Tree = … (no cycles) Spanning Tree = … Minimum = … (couple of examples with simple graph)

  7. Minimum spanning treePrim’s algorithm Prim(V, E): RETURN T Select (u,v)E with min weight SS{u,v}; PP{(u,v)}; EE\{(u,v)}; REPEAT Select (u,v) with min weight  (u,v)E, uS, vS SS{v}; PP{(u,v)}; EE\{(u,v)}; UNTIL S=V Return P;

  8. Example of Prim 117 216 110 246 199 182 170 121 231 315 142 79 242 136 191 148 78 120 191 126 178 149 89 116 234 170 112 51 79 131 109 86 73 163 143 72 90 63 53 105 59 27 58 135 116

  9. C C A A B B Proof of optimalityGeneral properties of spanning trees Spanning tree includes N-1 links There are no cycles Minimum spanning tree is the one with the smallest weights Cycle No cycle  Remove Link BC

  10. C C 2 2 A A 2 2 1 1 B B Proof of optimalityCase: minimum link  Add AB Link AB is minimum Suppose it is not in MST Path A→B must exist Adding AB we can remove another link (e.g. AC) Path A→C exists All nodes reached from C can now be reached from B

  11. E 3 C A 4 D B Proof of optimalityInduction step MST solved for Subset S  ReplaceCD by CE Suppose CE is minimum connecting S outside Path D→E must exist and is outside S

  12. Proof of optimalityInduction step E MST solved for Subset S 3 C A 4 D B Path D→E still exist as before All nodes reachable via D can now be reached via C→D

  13. Source of data just for fun 

  14. Minimum spanning treeKruskal’s algorithm 1. A   // initially A is empty 2. for each vertex v  V[G] // line 2-3 takes O(V) time 3. do Create-Set(v) // create set for each vertex 4. sort the edges of E by nondecreasing weight w 5. for each edge (u,v)  E, in order bynondecreasing weight 6. do if Find-Set(u)  Find-Set(v) // u&v on different trees 7. then A  A  {(u,v)} 8. Union(u,v) 9. return A Total running time is O(E lg E). Needs revisions: • Remove numbers • Change terminology

  15. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  16. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  17. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  18. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  19. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  20. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  21. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  22. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  23. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  24. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  25. Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 Nottingham 80 B/ham 110 Aberystwyth 70 100 90 120 Oxford 50 Bristol Cardiff 80 70 Southampton

  26. Generalized Knapsack problemProblem definition Input: Weight of N items {w1, w2, ..., wn} Cost of N items {c1, c2, ..., cn} Knapsack limit S Output: Selection for knapsack: {x1,x2,…xn} where xi{0,1}. Sample input: wi={1,1,2,4,12} ci ={1,2,2,10,4} S=15

  27. Generalized Knapsack problem Will appear 2014…

  28. Semi-blank space

  29. Traveling salesman problem GreedyTSP(V, E, home): RETURN T X[1]home; FOR i 1 TO N-1 DO Select (u,v) with min weight  (u,v)E, uS, vS X[………….. SS{v}; …. UNTIL V≠{} Return something; Needs to be done

  30. Greedy algorithms Not • Coin problem • Minimum spanning tree • Generalized knapsack problem • Traveling salesman problem Solved Solved Not

More Related