# Problem Set 4

Consider a directed graph with real-valued edge lengths and no negative-cost cycles. Let be a source vertex. Assume that there is a unique shortest path from to every other vertex. What can you say about the subgraph of that you get by taking the union of these shortest paths? [Pick the strongest statement that is guaranteed to be true.]

- It has no strongly connected component with more than one vertex.
- It is a path, directed away from .
- It is a tree, with all edges directed away from .
- It is a directed acyclic subgraph in which has no incoming arcs.

**ANSWER:** Option 3 is correct. Subpaths of shortest paths must themselves be shortest paths. Combining this with uniqueness, the union of shortest paths cannot include two different paths between any source and destination.

Consider the following optimization to the Bellman-Ford algorithm. Given a graph with real-valued edge lengths, we label the vertices . The source vertex should be labeled “1”, but the rest of the labeling can be arbitrary. Call an edge forward if and backward if . In every odd iteration of the outer loop (i.e., when ), we visit the vertices in the order from to . In every even iteration of the outer loop (when ), we visit the vertices in the order from to . In every odd iteration, we update the value of using only the forward edges of the form , using the most recent subproblem value for (that from the current iteration rather than the previous one). That is, we compute , where the inner minimum ranges only over forward edges sticking into (i.e., with ). Note that all relevant subproblems from the current round ( with ) are available for constant-time lookup. In even iterations, we compute this same recurrence using only the backward edges (again, all relevant subproblems from the current round are available for constant-time lookup). Which of the following is true about this modified Bellman-Ford algorithm?

- This algorithm has an asymptotically superior running time to the original Bellman-Ford algorithm.
- It correctly computes shortest paths if and only if the input graph has no negative-cost cycle.
- It correctly computes shortest paths if and only if the input graph is a directed acyclic graph.
- It correctly computes shortest paths if and only if the input graph has no negative edges.

Option 2 is correct. This is Yen’s improvement to Bellman-Ford. Recall that by relaxing the edges of a weighted DAG according to a topological sort of its vertices, we can compute shortest paths from a single source in time (CLRS section 24.2). Roughly, pass of this optimized Bellman-Ford algorithm computes shortest paths amongst those comprising at most “alternations” between forward and backward edges.

Consider a directed graph in which every edge has length 1. Suppose we run the Floyd-Warshall algorithm with the following modification: instead of using the recurrence , we use the recurrence . For the base case, set if is an edge and 0 otherwise. What does this modified algorithm compute – specificially, what is at the conclusion of the algorithm?

- The length of a longest path from to .
- The number of simple (i.e., cycle-free) paths from to .
- The number of shortest paths from to .
- None of the other answers are correct.

**ANSWER:** Option 1 is incorrect, nothing in the recurrence is indicative of the longest path from to (you’d expect a somewhere).

Option 2 is incorrect, nothing in the recurrence prevents cycles.

Option 1 is incorrect, nothing in the recurrence is indicative of the shortest path from to (you’d expect a somewhere).

Option 4 is correct. The recurrence computes the number of paths from from to , including those containing cycles.

Suppose we run the Floyd-Warshall algorithm on a directed graph in which every edge’s length is either -1, 0, or 1. Suppose further that is strongly connected, with at least one path for every pair of vertices. The graph may or may not have a negative-cost cycle. How large can the final entries be, in absolute value? Choose the smallest number that is guaranteed to be a valid upper bound. (As usual, denotes .) [WARNING: for this question, make sure you refer to the implementation of the Floyd-Wardshall algorithm given in lecture, rather than to some alternative source.]

- \(n^2\)
- \(+\infty\)
- \(n - 1\)
- \(2^n\)

**ANSWER:** Lets prove by induction that . Lets further assume that all edge lengths are -1. From the lecture, , which by the assumption that all edge lengths are -1, is . Thus, the base case is proved.

Now consider the recurrence . For , the R.H.S. is equal to , or . Thus, it is true in general for all values of k, and since the maximum value of can be , option 4 is correct.

Note that the induction doesn’t hold if we consider all edge lengths equal to 1, since the minimum is going to be the first term, not the sum of two terms.

Which of the following events cannot possibly occur during the reweighting step of Johnson’s algorithm for the all-pairs shortest-paths problem? (Assume that the input graph has no negative-cost cycles.)

- Reweighting strictly increases the length of some path, while strictly decreasing the length of some path.
- The length of some edge strictly decreases after the reweighting.
- In a directed acyclic graph, reweighting causes the length of every path to strictly increase.
- In a directed graph with at least one cycle, reweighting causes the length of every path to strictly increase.

**ANSWER:** Reweighting makes all path lengths non-negative, but it also reduces the length for some. Thus, option 1 is incorrect.

Option 2 and 3 are incorrect by the same argument.

Consider two “halves” of a cycle: The increase in length of one of these paths equals the decrease in length of the other path. Thus, option 4 is correct (it cannot possibly occur during the reweighting step).

Consider a directed graph with real-valued edge lengths and no negative-cost cycles. Let be a source vertex. Assume that each shortest path from to another vertex has at most edges. How fast can you solve the single-source shortest path problem? (As usual, and denote the number of vertices and edges, respectively.) [Pick the strongest statement that is guaranteed to be true.]

- \(O(kn)\)
- \(O(km)\)
- \(O(mn)\)
- \(O(m + n)\)

**ANSWER:** Option 1 is correct. We can limit the number of edges in the Bellman-Ford algorithm to .