algorithm design and analysis - university of delawareudel.edu/~amotong/teaching/algorithm...
TRANSCRIPT
-
Algorithm Design and Analysis
Summer 2018 Amo G. Tong 1
-
Lecture 11Greedy Strategy• Minimum Spanning Tree
Summer 2018 Amo G. Tong 2
-
• Consider a weighted undirected graph 𝐺 = (𝑉, 𝐸).
• A spanning tree 𝑇 of 𝐺 is a connected subgraph with no cycle.• A subgraph is defined by a subset of the edges.
• A graph is connected if there is a path between each pair of nodes.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 3
-
Minimum Spanning Tree
Summer 2018 Amo G. Tong 4
2
6
5
3
1
4
-
Minimum Spanning Tree
Summer 2018 Amo G. Tong 5
2
6
5
3
1
4
2
6
5
3
1
4
-
Minimum Spanning Tree
Summer 2018 Amo G. Tong 6
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
-
Minimum Spanning Tree
Summer 2018 Amo G. Tong 7
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
-
Minimum Spanning Tree
Summer 2018 Amo G. Tong 8
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
2
6
5
3
1
4
-
• Build channels such that the stations are able to connected with each other.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 9
2D map
-
• Build channels such that the stations are able to connected with each other.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 10
2D map
-
• The following definitions are equivalent.
• A spanning tree is
• a connected subgraph with no cycle. (original)
Minimum Spanning Tree
Summer 2018 Amo G. Tong 11
-
• The following definitions are equivalent.
• A spanning tree is
• a connected subgraph with no cycle. (original)
• a connected subgraph consisting of two trees connected by a single edge. (recursive definition)
Minimum Spanning Tree
Summer 2018 Amo G. Tong 12
-
• The following definitions are equivalent.
• A spanning tree is
• a connected subgraph with no cycle. (original)
• a connected subgraph consisting of two trees connected by a single edge. (recursive definition)
• A connected graph with exactly 𝑛 − 1 edges.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 13
-
• The following definitions are equivalent.
• A spanning tree is
• a connected subgraph with no cycle. (original)
• a connected subgraph consisting of two trees connected by a single edge. (recursive definition)
• A connected graph with exactly 𝑛 − 1 edges.
• A subgraph where there is a unique path between each pair of two vertices.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 14
-
• The following definitions are equivalent.
• A spanning tree is
• a connected subgraph with no cycle. (original)
• a connected subgraph consisting of two trees connected by a single edge. (recursive definition)
• A connected graph with exactly 𝑛 − 1 edges.
• A subgraph where there is a unique path between each pair of two vertices.
• Extra credits: prove the above four definitions are equivalent to each other.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 15
-
• Consider a weighted undirected graph 𝐺 = (𝑉, 𝐸).
• A spanning tree 𝑇 is connected subgraph with no cycle.
• A cost of a tree is the sum of the weights of the edges.
• Problem: find a spanning tree of the minimum cost.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 16
-
• Build channels such that the stations are able to connected with each other.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 17
2D map
-
• Naïve algorithms.• Enumerate all the subgraphs
• Check if it is a spanning tree.
• Find the one with the minimum cost.
• How many subgraphs are there?
• Enumerate all the subgraphs with n-1 edges.• Check if it is a spanning tree.
• Find the one with the minimum cost.
• How many subgraphs with n-1 edges are there?
• …
Minimum Spanning Tree
Summer 2018 Amo G. Tong 18
-
• Two greedy algorithms.• Kruskal’s algorithm
• Prim’s algorithm
• Conditions for a spanning tree to be minimum.• Cut-optimality condition
• Path-optimality condition
Minimum Spanning Tree
Summer 2018 Amo G. Tong 19
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 20
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 21
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 22
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 23
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 24
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 25
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 26
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 27
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 28
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 29
2
6
5
3
1
4
1
34
2
1
-
• Exchange Property.• (𝑒1, … , 𝑒𝑚)
• There exists a minimum spanning tree containing 𝑒1.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 30
-
• Self-reducibility• For an edge 𝑒 and a minimum spanning tree 𝑇 containing 𝑒, 𝑇 ∖ 𝑒 is
a minimum spanning tree of graph 𝐺 ’ where 𝐺 ’ is obtained from 𝐺by shrinking edge 𝑒 into a point.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 31
e
'TT
-
• Exchange Property.• (𝑒1, … , 𝑒𝑚)
• There exists a minimum spanning tree containing 𝑒1.
• Self-reducibility• For an edge 𝑒 and a minimum spanning tree 𝑇 containing 𝑒, 𝑇 ∖ 𝑒 is
a minimum spanning tree of graph 𝐺 ’ where 𝐺 ’ is obtained from 𝐺by shrinking edge e into a point.
• Kruskal’s algorithm is optimal.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 32
-
• Implement Kruskal’s algorithm • Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge e in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 33
-
• Disjoint sets Q: maintain a set of elements by a partition.• 𝑄 = {𝑆1, … , 𝑆𝑘} with 𝑆𝑖 ∩ 𝑆𝑗 = ∅. Each set 𝑆𝑖 is associated with a
pointer (id or index).
• Operations:
• Make-Set(𝑥): create a new set containing only 𝑥
• 𝑄 = {𝑆1, … , 𝑆𝑘} −> 𝑄 = {𝑆1, … , 𝑆𝑘 , {𝑥}}
• Union(𝑥, 𝑦): for 𝑥 and 𝑦 where there exist 𝑆𝑖 and 𝑆𝑗 containing 𝑥 and 𝑦, respectively, union 𝑆𝑖 and 𝑆𝑗.
• Find-Set(𝑥): return the pointer to the set containing 𝑥.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 34
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 35
2
6
5
3
1
4
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 36
2
6
5
3
1
4
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 37
2
6
5
3
1
4
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 38
2
6
5
3
1
4
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 39
2
6
5
3
1
4
𝑒 = (𝑢, 𝑣) results a cycle iff 𝑢 and 𝑣 are in the same set.
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly.
• Let (𝑒1, … , 𝑒𝑚) be the sorted sequence.
• 𝑇 = ∅
• Consider each edge 𝑒 in (𝑒1, … , 𝑒𝑚) in order. Add 𝑒 to 𝑇 if 𝑇 ∪{𝑒} does not have a cycle.
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣).
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠ Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 40
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
Minimum Spanning Tree
Summer 2018 Amo G. Tong 41
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 42
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 43
2
6
5
3
1
4
1
34
53
2
16
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 44
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 45
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}{1, 2}, {3}, {4}, {5}, {6}
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 46
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}{1, 2}, {3}, {4}, {5}, {6}{1, 2}, {3, 5}, {4}, {5, 6}
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 47
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}{1, 2}, {3}, {4}, {5}, {6}{1, 2}, {3, 5}, {4}, {5, 6}{1, 2, 3, 5}, {4}, {6}
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 48
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}{1, 2}, {3}, {4}, {5}, {6}{1, 2}, {3, 5}, {4}, {5, 6}{1, 2, 3, 5}, {4}, {6}{1, 2, 3, 4, 5}, {6}
-
• Kruskal’s algorithm• Order the edges by the weight non-decreasingly. (𝑒1, … , 𝑒𝑚)
• For each node 𝑣, Make-Set(𝑣). 𝑇 = ∅.
• Consider each edge 𝑒 = (𝑢, 𝑣) in (𝑒1, … , 𝑒𝑚) in order. If Find-Set(𝑢) ≠Find-Set(𝑣), Union(𝑢, 𝑣) and add 𝑒 to 𝑇.
Minimum Spanning Tree
Summer 2018 Amo G. Tong 49
2
6
5
3
1
4
1
34
53
2
16
{1}, {2}, {3}, {4}, {5}, {6}{1, 2}, {3}, {4}, {5}, {6}{1, 2}, {3, 5}, {4}, {5, 6}{1, 2, 3, 5}, {4}, {6}{1, 2, 3, 4, 5}, {6}{1, 2, 3, 4, 5, 6}
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 50
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 51
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 52
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 53
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 54
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 55
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 56
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 57
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 58
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 59
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 60
2
6
5
3
1
4
1
34
53
2
16
-
• Prim’s algorithm• Maintain two sets 𝑆 and 𝑇. Initially, S={1} and 𝑇 = ∅.
• While 𝑆 ≠ 𝑉
• Fine the edge 𝑒 = (𝑢, 𝑣) with minimum weight such that 𝑢 ∈𝑆 and 𝑣 ∈ 𝑉\S
• Add 𝑣 to 𝑆 and 𝑒 to 𝑇.
• End
Minimum Spanning Tree
Summer 2018 Amo G. Tong 61
2
6
5
3
1
4
1
34
53
2
16