Professional Documents
Culture Documents
Minimum Spanning Tree (Prim's and Kruskal's Algorithms)
Minimum Spanning Tree (Prim's and Kruskal's Algorithms)
Minimum Spanning Tree (Prim's and Kruskal's Algorithms)
•On the other hand, adding one edge to the spanning tree
will make it maximally acyclic thereby creating a loop.
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.
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
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.
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.
•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.
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.
#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