Minimum Spanning Tree
Minimum Spanning Tree
TREE
SPANNING TREE
• A spanning tree is a tree in which all nodes are connected without forming a
closed path or circuit.
• Formally, a spanning tree of graph G = (V, E)is defined as a sub graph.
• G’ = (V’, E’)
where,
V’ = V
E’ = |V|- 1
G’ is connected
G’ is acyclic
2
A B
Consider the graph:
8 1
D C
4
Following are the spanning trees
2
A B A B
8 1 8
D C D C
4 4
2
2 A B
A B
1
8 1
D C
D C 4
•A connected graph G can have more than one spanning tree.
•All possible spanning trees of graph G, have the same number of edges and
vertices.
•Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.
•Adding one edge to the spanning tree will create a circuit or loop, i.e. the
spanning tree is maximally acyclic.
A graph will have only one minimum spanning tree for all
the edges with distinct weights.
D C
4
The minimum spanning tree among these spanning tree is :
2
A B A B
8 1 8
D C D C
4 4
Cost = 8 + 4 + 1 = 13 Cost = 8 + 2 + 4 = 14
2 A 2 B
A B
8 1 1
D C D C
4
Cost = 8 + 2+ 1 = 11 Cost = 4 + 2 + 1 = 7
TWO ALGORITHMS TO FIND
MINIMUM SPANNING TREE
• Prim’s Algorithm
• Kruskal’s Algorithm
PRIM’S ALGORITHM
• Definition:
• Its one of the method to find the minimum spanning
tree of a given graph.
25
1 2
35
15
30
40 50
3 4 6
10
20
45
5
1 25 2
35
• Step 1: Cost Adjacency Matrix 15
30
3 40 4 50 6
10
20
5 45
W 1 2 3 4 5 6
1 0 25 15 ∞ ∞ ∞
2 25 0 ∞ 30 40 35
3 15 ∞ 0 ∞ 20 ∞
4 ∞ 30 ∞ 0 10 50
5 ∞ 40 20 10 0 45
6 ∞ 35 ∞ 50 45 0
• Step 2: the least cost with nodes are (4,5). Consider 4 as source.
• Step 3:
a) Distance matrix d can be given as (the row of source 4.) u is the
node value from source and d[u] is the cost from source to node u.
d[1] = ∞
For Reference.
d[2] = 30
1 25 2
d[3] = ∞ 15
35
30
3 40 4 50 6
d[4] = 0
10
20
d[5] = 10 5 45
d[6] = 50
b) Initialize the path matrix with the nodes of each other and other nodes with 0
p[1] = 0
p[2] = 0 4
10
p[3] = 0 5
p[4] = 5
p[5] = 4
p[6] = 0
Step 4:
Source vertex, Path for the New node found
For each vertices(v), find the minimum distance
vertices visited
Remaining current v = from d[v]
for source s.
so far. vertices current source.
4 1,2,3,5
,6 5,10 4,5
p[1] = 0
p[2] =4.0
5 is u found for source
4
add it to s.
p[3] = 0
10
p[4] = 5
5
p[5] = 4
p[6] = 0
S v = V - d[v] = p[v] = u u,d[u] Output,
S min(d[v],cost[u][v]) p[u],u
4, 1,2,3,6 d[1]=min(d[1],cost[5][1]
5 =min(∞,∞) = ∞
d[2] = min(d[2],cost[5][2])
=min(30,40) = 30 p[3] = 5 3,20 5,3
d[3] = min(d[3],cost[5][3]
=min(∞,20) = 20
d[6] = min(d[6],cost[5][6]
W 1 2 3 4 5 6 = min(50,45) = 45 d[1] = ∞
p[1] = 0
1 0 25 15 ∞ ∞ ∞ d[2] = 30
2 25 0 ∞ 30 40 35 p[2] = 0
p[3] = 5 d[3]
d[3] == 20
∞
3 15 ∞ 0 ∞ 20 ∞
4 ∞ 30 ∞ 0 10 50 p[4] = 5 d[4] = 0
5 ∞ 40 20 10 0 45 p[5] = 4 d[5] = 10
6 ∞ 35 ∞ 50 45 0 p[6] = 0 d[6] = 50
45
S v = V - d[v] = p[v] = u u,d[u] Output,
S min(d[v],cost[u][v]) p[u],u
4, 1,2,6
d[1] = min(d[1],cost[3][1]
5,
3 = min(∞,15) = 15
d[2]=min(d[2],cost[3][2] p[1] = 3 1,15 3,1
= min(30,∞) = 30
p[1] = 3
d[6] = min(d[6],cost[3][6] p[2] = 0
=min(45,∞) = 45 p[3] = 5 d[1] == 15
d[1] ∞
W 1 2 3 4 5 6
p[4] = 5 d[2] = 30
1 0 25 15 ∞ ∞ ∞
2 25 0 ∞ 30 40 35 p[5] = 4 d[3] = 20
3 15 ∞ 0 ∞ 20 ∞ p[6] = 0 d[4] = 0
4 ∞ 30 ∞ 0 10 50
d[5] = 10
5 ∞ 40 20 10 0 45
d[6] = 45
6 ∞ 35 ∞ 50 45 0
S v = V - d[v] = p[v] = u u,d[u] Output,
S min(d[v],cost[u][v]) p[u],u
4, 6
5,
3, d[6] =min(d[6],cost[2][6] p[6] = 2 6,35 2,6
1, =min(45,35) = 35
2
W 1 2 3 4 5 6 d[1] = 15 p[1] = 3
1 0 25 15 ∞ ∞ ∞ d[2] = 30
25 p[2] = 1
2 25 0 ∞ 30 40 35 p[3] = 5
d[3] = 20
3 15 ∞ 0 ∞ 20 ∞ p[4] = 5
4 ∞ 30 ∞ 0 10 50 d[4] = 0
p[5] = 4
5 ∞ 40 20 10 0 45 d[5] = 10 p[6] = 2
6 ∞ 35 ∞ 50 45 0 d[6] = 35
45
S v = V - d[v] = p[v] = u u,d[u] Output,
S min(d[v],cost[u][v]) p[u],u
4,
5,
3,
1,
2,
6
The output column yields, following paths:
10
20
5
HOME WORK
60
0 1
40
10 20
70
2 3 4
80
50 30
5
Kruskal’s Algorithm
• Definition:
• It’s an algorithm that allows us to find the minimum
cost spanning tree.
Procedure
For a given graph G = (V , E) , be a directed or undirected graph, this
algorithm selects n – 1 edges in the minimum spanning tree,
where n be the number of vertices.
1. Sort the edge in ascending order of the cost
2. Start adding edges to the minimum spanning tree from the edge with the
smallest cost until the edge of the largest cost. Only add edges which
don't form a cycle—edges which connect only disconnected components
3. The algorithm terminates when n – 1 edges are selected and there are no
more edges to consider.
Union and Find Operation
• These are the two functions important for designing the Kruskal’s
Algorithm.
• Definition: It’s a data structure that keeps track of elements which are
split into one or more disconnected sets(disjoint sets).
5 9 6 7 1
find(4) = 5 2
4 3 find(3) = 5 2
UNION
• Say you have two different trees, and you want to
merge them, into one tree to form a minimum
spanning tree,
5 9 6 7 1
4 3
5 9 6 7 1
4 3
Array S [1] [2] [3] [4] [5] [6] [7] [8] [9]
Vertex 8 2 5 5 2 2 8 2 2
Root of
8 is 2
Problem
Consider the following graph
Edge Cost
a,b 2
d,e 2
a,c 3
b,d 3
d,f 3
b,e 4
c,e 4
e,f 5
b,c 5
Edge Cost Step Deleted i = find(u) O/P S[a] [b] [c] [d] [e] [f]
s Edge j = find(v) u,v a b c d e f
(u,v)
a,b 2
1 a,b i = find(a) = a a,b
a a c d e f
d,e 2 J = find(b) = b
b,c 5
3
b d 3
2
2
a f
3 c e