Lably
Skip to content

Prim's algorithm

Method for finding minimum spanning trees

  • 7 min read
  • 4 chapters

Related on Lably

Through Computer science, Graph (discrete mathematics) and Pseudocode

The algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník and later rediscovered and republished by Joseph Kruskal in 1956, Robert C. Prim in 1957, and Edsger W. Dijkstra in 1959. Therefore, it is also sometimes called Jarník's algorithm, the Prim–Jarník algorithm, the Prim–Dijkstra algorithm or the DJP algorithm.

Other well-known algorithms for this problem include Kruskal's algorithm and Borůvka's algorithm. These algorithms find the minimum spanning forest in a possibly disconnected graph; in contrast, the most basic form of Prim's algorithm only finds minimum spanning trees in connected graphs. However, running Prim's algorithm separately for each connected component of the graph, it can also be used to find the minimum spanning forest. In terms of their asymptotic time complexity, these three algorithms are equally fast for sparse graphs, but slower than other more sophisticated algorithms. However, for graphs that are sufficiently dense, Prim's algorithm can be made to run in linear time, meeting or improving the time bounds for other algorithms.

Description

The algorithm may informally be described as performing the following steps:

  1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.
  2. Grow the tree by one edge: Of the edges that connect the tree to vertices not yet in the tree, find the minimum-weight edge, and transfer it to the tree.
  3. Repeat step 2 (until all vertices are in the tree).

In more detail, it may be implemented following the pseudocode below.

<b id="mwYg">function</b> Prim(vertices, edges) <b id="mwYw">is</b> <b id="mwZA">for</b> <b id="mwZQ">each</b> vertex <b id="mwZg">in</b> vertices <b id="mwZw">do</b> cheapestCost[vertex] ← ∞ cheapestEdge[vertex] ← null explored ← empty set unexplored ← set containing all vertices startVertex ← any element of vertices cheapestCost[startVertex] ← 0 <b id="mwaA">while</b> unexplored <b id="mwaQ">is</b> not empty <b id="mwag">do</b> // Select vertex in unexplored with minimum cost currentVertex ← vertex in unexplored with minimum cheapestCost[vertex] unexplored.remove(currentVertex) explored.add(currentVertex) <b id="mwaw">for</b> <b id="mwbA">each</b> edge (currentVertex, neighbor) <b id="mwbQ">in</b> edges <b id="mwbg">do</b> <b id="mwbw">if</b> neighbor <b id="mwcA">in</b> unexplored <b id="mwcQ">and</b> weight(currentVertex, neighbor) < cheapestCost[neighbor] <b id="mwcg">then</b> cheapestCost[neighbor] ← weight(currentVertex, neighbor) cheapestEdge[neighbor] ← (currentVertex, neighbor) resultEdges ← empty list <b id="mwcw">for</b> <b id="mwdA">each</b> vertex <b id="mwdQ">in</b> vertices <b id="mwdg">do</b> <b id="mwdw">if</b> cheapestEdge[vertex] ≠ null <b id="mweA">then</b> resultEdges.append(cheapestEdge[vertex]) <b id="mweQ">return</b> resultEdges

As described above, the starting vertex for the algorithm will be chosen arbitrarily, because the first iteration of the main loop of the algorithm will have a set of vertices in Q that all have equal weights, and the algorithm will automatically start a new tree in F when it completes a spanning tree of each connected component of the input graph. The algorithm may be modified to start with any particular vertex s by setting C[s] to be a number smaller than the other values of C (for instance, zero), and it may be modified to only find a single spanning tree rather than an entire spanning forest (matching more closely the informal description) by stopping whenever it encounters another vertex flagged as having no associated edge.

Different variations of the algorithm differ from each other in how the set Q is implemented: as a simple linked list or array of vertices, or as a more complicated priority queue data structure. This choice leads to differences in the time complexity of the algorithm. In general, a priority queue will be quicker at finding the vertex v with minimum cost, but will entail more expensive updates when the value of C[w] changes.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Discrete mathematics