Data Structures & Algorithms

# Prims Algorithm

## Minimum Spanning Tree:

A graph that doesn’t have directions is called an undirected graph. Every graph must have a path from one node to another node. A spanning tree is also an undirected connected graph where all the nodes of the graph are present with minimum edges. If a spanning tree doesn’t have all the nodes of the graph, then we cannot say that it is a spanning tree. The spanning-tree total weights will be less than the original weight of the graph as we connected it through the minimum weight edges. The spanning-tree also does not have a cycle. Any graph has more than one spanning tree, but only one of those will be unique. We call it a minimal spanning tree since we’re attempting to create a full graph with all nodes while keeping the weight low.

We can draw a spanning tree with the help of the following two methods:

1. Kruskal’s algorithm
2. Prim’s algorithm

In this article, we are going to discuss Prim’s algorithm. Kruskal’s algorithm will be discussed in the next article.

## Prim’s Algorithm:

Prim’s algorithm is used to find the minimum spanning tree of a graph. The prim’s algorithm starts from any node and then adds any adjacent node whose weight is the minimum, and this process continues till all the nodes in the graphs are visited. When creating the minimum spanning tree of a graph, we also have to do not create any cycles because cycles should not be in the minimum spanning tree.

## Prim’s Algorithm Steps:

The prim’s algorithm employs the greedy approach for the minimum spanning tree. We have to choose any vertex of the graph and then choose the next adjacency vertex whose weight is less, and we continue this process till we do not get the whole graph nodes connected.

Step 1: Choose any source vertex in the graph.

Step 2: Find the minimum weight edge that is adjacent to the source and then connect it to the spanning tree.

Step 3: Repeat step 2 until all nodes are not added into the minimum spanning tree.

## Example :

The below is an example to search a minimum spanning tree using Prim’s algorithm.

1. We choose any random node from graph G and add it to the MST (minimum spanning tree). We select here node 0. 2. Now, we select that edge that is adjacent to the source node (0) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. 3. Now, we select that edge that is adjacent to the source node (0 or 1) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. 4. Now, we select that edge that is adjacent to the source node (0, 1, or 3) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. 5. Now, we select that edge that is adjacent to the source node (0, 1, 3, or 4) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. 6. Now, we select that edge that is adjacent to the source node (0, 1, 3, 4, or 6) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. 7. Now, we select that edge that is adjacent to the source node (0, 1, 3, 4, 6, or 2) but with the smallest weight, and then add that smallest weight node to the minimum spanning tree. Above is our final MST (minimum spanning tree), and the total cost is 6.

## Output:

 123 Total cost of minimum spanning tree after Prim's algorithm : 6 Process finished with exit code 0

## Time Complexity of Prim’s MST Algorithm:

1. The total time required to process and select the specific priority queue node that has yet to be added to the MST is logV.But as it works for every vertex, the total time complexity is V (logV).

2. The graph is undirected, and the total edges will be 2E. As we have to push the nodes into the priority queue, it will take a total time log (V). However, because we have a total of 2E edges, our total push operation will be 2E (log (V)).

3. Total complexity after operation 1 and 2 is O( ( E + V ) log ( V )).

## Conclusion:

We have studied the Prim’s minimum spanning tree, which is the first preference of most people when they have to find the MST graph from a graph. The Prim’s algorithm is simple to grasp and implement in a real-world application.Prim’s algorithm is very useful in real-life applications, for example, connecting railway tracks to whole over the cities. So it’s just a single example, but its application is huge, so we must have to understand this algorithm. 