Supermarine

The algorithm of Dijkstra is used to solve the problem of the shortest way between two tops of a related graph whose weight related to the edges is positive or null.

To illustrate the interest of this algorithm, one can take for example the highway network of an area: each top is a city and each arc is a road whose weight is mileage. The algorithm of Dijkstra then makes it possible to find the shortest way of a city to another.

The algorithm carries the name of its inventor, the Informaticien Dutch Edsger Dijkstra and was published in 1959.

Notations

The graph is noted G= (S, A) where:
  • the S unit is the whole of the tops of the graph G;
  • the A unit is the whole of the edges of G such as: if (s_1, s_2) is in A, then there exists an edge since the node s_1 towards the node s_2;
  • one defines the procedure Poids (s_1, s_2) definite on A which returns the positive weight of the edge connecting s_1 to s_2 (and an infinite weight for the pairs of tops which is not connected by an edge).

Principles

The weight of the way between two tops is the sum of the weights of the edges which compose it. For a given pair of tops s_ {deb.} (the top of the departure) s_ {fine} (top of arrival) pertaining to S, the algorithm finds the way since s_ {fine deb.} towards s_ {} of less weight (in other words the lightest way or shortest).

The algorithm functions by building a subgraph P so that the distance between a top s of P since s_ {deb.} either known and or a minimum in G. Initially P contains simply the node s_ {deb.} insulated, and s_ {deb.} with itself outdistances it is worth zero. Arcs are added to P with each stage:

1. by identifying all the edges a_i= (s_ {i1}, s_ {i2}) in P \ times G such as s_ {i1} is in P and s_ {i2} is in G;

2. by choosing the edge a_j= (s_ {j1}, s_ {j2}) in P \ times G which gives the distance minimum since s_ {deb.} to s_ {j2} while passing all the ways created driving to this node.

The algorithm finishes either when P becomes a covering tree of G, or when all the nodes of interest are in P.

Algorithm

Function appendices

The algorithm uses the following additional functions.

Initialization of the algorithm

Initialization (G, sdeb) 1 for each point S of G 2 to make D: = infinite/* one initializes the tops other than sdeb with 0 * 3 predecessor: = 0/* because one does not know at the beginning any way between S and sdeb * 4 D: = 0/* sdeb being the point nearest to sdeb *

Seek node nearest

  • One seeks the node nearest (connected by the weakest edge of the weight) to s_ {deb.} among the nodes located in a Q whole, made up of the nodes distant from an edge of the elements of P. One uses for that the function Trouve_min () . The found node is then unobtrusive of the Q unit and is then turned over to the principal function like result of the function.

Update of the distances

  • One updates distances between s_ {deb.} and s_ {2} by raising the question: is it to better pass by s_ {1} or not?
maj_distances (s1, s2) 1 if D > D + Weight (s1, s2) 2 then D: = D + Weight (s1, s2) 3 predecessor: = s1/* one makes pass the way by s1 *

Principal function

Here the principal function using the preceding additional functions:

Dijkstra (G, Weight, sdeb) 1 Initialization (G, sdeb) 2 P: = empty set 3 Q: = together of all the nodes 4 as long as Q is not an empty set 5 to make s1: = Trouve_min (Q) 6 P: = P union {s1} 7 for each node s2 close to s1 8 to make maj_distances (s1, s2)

The shortest way of s_ {fine deb.} with s_ {} can be then calculated repeatedly according to the following algorithm, with A the continuation representing the shortest way of s_ {fine deb.} with s_ {} : 1 has = empty continuation 2 S: = sfin 3 = S has + has/* inserts S into the beginning of A * 4 as long as S! = sdeb 5 S = predecessor/* one continues to follow the way * 6 = S has + has

Improvements of the algorithm

It is possible to slightly improve the principal algorithm by stopping research when the equality s_1=s_ {fine} is checked, in condition of course of seeking only the minimal distance between s_ {fine deb.} and s_ {} .

The algorithm of Dijkstra could be implemented effectively by storing the graph in the form of lists of adjacency and by using a Tas like a File at priorities to fulfill the function Trouve_min. If the graph has m arcs and n nodes, by supposing that the comparisons of the weights of arcs are at constant time, then the Complexité of the algorithm is: \ sigma ln (N) . It will be noted that the use of Tas of Fibonacci gives a better execution time deadened: \ sigma ln (N) .

Example

The following example shows the successive stages in the resolution of the shortest way in a graph. The nodes symbolize cities in Germany and the edges indicate the distance between the cities. One wants to go from Frankfurt ( Frankfurt ) to Munich ( München ).










Codes

Pseudo code

function Dijkstra (nodes, wire, distance, beginning, end) For N traversing nodes n.parcouru = infinite // Can be implemented with -1 = 0 n.precedent End for debut.parcouru = 0 Empty DejaVu = list PasEncoreVu = nodes As long as PasEncoreVu! = empty list n1 = minimum (PasEncoreVu) // the node in PasEncoreVu with traversed smallest DejaVu.ajouter (n1) PasEncoreVu.enlever (n1) For N2 traversing wire (n1) // nodes connected to n1 by an arc If n2.parcouru > n1.parcouru + distance (n1, N2) // distance corresponds to the weight of the arc connecting n1 and N2 n2.parcouru = n1.parcouru + distance (n1, N2) n2.precedent = n1 // Say that to go with N2, it is necessary to pass by n1 End if End for End as long as empty chemin = list N = fine As long as N! = beginning chemin .ajouterAvant (N) N = n.precedent End as long as To turn over way Fine Dijkstra function

Caml implementation

Here the code of implementation Caml: (* one supposes given files of priority *) modulate H: sig type 'has T valley empty: 'T has valley is_empty: 'T - > bool has valley add: int * 'has - > 'T has - > 'has T valley extract_min: 'T - > (int * 'a) has * 'has T end (* the adjacency is given in the form of a function: adj v is the list of the neighbors of v, with their distance; the following function looks for the shortest way of v1 with v2 *) let will dijkstra (adj: 'has - > ('* int A) list) (v1: 'a) (v2: 'a) = let visited = Hashtbl.create 97 in let rec loop H = yew H.is_empty H then raise Not_found; let (W, (v, p)), H = H.extract_min H in yew v = v2 then List.rev p, W else let H = yew not (Hashtbl.mem visited v) then begin Hashtbl.add visited v (); List.fold_left (fun H (E, d) - > H.add (w+d, (E, E:: p)) H) H (adj v) end else H in loop H in loop (H.add (0, (v1,)) H.empty)

Applications

An application of most current of the algorithm of Dijkstra is the protocol open shortest path first which allows a very effective routing Internet of information by seeking the most effective course.

The routers IS-IS also use the algorithm. The sites of road routes use it same manner and allow many adaptations by adjusting the weight of the arcs such as for example: the most economic way, fastest…

Appearance in the popular culture

The algorithm of Dijkstra is used by the investigators of the series NUMB3RS, in the episode 23 of the season 3.

Appendices

References

  • “'' has shorts introduction to the art off programming ''” of Edsger W. Dijkstra, containing the original article describing the algorithm of Dijkstra (pages 67 to 73)
  • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein: “ Introduction to algorithmic the ”, (version 2nd edition, 2001, MIT Close and McGraw-Hill, section 24.3, “ Dijkstra' S algorithm ”, pages 595-601)

Internal bonds

External bonds

  • detailed Explanation of the algorithm of Dijkstra and implementation supplements in C++
  • Rapport on the algorithm of Dijkstra and the play of simulation OpenCity
  • Applet Java showing the algorithm of Dijkstra stage by stage
  • Applets Java using the algorithm.

Additional sources and indications

Random links:Ventured | Eastern (Congo-Kinshasa) | Canton of Dijon-8 | Birotonde bilunaire | FIFA Soccer 95