In the field of computer science, the algorithm of Prim’s, a greedy algorithm enables finding the minimum spanning tree for the weighted undirected graph. It implies solving the wedges subset which enables a tree formation and accompanies every vertex where the overall weight of edges is minimized in the tree. The algorithm’s conduction indicates by having the tree constructed at a singular vertex at a time, from the arbitrary starting vertex, at every step adding the cheapest connection from the tree to a different vertex.

The algorithm of Prim had been most preliminarily devised by Vojtech Jarnik, a Czech Mathematician in the year 1930 and had been later re-developed by Robert C. Prim in the year 1957 and Edsger W. Sijkstra in the year 1959. Hence it is at times even called the DJP algorithm. The other algorithms which are well-known in this kind of problems include Boruvka’s algorithm and Kruskal’s algorithm. These algorithms are used to find a solution to the minimum spanning forest in a graph which is plausibly not connected. Contrarily, Prim’s algorithm form just finds the minimum spanning trees in the connected graphs.

The time complexity of Prim’s algorithm depends upon the data structures.

The algorithm of Prim can be explicated as below:

- Have the tree initialized with a singular vertex, which is selected randomly from the graph
- With one edge grow the tree from the edges which have the vertices connected to the edges not in the tree. Find the edge of minimum weight and have it transferred to the tree
- Follow the step to once again until all the vertices are included in the tree.

As given from above, the starting vertex for this algorithm can be selected randomly as because the most initial iteration of the algorithm’s main loop shall have a series of vertices in Q which has equal weights. Automatically, the algorithm shall begin a new tree when a spanning tree for every connected input component is completed.

Below, we shall explain an example of the weighted connected graph G having n number of vertices. The Prim’s Algorithm enables finding minimum spanning tree of the weighted connected graph G.

Technique:

Prim: (G denotes the weighted connected graph having n number of vertices) T denotes the minimum-weight edge wherein i = 1 up till n − 2 begin e denotes a minimum weight edge which is incident in T, to a vertex and does not form a circuit in T if added to T T := T with e added

end

return(T)

Eg: Utilize the algorithm of Prim in order to find a solution of the minimum spanning tree in the below given weighted graph. In order to break ties, enable using the alphabetical order.

Solution:

The algorithm of Prim shall progress as below:

- Most preliminarily add the edge {d, e} having a 1 weight.
- Then, enable adding edge {d, e} of 2 weight
- Next, enable adding {d, z} having weight 2
- Then, enable adding edge {b, e} of 3 weight
- Finally add edge {a, b} with a weight of 2.

This shall thus generate a weight 10 minimum spanning tree which can be given as below: