|
|
|
Dijkstra's algorithmDefinition: See also Bellman-Ford algorithm, Johnson's algorithm, all pairs shortest path. Note: This is used in Johnson's algorithm. Since Dijkstra's algorithm chooses the closest vertex, it is a greedy algorithm. Djikstra's algorithm (named after its discover, E.W. Dijkstra) solves the problem of finding the shortest path from a point in a graph (the source) to a destination. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time, hence this problem is sometimes called the single-source shortest paths problem. The somewhat unexpected result that all the paths can be found as easily as one further demonstrates the value of reading the literature on algorithms! This problem is related to
the spanning tree one. The graph representing all the paths from one vertex to
all the others must be a spanning tree - it must include all vertices. There
will also be no cycles as a cycle would define more than one path from the
selected vertex to at least one other vertex. For a graph,
The basic mode of operation is:
RelaxationThe relaxation process updates the costs of all the vertices, v, connected to a vertex, u, if we could improve the best estimate of the shortest path to v by including (u,v) in the path to v.The relaxation procedure proceeds as follows: initialise_single_source( Graph g, Node s )
for each vertex v in Vertices( g )
g.d[v] := infinity
g.pi[v] := nil
g.d[s] := 0;
This sets up the graph so
that each node has no predecessor (pi[v] = nil) and the estimates of the
cost (distance) of each node from the source (d[v]) are infinite, except
for the source node itself (d[s] = 0).
The relaxation procedure checks whether the current best estimate of the shortest distance to v (d[v]) can be improved by going through u (i.e. by making u the predecessor of v): relax( Node u, Node v, double w[][] )
if d[v] > d[u] + w[u,v] then
d[v] := d[u] + w[u,v]
pi[v] := u
The algorithm itself is now:
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Operation of Dijkstra's algorithm As usual, proof of a greedy algorithm is the trickiest part.
This sequence of diagrams illustrates the operation of Dijkstra's Algorithm.
|
user comments and suggestions are invited at KmailDrive@gmail.com