0% found this document useful (0 votes)
9 views22 pages

Unit 5 Session 3 Kruskals

Uploaded by

fordgt90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Unit 5 Session 3 Kruskals

Uploaded by

fordgt90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

SR

M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
18CSC201J
DATA STRUCTURES AND ALGORITHMS
Unit- V
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.

MINIMUM SPANNING TREE


KRUSKAL’S ALGORITHM
Minimum Spanning Tree
Spanning Tree
Given an undirected and connected graph G=(V,E), a spanning tree
of the graph G is a tree that spans G (that is, it includes every vertex
of G) and is a subgraph of G (every edge in the tree belongs to G)

Minimum Spanning Tree

The cost of the spanning tree is the sum of the weights of all the
edges in the tree. There can be many spanning trees. Minimum
spanning tree is the spanning tree where the cost is minimum among
all the spanning trees. There also can be many minimum spanning
trees.
Minimum Spanning Tree
Minimum spanning tree has direct application in the design of
networks. It is used in algorithms approximating the travelling
salesman problem, multi-terminal minimum cut problem and
minimum-cost weighted perfect matching. Other practical
applications are:
1.Cluster Analysis
2.Handwriting recognition
3.Image segmentation
Kruskal’s Algorithm

Kruskal's algorithm is a minimum spanning tree algorithm that takes


a graph as input and finds the subset of the edges of that graph
which

1. form a tree that includes every vertex


2. has the minimum sum of weights among all the trees that can be
formed from the graph
Algorithm

Input : Graph with V edges


Output : Minimum Spanning tree with V-1 edges

Step 1. Sort all the edges in non-decreasing order of their weight.


Step 2. Pick the smallest edge. Check if it forms a cycle with the
spanning tree formed so far. If cycle is not formed, include this edge.
Else, discard it.
Step 3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Example

3 4
2 3 4
4 5
2
9
5 6
1
1
12
8 10
7 8 9
7

The graph contains 9 vertices and 11 edges. So, the minimum spanning
tree formed will be having (9 – 1) = 8 edges.
Sort the weights of the graph
Weight Source Destination
1 8 5
2 3 5
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
Now pick all edges one by one from sorted list of edges

5
Weigh
t
Source Destina
tion 1
1 8 5
8
2 3 5
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3

2
5

Weigh Source Destina


1
t tion

1 8 5 8
2 3 5
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3
2 3

2
5
Weigh Source Destina
t tion
1
1 8 5
2 3 5 8
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3
2 3
4
2
1 5
Weigh Source Destina
t tion 1
1 8 5
2 3 5 8
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
4
2
1 5
Weigh Source Destina
t tion 1
1 8 5
2 3 5
8
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
6
1 5
1
Weigh Source Destina
t tion 8
1 8 5
2 3 5
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
6
1 5
1
Weigh Source Destina
t tion 7 8
7
1 8 5
2 3 5
3 2 3
4 1 2
4 3 4
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3
2 3 4
5
4 4
2
6
1 5
1
Weigh Source Destina 8
t tion
7 8
1 8 5 7
2 3 5
3 2 3
4 1 2
4 3 4 Cycle is formed because of weight 8. hence it is discarded
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
9 6
1 5
1

7 8
Weigh Source Destina 7
t tion

1 8 5
2 3 5
3 2 3
4 1 2
4 3 4 Cycle is formed because of weight 9. hence it is discarded.
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
6
1 5
1
10
7 8 9
7
Weigh Source Destina
t tion

1 8 5
2 3 5
3 2 3
4 1 2
4 3 4 Cycle is formed because of weight 12. hence it is discarded.
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
6
1 5
1
12
10
7 8 9
7
Weigh Source Destina
t tion

1 8 5
2 3 5
3 2 3
4 1 2
4 3 4 Cycle is formed because of weight 12. hence it is discarded.
5 4 6
7 7 8
8 1 7
9 5 6
10 8 9
12 9 6
3 4
2 3 4
5
4
2
6
1 5
1
10
7 8 9
7

Minimum Spanning tree generated by Kruskal’s algorithm


References:
1. A.V.Aho, J.E Hopcroft , J.D.Ullman, Data structures and Algorithms, Pearson Education, 2003
2. Reema Thareja, Data Structures Using C, 1 st ed., Oxford Higher Education, 2011
Thank You

You might also like