Dijkstra's Shortest Distance Algorithm

Input: A directed graph with positive, integer edge weights and n vertices.One of the vertices, called start, is specified as the start vertex.

Output: A list of the shortest distances from the start vertex to every other vertex in the graph.

Dijkstra's Shortest Distance Algorithm (2)

The algorithm uses an array of n integers (called distance) and a set of vertices (called allowedVertices). the variables v, allowedSize, and sum are local integer variables. there is some special value ( inf) that we can place in the distance array to indicate that there is no path.

Step 1. Initialize the distance array to contain all inf except distance[start], which is set to 0.

Step 2. Initialize the set of allowed vertices to be the empty set.

Step 3. Compute the distance array:

   for (allowedSize =1; allowedSize < n; allowedSize++)
   {
 		// at this point, allowedVertices contains allowedSize-1 vertices, which are the
		// allowedSize-1 closest vertices to the start vertex. Also, for each vertex v, 
		// distance[v] is the shortest distance from the start vertex  to vertex v,
		// provided that we are considering only permitted paths (i.e., paths where
		// each vertex except the final vertex must be in allowedVertices
	
		Step3a. Let next be the closest vertex to the start vertex, which is not yet in the 
			set allowedVertices.

		Step 3b. Add the vertex next to the set allowedVertices.

		Step 3c. Revise the distance array so that the new vertex (next) may appear
			on permitted paths:

			for (v = 0; v < n; v++)
			if ((v is not an allowed vertex) and (there is an edge from next to v))
			{
				sum = distance[next] + (weight of the edge from next to v);
				if (sum < distance[v]) 
						distance[v] = sum;
			}
	}

Step 4. Output the values in the distance array.

Finding the shortest path is described in the text.

"Discussion: The problem of finding shortest paths in a graph has a surprising variety of applications:

Here's what the Dictionary of Algorithms and Data Structures has to say about Dijkstra's algorithm

Dijkstra's Algorithm and Best-First-Search

 

One more link: Data Structures and algorithms: Dijkstra's Algorithm

Programming assignment.

Find directions in a map.

 

Minimum spanning trees


Creative Commons License
This work is licensed under a Creative Commons License.
Ernest Ackermann Department of Computer Science, Mary Washington College
CPSC 110 | CPSC 321

Bibliographic Information:
.