Dijkstra's Shortest-Path Algorithm

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

The problem of computing the shortest path in a graph can be solved with bread-first search, but the breadth-first search-based shortest path algorithm works in the special case when the length of every edge in the graph ℓe is 1. Dijkstra's shortest-path algorithm solves a more general problem, where the edges have arbitrary non-negative lengths.

The problem of computing the shortest path in a graph with arbitrary edge lengths can be theoretically reduced to the problem of computing the shortest path in a graph with edges of length ℓe=1, by replacing an edge with an arbitrary length n with n edges with length 1, and adding extra vertices. The breadth-first search-based shortest path algorithm would theoretically work fine in this situation, producing the same results as the Dijkstra's algorithm, but the problem is that we are increasing the size of the graph, in terms of adding extra nodes and edges, and this increase my be non-trivial: for an edge with length 1000, we need to add extra 999 edges and 999 extra nodes. This representation would be wasteful, and even if the breadth-first search-based shortest path algorithm works in linear time, it is on a much larger graph, and we would much prefer an linear-time (or close to linear-time algorithm) that achieves the same result on the smaller graph. This is exactly what the Dijkstra's algorithm accomplishes.

If all edges are unit-length edges, Dijkstra's becomes the breadth-first search-based algorithm.

The Problem

The single-source shortest paths is formally defined as follows: given a directed graph G=(V, E) , with n = │V│ and m=│E│, where each edge e has a non-negative length ℓe, and a source vertex s, compute for each v ∈ V the length L(v) of the shortest path s → v.

The assumption that the length is non-negative is important. Dijkstra's shortest-path algorithm does not work correctly in presence of negative length edges.

The Non-Optimized Implementation

The general idea is to start at the source vertex s, and iterate in such a way that in each iteration we explore a new vertex and add it to the "explored territory".

The new vertex is chosen in such a way that it minimizes Dijkstra's Greedy Criterion, as described below.

DijkstraStep.png

 Initialize X={s} # X is the set of vertices for which the shortest path to s has been computed so far
 Initialize A[s]={0} # A maintains computed shortest path distances from the vertices explored so far to s
 Initialize B[s]={empty path} # B maintains the actual shortest path distances from the vertices explored 
                              # so far to s. This is not necessary in the algorithm but helps with the 
                              # understanding of the algorithm.

 while X ≠ V: # The main loop, each iteration grows X with one node
     for every directed edge (v, w) ∈ E that crosses X/(V-X) boundary v ∈ V, w ∈ V-X
         pick the (v*, w*) edge that minimizes A[v] + ℓvw # The Dijkstra's Greedy Criterion
     add w* to X
     set A[w*] = A[v*] + ℓv*w*
     set B[w*] = B[v*] ⋃ (v*,w*)

Dijkstra's Greedy Criterion

At each step, for every directed edge (v, w) ∈ E that crosses X/(V-X) boundary v ∈ V, w ∈ V-X, minimize:

A[v] + ℓvw 

Examples

https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/Jfvmn/dijkstras-algorithm-examples

Negative Edge Length Intuition

One approach would be to try to reduce the problem of computing shortest path with negative edge length to the problem of computing the shortest path with non-negative edge lengths, by adding a big number to all the edges that makes them all non-negative. The problem with this approach is that different paths between a source and destination vertex have different number of edges, and adding the same amount to each edge in the graph affects different paths in different ways - change path lengths by different amounts, so this reduction does not work. The reduction does not preserve the shortest path. This is immediately noticeable in the following example:

DijkstraNegative.png

Another intuition is that Dijkstra's algorithm commits to the shortest path identified so far as being the shortest. If there are negative edges, this has the potential to reduce the length, in the future. This idea can be formally used in the inductive proof.

Correctness

https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/VCHYw/correctness-of-dijkstras-algorithm

The Optimized Implementation

https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/Pbpp9/dijkstras-algorithm-implementation-and-running-time

In the non-optimized version of the algorithm, there was a step, repeated for each remaining graph vertex, where we needed to consider all edges traversing the boundary between explored and non-explored territory, and find out a minimum of the Dijkstra's greedy criterion. The number of edges is O(m) so the non-optimized version of the algorithm has a O(n*m) running time.


The Dijkstra's algorithm can be sped up with the use of a heap.