## Dijkstra's algorithm

E. W. Dijkstra (1930-2002) a proposé en 1959 un algorithm (nommé algorithme de Dijkstra) qui permet de déterminer le shortest way entre deux sommets d’un graph connexe pondéré. L’algorithme de Dijkstra est basé sur l’observation suivante : une fois que nous déterminons le chemin le plus court vers un sommet v, alors les chemins qui vont de v à chacun de ses sommets adjacents pourraient être le plus court chemin vers chacun de ces sommets voisins. L’algorithme de Dijkstra est un algorithme de dynamic programming glouton, il visite toutes les solutions possibles.

**Conditions**

- No negative length
- Arc or edge
- Finite number of vertices
- A source (and incidentally a target) defined

Dijkstra's algorithm takes as input a directed graph weighted by positive reals and a source vertex. This involves progressively building a subgraph in which the different vertices are classified in increasing order of their minimum distance from the starting vertex. The distance corresponds to the sum of the weights of the borrowed edges.

**Summit visited:**A summit for which we have determined the shortest route. Once we have defined a vertex as VISIT, this is final, and we will not come back to that vertex.

**Peak marked**: A summit for which a path has been found. We mark this summit as a CANDIDATE for the shortest path.

Initially, we consider that the distances from each vertex to the starting vertex are infinite except for the starting vertex for which the distance is 0. The starting subgraph is the empty set.

During each iteration, we choose outside the subgraph a vertex of minimum distance and we add it to the subgraph (it becomes a visited vertex). Then, we update the distances of the neighboring vertices of the added one (the vertices are marked). The update takes place as follows: the new distance from the neighboring vertex is the minimum between *the existing distance* and that obtained by adding *the weight of the arc between neighboring vertex and vertex added to the distance from the added vertex*.

We continue in this way until the subgraph is completely completed (or until the arrival vertex is selected). Example :

distance | to A | to B | to C | to D | to E | to F | to G | to H | have | to J |
---|---|---|---|---|---|---|---|---|---|---|

initial step | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |

A (0) | 85 | 217 | ∞ | 173 | ∞ | ∞ | ∞ | ∞ | ∞ | |

B (85_{TO}) | – | 217 | ∞ | 173 | 165 | ∞ | ∞ | ∞ | ∞ | |

F (165_{B}) | – | 217 | ∞ | 173 | – | ∞ | ∞ | 415 | ∞ | |

E (173_{TO}) | – | 217 | ∞ | – | – | ∞ | ∞ | 415 | 675 | |

C (217_{TO}) | – | – | ∞ | – | – | 403 | 320 | 415 | 675 | |

H (320_{VS}) | – | – | 503 | – | – | 403 | – | 415 | ||

G (403_{VS}) | – | – | 503 | – | – | – | – | 415 | 487 | |

I (415_{F}) | – | – | 503 | – | – | – | – | – | 487 | |

J (487_{H}) | – | – | 503 | – | – | – | – | – | – | |

D (503_{H}) | – | – | – | – | – | – | – | – | – |

Pour des raisons pratiques, la résolution de l’algorithme de Dijkstra ne retourne qu’un vecteur contenant le sommet visité, la liste des prédécesseurs (les sommets déjà validés) et les valeurs des plus courts chemins vers tous les autres sommets. Ce qui correspond à une ligne courante du tableau présenté. Grâce à la colonne de gauche, nous pouvons créer un tree des chemins les plus courts du sommet A à tous les sommets.

**Optimality**

The starting summit is the shortest way to reach itself. Then, we compute all the size 1 paths starting from this vertex, in order to validate the shortest one. This path is therefore the shortest path from the starting summit because there is no edge of negative weight. By induction, we deduce that the algorithm always validates a shorter path.

d [0] = 0, d [i] = infinityfor any vertex other than originas long as'there is a vertex outside the subgraphPchoose a topToout ofPsmaller distanced [a]to putToinPforeach peakbout ofPneighbor ofTod [b] = min (d [b], d [a] + weight (a, b))endendreturn d