Minimum Spanning Tree
Minimum Spanning Tree
Spanning Tree
Def. Given a connected, undirected graph ! = ($, &), an acyclic subset
of the edges ( ⊆ & that connects all of the vertices of ! is called a
spanning tree (of !).
• A given graph ! = ($, &) can have many possible spanning trees
• If |$| = +, there are + − 1 edges in a spanning tree
• We specify the spanning tree by indicating the edges.
1
10/27/20
4 24
4
23 9
6 6 9
18
5 5
16 11 11
8 8
7 7
10 14
21
G = (V, E, w) T, SeÎT we = 50
Greedy Algorithms
Strategy 1. Start with ! = ∅ Consider edges in ascending order of
weight. Insert edge $ in ! unless doing so would create a cycle.
Strategy 2. Start with some root node % and greedily grow a tree
! from % outward. At each step, add the cheapest edge $ to ! that
connects the current tree ! to an isolated vertex.
2
10/27/20
Greedy Algorithms
Kruskal's algorithm. Start with ! = ∅ Consider edges in ascending order of
weight. Insert edge $ in ! unless doing so would create a cycle.
Prim's algorithm. Start with some root node % and greedily grow a tree
! from % outward. At each step, add the cheapest edge $ to ! that connects
the current tree ! to an isolated vertex.
Kruskal’s Algorithm
Kruskal's algorithm. Start with ! = ∅ 1
Consider edges in ascending order of 15
weight. Insert edge $ in ! unless doing 8
7
so would create a cycle. 4
3
5 7
1 2
Kruskal ( G=(V,E,w) ) 1
1. Let T = Ø; 6
2. Sort the edges in increasing order of weight
3. For edge e do 2
4. If T U e does not contain a cycle then
5. Add e to T
6. Return T
3
10/27/20
Cut property. Let # be any subset of nodes, and let " be the min weight
edge with exactly one endpoint in #. Then the MST contains ".
Note: the cut property does not rule
out the possibility that other edges
that have exactly one endpoint in S
S are part of the MST.
e
e is in the MST
Cut property. Let # be any subset of nodes, let " be the min weight edge with
exactly one endpoint in #. Then the MST contains ". Let’s call this MST $ ∗ .
4
10/27/20
5
10/27/20
6
10/27/20
Kruskal's Algorithm
Kruskal's algorithm. Start with ! = ∅ Consider edges in ascending
order of weight. Insert edge $ in ! unless doing so would create a
cycle.
Kruskal ( G=(V,E,w) ) using Union-Find data structure
1. Let T = Ø;
2. Sort the edges in increasing order of weight
3. Init(V) //initialize union-find arrays
4. For edge e = (u,v) do
5. If Find(u) ≠ Find(v) //no cycle in T ∪ e
6. Add e to T
7. Union(u,v) //union of sets containing u, v
8. Return T
Find (u)
1. Return boss[u]
Union (u,v)
1. if size[boss[u]]>=size[boss[v]] then
2. set[boss[u]]= set[boss[u]]∪ set[boss[v]]
3. size[boss[u]]+=size[boss[v]]
4. for every z in set[boss[v]] do
5. boss[z]=boss[u]
6. else do steps 2.-5. with u,v switched
7
10/27/20
Complexity Analysis
! = ($, &), |$| = ), |&| = *
• Sort the edges: +(* log *) = +(* log )2) = +(* log )) Total Complexity: +(* log ))
Complexity dominated by
• Init. Union-Find Data Structures: +()) initial sorting of edges by
• All 01)2(3) operations together: +(*) weight.
• All 4)15)(3, 6) operations together: +() log )) * see next slide
Complexity Analysis
• All !"#$"(&, () operations together: *(" log ")
• Cost of union operation dominated by updating boss[]
• How many total boss updates are done across all unions?
• we always choose to update the smaller set, it guarantees that any time a vertex has its
boss updated, the set it belongs to at least doubles in size.
• Each vertex has its boss updated at most log n times
Union (u,v)
1. if size[boss[u]]>size[boss[v]] then
2. set[boss[u]]=set[boss[u]] ∪ set[boss[v]]
3. size[boss[u]]+=size[boss[v]]
4. for every z in set[boss[v]] do
5. boss[z]=boss[u]
6. else do steps 2.-5. with u,v switched
8
10/27/20
Prim’s Algorithm
Prim's algorithm. Start with some root node ! and greedily grow a tree
" from ! outward. At each step, add the cheapest edge # to " that
connects the current tree " to an isolated vertex.
1
15
7
8
4
3
5 7
1 2
1
6
1
Prim ( G=(V,E,w) ) 15
7
1. Let T = Ø; H = V – {s} 8
2. For every vertex v in H do 4
3. cost[v]= ∞, parent[v]=null 3
4. cost[s]=0, parent[s] = none 5 7
5. Update (s) 2
6. For i=1 to n-1 do 1 1
6
7. u=vertex from H of
smallest cost //extract_min
• Add (u,parent[u]) to T 2
• Update(u)
• Return T
Update (u)
1. For every neighbor v of u (such that v in H)
2. If cost[v]>w(u,v) then
3. cost[v]=w(u,v), parent[v]=u //change_key
9
10/27/20
Complexity Analysis
Complexity Analysis for ! = ($, &), |$| = ), |&| = *
• We store attachment weight information for each vertex in a heap data
structure
• Each heap operation requires O(log )) time
• ) − 1 iterations in which an EXTRACT_MIN heap operation is performed
• 0() log ))
• Each edge can result in at most one CHANGE_KEY heap operation
• 0(* log ))
10