8  Shortest Paths

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.”
– Dennis Ritchie

For a weighted graph G=(V, E), the edge weights are given by a function w\colon E\to\mathbb{R} mapping each to a real-valued weight. For example, see the weighted graph below:

A path p in G is denoted by the sequence of vertices \langle v_0,v_1,\ldots,v_k\rangle forming the path. The weight w(p) of path p is the sum of the weights of its constituent edges: w(p)=\sum_{i=1}^k w(v_{i-1}, v_i).

Exercise 8.1 In the graph above, compute the weight of the path p, given by the vertices \langle 0, 3, 1, 4\rangle.

The shortest-path weight \delta(u, v) from u to v is defined by \delta(u, v)=\begin{cases} \min\{w(p)\colon u\overset{p}{\leadsto} v\}&\text{ if }v\text{ is reachable from }u \\ \infty&\text{ otherwise.} \end{cases}

Exercise 8.2 In the graph above, compute the shortest-path weight from 0 to 4.

A shortest path from vertex u to v is then defined as any path u\overset{p}{\leadsto} v with weight w(p)=\delta(u, v).

Exercise 8.3 In the graph above, compute the shortest-path weight from 0 to 4. Also, describe a shortest path.

Exercise 8.4 (Optimal Substructure) Show that subpaths of shortest paths are shortest paths.

We have already seen that the breath-first-search (BFS) is a shortest-path algorithm—if the graph has unit weight for all edges. This is chapter, we discuss variations of the problem of finding shortest-paths between vertices in a graphs.

8.1 Single-Source

We first visit the single-source shortest-paths problem: given a graph G=(V, E), find a shortest path from a given source vertex s\in V to every vertex v\in V.

Exercise 8.5 (Negative-weight Cycles) Explain what would happen to the shortest-paths problem if there was a cycle with a negative weight.

Hint: See the picture below and compute the shortest-path distance from 0 to 3.

Exercise 8.6 (Shortest-paths and Cycles) Can a shortest path contain a cycle—even when the edges have positive weights?

Hint: See the picture below and compute the shortest-path distance from 1 to 2.

Belman-Ford Algorithm

\begin{algorithm} \caption{BellmanFord} \begin{algorithmic} \Procedure{Bellman-Ford}{$G, s, w$} \State{▷ Initialize source} \For{each vertex $v\in G.V$} \State{$v.d=\infty$} \EndFor \State{$s.d=0$} \State{▷ Main subroutine} \For{$i=1$ to $|G.V|-1$} \For{each edge $(u,v)\in G.E$} \State{▷ Relax edge} \If{$v.d>u.d + w(u, v)$} \State{$v.d = u.d + w(u,v)$} \EndIf \EndFor \EndFor \State{▷ Find negative-cycle} \For{each edge $(u,v)\in G.E$} \If{$v.d>u.d + w(u, v)$} \Return{False} \EndIf \EndFor \Return{True} \EndProcedure \end{algorithmic} \end{algorithm}

Demo

Exercise 8.7 (Shortest-paths and Cycles) Describe the time-complexity of BELLMAN-FORD.

Solution. \mathcal{O}(V^2 + VE).

8.2 All-Sources

We now compute shortest paths between all pairs of vertices in a graph. An application of all-pairs shortest paths is to determine the diameter of a network: the longest of all shortest paths. If a directed graph models a communication network, with the weight of an edge indicating the time required for a message to traverse a communication link, then the diameter gives the longest possible transit time for a message in the network.

Given a graph G=(V, E) and a weight function w:E\to\mathbb{R}, our goal is to find, for every pair of vertices u,v\in V, a shortest (least-weight) path from u to v so that the weight of a path is the sum of the weights of its constituent edges.

What is the time-complexity of running BELLMAN-FORD on every vertex of the graphs in order to compute all-pairs shortest paths?

Solution. \mathcal{O}(V^4).

Dynamic Programming

Let l_{ij}^(r) be the minimum weight of any path from vertex i to vertex j that contains at most r edges.