# Prim’s Algorithm:

Prim’s algorithm uses a greedy algorithm to discover the minimum cost spanning tree for a connected weighted undirected graph. As a result of this algorithm, a tree that includes all the vertex points and a subset of both edges is constructed in such a way that all the parameters associated with it are the smallest.

The shortest path first algorithm is being used to find the minimum spanning tree in the prims algorithm. This algorithm builds a minimum spanning tree by adding new nodes from the Graph with the lowest vertices and edges.

## Prim’s Algorithm and How It Works;

Prim’s algorithm employs a greedy algorithm to find the minimum spanning tree. In a greedy algorithm, the next item has always been selected that offers the most local optimal solution to the problem, in which making a choice locally optimum solution also leads to a global solution.

Initially, we begin with one vertex, and then add vertices that are connected with the smallest edge weight until we reach our goal.

## The series of steps for trying to implement Prim’s algorithm:

Get rid of all loops and parallel edges.

Start the minimum spanning tree from every vertex in the graph.

Find all the edges that connect the tree to new unvisited vertices, then find the shortest set of edges and add them to the tree.

The procedure should be repeated until we have a minimum spanning tree.

## Prim’s Algorithm Time Complexity Analysis:

Prim’s Algorithm’s Worst Case Time Complexity is: –

Binary Heap: O(ElogV)

Fibonacci Heap: O(E+VlogV)

The vertices need to be traversed using Breadth-first Search, and then it will be traversed O(V+E) times. The minimum element value is calculated using the Min heap procedure in O(log V) time. Combining the two will result in an O(Elogv) time complexity.

## Parallel algorithm:

Prim’s algorithm’s main loop is innately sequential and thus cannot be executed in parallel. The inner loop, which determines the next edge of minimum weight that does not form a cycle, on the other hand, can be executed in parallel by dividing the vertices and edges among the available processors.

## Conclusion:

This algorithm also works with undirected connected graphs, but no negative edges are allowed. When applied to this type of graph, the algorithm is quite efficient. In the absence of non-negative weight cycles, the shortest path always exists.