0% found this document useful (0 votes)
17 views54 pages

Minimum Spanning Tree 30

A Minimum Spanning Tree (MST) is a connected, acyclic subgraph of an undirected graph that includes all nodes and has the minimum total edge weight. Algorithms like Prim's and Kruskal's are used to find MSTs, both of which are greedy algorithms that work with weighted graphs. MSTs have various applications, including network design and cluster analysis.

Uploaded by

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

Minimum Spanning Tree 30

A Minimum Spanning Tree (MST) is a connected, acyclic subgraph of an undirected graph that includes all nodes and has the minimum total edge weight. Algorithms like Prim's and Kruskal's are used to find MSTs, both of which are greedy algorithms that work with weighted graphs. MSTs have various applications, including network design and cluster analysis.

Uploaded by

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

Minimum Spanning

Tree
Definition : Minimum Spanning
Tree
A Minimum Spanning Tree (MST)
is a sub graph of an undirected
graph such that the sub graph
spans (includes) all nodes, is
connected, is acyclic, and has
minimum total edge weight
Minimum Spanning Tree (MST)
• A spanning tree is a subset of Graph G, which has all the vertices covered
with minimum possible number of edges. Hence, a spanning tree does
not have cycles and it cannot be disconnected.
Algorithm Characteristics

• Both Prim’s and Kruskal’s Algorithms work with


undirected graphs
• Both work with weighted and unweighted graphs
but are more interesting when edges are weighted
• Both are greedy algorithms that produce optimal
solutions
Properties of MST
• 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
Mathematical Properties
• Spanning tree has n-1 edges, where n is the
number of nodes (vertices).
• From a complete graph, by removing maximum
e - n + 1 edges, we can construct a spanning tree.
• A complete graph can have maximum nn-2 number
of spanning trees.
Applications of MST
• 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:
• Cluster Analysis
• Handwriting recognition
• Image segmentation
• Civil Network Planning
• Computer Network Routing Protocol
Kruskal's algorithm

• To find the minimum cost spanning tree uses the


greedy approach.
• This algorithm treats the graph as a forest and every
node it has as an individual tree.
• A tree connects to another only and only if, it has the
least cost among all available options and does not
violate MST properties.
Kruskal's algorithm
• Remove all loops and Parallel Edges
• Arrange all edges in their increasing order of
weight
• Add the edge which has the least weightage
Kruskal's algorithm
STEP 1
STEP 2

STEP 3 (Iterative)
Kruskal’s Algorithm
• Work with edges, rather than nodes
• Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not generate a cycle
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1 (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10

Accepting edge (E,G) would create a cycle


Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1 (C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
F C edge dv edge dv

A 4
3 (D,E) 1  (B,E) 4 
(D,G) 2  (B,F) 4 
5
B D (E,G) 3  (B,H) 4 
H 1 (C,D) 3  (A,H) 5 
2

}
3 (G,H) 3 (D,F) 6
G E (C,F) 3  (A,B) 8
not
considered
(B,C) 4  (A,F) 10
Done
Total Cost =  dv = 21
Exercise – Kruskal’s Algorithm
Prim’s Algorithm
• Remove all loops and parallel edges
• Choose any arbitrary node as root node
• Check outgoing edges and select the one with
less cost
Walk-Through
2
Initialize array
3
10
F C K dv pv
Step 1
A 7
4
3 A F  
8
18 B F  
4
9
B D C F  
10
H 25 D F  
2
3 E F  
G 7
E F F  
G F  
H F  
2 Step 2
3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
B Start with any node, say D
9 D C
10
H 25 D T 0 
2
3 E
G 7
E F
G
H
2

3
10
F C K dv pv
Step 3
A 7
4
3 A
8
18 B
4 Update distances
9
B D C 3 D
10 of adjacent,
H D T 0 
2 25 unselected nodes
3 E 25 D
G 7
E F 18 D
G 2 D
H
2

3
10
F C K dv pv

A 7
4
3 A
8
4
18 B Select node with
9
B D C 3 D
10 minimum distance
H 25 D T 0 
2
3 E 25 D
G 7
E F 18 D
G T 2 D
H
2

3
10
F C K dv pv

A 7
4
3 A
8
4
18 B Update distances
9
B D C 3 D of adjacent,
10
H 25 D T 0  unselected nodes
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A
8
18 B
4
B Select node with
9 D C T 3 D
10
H 25 D T 0  minimum distance
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D Update distances
10
H 25 D T 0  of adjacent,
2
3 E 7 G
unselected nodes
G 7
E F 3 C
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
Select node with
10
H 25 D T 0  minimum distance
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
B Update distances
9 D C T 3 D
of adjacent,
10
H 25 D T 0  unselected nodes
2
3 E 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D Select node with
10
H 25 D T 0 
2 minimum distance
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
2

3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D Update distances
10
H 25 D T 0  of adjacent,
2
3 E T 2 F unselected nodes
G 7
E F T 3 C
G T 2 D
H 3 G
Table entries unchanged
2

3
10
F C K dv pv

A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25 D T 0  Select node with
2
3 E T 2 F
G E minimum distance
7 F T 3 C
G T 2 D
H T 3 G
2

3
10
F C K dv pv

A 7
4
3 A 4 H
8
18 B 4 C
4
9
B D C T 3 D
Update distances
H 10 of adjacent,
25 D T 0 
2 unselected nodes
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2

3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B 4 C
4 Select node with
9
B D C T 3 D
10
H 25 D T 0  minimum distance
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
2

3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10 Update distances
H 25 D T 0  of adjacent,
2
3 E T 2 F unselected nodes
G 7
E F T 3 C
G T 2 D
H T 3 G
Table entries unchanged
2

3
10
F C K dv pv

A 7
4
3 A T 4 H
8
18 B T 4 C
4
9
B D C T 3 D
10 Select node with
H 25 D T 0 
2
3 E T 2 F minimum distance
G 7
E F T 3 C
G T 2 D
H T 3 G
2 K dv pv
A T 4 H
3
F C B T 4 C
C T 3 D
A 4
3
D T 0 
4
B D E T 2 F
H F T 3 C
2
3
G T 2 D
G E H T 3 G

Cost of Minimum Spanning Tree =  dv = 21

Done
Algorithm
1) Create a set mstSet that keeps track of vertices already
included in MST.
2) Assign a key value to all vertices in the input graph. Initialize all
key values as INFINITE. Assign key value as 0 for the first vertex so
that it is picked first.
3) While mstSet doesn’t include all vertices
….a) Pick a vertex u which is not there in mstSet and has minimum
key value.
….b) Include u to mstSet.
….c) Update key value of all adjacent vertices of u. To update the
key values, iterate through all adjacent vertices. For every
adjacent vertex v, if weight of edge u-v is less than the previous
key value of v, update the key value as weight of u-v
Algorithm Prim(G)
//Prims algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = (V,E)
//Output: ET the set of edges composing minimum spaning tree og G
VT  {v0}
for i  1 to |V| -1 do
find a minimum weight edge among all edge such that v is in VT u is in V- VT
VT  VT U {u}
ET  ET U {e}
return ET
Exercise – Prim’s Algorithm

You might also like