320 likes | 488 Views
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.
E N D
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 Coin problemAnother coin set Amount to be paid: 30 Greedy: Optimal:
Minimum spanning treeWhen greedy works Needs problem definition! Tree = … (no cycles) Spanning Tree = … Minimum = … (couple of examples with simple graph)
Minimum spanning treePrim’s algorithm Prim(V, E): RETURN T Select (u,v)E with min weight SS{u,v}; PP{(u,v)}; EE\{(u,v)}; REPEAT Select (u,v) with min weight (u,v)E, uS, vS SS{v}; PP{(u,v)}; EE\{(u,v)}; UNTIL S=V Return P;
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Generalized Knapsack problem Will appear 2014…
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, uS, vS X[………….. SS{v}; …. UNTIL V≠{} Return something; Needs to be done
Greedy algorithms Not • Coin problem • Minimum spanning tree • Generalized knapsack problem • Traveling salesman problem Solved Solved Not