Prim’s and Kruskal’s Algorithm
Presented ByV.Muthumeena
Minimum Spanning Tree
What is minimum spanning tree What is minimum cost spanning tree Applications of minimum cost spanning tree Prim’s Algorithm Kruskal’s Algorithm
What is Minimum Spanning Tree
simple, connected, undirected graph that is not edge-weighted.
Subset graph t=(V,E’) of G=(V,E).
Has | V | - 1 edges that connects all the vertices of the graph.
Thus a minimum spanning tree for G is a graph, T = (V’, E’) with the following properties: V’ = V T is connected T is acyclic.
Example
Undirected Graph G
G’s Spanning Trees
Minimum Cost Spanning Tree
simple, connected, undirected graph that is edge-weighted
The total cost of G is the sum of the weights on all its edges.
A minimum-cost spanning tree for G is a minimum spanning
tree of G that has the least total cost.
Example
Given Graph G
Some Spanning Trees of G are:
Cost: 20
Cost: 13 Cost: 12 Cost: 6Cost: 6Cost: 15
Minimum Cost Spanning Tree (contd..)
The Minimum spanning trees for the above graph:
Least Cost: 6
Applications
Minimum-cost spanning trees have many applications. Some are:
Building cable networks that join n locations with minimum cost.
Building a road network that joins n cities with minimum cost.
Obtaining an independent set of circuit equations for an electrical network.
In pattern recognition minimal spanning trees can be used to find noisy pixels.
1. All vertices are marked as not visited
2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C)
3. The smallest- weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST.
4. The process is repeated until a spanning tree is formed
Prim’s Algorithm
Example
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
Select A
E
A
2
E is of least cost
E
A
D2
1
From E, D is of Least Cost
FE
A
D2
1 2
From D, F is of Least Cost
D has equal weight 2. so B is also used
FE
A B
D2
1 2
2
From B to F, it forms a Cycle. (Discard)
FE
A B
D2
1 2
2
Cycle
The remaining vertex C is used
C
FE
A B
D
3
2
1 2
2
C
FE
A B
D
3
2
1 2
3
Minimum Spanning Tree
Running time: O ( m log n )
By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time
Time Complexity
1. Each vertex is in its own cluster
2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a single cluster - if e connects two vertices, which are already in the same cluster, ignore it
3. Continue until n-1 edges were selected
Kruskal’s Algorithm
C
FE
A B
D
5
64
3
4
2
1 2
3
2
Example
E
D
1
Least Cost of given Graph is 1
E
B
D
1
2
The next Least Cost is 2
E
A B
D2
1
2
Least Cost 2
FE
A B
D2
1 2
2
Least Cost 2
FE
A B
D2
1 2
3
2
Cycle
Next Least Cost is 3,from B to F
FE
A B
D2
1 2
2
Cycle is Discarded
CFE
A B
D
3
2
1 2
2
The Next Least Cost is 3 with vertex C
C
FE
A B
D
3
2
1 2
2
Minimum Spanning Tree
Running time: O ( m log n )
By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time
Time Complexity
Thank you