0% found this document useful (0 votes)
6 views48 pages

3.5 Minimum Cost Spanning Trees Kruskal and Prim's Algorithms

The document discusses Minimum Spanning Trees (MST) and algorithms for finding them, specifically Prim's and Kruskal's algorithms. It outlines the properties of MSTs, the steps involved in both algorithms, and provides examples to illustrate their application. Additionally, it compares the two algorithms highlighting their differences in approach and execution.

Uploaded by

pratikd7150
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)
6 views48 pages

3.5 Minimum Cost Spanning Trees Kruskal and Prim's Algorithms

The document discusses Minimum Spanning Trees (MST) and algorithms for finding them, specifically Prim's and Kruskal's algorithms. It outlines the properties of MSTs, the steps involved in both algorithms, and provides examples to illustrate their application. Additionally, it compares the two algorithms highlighting their differences in approach and execution.

Uploaded by

pratikd7150
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/ 48

Module 3.

5 Minimum
Spanning Tree

Pradnya Bhangale
[email protected]
Tree and Graph
• A tree data structure is a hierarchical data structure that
consists of nodes connected by edges.
• Each node can have multiple child nodes, but only one
parent node.
• The topmost node in the tree is called the root node.
Spanning Tree

Conditions:
• V’=V
• E’ C E
• E’ = IVI-1
• Spanning tree no
G(V’,E’) cycle and is
connected
G(V,E)
Example

No.of Spanning Tree = 04


Minimizing Cost
Example
Minimum Spanning Tree
Minimum Spanning Tree:
Properties
Minimum Spanning Tree:
Properties
• Removing one edge from Spanning tree will make it
disconnected
• Adding one edge in Spanning tree will create a loop
• If each edge has distinct weight then there will be only one
and unique Minimum Spanning Tree(MST)
• A complete undirected graph will have nn-2 spanning tree
• Every connected and undirected graph has atleast one
spanning tree
• Disconnect graph does not have any spanning tree
Finding Spanning Tree
How does Prim’s Algorithm
Work?
• Step 1: Determine an arbitrary vertex as the starting vertex of
the MST.
• Step 2: Follow steps 3 to 5 till there are vertices that are not
included in the MST (known as fringe vertex).
• Step 3: Find edges connecting any tree vertex with the fringe
vertices.
• Step 4: Find the minimum among these edges.
• Step 5: Add the chosen edge to the MST if it does not form any
cycle.
• Step 6: Return the MST and exit
Prim’s Algorithm: Working
Prim’s Algorithm: Working
• Step 1: Firstly, we select an arbitrary vertex that acts as the
starting vertex of the Minimum Spanning Tree. Here we have
selected vertex 0 as the starting vertex.
Prim’s Algorithm: Working
• Step 2: All the edges connecting the incomplete MST and
other vertices are the edges {0, 1} and {0, 7}. Between these
two the edge with minimum weight is {0, 1}. So include the
edge and vertex 1 in the MST.
Prim’s Algorithm: Working
• Step 3: The edges connecting the incomplete MST to other
vertices are {0, 7}, {1, 7} and {1, 2}. Among these edges the
minimum weight is 8 which is of the edges {0, 7} and {1, 2}. Let
us here include the edge {0, 7} and the vertex 7 in the MST. [We
could have also included edge {1, 2} and vertex 2 in the MST].
Prim’s Algorithm: Working
• Step 4: The edges that connect the incomplete MST with the
fringe vertices are {1, 2}, {7, 6} and {7, 8}. Add the edge {7, 6}
and the vertex 6 in the MST as it has the least weight (i.e., 1).
Prim’s Algorithm: Working
• Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and
{6, 5}. Include edge {6, 5} and vertex 5 in the MST as the edge
has the minimum weight (i.e., 2) among them.
Prim’s Algorithm: Working
• Step 6: Among the current connecting edges, the edge {5, 2}
has the minimum weight. So include that edge and the vertex
2 in the MST.
Prim’s Algorithm: Working
• Step 7: The connecting edges between the incomplete MST
and the other edges are {2, 8}, {2, 3}, {5, 3} and {5, 4}. The
edge with minimum weight is edge {2, 8} which has weight 2.
So include this edge and the vertex 8 in the MST.
Prim’s Algorithm: Working
• Step 8: See here that the edges {7, 8} and {2, 3} both have
same weight which are minimum. But 7 is already part of MST.
So we will consider the edge {2, 3} and include that edge and
vertex 3 in the MST.
Prim’s Algorithm: Working
• Step 9: Only the vertex 4 remains to be included. The
minimum weighted edge from the incomplete MST to 4 is {3,
4}.
Prim’s Algorithm: Working
• The final structure of the MST is as follows and the weight of
the edges of the MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
Prim’s Algorithm: Working
• Note: If we had selected the edge {1, 2} in the third step then
the MST would look like the following.
Prim’s Algorithm: Example
Example: Prim’s Algorithm
Example: Prim’s Algorithm
Example: Prim’s Algorithm
Example: Prim’s Algorithm
Example: Prim’s Algorithm
Example: Prim’s Algorithm

E
Analysis
Example 1

Solution

Cost of MST = 37 units


Kruskal’s Algorithm
• Kruskal's algorithm develops the MST of a given graph by
constructing a forest of minimum weighted edges that can be
completed into a spanning tree if it is acyclic.
The general procedure
1. Construct a forest of n distinct partial trees each of them
having a single node of a given graph of n vertices.
2. Connect two distinct partial trees into a single partial tree by
adding a minimum weighted edge of a graph to a forest.
(The ties for the selection of minimum weighted edge are
broken randomly.)
3. Repeat step (2) until a single partial tree is obtained.
• This is a minimum spanning tree of a given graph.
Example 1
Find the minimum spanning tree of the following graph using
Kruskal's algorithm.

(1) Arrange all edges in ascending


order of their weights as below :
<D, F>, <B, E>, <A, C>, <B, D>,
<B, C>, <A, B>, <D, E>, <C, D>, <C, F>
Example 1: Solution
(2) Construct a forest of 6 (3) Include a minimum weighted
partial trees each with a edge <D, F> to connect
single node. nodes D and F.
Example 1: Solution
(4) Include the next minimum (5) <A, C> and <B, D> are two
weighted edge <B, E> to edges with the same
connect node B and E. minimum weight. Randomly
breaking this tie, include
an edge <A, C> and then the edge
<B, D>.
Example 1: Solution
(6) Include the next minimum (7) The addition of any of the
edge <B, C> to connect two remaining edges <A, B>,
subtrees <D, E>, <C, D>, <C, F> to the forest
(i) a subtree with nodes A and obtained in step (6) forms a cycle,
C, so we cannot add any of them to
the forest.
(ii) (ii) a subtree with nodes B, D,
A tree formed in the above step (6)
E, F. is a spanning tree of a given graph
with the cost = 1 + 2 + 4 + 4 + 6 = 17
units.
Example 2:
• Find the MST of the following graph using Kruskal's algorithm.
Example 2: Solution
(1) Arrange all edges in ascending order of their weights as
below :
<0,4>, <3, 5>, <0, 1>, <1, 2>, <4, 5>, <1, 4>, <3, 4>, <0, 3>,<2, 4>,
<2, 5>

(2) Construct a forest of 5 partial trees each with a single node.


Example 2: Solution
(3) Include the first minimum
(4) Include the next minimum
weighted edge <0, 4> to
weighted edge <3, 5> to
connect nodes 0 and 4.
connect nodes 3 and 5.
Example 2: Solution
(5) Include the next minimum weighted edge <0, 1> or <1, 2> to
connect weight nodes 0 and 1 or nodes 1 and 2 respectively. The
ties

OR
Example 2: Solution
(6) If an edge <0, 1> is added in step (5) then the next minimum
weighted edge is <1, 2> to be added to connect nodes 1 and 2. If
an edge <1, 2> is added in step (5) then the next minimum
weighted edge is <0, 1> to be added to connected nodes 0 and
1.
Example 2: Solution
(7) Include the next minimum weighed edge is <4, 5> to connect
two subtrees
(i) a subtree with nodes 0, 1, 2, 4 and
(ii) a subtree with nodes 3, 5
Example 2: Solution
(8) The addition of any of the remaining edges <1, 4>,<3, 4>, <0,
3>, <2, 4>, <2, 5> to the forest obtained in step (7) forms a cycle,
so we cannot add any of them to the forest. A tree formed in the
above step (7) is a spanning tree of a given graph with the cost =
1 + 2 + 3 + 3 + 4 = 13 units
Total Time Complexity = O(V) + O(ElogE) + O(E) + O(log V) = O(ElogE)
Example: Kruskal’s Algorithm

Solution

Cost of MST = 99 units


Difference
Sr No Prim’s algorithm Kruskal’s algorithm
1 It first checks for the feasibility It first checks for the optimality
(unvisited and adjacent nodes) (minimum weighted edge) and
and then for optimality (the then for the feasibility
nearest node). (connectedness and acyclic nature
of a graph).
2 No sorting of edges of a graph is It arranges all edges of
required a graph in ascending
order.
3 At every stage, one and only one At every stage, one or more than
connected component is formed one connected components are
formed
4 No forest but only a single Every intermediate step builds a
connected subtree is built in all forest of partial subtrees
intermediate steps
5 It begins with the random It begins with all the nodes of a
selection of any single node as a graph forming a forest
Connected component.
Thank You!

You might also like