You are on page 1of 31

Prim’s and Kruskal’s Algorithm

Presented
By
V.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

Cost: 20

Some Spanning Trees of G are:

Cost: 13 Cost: 12 Cost: 15 Cost: 6 Cost: 6


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.
Prim’s Algorithm

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


Example

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Select A

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
E is of least cost

E
From E, D is of Least Cost

2 D

1
E
From D, F is of Least Cost

2 D

1 2
E F
D has equal weight 2. so B is also used

A B
2

2 D

1 2
E F
From B to F, it forms a Cycle.
(Discard)

A B
2

2 D
Cycl
e

1 2
E F
The remaining vertex C is used

A B
2

2 D

C 1 2

3 E F
Minimum Spanning Tree

A B

2 D 3
C

3 1 2
E F
Time Complexity

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
Kruskal’s Algorithm

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


Example

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Least Cost of given Graph is 1

1
E
The next Least Cost is 2

B
2

1
E
Least Cost 2

A B
2

2 D

1
E
Least Cost 2

A B
2

2 D

1 2
E F
Next Least Cost is 3,from B to F

A B
2

2 3 Cycl
D e

1 2
E F
Cycle is Discarded

A B
2

2 D

1 2
E F
The Next Least Cost is 3 with vertex C

A B
2

2 D

1 2
C
3 E F
Minimum Spanning Tree

A B
2

2 D
C

3 1 2
E F
Time Complexity

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
Thank you

You might also like