# Single Source Shortest Paths

Given a connected weighted directed graph $G\left(V,E\right)$, associated with each edge $⟨u,v⟩\in E$, there is a weight $w\left(u,v\right)$. The single source shortest paths (SSSP) problem is to find a shortest path from a given source $r$ to every other vertex $v\in V-\left\{r\right\}$. The weight (length) of a path $p=⟨{v}_{0},{v}_{1},\dots ,{v}_{k}⟩$ is the sum of the weights of its constituent edges:

 $w\left(p\right)=\sum _{i=1}^{k}w\left({v}_{i-1},{v}_{i}\right).$

The weight of a shortest path from $u$ to $v$ is defined by $\delta \left(u,v\right)=min\left\{w\left(p\right):$ $p$ is a path from $u$ to $v\right\}$.

## 1  Negative Cycles

In some applications, graphs may contain edges with negative weights. Such edges may result in negative cycles (e.g. Figure 1). If there is a negative weight cycle which is reachable from the source $s$, the weight $\delta \left(s,v\right)$ is undefined, where $v$ is a vertex in the cycle.
Figure 1: Shortest paths from root vertex $s$. Negative cycles are formed by $g$ and $h$, as well as $b$, $c$, and $f$. $\delta \left(a,g\right)$, $\delta \left(a,h\right)$, and $\delta \left(a,i\right)$ are $-\infty$ because of the negative cycle formed by $g$ and $h$. The weights for $b$, $c$ and $f$ are $\infty$, despite of the negative cycle, because they are unreachable from root vertex $s$.

## 2  Representation of a shortest path tree

The shortest path tree rooted at $s$ is a directed subgraph $G\text{'}=\left(V\text{'},E\text{'}\right)$ where $V\text{'}\subseteq V$ and $E\text{'}\subseteq E$ such that every $v\in V\text{'}$ is reachable from $s$; and for all $v\in V\text{'}$, the unique simple path in $G\text{'}$ from $s$ to $v$ is a shortest path from $s$ to $v$ in $G$. In other words, for each vertex $v\in V$, we maintain a predecessor $p\left(v\right)$ that is either a vertex or NIL. Thus, the output for a single shortest paths problem is a tree rooted at the source.
Figure 2: Shortest Path Tree. The shortest path tree rooted at $s$ has its edges in bold. Two variants (a) and (b) are shown for the same graph. In (a) vertex $t$ is reached through the edge $\left(q,t\right)$, whereas in (b), edge $\left(p,t\right)$ is used instead.

## 3  Initialization

The algorithms introduced here make use of a common initialization that is as follows:
INITIALIZE $\left(G,s\right)$
1     $d\left[s\right]$ $←$  $0$
2     $p\left[s\right]$ $←$ NIL
3    for all $v\in V-\left\{s\right\}$
4             do  $d\left[v\right]$ $←$  $\infty$
5                    $p\left[v\right]$ $←$ NIL

## 4  Shortest Paths and Relaxation

The main technique used by the shortest path algorithms introduced here is relaxation, a method that repeatedly decreases an upper bound on the length of an actual shortest path for each vertex until the upper bound equals the length of the shortest path.
For each vertex $v$, we maintain an attribute $d\left[v\right]$ which is an upper bound on the length of a shortest path from source $s$ to $v$. $d\left[v\right]$ is also called the shortest path estimate.
RELAX $\left(u,v,w\right)$
1    if  $d\left[v\right]>d\left[u\right]+w\left(u,v\right)$
2          then  $d\left[v\right]$ $←$  $d\left[u\right]+w\left(u,v\right)$
3                       $p\left[v\right]$ $←$  $u$

## 5  Dijkstra's Algorithm

Dijkstra's algorithm assumes that all the edges in G are non-negative. The algorithm maintains a set $S$ of vertices whose final shortest path weights from the source $s$ have already been determined. That is, for all the vertices $v\in S$, we have $d\left[v\right]=\delta \left(s,v\right)$.
The algorithm repeatedly selects a vertex $u\in V-S$ with the minimum shortest-path estimate, inserts $u$ to $S$, and relaxes all the edges leaving $u$. Also, a priority queue $Q$ that contains all the vertices in $V-S$ is maintained, keyed by their $d$ values. Figure 3 shows the execution of the DIJKSTRA-SHORT algorithm listed below.
DIJKSTRA-SHORT $\left(G,w,s\right)$
1     INITIALIZE $\left(G,s\right)$
2     $S$ $←$  $\varnothing$
3     $Q$ $←$  $V$
4    while  $Q\ne \varnothing$
5                  do  $u$ $←$  EXTRACT-MIN $\left(Q\right)$
6                         $S$ $←$  $S\cup \left\{u\right\}$
7                        for each vertex $v\in \text{Adj}\left[u\right]$
8                                 do  RELAX $\left(u,v,w\right)$
Figure 3: Execution of DIJKSTRA-SHORT. Step (a) shows the state of the graph after initialisation. In step (b), vertex $s$ (the source) is extracted (shaded gray) from $Q$ and the adjacent vertices $a$ and $e$ relaxed. Since $d\left[a\right]>d\left[s\right]+w\left(s,a\right)$, $d\left[a\right]$ is updated and $p\left[a\right]$ set to $s$ (arrow from $s$ to $a$ is made bold). Ditto for vertex $e$. In step (c), vertex $e$ is extracted (and shaded) as it is $Q$'s minimum. As before, its adjacent vertices are relaxed. Step (d) illustrates the RELAX mechanism. Here $a$ is extracted and $b$ and $f$ relaxed. In doing so, $f$'s parent is updated to $a$ (formerly $e$). This execution carries on until $Q$ is empty.

## 6  The Bellman-Ford Algorithm

Like Dijkstra's algorithm, the Bellman-Ford algorithm uses the technique of relaxation, progressively decreasing an estimate $d\left[v\right]$ on the weight of a shortest path from a source $s$ to each other vertex $v\in V$ until it reaches the actual shortest path weight $\delta \left(s,v\right)$. The algorithm is capable of detecting negative cycles and returns true if and only if the graph contains no negative cycles that are reachable from the source. If BELLMAN-FORD returns true, then we have $d\left[u\right]=\delta \left(s,v\right)$ for all vertices $v$. Also, for all $v\in V$ not reachable from $s$, $\delta \left(s,v\right)=\infty$. Figure 4 shows the execution of the BELLMAN-FORD algorithm listed below on an example graph.
BELLMAN-FORD $\left(G,w,s\right)$
1     INITIALIZE $\left(G,s\right)$
2    for  $i$ $←$  $1$ to  $|V|-1$
3             do for each edge $\left(u,v\right)\in E$
4                            do  RELAX $\left(u,v,w\right)$
5    for each edge $\left(u,v\right)\in E$
6             do if  $d\left[v\right]>d\left[u\right]+w\left(u,v\right)$
7                         then return FALSE
8    return TRUE
Figure 4: Executing BELLMAN-FORD. Step (a) shows the state of the graph after initialisation. In each of the $|V|-1$ iterations, we assume that the edges are visited in a left to right order $\left(s,a\right)$, $\left(s,b\right)$, $\left(s,e\right)$, $\left(a,f\right)$, ... $\left(q,r\right)$. Due to this visiting order, within iteration 1, vertex $q$ is first relaxed through edge $\left(f,q\right)$, setting $d\left[q\right]$ to 8. Soon after, edge $\left(p,q\right)$ is considered and $d\left[q\right]$ set to 7. Accordingly, $p\left[q\right]$ is also updated.

## 7  Single-Source Shortest Paths in DAGs

By relaxing the edges in a DAG according to their topological sort of its vertices. We can achieve $\Theta \left(n+m\right)$ time complexity.
DAG-SHORTEST $\left(G,w,s\right)$
1    Topologically sort the vertices of $G$
2     INITIALIZE $\left(G,s\right)$
3    for each vertex $u$ taken in topologically sorted (increasing) order
4             do for  $v\in \text{Adj}\left[u\right]$
5                            do  RELAX $\left(u,v,w\right)$
Figure 5 shows an example execution of DAG-SHORTEST on a DAG.
Figure 5: Excution of DAG-SHORTEST. Step (a) shows the state of the graph after initialisation. Following this, the vertices are considered in topological order: $c$, $s$, $i$, and then $t$. As each vertex $u$ is considered, vertices in $\text{Adj}\left[u\right]$ are relaxed.

## 8  Difference constraints and shortest paths

Linear Programming: Given an $m×n$ matrix $A$, an $m$-vector $b$, and an $n$-vector $c$, we wish to find a vector $x$ of $n$ elements that maximise the objective function $\sum _{i=1}^{n}{c}_{i}{x}_{i}$ subject to the $m$ constraints given by $\mathrm{Ax}\le b$.
Approaches to solving linear programming.
• Simplex algorithm
• Ellipsoid algorithm
• Karmarkkar's algorithm

## 9  Systems of difference constraints

Special linear programming: each row of the linear programming matrix $A$ contains 1 or -1, and all other entries of $A$ are 0s. Thus, the constraints can be expressed as

 ${x}_{j}-{x}_{i}\le {b}_{k}$

where $1\le i,j\le n$ and $1\le k\le m$.
For example, a set of four unknowns $\left\{{x}_{1},{x}_{2},{x}_{3},{x}_{4}\right\}$, and 6 difference constraints:

 $\begin{array}{ccc}\multicolumn{1}{c}{{x}_{1}-{x}_{2}}& \le \hfill & 6\hfill \\ \multicolumn{1}{c}{{x}_{1}-{x}_{3}}& \le \hfill & -4\hfill \\ \multicolumn{1}{c}{{x}_{2}-{x}_{3}}& \le \hfill & -10\hfill \\ \multicolumn{1}{c}{{x}_{3}-{x}_{4}}& \le \hfill & 14\hfill \\ \multicolumn{1}{c}{{x}_{4}-{x}_{2}}& \le \hfill & -4\hfill \\ \multicolumn{1}{c}{{x}_{4}-{x}_{1}}& \le \hfill & -10\hfill \end{array}$

... can be represented as ...

 $\left(\begin{array}{cccc}\hfill 1\hfill & \hfill -1\hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill 1\hfill & \hfill 0\hfill & \hfill -1\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 1\hfill & \hfill -1\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill & \hfill -1\hfill \\ \hfill 0\hfill & \hfill -1\hfill & \hfill 0\hfill & \hfill 1\hfill \\ \hfill -1\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 1\hfill \end{array}\right)\left(\begin{array}{c}\hfill {x}_{1}\hfill \\ \hfill {x}_{2}\hfill \\ \hfill {x}_{3}\hfill \\ \hfill {x}_{4}\hfill \end{array}\right)\le \left(\begin{array}{c}\hfill 6\hfill \\ \hfill -4\hfill \\ \hfill -10\hfill \\ \hfill 14\hfill \\ \hfill -4\hfill \\ \hfill -10\hfill \end{array}\right)$

Given a system $\mathrm{Ax}\le b$ of difference constraints, the corresponding constraint graph is a weighted directed graph $G=\left(V,E\right)$ where

 $V=\left\{{v}_{0},{v}_{1},{v}_{2},\dots ,{v}_{n}\right\}$

... where $n$ is the number of unknowns, ${v}_{0}$ is a special source vertex created, and ${v}_{i}$ corresponds to ${x}_{i}$ for $1\le i\le n$. Also, the set of edges ...

 $E=\left\{⟨{v}_{i},{v}_{j}⟩:{x}_{j}-{x}_{i}\le {b}_{k} \mathrm{is} a \mathrm{constraint}\right\}\cup \left\{⟨{v}_{0},{v}_{i}⟩:i=1,\dots ,n\right\}$

Theorem: Given a system $\mathrm{Ax}\le b$ of difference constraints, let $G$ be the corresponding constraint graph. If $G$ contains no negative weight cycles, then
 $x=\left\{\delta \left({v}_{0},{v}_{1}\right),\delta \left({v}_{0},{v}_{1}\right),\dots ,\delta \left({v}_{0},{v}_{n}\right)\right\}$

is a feasible solution for the system. Figure 6 shows how this is done for the example with four unknowns above.
Figure 6: A constraint graph constructed from our on going example using simple rules: (1) Each unknown ${x}_{i}$ maps onto a vertex ${v}_{i}$. (2) For each constraint ${x}_{j}-{x}_{i}\le {b}_{k}$, there is an edge $\left({v}_{i},{v}_{j}\right)$ with weight ${b}_{k}$. (3) Add a vertex ${v}_{0}$ and have edges from it to all other vertices with weight $0$. Next, use one of the SSSP algorithms to solve $d\left[{v}_{i}\right]$ for $0\le i\le n$; using ${v}_{0}$ as the source.

File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.