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 7The vertices are mapped as follows: 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.
At the time of this writing, the tool becomes unresponsive when there are negative-weight cycles reachable from the source. This happens in the second part of this exercise, so the answer should be FALSE.
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, ).
22.2-2
Available in the latest revision of the IM.
22.2-3 🌟
Shows two ways to transform a PERT chart with weights on vertices to a PERT chart with weights on edges. This allows running the original code on a new graph.
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 . This allows us to emulate the weights with distances by inserting extra vertices comprising the set . For example, the edge would be transformed into a series of edges and vertices in : , where . If , just leave the original edge. Therefore,
By construction, the unweighted shortest-path distance in the new graph, let's call it , is exactly equal to the weighted shortest-path distance in the original graph for all . 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 🌟
When keys are known to be integers in the range 0 to and the key values extracted are monotonically increasing over time, we can implement a min-priority queue so that any sequence of INSERT, EXTRACT-MIN, and DECREASE-KEY operations takes time. This exercise shows how this proprietary priority queue can reduce the time of Dijkstra’s algorithm.
Available in the latest revision of the IM.
22.3-10
Available in the latest revision of the IM.
22.3-11 🌟
Highlights an important special case, where Dijkstra's algorithm still works even though edges that leave the source vertex may have negative weights.
Available in the latest revision of the IM.
22.3-12
Available in the latest revision of the IM.
22.4-1
By running the Bellman-Ford algorithm from the super-source vertex , 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 , which are displayed inside their respective vertices.
22.4-2
The graph contains a negative-weight cycle , so no feasible solution exists.
22.4-3
Available in the latest revision of the IM.
22.4-4 🌟
Expresses the single-pair shortest-path problem as a linear program.
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 —it actually maximizes each individual variable simultaneously! You may also want to take a look at Exercise 24.4-4.
Let the solution produced by the Bellman-Ford algorithm be , where each . By the definition of the constraint graph, there is a directed edge from to every other vertex with a weight of exactly 0. Therefore, the shortest path from to can never be greater than 0. This guarantees for all .
Suppose there is some other arbitrary feasible solution that satisfies both the system and the condition for all . We want to see how compares to our Bellman-Ford solution .
Pick any vertex . In the constraint graph, consider the shortest path from to that Bellman-Ford found. Let's say this path goes through a sequence of vertices:
Because is a valid solution to the system, it must satisfy the difference constraints corresponding to every single edge along this path:
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:
Since , the left side is just . The right side is exactly the total weight of the shortest path from to , which is our Bellman-Ford solution . Therefore, we have proven that for every single variable. Because the Bellman-Ford solution is greater than or equal to any other feasible solution on a component-by-component basis, it mathematically follows that their sums share the same relationship:
Thus, the Bellman-Ford algorithm maximizes the sum.
★ 22.4-9
Let the solution produced by the Bellman-Ford algorithm be , where each . 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, , so the spread of the Bellman-Ford solution is
Suppose there is some other solution that satisfies the system . Let's shift it, so its maximum value is exactly 0. We define a new solution where (Lemma 22.8 ensures that is also a valid solution), thus for all . By the property established in the previous exercise, we have for every single variable. Thus,
Since shifting a solution by a constant doesn't change its spread, the spread of the original arbitrary solution is larger. Therefore, no feasible solution can have a tighter spread than the Bellman-Ford solution.
Practical Application: Construction Scheduling
The quantity 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 🌟
Shows how to adapt the Bellman-Ford algorithm to solve a constraint system with single-variable inequalities.
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 to , iterate over every edge with weight :
If is constrained to be an integer:
If is allowed to be real-valued:
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 with edge . 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 contains a negative-weight cycle, then , so means contains no negative-weight cycles.
Because , every cycle in the graph has a weight . If you have a path from to that contains a cycle, you can remove that cycle to create a new path. Since the cycle's weight was , the new path's weight will be less than or equal to the original path. Therefore, for any shortest path from to , there is always a simple path that achieves that same shortest-path weight . Furthermore, must be the minimum of the shortest paths of length . This perfectly matches the formula .
b.
The denominator is always positive. The is either (when there is no path of length from to ) or some finite value . In the latter case, the path must contain a simple cycle of length . We also know that (see part(a)). If we remove from , we are left with a new path, let's call it , which has exactly edges. The weight of this specific path is . Thus,
Therefore, for the numerator is non-negative, which concludes the proof.
c.
We know that contains no negative-weight cycles from part (a). Using the hint from the book, we get the following two inequalities by traversing the cycle from different starting points:
Consequently, we have .
d.
We need to show that there is some vertex on the cycle, where
Combining this with part (b) shows that the maximum is 0 for some vertex .
Let be our minimum mean-weight cycle. Since , has a total weight of exactly 0.
Pick any vertex on the cycle . Because all vertices are reachable from , there exists a shortest path from to . Let this shortest path have edges. Because we established in part (a) that shortest paths can be simple, we know . The weight of this path is exactly .
Now, follow the hint: extend this path forward by continuing along the edges of the cycle for exactly more edges. Let be the vertex you land on after tracing those edges. (Note: Because you are tracing along a cycle, you might loop around it, but you will eventually land on some vertex that is also on ).
The first part of the path to has edges, and the extension has edges. The total length is exactly edges. The first part has a weight of . 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 to which we will call .
e.
If you have a set of numbers where every number is (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 to the weight of each edge of , then increases by for every cycle. Also, , which entails that also increases by .
Part (e) proved that the formula equals 0, but only under the strict assumption that . Let be a graph with a minimum mean-weight cycle of . We can construct a modified graph by subtracting from the weight of every edge (which is the same as adding ). By the rule we just proved above, the new minimum mean-weight cycle of is precisely . Hence, we can safely apply the theorem from part (e) to this new graph. Let be the shortest path of exactly edges in :
A path of exactly edges in weighs less than it did in . Thus,
Substituting this into the numerator:
Substitute this back into the fraction inside our max function:
Because is a constant that applies equally to every term, we can pull it completely outside of the max and min operators:
g.
The total time assumes that .
22-6 Bitonic shortest paths
Available in the latest revision of the IM.
Last updated