Jarník-Prim algorithm
Jarník-Prim algorithm

The Jarník-Prim algorithm (Jarník's algorithm, Prim's algorithm, DJP algorithm) is used to find a minimum/maximum spanning tree of the graph (spanning tree, in which is the sum of its edges weights minimal/maximal). The algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník, in 1957 it was rediscovered by American mathematician Robert Prim. And again in 1959 the algorithm was rediscovered by Dutch computer scientist Edsger Dijkstra.

Description

The algorithm traverses the graph starting at a random node and maintains a list of already discovered nodes with their distance from the already connected (processed) subgraph. In every step the procedure connects a node, which is in the least distance, discovers its neighbours or recalculates their distance, if they had been already discovered and a more favorable edge was found. When all the nodes are connected to the subgraph, the algorithm terminates.

Complexity analysis

The complexity of the Jarník-Prim algorithm is dependent on the implementation of the list. The list itself may be modified at most \\vert E \\vert times, because every discovery of a new edge may add a new node to the list or change a weight of some already discovered node.

If we implement the list using a binary heap, which acts as a priority queue, than its each modification takes O(\\log_{2}(\\vert V \\vert)) steps. Hence the complexity of the Jarník-Prim algorithm is O(\\vert E \\vert  \\log_{2}(\\vert V \\vert)).


Code

01./**
02. * Jarnik-Prim algorithm
03. * Finds the minimal spanning tree of the given graph
04. * @graph graph
05. * @weight edge weights 
06. * @return array of predecessors 
07. */
08.node[] jarnikPrimAlgorithm(graph, weight)
09.    Queue q
10.    q.addAllVetices(graph.vertices) //add all vertices to the queue
11.     
12.    distances = new int[q.size()] //array of distances
13.    distances[0] = 0 //root
14.    for i in 1 -> distances.length - 1 do                                
15.        distances[i] = +inf //all remaining vertices are in infinite distance
16.      
17.    predecessors = new node[q.size()] //array of predecessors
18.    predecessors[0] = null //the root does not have any predecessor
19.     
20.    while !queue.empty() do //while the queue is not empty
21.        u = queue.extractMin() //return a node in a minimal distance
22.        for node in descendants(u) do //for all descendants of u
23.            if queue.contains(node) AND weight(u, node) < d[node] //if we have found some improving path to the descendant
24.                then predecessors[node] = u //than u is his predecessor
25.                     d[node] = weight(u, node) //and set a new distance   
26.     
27.    return predecessors //return the array of predecessors

Sources

  • KOLÁŘ, Josef. Teoretická informatika. 2. edition. Prague : Česká informatická společnost, 2004. 205 p. ISBN 80-900853-8-5.







       
 

Place for your banner

Here is the position ready for our customer's banners.