Chapter11C
Chapter11C
(BCSC1012)
Chapter 11: Minimum Spanning Tree
A complete undirected graph can have maximum nn-2 number of spanning trees,
where n is the number of nodes.
General Properties of Spanning Tree
Following are few properties of the spanning tree connected to graph G −
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of 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.
• Spanning tree has n-1 edges, where n is the number of nodes (vertices).
Application of Spanning Tree
Spanning tree is basically used to find a minimum path to connect all nodes in a
graph. Common applications of spanning trees are −
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
• Designing Local Area Networks
• Construct highways or railroads
Let us understand this through a small example. Consider, city network as a huge
graph and now plans to deploy telephone lines in such a way that in minimum lines
we can connect to all city nodes. This is where the spanning tree comes into
picture.
Application of Spanning Tree …
Example, Problem laying Telephone Wire
Application of Spanning Tree …
Example, Problem laying Telephone Wire
Minimum Spanning Tree (MST)
• In a weighted graph, a minimum spanning tree is a spanning tree that
has minimum weight than all other spanning trees of the same graph.
• In real-world situations, this weight can be measured as distance,
congestion, traffic load or any arbitrary value denoted to the edges.
Algorithm Steps:
• Sort the graph edges with respect to their weights.
• Start adding edges to the MST from the edge with the smallest weight until the
edge of the largest weight.
• Only add edges which doesn't form a cycle , edges which connect only disconnected
components.
Minimum Spanning Tree (MST)
Kruskal’s Algorithm
Minimum Spanning Tree (MST)
Kruskal’s Algorithm
In the Algorithm:
• Lines 1–3 initialize the set A to the empty set and create V trees, one containing
each vertex.
• The for loop in lines 5–8 examines edges in order of weight, from lowest to highest
• The loop checks, for each edge (u,v), whether the endpoints u and v belong to the
same tree.
• If they do, then the edge (u,v) cannot be added to the forest without creating a
cycle, and the edge is discarded.
• Otherwise, the two vertices belong to different trees. In this case, line 7 adds the
edge (u,v) to A
• and line 8 merges the vertices in the two trees
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Arrange all edges in a sorted list by their edge weights
The Edges of the Graph Edge
Source Vertex Destination Vertex Weight
E F 2
F D 2
B C 3
C F 3
C D 4
B F 5
B D 6
A B 7
A C 8
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Pick is edge EF, as it has a minimum edge weight that is 2.
The Edges of the Graph
Edge
Source Destination
Weight
Vertex Vertex
E F 2
F D 2
B C 3
C F 3
C D 4
B F 5
B D 6
A B 7
A C 8
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Add edge FD to the spanning tree.
The summation of all the edge weights in MST T(V’, E’) is equal to 17, which is the least
possible edge weight for any possible spanning tree structure for this particular graph.
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Solution
Algorithm Steps:
• Maintain two disjoint sets of vertices. One containing vertices that are in the growing
spanning tree and other that are not in the growing spanning tree.
• Select the cheapest vertex that is connected to the growing spanning tree and is not
in the growing spanning tree and add it into the growing spanning tree. This can be
done using Priority Queues. Insert the vertices, that are connected to growing
spanning tree, into the Priority Queue.
Minimum Spanning Tree (MST)
Prim’s Algorithm
Lines 1–5 set the key of each vertex to infinity
(except for the root r, whose key is set to 0 so
that it will be the first vertex processed),
set the parent of each vertex to NIL, and
initialize the min priority queue Q to contain all
the vertices.
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Prim’s Algorithm: Example
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Problem: Construct the minimum spanning tree (MST) for the given
graph using Prim’s Algorithm-
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Step-02: Step-03:
Step-01:
Step-04:
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Step-05:
Step-06:
Problem: Construct the minimum spanning tree (MST) for the given
graph using Prim’s Algorithm-
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Solution
Important concepts
Concept-01:
If all the edge weights are distinct, then both the algorithms are guaranteed to find the
same MST.
Example:
Here, both the algorithms on the above given graph produces the same MST
https://www.gatevidyalay.com/prims-and-kruskal-algorithm-difference/
Prim’s Vs Kruskal’s Algorithm
Important concepts
Concept-02:
• If all the edge weights
are not distinct, then
both the algorithms
may not always produce
the same MST.
• However, cost of both
the MSTs would always
be same in both the
cases.
Prim’s Vs Kruskal’s Algorithm
Important concepts
Concept-03: