Shortest Path
Dijkstraβs algorithm
Procedure DIJKSTRA(G, s)
Inputs:
* G: a directed graph containing a set V of n vertices and a set E of m
directed edges with nonnegative weights.
* s: a source vertex in V.
Result: For each non-source vertex v in V, shortest[v] is the weight sp(s, v)
of a shortest path from s to v and pred[v] is the vertex preceding v
on some shortest path. For the source vertex s, shortest[s] = 0 and
pred[s] = NULL. If there is no path from s to v, then shortest[v] = β
and pred[v] = NULL.
1. Set shortest[v] to β for each vertex v except s, set shortest[s] to 0,
and set pred[v] to NULL for each vertex v.
2. Set Q to contain all vertices.
3. While Q is not empty, do the following:
A. Find the vertex u in set Q with the lowest shortest value and
remove it from Q.
B. For each vertex v adjacent to u:
i. Call RELAX(u, v).
Simple array implementation
Operations:
INSERT (Q, v) inserts vertex v into set Q. (Dijkstraβs algorithm calls INSERT n times.)
EXTRACT-MIN (Q) removes the vertex in Q with the minimum shortest value and returns this vertex to its caller. (Dijkstraβs algorithm calls EXTRACT-MIN n times.)
DECREASE-KEY (Q, v) performs whatever bookkeeping is necessary in Q to record that shortest[v] was decreased by a call of RELAX. (Dijkstraβs algorithm calls DECREASE-KEY up to m times.)
Binary heap implementation
A binary heap organizes data as a binary tree stored in an array. A binary tree is a type of graph, but we refer to its vertices as nodes, the edges are undirected, and each node has 0, 1, or 2 nodes below it, which are its children. On the left side of the figure on the next page is an example of a binary tree, with the nodes numbered. Nodes with no children, such as nodes 6 through 10, are leaves.

A binary heap is a binary tree with three additional properties. First, the tree is completely filled on all levels, except possibly the lowest, which is filled from the left up to a point. Second, each node contains a key, shown inside each node in the figure. Third, the keys obey the heap property: the key of each node is less than or equal to the keys of its children. The binary tree in the figure is also a binary heap.
We can store a binary heap in an array, as shown on the right in the figure. Because of the heap property, the node with the minimum key must always be at position 1. The children of the node at position i are at positions and , and the node above the node at position iβits parentβis at position . It is easy to navigate up and down within a binary heap when we store it in an array.
The Bellman-Ford algorithm
The Floyd-Warshall algorithm
Now suppose that you want to find a shortest path from every vertex to every vertex. Thatβs the all-pairs shortest-paths problem.
The classic example of all-pairs shortest pathsβwhich I have seen several authors refer toβis the table that you see in a road atlas giving distances between several cities. You find the row for one city, you find the column for the other city, and the distance between them lies at the intersection of the row and column.
Last updated