Minimum Spanning Tree (Prim's and Kruskal's Algorithms)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Minimum spanning tree

(prim's and Kruskal's


algorithms)
GROUP 8
GROUP MEMBER
21BCE11612 DEVAYAN DEWARI
21BCE11631 HANISH THARWANI
21BCE11643 ASHISH SHUKLA
21BCG10106 YOGRAJ NIMGANKOR
21BCG10122 VEDANG GUPTA
21MIP10001 DEVENDRA THAKUR
21MIP10036 JATIN KUSHWAHA
21MIP10036 ASHISH KUMAR PATEL
Spanning Tree
A Spanning tree can be defined as a subset of a graph,
which consists of all the vertices covering minimum
possible edges and does not have a cycle. Spanning tree
cannot be disconnected.
Every connected and undirected graph has at least one
spanning tree. A disconnected graph does not have a
spanning tree as it is not possible to include all vertices.

Consider the following connected graph.

As shown above, for the given connected Graph


containing 3 vertices, we have three spanning trees. In
general, if N is the number of nodes in a graph, then a
complete connected graph has maximum NN-2 number of
spanning trees. Thus in the above graph N =3, therefore,
it has 3(3-2) = 3 spanning trees.
Some of the properties of the spanning tree are listed
below:
•A connected graph can have more than one spanning
trees.

•All spanning trees in a graph have the same number of


nodes and edges.

•If we remove one edge from the spanning tree, then it


will become minimally connected and will make the
graph disconnected.

•On the other hand, adding one edge to the spanning tree
will make it maximally acyclic thereby creating a loop.

•A spanning tree does not have a loop or a cycle.


What Is A Minimum Spanning The initial graph is:

Tree (MST)
A minimum spanning tree is the one that
contains the least weight among all the The possible spanning trees from the above
graph are:
other spanning trees of a connected
weighted graph. There can be more than
one minimum spanning tree for a graph.

Example of a Spanning Tree


Let's understand the above
definition with the help of the
example below.
There are two most popular algorithms that are used to find the minimum spanning tree
in a graph.
They include:
•Kruskal’s algorithm
•Prim’s algorithm
Let’s discuss both these algorithms!

Kruskal’s Algorithm
Kruskal’s algorithm is the concept that is introduced in the graph theory of discrete mathematics. It is used to
discover the shortest path between two points in a connected weighted graph. This algorithm converts a given
graph into the forest, considering each node as a separate tree. These trees can only link to each other if the edge
connecting them has a low value and doesn’t generate a cycle in MST structure.
Kruskal’s algorithm is the concept that is introduced in the graph theory of discrete mathematics. It is used to
discover the shortest path between two points in a connected weighted graph. This algorithm converts a given
graph into the forest, considering each node as a separate tree. These trees can only link to each other if the edge
connecting them has a low value and doesn’t generate a cycle in MST structure
Now we choose the edge with the least weight which is 2-4.
Pseudocode for Kruskal’s Algorithm

Next, choose the next shortest edge 2-3.

Then we choose next edge with the shortest edge and that does
Now let us see the illustration of Kruskal’s algorithm. not create a cycle i.e. 0-3
The next step is to choose the shortest edge so that it doesn’t
form a cycle. This is 0-1.

As we can see, we have covered all


the vertices and we have a spanning
tree with minimum cost here.

What Is Union Find Algorithm?


Union Find is an algorithm that keeps track of elements that are split into one or over one disjoint set. It has
two primary operations: Find and Union. The Find operation returns the set of elements to which the given
element (argument) belongs, whereas the Union operation merges two disjoint sets.
You need to divide the provided graph G(V, E) into three separate sets while building the Minimum Spanning
Tree using Kruskal's approach. The first contains edge weight values, the second has a tree hierarchy for
distinct nodes, and the third includes the rank of all nodes. By using Union and Find operations, it joins the
distinct nodes which are treated as different trees themselves to formulate a minimum spanning tree. 
Implementation of Kruskal Algorithm
Any MST algorithm revolves around determining whether adding an edge would result in a loop or not. Union Find
is the most popular algorithm for determining this. The Union-Find algorithm separates vertices into clusters,
allowing you to determine whether two vertices belong to the same cluster and hence if adding an edge will produce
a cycle.

The strategy to implement the Kruskal algorithm using Union-Find is given below:

•Construct a structure to keep track of the source and destination nodes, as well as their weight.

•Sort all the edges of a graph according to their edge-weight values.

•Create three distinct sets to maintain nodes of a graph, their hierarchy in a tree, and corresponding ranks for every
node.

•Primarily, initialize all rank values to 0 and parent values to -1 (representing each node as its own tree itself).

•For each insertion of an edge in MST, you will update the rank and parent of each node.

•Do not insert the edge connecting two nodes if they have the same parent node, as this will cause a cycle in the tree
structure.
Prim’s Algorithm
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim's algorithm finds
the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting edges at every step. The
edges with the minimal weights causing no cycles in the graph got selected.

How does the prim's algorithm work?


Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the edges with the
smallest weight until the goal is reached. The steps to implement the prim's algorithm are given as follows -
•First, we have to initialize an MST with the randomly chosen vertex.
•Now, we have to find all the edges that connect the tree in the above step with the new vertices. From the
edges found, select the minimum edge and add it to the tree.
•Repeat step 2 until the minimum spanning tree is formed.
The applications of prim's algorithm are -
•Prim's algorithm can be used in network designing.
•It can be used to make network cycles.
•It can also be used to lay down electrical wiring cables.
Example of prim's algorithm
Now, let's see the working of prim's algorithm using an example. It will be easier to understand the prim's
algorithm using an example.
Suppose, a weighted graph is -

Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.

Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from vertex B that
are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge BD has the minimum
weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In this case, the edges
DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E and A. So, select the edge DE
and add it to the MST.

Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to the
graph. So, choose the edge CA and add it to the MST.

So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost of the MST is
given below -
Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Algorithm
1.Step 1: Select a starting vertex  
2.Step 2: Repeat Steps 3 and 4 until there are fringe vertices  
3.Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight  
4.Step 4: Add the selected edge and the vertex to the minimum spanning tree T  
5.[END OF LOOP]  
6.Step 5: EXIT  
Pseudocode for Prim’s Algorithm

Time Complexity
Prim's algorithm can be simply implemented by using the adjacency matrix or adjacency list graph
representation, and to add the edge with the minimum weight requires the linearly searching of an array
of weights. It requires O(|V|2) running time. It can be improved further by using the implementation of
heap to find the minimum weight edges in the inner loop of the algorithm.
The time complexity of the prim's algorithm is O(E logV) or O(V logV), where E is the no. of edges,
and V is the no. of vertices.
Applications Of Spanning Tree
#1) Communications Network Setup: When we want to set up a communication network
using communication links, then the cost of setting up communication links between two
points is best determined using an MST
.
#2) Cluster Analysis: It can be used to solve the K-clustering problem by finding a
minimum spanning tree and deleting the k-1 most expensive edges.

#3) Laying Out Road/Rail Networks: When we lay various road or rail networks
between or within cities, the cost of the project is a very important factor. We can find the
best path with minimum cost using minimum spanning trees.

#4) Planning Housing Facilities: Facilities like electricity, water, sewage, etc. to be


provided to a number of houses also require to be at optimum cost and this is done using an
MST.

#5) Solving the Travelling Salesman Problem: We can use an MST to solve the traveling
salesman problem which requires to visit each point at least once.
Conclusion
The minimum spanning tree is the subset of
graph g and this subset has all the vertices of the
graph and the total cost of edges connecting the
vertices is minimum.
We discussed two algorithms i.e. Kruskal’s and
Prim’s, to find the minimum spanning tree from
the graph

You might also like