Min Spanning Trees
Min Spanning Trees
Spanning Trees
• Given (connected) graph G(V,E),
a spanning tree T(V’,E’):
› Is a subgraph of G; that is, V’ V, E’ E.
› Spans the graph (V’ = V)
› Forms a tree (no cycle);
› So, E’ has |V| -1 edges
10/8/2024 2
Minimum Spanning Trees
• Edges are weighted: find minimum cost
spanning tree
• Applications
› Find cheapest way to wire your house
› Find minimum cost to send a message on
the Internet
10/8/2024 3
Strategy for Minimum
Spanning Tree
• For any spanning tree T, inserting an
edge enew not in T creates a cycle
• But
› Removing any edge eold from the cycle
gives back a spanning tree
› If enew has a lower cost than eold we have
progressed!
10/8/2024 4
Strategy
• Strategy for construction:
› Add an edge of minimum cost that does
not create a cycle (greedy algorithm)
› Repeat |V| -1 times
› Correct since if we could replace an edge
with one of lower cost, the algorithm would
have picked it up
10/8/2024 5
Two Algorithms
• Prim: (build tree incrementally)
› Pick lower cost edge connected to known
(incomplete) spanning tree that does not create a
cycle and expand to include it in the tree
• Kruskal: (build forest that will finish as a tree)
› Pick lowest cost edge not yet in a tree that does
not create a cycle. Then expand the set of
included edges to include it. (It will be somewhere
in the forest.)
10/8/2024 6
Prim’s algorithm
1
Starting from empty T, 10
choose a vertex at 5
random and initialize 1
V = {1), E’ ={} 3
2 8 3 4
1 1 6
4
2
6 5
10/8/2024 7
Prim’s algorithm
1
Choose the vertex u not in 10
V such that edge weight 5
from u to a vertex in V is 1
minimal (greedy!)
8 3
V={1,3} E’= {(1,3) } 2 3 4
1 1 6
4
2
6 5
10/8/2024 8
Prim’s algorithm
Repeat until all vertices have
1
been chosen
10 5
Choose the vertex u not in V 1
such that edge weight from v to a
vertex in V is minimal (greedy!)
8 3
V= {1,3,4} E’= {(1,3),(3,4)} 2 3 4
V={1,3,4,5} E’={(1,3),(3,4),(4,5)}
1 1 6
….
V={1,3,4,5,2,6} 4
2
E’={(1,3),(3,4),(4,5),(5,2),(2,6)} 6 5
10/8/2024 9
Prim’s algorithm
Repeat until all vertices have
1
been chosen
10 5
V={1,3,4,5,2,6} 1
E’={(1,3),(3,4),(4,5),(5,2),(2,6)}
8 3
2 3 4
Final Cost: 1 + 3 + 4 + 1 + 1 = 10
1 1 6
4
2
6 5
10/8/2024 10
Prim’s Algorithm
Implementation
• Assume adjacency list representation
Initialize connection cost of each node to “inf” and “unmark” them
Choose one node, say v and set cost[v] = 0 and prev[v] =0
While they are unmarked nodes
Select the unmarked node u with minimum cost; mark it
For each unmarked node w adjacent to u
if cost(u,w) < cost(w) then cost(w) := cost (u,w)
prev[w] = u
10/8/2024 11
Prim’s algorithm Analysis
• If the “Select the unmarked node u with minimum cost” is
done with binary heap then O((n+m)logn)
10/8/2024 12
Kruskal’s Algorithm
• Select edges in order of increasing cost
• Accept an edge to expand tree or forest
only if it does not cause a cycle
• Implementation using adjacency list,
priority queues and disjoint sets
10/8/2024 13
Kruskal’s Algorithm
Initialize a forest of trees, each tree being a single node
Build a priority queue of edges with priority being lowest cost
Repeat until |V| -1 edges have been accepted {
Deletemin edge from priority queue
If it forms a cycle then discard it
else accept the edge – It will join 2 existing trees yielding a
larger tree and reducing the forest by one tree
}
The accepted edges form the minimum spanning tree
10/8/2024 14
Detecting Cycles
• If the edge to be added (u,v) is such
that vertices u and v belong to the same
tree, then by adding (u,v) you would
form a cycle
› Therefore to check, Find(u) and Find(v). If
they are the same discard (u,v)
› If they are different Union(Find(u),Find(v))
10/8/2024 15
Properties of trees in K’s
algorithm
• Vertices in different trees are disjoint
› True at initialization and Union won’t modify the
fact for remaining trees
• Trees form equivalent classes under the
relation “is connected to”
› u connected to u (reflexivity)
› u connected to v implies v connected to u
(symmetry)
› u connected to v and v connected to w implies a
path from u to w so u connected to w (transitivity)
10/8/2024 16
Example
1
10 5
1 1
8 3
2 3 4
1 1 6
4
2
6 5
10/8/2024 17
Initialization
1
Initially, Forest of 6 trees
F= {{1},{2},{3},{4},{5},{6}}
6 5
10/8/2024 18
Step 1
1
Select edge with lowest
cost (2,5)
Find(2) = 2, Find (5) = 5
Union(2,5)
2 3 4
F= {{1},{2,5},{3},{4},{6}}
1 edge accepted 1
6 5
10/8/2024 19
Step 2
1
Select edge with lowest
cost (2,6)
Find(2) = 2, Find (6) = 6
Union(2,6)
2 3 4
F= {{1},{2,5,6},{3},{4}}
2 edges accepted 1
1
6 5
10/8/2024 20
Step 3
1
Select edge with lowest
cost (1,3)
1
Find(1) = 1, Find (3) = 3
Union(1,3)
2 3 4
F= {{1,3},{2,5,6},{4}}
3 edges accepted 1
1
6 5
10/8/2024 21
Step 4
1
Select edge with lowest
cost (5,6)
1
Find(5) = 2, Find (6) = 2
Do nothing
2 3 4
F= {{1,3},{2,5,6},{4}}
3 edges accepted 1
1
6 5
10/8/2024 22
Step 5
1
Select edge with lowest
cost (3,4)
1
Find(3) = 1, Find (4) = 4
Union(1,4) 3
2 3 4
F= {{1,3,4},{2,5,6}}
4 edges accepted 1
1
6 5
10/8/2024 23
Step 6
Select edge with lowest 1
cost (4,5)
Find(4) = 1, Find (5) = 2
1
Union(1,2)
3
F= {{1,3,4,2,5,6}} 2 3 4
10/8/2024 26