Shortest-Path Algorithms

Dijkstra’s Algorithm

A similar algorithm to BFS can be applied to find the shortest path to arbitrary graphs with nonnegative edge weights.

Implementation

An efficient implementation of Dijkstra’s algorithm requires that it is possible to efficiently find the minimum distance node that has not been processed. We thus use a Priority Queue so that the next node to be processed can be retrieved in .

Graph is stored as adjacency lists so that adj[a] contains a pair always when there is an edge from node a to node b with weight w.

The Priority Queue q contains pairs of the form , meaning that the current distance to node is .

  • dist[i] is the distance to node i
  • processed[i] indicates whether node i has been processed
    • IMPORTANT: Whenever a node is processed, its distance is final.

Initially the distance is 0 to x and to all other nodes.

for (int i = 1; i <= n; i++) distance[i] = INF; 
distance[x] = 0;
q.push({0,x});
while (!q.empty()) {
	int a = q.top().second; 
	q.pop();
	if (processed[a]) continue;
	processed[a] = true;
	for (auto u : adj[a]) {
		int b = u.first, w = u.second;
		if (distance[a]+w < distance[b]) {
			distance[b] = distance[a]+w;
			q.push({-distance[b],b});
	}
}

The computational complexity of Dijkstra’s Algorithm is .

Complexity Analysis

From CS341 class.

What happens if you use a regular queue?

Using a regular queue in Dijkstra’s algorithm instead of a priority queue will not maintain the algorithm’s property of always selecting the next closest vertex to the source. This can lead to incorrect shortest path calculations.

  • TODO: show counter-example?

The proof of correctness goes along the lines of a contradiction. In my own words:

is the shortest distance from to .

Let be the source vertex Let be the source vertex. Assume is the first vertex where is not set correctly. Let time be the time when is added to . We let be the actually shortest path from to .

On this path , we use 2 vertices: and . We define as the first vertex in on path . Let be the predecessor of .

  • At time , we have
    • for the case where , then can you still make the argument?
    • We make the argument by talking about

When was visited and added to ,

  • is correct
  • its neighbours are added, and is a neighbours, so

How can you argue that ? Because and are on the shortest path from to , so connected by is a shortest path too.

  • But what if there is another path from to ? IDK, like there can be multiple paths
  • Like the case in the image below, where the edge directly connect to is not the shortest path.
  • Then doesn’t actually satisfy the initial definition

We have vertices and , in , not in . is correct at time .

What is s is directly connected to u?

Then you can define , .