Download presentation
Presentation is loading. Please wait.
1
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?
2
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
3
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|)
4
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
5
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
6
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
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.