Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA) always makes choice which is the best at the moment does not look ahead: local very powerful in practice: simplest and fastest in general, does not return the optimal solution Example: convex polygon minimum triangulation convex = each diagonal is completely inside triangulation = cut into triangles minimum = the sum of all edges is minimized Homework (extra grade): does greedy always give optimum solution? how far can greedy be from optimum?
Minimum Spanning Tree (24.1/23.1) MST (G) = tree: acyclic subgraph of G spanning: spans (connects) all the vertices of G has the minimum total weight = sum of edge weights G is matroid GA gives maximum spanning tree how GA can find minimum spanning tree? Greedy edge selection Kruskal’s: between two connected components Prim’s: from connected component to new vertex
Prim’s Algorithm (24.2/23.2) Priority queue binary heap Q V(G) for each u Q do key(u) key(r) 0; (r) Nil; while Q do u Extract-Min(Q) for each v adjacent to u do //total 2|E| times if v Q and w(u,v) < key(v) //membership bit then (v) u; key(v) w(u,v) //Decrease-Key Priority queue binary heap Extract-Min, Decrease-Key = O(log |V|) Total run-time = O(|E|log |V|)
2 1 6 3 4 uExtract-Min-Key(Q) u = 1; for each adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 1; for each adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 2; for each adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 2; for each adjacent to u do 8 5 12 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 4; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 4; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 4; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 4; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 8 3 12 11 6 4 20 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 6; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 6; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 3 12 8 4 11 20 6 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 6; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 6; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 12 9 14 1 10 3 12 8 4 11 20 6 3 10 8 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 11; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 11; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 14 12 9 1 10 3 12 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 11; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 11; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 6 5 22 11 7 12 14 12 9 1 10 3 12 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 12; for each adjacent to u do 8 5 17 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 12; for each adjacent to u do 8 5 17 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 10; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 10; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 10; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 10; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 22 5 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 7; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 7; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 7; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 7; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 3; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 3; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 3; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 3; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 9; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 9; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 9; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 9; for each adjacent to u do 2 8 17 3 2 13 5 10 9 8 6 5 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 5; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 5; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 5; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 5; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 8; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 8; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
2 1 6 3 4 uExtract-Min-Key(Q) u = 8; for each adjacent to u do 8 5 12 uExtract-Min-Key(Q) u = 8; for each adjacent to u do 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 22 11 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 12 9 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 13 5 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7
4 12 2 8 3 17 2 5 13 10 9 8 5 6 11 22 7 12 9 12 14 1 10 12 3 8 4 11 20 6 3 8 10 1 7