For the complete documentation index, see llms.txt. This page is also available as Markdown.

22. Single-Source Shortest Paths

Exercises

22.1-1

Use the VisuAlgo tool to see the Bellman-Ford algorithm in action on the graph of Figure 22.4. Choose the option Input Graph using 0-indexing of vertices and the edge list format (click the Help! button for further details or see Exercise 22.1-5). Enter the following content and press Submit:

5 10
0 1 5
0 2 8
0 3 -4
1 0 -2
2 1 -3
2 3 9
3 1 7
3 4 2
4 0 6
4 2 7

The vertices are mapped as follows: s4,t0,y2,x1,z3s \to 4, t \to 0, y \to 2, x \to 1, z \to 3 to force the tool to relax the edges in the same order as in the book.

The tool shows you the graph in some layout. Press the Help button for instructions how to manually rearrange the vertices to recreate Figure 22.4. Once ready, press the Done button. After selecting the Bellman-Ford algorithm, you can specify its variant and the source vertex. Press the play button to watch how edges are relaxed.

22.1-2

Available in the latest revision of the IM.

22.1-3

Available in the latest revision of the IM.

22.1-4

Available in the latest revision of the IM.

22.1-5

Available in the latest revision of the IM.

22.1-6

Available in the latest revision of the IM.

22.1-7

Available in the latest revision of the IM.

22.2-1

Use the VisuAlgo tool as in Exercise 22.1-1 and enter the following graph of Figure 22.5:

Observe that after one iteration of Bellman-Ford all vertices are set to their final values. This emulates finding shortest paths in a dag. The vertices are mapped in topological order (for example, r0,s1,r \to 0, s\to 1, \dots).

22.2-2

Available in the latest revision of the IM.

22.2-3 🌟

Available in the latest revision of the IM.

★ 22.2-4

Available in the latest revision of the IM.

22.3-1

Use the VisuAlgo tool as in Exercise 22.1-1 and Exercise 22.2-1 and select Dijsktra's original version of the algorithm. Also, reuse the examples from the previously mentioned exercises to set up the matching edge list.

22.3-2

Available in the latest revision of the IM.

22.3-3

Available in the latest revision of the IM.

22.3-4

Available in the latest revision of the IM.

22.3-5

Available in the latest revision of the IM.

22.3-6

Available in the latest revision of the IM.

22.3-7

Available in the latest revision of the IM.

22.3-8

The following citation from the book is the essence of the solution:

You can think of Dijkstra’s algorithm as generalizing breadth-first search to weighted graphs. A wave emanates from the source, and the first time that a wave arrives at a vertex, a new wave emanates from that vertex. Whereas breadth-first search operates as if each wave takes unit time to traverse an edge, in a weighted graph, the time for a wave to traverse an edge is given by the edge’s weight.

As given in the exercise, we can assume that no two vertices have the same shortest-path weights from source vertex ss. This allows us to emulate the weights with distances by inserting extra vertices comprising the set VV'. For example, the edge e=(u,v)Ee=(u,v) \in E would be transformed into a series of edges and vertices in GG': (u,uv1),(uv1,uv2),,(uvk,v)(u,uv_1),(uv_1,uv_2),\dots,(uv_k,v), where k=w(e)1k=w(e)-1. If k=0k=0, just leave the original edge. Therefore,

V=eE(w(e)1)=eEw(e)E.\vert{}V'\vert{} = \sum_{e \in E} (w(e) - 1) = \sum_{e \in E} w(e) - \vert{}E\vert{}.

By construction, the unweighted shortest-path distance in the new graph, let's call it δ(s,v)\delta'(s, v), is exactly equal to the weighted shortest-path distance δ(s,v)\delta(s, v) in the original graph for all vVv \in V. BFS always discovers and colors vertices black in monotonically increasing order of their unweighted distance from the source. Dijkstra's algorithm always extracts vertices from its priority queue in monotonically increasing order of their weighted distance from the source. Because all distances are unique, there are no ties for either algorithm to break arbitrarily. Since both algorithms process the vertices in the same order based on the exact same distance values, the order must be identical.

22.3-9 🌟

Available in the latest revision of the IM.

22.3-10

Available in the latest revision of the IM.

22.3-11 🌟

Available in the latest revision of the IM.

22.3-12

Available in the latest revision of the IM.

22.4-1

The constraint graph representing the given system of difference constraints.

By running the Bellman-Ford algorithm from the super-source vertex v0v_0, we find that there are no negative-weight cycles, which means a feasible solution exists. The resulting shortest-path weights provide the solution for each variable xix_i, which are displayed inside their respective vertices.

22.4-2

The constraint graph representing the given system of difference constraints.

The graph contains a negative-weight cycle v4v2v3v5v1v4v_4 \to v_2 \to v_3 \to v_5 \to v_1 \to v_4, so no feasible solution exists.

22.4-3

Available in the latest revision of the IM.

22.4-4 🌟

Available in the latest revision of the IM.

22.4-5

Available in the latest revision of the IM.

22.4-6

Available in the latest revision of the IM.

22.4-7

Available in the latest revision of the IM.

★ 22.4-8

The trick to solving this exercise is realizing that Bellman-Ford doesn't just maximize the sum i=1nxi\sum_{i=1}^n x_i—it actually maximizes each individual variable xix_i simultaneously! You may also want to take a look at Exercise 24.4-4.

Let the solution produced by the Bellman-Ford algorithm be x=(x1,x2,,xn)x = (x_1, x_2, \dots, x_n), where each xi=δ(v0,vi)x_i = \delta(v_0, v_i). By the definition of the constraint graph, there is a directed edge from v0v_0 to every other vertex viv_i with a weight of exactly 0. Therefore, the shortest path from v0v_0 to viv_i can never be greater than 0. This guarantees xi0x_i \le 0 for all xix_i.

Suppose there is some other arbitrary feasible solution y=(y1,y2,,yn)y = (y_1, y_2, \dots, y_n) that satisfies both the system AybAy \le b and the condition yi0y_i \le 0 for all yiy_i. We want to see how yiy_i compares to our Bellman-Ford solution xix_i.

Pick any vertex viv_i. In the constraint graph, consider the shortest path from v0v_0 to viv_i that Bellman-Ford found. Let's say this path goes through a sequence of vertices:

v0vk1vk2vkmvi.v_0 \to v_{k_1} \to v_{k_2} \to \dots \to v_{k_m} \to v_i.

Because yy is a valid solution to the system, it must satisfy the difference constraints corresponding to every single edge along this path:

  • yk1y0w(v0,vk1)y_{k_1} - y_0 \le w(v_0, v_{k_1})

  • yk2yk1w(vk1,vk2)y_{k_2} - y_{k_1} \le w(v_{k_1}, v_{k_2})

  • \dots

  • yiykmw(vkm,vi)y_i - y_{k_m} \le w(v_{k_m}, v_i)

If we sum all of these inequalities together, the intermediate variables on the left side completely cancel each other out in a telescoping sum. We are left with:

yiy0w(v0,vk1)+w(vk1,vk2)++w(vkm,vi).y_i - y_0 \le w(v_0, v_{k_1}) + w(v_{k_1}, v_{k_2}) + \dots + w(v_{k_m}, v_i).

Since y0=0y_0 = 0, the left side is just yiy_i. The right side is exactly the total weight of the shortest path from v0v_0 to viv_i, which is our Bellman-Ford solution xix_i. Therefore, we have proven that yixiy_i \le x_i for every single variable. Because the Bellman-Ford solution xix_i is greater than or equal to any other feasible solution yiy_i on a component-by-component basis, it mathematically follows that their sums share the same relationship:

i=1nyii=1nxi.\sum_{i=1}^n y_i \le \sum_{i=1}^n x_i.

Thus, the Bellman-Ford algorithm maximizes the sum.

★ 22.4-9

Let the solution produced by the Bellman-Ford algorithm be x=(x1,x2,,xn)x = (x_1, x_2, \dots, x_n), where each xi=δ(v0,vi)x_i = \delta(v_0, v_i). At least one vertex must have a shortest path of exactly 0 (otherwise, all vertices would be part of a negative-weight cycle, which means no feasible solution exists). Therefore, max{xi}=0\max \{x_i\} = 0, so the spread of the Bellman-Ford solution is

(max{xi}min{xi})=0min{xi}=min{xi}.(\max \{x_i\} - \min \{x_i\}) = 0 - \min \{x_i\} = -\min \{x_i\}.

Suppose there is some other solution y=(y1,y2,,yn)y = (y_1, y_2, \dots, y_n) that satisfies the system AybAy \le b. Let's shift it, so its maximum value is exactly 0. We define a new solution yy' where yi=yimax{yj}y'_i = y_i - \max \{y_j\} (Lemma 22.8 ensures that yy' is also a valid solution), thus yi0y'_i \le 0 for all ii. By the property established in the previous exercise, we have yixiy'_i \le x_i for every single variable. Thus,

min{yi}min{xi}    min{yi}min{xi}.\min \{y'_i\} \le \min \{x_i\} \iff -\min \{y'_i\} \ge -\min \{x_i\}.

Since shifting a solution by a constant doesn't change its spread, the spread of the original arbitrary solution yy is larger. Therefore, no feasible solution can have a tighter spread than the Bellman-Ford solution.

Practical Application: Construction Scheduling

The quantity (max{xi}min{xi})(\max \{x_i\} - \min \{x_i\}) represents the time elapsed between the start of the very first job and the start of the very last job. This is effectively the overall duration of the active project. By minimizing this spread, the Bellman-Ford algorithm produces a highly compressed schedule.

22.4-10 🌟

Available in the latest revision of the IM.

22.4-11

Available in the latest revision of the IM.

★ 22.4-12

We can solve this by running Bellman-Ford on the constraint graph with a slightly modified relaxation step, using the idea from the previous exercise.

For i=1i = 1 to V1\vert{}V\vert{} - 1, iterate over every edge (u,v)E(u, v) \in E with weight ww:

  • If vv is constrained to be an integer: xv=min(xv,xu+w)x_v = \min(x_v, \lfloor x_u + w \rfloor)

  • If vv is allowed to be real-valued: xv=min(xv,xu+w)x_v = \min(x_v, x_u + w)

We also need a way to specify the list of variables in the model that should be integers. The total asymptotic running time of this altered algorithm is the same as the original version.

22.5-1

In Figure 22.2 (b) replace edge (t,x)(t,x) with edge (y,x)(y,x). In Figure 22.2 (c) do the opposite.

22.5-2

Available in the latest revision of the IM.

22.5-3

Available in the latest revision of the IM.

22.5-4

Available in the latest revision of the IM.

22.5-5

Available in the latest revision of the IM.

22.5-6

Available in the latest revision of the IM.

22.5-7

Available in the latest revision of the IM.

22.5-8

Available in the latest revision of the IM.

Problems

22-1 Yen’s improvement to Bellman-Ford

Available in the latest revision of the IM.

22-2 Nesting boxes

Available in the latest revision of the IM.

22-3 Arbitrage

Available in the latest revision of the IM.

22-4 Gabow’s scaling algorithm for single-source shortest paths

Available in the latest revision of the IM.

22-5 Karp’s minimum mean-weight cycle algorithm

a.

If GG contains a negative-weight cycle, then μ<0\mu^*<0, so μ=0\mu^*=0 means GG contains no negative-weight cycles.

Because μ=0\mu^*=0, every cycle in the graph has a weight 0\ge 0. If you have a path from ss to vv that contains a cycle, you can remove that cycle to create a new path. Since the cycle's weight was 0\ge 0, the new path's weight will be less than or equal to the original path. Therefore, for any shortest path from ss to vv, there is always a simple path that achieves that same shortest-path weight δ(s,v)\delta(s,v). Furthermore, δ(s,v)\delta(s,v) must be the minimum of the shortest paths of length k[0,n1]k \in [0,n-1]. This perfectly matches the formula min{δk(s,v):0kn1}\min \{\delta_k(s, v) : 0 \le k \le n - 1\}.

b.

The denominator is always positive. The δn(s,v)\delta_n(s,v) is either \infty (when there is no path of length nn from ss to vv) or some finite value w(p)w(p). In the latter case, the path pp must contain a simple cycle cc of length l1l \ge 1. We also know that w(c)0w(c) \ge 0 (see part(a)). If we remove cc from pp, we are left with a new path, let's call it pp', which has exactly nln-l edges. The weight of this specific path is w(p)=δn(s,v)w(c)w(p') = \delta_n(s,v) - w(c). Thus,

δnl(s,v)w(p)    δn(s,v)δnl(s,v)+w(c)    δn(s,v)δnl(s,v).\delta_{n-l}(s,v) \le w(p') \implies \delta_n(s,v) \ge \delta_{n-l}(s,v) + w(c) \implies \delta_n(s,v) \ge \delta_{n-l}(s,v).

Therefore, for k=nlk=n-l the numerator is non-negative, which concludes the proof.

c.

We know that GG contains no negative-weight cycles from part (a). Using the hint from the book, we get the following two inequalities by traversing the cycle cc from different starting points:

  • δ(s,u)δ(s,v)x    δ(s,v)δ(s,u)+x\delta(s,u) \le \delta(s,v)-x \implies \delta(s,v) \ge \delta(s,u)+x

  • δ(s,v)δ(s,u)+x\delta(s,v) \le \delta(s,u)+x

Consequently, we have δ(s,v)=δ(s,u)+x\delta(s,v) =\delta(s,u)+x.

d.

We need to show that there is some vertex vv on the cycle, where

δn(s,v)δk(s,v) for all 0kn1    δn(s,v)δ(s,v).\delta_n(s, v) \le \delta_k(s, v) \text{ for all } 0 \le k \le n-1 \iff \delta_n(s, v) \le \delta(s, v).

Combining this with part (b) shows that the maximum is 0 for some vertex vv.

Let cc be our minimum mean-weight cycle. Since μ=0\mu^* = 0, cc has a total weight of exactly 0.

  • Pick any vertex uu on the cycle cc. Because all vertices are reachable from ss, there exists a shortest path from ss to uu. Let this shortest path have kk edges. Because we established in part (a) that shortest paths can be simple, we know kn1k \le n - 1. The weight of this path is exactly δ(s,u)\delta(s, u).

  • Now, follow the hint: extend this path forward by continuing along the edges of the cycle cc for exactly nkn-k more edges. Let vv be the vertex you land on after tracing those nkn-k edges. (Note: Because you are tracing along a cycle, you might loop around it, but you will eventually land on some vertex vv that is also on cc).

  • The first part of the path to vv has kk edges, and the extension has nkn-k edges. The total length is exactly nn edges. The first part has a weight of δ(s,u)\delta(s, u). The extension is a walk along a 0-weight cycle. Even if it wraps around the cycle multiple times, those full loops add 0 weight. The net weight added is just the weight of the simple path along the cycle from uu to vv which we will call xx.

δn(s,v)δ(s,u)+x=δ(s,v) by part (c)    δn(s,v)δ(s,v).\delta_n(s, v) \le \delta(s, u) + x=\delta(s, v) \text{ by part (c)} \implies \delta_n(s, v) \le \delta(s, v).

e.

If you have a set of numbers where every number is 0\ge 0 (part (b)), and at least one of those numbers is exactly 0 (part (d)), the minimum value of that entire set is strictly 0.

f.

If you add a constant tt to the weight of each edge of GG, then μ(c)\mu(c) increases by tt for every cycle. Also, min{μ(c)+t}=t+min{μ(c)}\min \{\mu(c)+t\}=t+\min \{\mu(c)\}, which entails that μ\mu^* also increases by tt.

Part (e) proved that the formula equals 0, but only under the strict assumption that μ=0\mu^* = 0. Let GG be a graph with a minimum mean-weight cycle of μ\mu^*. We can construct a modified graph GG' by subtracting μ\mu^* from the weight of every edge (which is the same as adding t=μt=-\mu^*). By the rule we just proved above, the new minimum mean-weight cycle of GG' is precisely μ=μμ=0\mu'^* = \mu^* - \mu^* = 0. Hence, we can safely apply the theorem from part (e) to this new graph. Let δk(s,v)\delta'_k(s, v) be the shortest path of exactly kk edges in GG':

minvVmax0kn1{δn(s,v)δk(s,v)nk}=0.\min_{v \in V} \max_{0 \le k \le n-1} \left\{ \frac{\delta'_n(s, v) - \delta'_k(s, v)}{n - k} \right\} = 0.

A path of exactly kk edges in GG' weighs kμk \cdot \mu^* less than it did in GG. Thus,

δk(s,v)=δk(s,v)kμ.\delta'_k(s, v) = \delta_k(s, v) - k \mu^*.

Substituting this into the numerator:

δn(s,v)δk(s,v)=(δn(s,v)nμ)(δk(s,v)kμ)=δn(s,v)δk(s,v)(nk)μ.\begin{align*} \delta'_n(s, v) - \delta'_k(s, v) &= (\delta_n(s, v) - n \mu^*) - (\delta_k(s, v) - k \mu^*) \\ &= \delta_n(s, v) - \delta_k(s, v) - (n - k)\mu^*. \end{align*}

Substitute this back into the fraction inside our max function:

δn(s,v)δk(s,v)(nk)μnk=δn(s,v)δk(s,v)nkμ.\frac{\delta_n(s, v) - \delta_k(s, v) - (n - k)\mu^*}{n - k} = \frac{\delta_n(s, v) - \delta_k(s, v)}{n - k} - \mu^*.

Because μ\mu^* is a constant that applies equally to every term, we can pull it completely outside of the max and min operators:

(minvVmax0kn1{δn(s,v)δk(s,v)nk})μ=0.\left( \min_{v \in V} \max_{0 \le k \le n-1} \left\{ \frac{\delta_n(s, v) - \delta_k(s, v)}{n - k} \right\} \right) - \mu^* = 0.

g.

The total O(VE)O(VE) time assumes that E=Ω(V)E=\Omega(V).

22-6 Bitonic shortest paths

Available in the latest revision of the IM.

Last updated