0% found this document useful (0 votes)
164 views34 pages

Minimum Spanning Tree

The document summarizes the minimum spanning tree algorithm in 3 sentences: A minimum spanning tree is a spanning tree that connects all nodes of a graph with the minimum total cost of edges. Prim's algorithm and Kruskal's algorithm are two common algorithms used to find the minimum spanning tree by iteratively adding edges that do not form cycles while minimizing the total cost. The document provides an example application of Prim's algorithm on a sample graph to find its minimum spanning tree.

Uploaded by

AKHILA KHODAY
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)
164 views34 pages

Minimum Spanning Tree

The document summarizes the minimum spanning tree algorithm in 3 sentences: A minimum spanning tree is a spanning tree that connects all nodes of a graph with the minimum total cost of edges. Prim's algorithm and Kruskal's algorithm are two common algorithms used to find the minimum spanning tree by iteratively adding edges that do not form cycles while minimizing the total cost. The document provides an example application of Prim's algorithm on a sample graph to find its minimum spanning tree.

Uploaded by

AKHILA KHODAY
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/ 34

MINIMUM SPANNING

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.

•The spanning tree does not have any cycle (loops).

•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.

PROPERTIES OF SPANNING TREE


MINIMUM SPANNING TREE
• Definition:
A minimum spanning tree of a given graph G is a spanning
tree whose cost is minimum.

A graph will have only one minimum spanning tree for all
the edges with distinct weights.

Useful in Routing Applications, like communication


networking.
2
A B
Consider the same graph:
8 1

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.

• It’s a modified version of Dijkstra’s Algorithm.


Steps to solve the Prims algorithm

• Step1: Draw the cost adjacency matrix

• Step 2: Find the least cost nodes(vertices) and


consider the first nodes as source.
• Step 3:
a) Initialize the matrix d with the values of the source
from the cost adjacency matrix.
b) Initialize the path matrix p with the values of the
edges in each other.
• Step 4: Find the next least path by following formulas
in tabular form.
S v = V - d[v] = p[v] = u u,d[u] Output,
S min(d[v],cost[u][v]) p[u],u
Prim’s Algorithm Problem
Consider the following 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.

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,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, 2,6 d[2] = min(d[2],cost[1][2]


5, =min(30,25) = 25
3, p[2] = 1 2,25 1,2
1 d[6] =min(d[6],cost[1][6]
=min(45,∞) = 45
p[1] = 3
W 1 2 3 4 5 6 d[1] = 15
1 0 25 15 ∞ ∞ ∞
p[2] = 1
d[2] = 30
25
2 25 0 ∞ 30 40 35 p[3] = 5
d[3] = 20 p[4] = 5
3 15 ∞ 0 ∞ 20 ∞
4 ∞ 30 ∞ 0 10 50 d[4] = 0 p[5] = 4
5 ∞ 40 20 10 0 45 d[5] = 10 p[6] = 0
6 ∞ 35 ∞ 50 45 0 d[6] = 45
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:

(4,5) (5,3) (3,1) (1,2) (2,6)

Minimum Spanning Tree: The cost of minimum


spanning tree:
25
1 2 10 + 20 +15 + 25 + 35
35
15 =
105
3 4 6

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).

• It has two primary operations: UNION and FIND


FIND
• Say the selected vertex is u, this function find(u) will return the root
vertex of the corresponding tree.
find(2) = 2 8 find(8) = 8
2
find(5) = 2 find(7) = 8
find(9) = 2 find(1) = 8
find(6) = 2

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,

union(i,j) will merge two trees into a single tree.


Suppose I want to merge these two tree to form a spanning tree, with help of union function,
as follows,
2 8

5 9 6 7 1

4 3

Arrange the vertex in an array of integers


Initially
Array S [1] [2] [3] [4] [5] [6] [7] [8] [9]
Vertex 1 2 3 4 5 6 7 8 9

Once you find the root node, update it in table


Array S [1] [2] [3] [4] [5] [6] [7] [8] [9]
Vertex 8 2 5 5 2 2 8 8 2

Root of Root of 3, 4 Root of


7,1 is 8 is 5 5, 6,9 is 2
2 8

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

2 d,e i = find(d) = d d,e


a,c 3 a a c d d f
J = find(e) = e

b,d 3 3 a,c i = find(a) = a a,c


a a a d d f
J = find(c) = c
d,f 3
4 b,d i = find(b) = a b,d
J = find(d) = d a a a b d f
b,e 4

5 d,f i = find(d) = a d,f a a a b d d


c,e 4 J = find(f) = f

e,f 5 6 b,e i = find(b) = a discard


J = find(e) = a a a a b d d

b,c 5
3
b d 3
2

2
a f
3 c e

The cost of NOTE: Kruskal’s Algorithm allows


minimum spanning tree: to form a minimum spanning tree
with |V| -1 edges only.
2+3+3+2+2 = 13

You might also like