Greedy Algorithms Pasi Frnti 15 11 2017 Greedy
- Slides: 35
Greedy Algorithms Pasi Fränti 15. 11. 2017
Greedy algorithm 1. Coin problem 2. Minimum spanning tree 3. Generalized knapsack problem 4. Traveling salesman problem
Coin problem Task: Given a coin set, pay the required amount (36 snt) using least number of coins.
Coin problem Another coin set 25 10 1 Amount to be paid: 30 Greedy: Optimal:
Blank space for notes
Minimum spanning tree When greedy works Input: Output: Graph Sub-graph Tree Spanning tree Minimum No cycles All nodes reachable Sum of weights minimum
Minimum spanning tree Prim’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;
Example of Prim 117 216 110 246 199 121 79 182 315 142 170 242 191 148 231 136 78 120 126 149 89 116 234 170 51 112 79 131 109 86 73 163 143 72 63 90 53 191 178 59 58 116 105 135 27
Proof of optimality General 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 C A B No cycle Remove Link BC C A B
Proof of optimality Case: minimum link C 2 A 1 B 2 C 2 Add AB • Link AB is minimum • Suppose it is not in MST • Path A→B must exist A 1 B 2 • 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
Proof of optimality Induction step MST solved for Subset S 3 E C 4 A B D Replace CD by CE • Suppose CE is minimum connecting S outside • Path D→E must exist and is outside S
Proof of optimality Induction step MST solved for Subset S 3 E C 4 A B D • Path D→E still exist as before • All nodes reachable via D can now be reached via C→E→D
Sou rce o f da ta ju st fo r fun
Minimum spanning tree Kruskal’s algorithm Kruskal(V, E): RETURN T FOR ALL v V Create. Set(v); P ; SORT(E); // according to decreasing weights FOR each e E DO IF Find. Set(u) Find. Set(v) THEN UNION(u, v); P P {(u, v)}; Return P;
Blank space for notes
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Manchester 40 30 Liverpool Sheffield 110 70 40 Shrewsbury 50 50 80 Nottingham B/ham 110 Aberystwyth 70 100 90 120 50 Cardiff Oxford Bristol 80 Southampton 70
Further questions • • What is the time complexity of Prim? What should we use: array, list or heap? How about union-find structure? Nearest neighbor link solution better?
Blank space for notes
Knapsack problem Problem definition Input: Weight of N items {w 1, w 2, . . . , wn} Cost of N items {c 1, c 2, . . . , cn} Knapsack limit S Output: Selection for knapsack: {x 1, x 2, …xn} where xi {0, 1}. Sample input: wi={1, 1, 2, 4, 12} ci ={1, 2, 2, 10, 4} S=15 Binary choice
Generalized Knapsack problem Input: Weight of N items {w 1, w 2, . . . , wn} Cost of N items {c 1, c 2, . . . , cn} Knapsack limit S Output: Selection for knapsack: {x 1, x 2, …xn} where xi [0, 1]. Any real value between 0 and 1 ! Sample input: wi={1, 1, 2, 4, 12} ci ={1, 2, 2, 10, 4} S=15 ci/wi = {1, 2, 1, 2. 5, 0. 33} Sorted = {2. 5, 2, 1, 1, 0. 33} Select = 1. 0, 0. 58 Costs Weights 10 4 2 1 2 2 1 1 2. 3 7. 0
Blank space for notes
Traveling salesman problem Greedy. TSP(V, E, home): RETURN T For i 1 TO N DO Visited[i] FALSE; X[1] home; Visited[home] TRUE; Sub -op tim (Exe al! rcise task ) FOR i 1 TO N-1 DO Select (X[i], v) E weight is minimal X[2] v; Visited[v] TRUE;
Semi-blank space
Greedy algorithms Not 1. Coin problem Solv 2. Minimum spanning tree ed Solv 3. Generalized knapsack problem ed Not 4. Traveling salesman problem
- Advantages and disadvantages of greedy algorithm
- List of greedy algorithms
- Huffman decoding for greedy algorithms
- Fractional knapsack problem
- Frnti
- Frnti
- Frnti
- Frnti
- Frnti
- Frnti
- Frnti
- Pasi fränti
- Cluster validation methods
- Level measure
- Frnti
- Frnti
- Frnti
- Frnti
- Dmedi
- Pasi kaipainen
- Notiunea de algoritm
- Elenger marine
- Lidheza nenrenditese
- Typasi
- Sehr geehrte frau doktorin
- Pasi rantahalvari
- Pasi fränti
- Un pasi
- Regulament baschet
- Nicoletta pasi
- Pasi fränti
- Sfs-en 1838
- Pasi puranen
- Introduction to sorting algorithms
- C sorting algorithms
- Design and analysis of algorithms