0% found this document useful (0 votes)
4 views

Chapter11C

This document discusses the concept of Minimum Spanning Trees (MST) in graph theory, explaining their properties, applications, and algorithms for finding them, specifically Kruskal's and Prim's algorithms. It highlights the differences between these algorithms, including their efficiency based on graph density and edge weight characteristics. The document also provides examples and outlines the time complexity for each algorithm.

Uploaded by

Mahima Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter11C

This document discusses the concept of Minimum Spanning Trees (MST) in graph theory, explaining their properties, applications, and algorithms for finding them, specifically Kruskal's and Prim's algorithms. It highlights the differences between these algorithms, including their efficiency based on graph density and edge weight characteristics. The document also provides examples and outlines the time complexity for each algorithm.

Uploaded by

Mahima Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

DESIGN & ANALYSIS OF ALGORITHM

(BCSC1012)
Chapter 11: Minimum Spanning Tree

Prof. Anand Singh J a l a l


Department of Computer Engineering & Applications
Spanning Tree
• 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
• Every connected and undirected Graph G has at least one 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.

Two most important spanning tree algorithms here −


1.Kruskal's Algorithm
2.Prim's Algorithm

Both are greedy algorithms.


Minimum Spanning Tree (MST)
Kruskal’s Algorithm
• 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.

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 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 BC and edge CF to the spanning tree as it does not generate any
loop.
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

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

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

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

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

Minimum spanning tree with total cost 11 (= 1 + 2 + 3 + 5).


Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example
Problem: Construct the minimum spanning tree (MST) for the given graph using
Kruskal’s Algorithm-
Minimum Spanning Tree (MST)
Kruskal’s Algorithm: Example

Solution

Weight of the MST = Sum of all edge weights = 10 + 25 + 22 + 12 + 16 + 14 =


99 units
Minimum Spanning Tree (MST)
Prim’s Algorithm
• Prim’s Algorithm also use Greedy approach to find the minimum spanning tree.
• In Prim’s Algorithm we grow the spanning tree from a starting position.
• Unlike an edge in Kruskal's, we add vertex to the growing spanning tree in Prim's.

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:

Cost of Minimum Spanning Tree= Sum of all edge weights= 10 + 25 + 22 + 12 +


16 + 14= 99 units
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

Solution

Cost of Minimum Spanning Tree = Sum of all edge weights = 1 + 4 + 2 + 6 + 3 + 10


= 26 units
Minimum Spanning Tree (MST)
Prim’s Algorithm: Time Complexity
 The running time of Prim’s algorithm depends on how we implement the min priority queue
Q.
 If we implement Q as a binary min-heap, we can use the BUILD-MIN-HEAP procedure to
perform lines 1–5 in O(V) time.
 The body of the while loop executes V times, and since each EXTRACT-MIN operation takes
O(lg V) time, the total time for all calls to EXTRACT-MIN is O(V lg V).
 The for loop in lines 8–11 executes O(E) times altogether, since the sum of the lengths of all
adjacency lists is 2E.
 The assignment in line 11 involves an implicit DECREASE-KEY operation on the min-heap,
which a binary min-heap supports in O(lg V) time.

 Thus, the total time for Prim’s algorithm is O(V lg V + E lg V) = O(E lg V)


 If we use a Fibonacci heap to implement the min-priority queue Q, the running time of
Prim’s algorithm improves to O(E+V lg V). // DECREASE-KEY operation (to implement
line 11) takes O(1) amortized time
Prim’s Vs Kruskal’s Algorithm

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:

Kruskal’s Algorithm is preferred when-


• The graph is sparse.
• There are less number of edges in the graph like E = O(V)
• The edges are already sorted or can be sorted in linear time.

Prim’s Algorithm is preferred when-


• The graph is dense.
• There are large number of edges in the graph like E = O(V2).
Difference Between Prim’s and Kruskal’s
Algorithm
Prim’s Algorithm Kruskal’s Algorithm
The tree that we are making or growing The tree that we are making or growing
always remains connected. usually remains disconnected.
Prim’s Algorithm grows a solution from a Kruskal’s Algorithm grows a solution
random vertex by adding the next cheapest from the cheapest edge by adding the
vertex to the existing tree. next cheapest edge to the existing tree
Prim’s Algorithm is faster for dense Kruskal’s Algorithm is faster for sparse
graphs. graphs.
Prim’s algorithm has a time complexity
Kruskal’s algorithm’s time complexity is
of O(V2), V being the number of vertices
O(E log V), V being the number of
and can be improved up to O(E log V)
vertices.
using Fibonacci heaps.
Any Questions ?
Dr. Anand Singh Jalal
Professor
Email: [email protected]

You might also like