red-rule blue-rule demo prim’s algorithm demo kruskal...
TRANSCRIPT
Lecture slides by Kevin WayneCopyright © 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 2/26/18 4:51 PM
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
SECTION 6.1
Red-rule blue-rule demo
Red rule. Let C be a cycle with no red edges. Select an uncolored edge of Cof max weight and color it red.
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
3
1 5
3
9 2
47 6
8
the input graph
Red-rule blue-rule demo
Red rule. Let C be a cycle with no red edges. Select an uncolored edge of Cof max weight and color it red.
4
3
9 2
4
8
apply the red rule to the cycle
1
Red-rule blue-rule demo
Red rule. Let C be a cycle with no red edges. Select an uncolored edge of Cof max weight and color it red.
6
2
47
8
apply the red rule to the cycle
Red-rule blue-rule demo
Red rule. Let C be a cycle with no red edges. Select an uncolored edge of Cof max weight and color it red.
7
current set of red and blue edges
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
8
1 5 47 6
apply the blue rule to the cutset
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
10
5
3
9
apply the blue rule to the cutset
Red-rule blue-rule demo
Red rule. Let C be a cycle with no red edges. Select an uncolored edge of Cof max weight and color it red.
12
2
46
apply the red rule to the cycle
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
14
9 2
4
8
apply the blue rule to the cutset
7
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
16
46
8
apply the blue rule to the cutset
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
18
9
7
8
apply the blue rule to the cutset
Red-rule blue-rule demo
Blue rule. Let D be a cutset with no blue edges. Select an uncolored edge in
D of min weight and color it blue.
20
1 5
3
apply the red rule to the cycle
Red-rule blue-rule demo
Greedy algorithm. Upon termination, the blue edges form a MST.
22
1
3
2
47
a minimum spanning tree
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
SECTION 4.5
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
24
1 6
4
7 3
9
2
10
11
58 12
13
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
25
3 11
5
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
26
3
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
27
7 11
58 12
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
28
5
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
29
7 11
8
13 2
10
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
30
2
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
31
7
9
10
11
8
13
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
32
7
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
33
1 6 9
11
8
13
10
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
34
1
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
35
6
4
9
11
8
13
10
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
36
4
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
37
9
11
10
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
38
9
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
39
Prim’s algorithm demo
Initialize S = any node, T = ∅.
Repeat n – 1 times:
・Add to T a min-weight edge with one endpoint in S.
・Add new node to S.
40
1
4
3
9
2
5
7
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
SECTION 4.5
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
42
1 5
3
9 2
47 6
8
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
43
1
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
44
2
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
45
3
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
46
4
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
47
5
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
48
6
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
49
7
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
50
8
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
51
9
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
52
Kruskal’s algorithm demo
Consider edges in ascending order of weight:
・Add to T unless it would create a cycle.
53
1
3
2
47
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
SECTION 4.5
Reverse-delete algorithm demo
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
55
1 5
3
9 2
47 6
8
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
56
9
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
57
8
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
58
7
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
59
6
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
60
5
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
61
4
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
62
3
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
63
2
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
64
1
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
65
Reverse-delete algorithm
Start with all edges in T and consider them in descending order of weight:
・Delete edge from T unless it would disconnect T.
66
1
3
2
47
4. GREEDY ALGORITHMS II
‣ red-rule blue-rule demo
‣ Prim’s algorithm demo
‣ Kruskal’s algorithm demo
‣ reverse-delete algorithm demo
‣ Boruvka’s algorithm demo
SECTION 6.2
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
68
1 6
4
7 3
9
2
10
11
58 12
13
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
69
1 6
4
7 3
9
2
10
11
58 12
13
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
70
1 6
4
7 3
9
2
10
11
58 12
13
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
71
7 11
58 12
13
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
72
7 11
58 12
13
Borůvka’s algorithm demo
Repeat until only one tree.
・Apply blue rule to cutset corresponding to each blue tree.
・Color all selected edges blue.
73