Run SLOW-ALL-PAIRS-SHORTEST-PATHS on the weighted, directed graph of Figure 25.2, showing the matrices that result for each iteration of the loop. Then do the same for FASTER-ALL-PAIRS-SHORTEST-PATHS.
Initial:
$$ \left \{ \begin{matrix} 0 & \infty & \infty & \infty & -1 & \infty\\ 1 & 0 & \infty & 2 & \infty & \infty\\ \infty & 2 & 0 & \infty & \infty & -8\\ -4 & \infty & \infty & 0 & 3 & \infty\\ \infty & 7 & \infty & \infty & 0 & \infty\\ \infty & 5 & 10 & \infty & \infty & 0\\ \end{matrix} \right \} $$ Slow:
Why do we require that
$w_{ii}=0$ for all$1 \le i \le n$ ?
To simplify (25.2).
What does the matrix
$$ L^{(0)} = \left ( \begin{matrix} 0 & \infty & \infty & \cdots & \infty \\\ \infty & 0 & \infty & \cdots & \infty \\\ \infty & \infty & 0 & \cdots & \infty \\\ \vdots & \vdots & \vdots & \ddots & \vdots \\\ \infty & \infty & \infty & \cdots & 0 \\\ \end{matrix} \right ) $$
used in the shortest-paths algorithms correspond to in regular matrix multiplication?
Unit.
Show that matrix multiplication defined by EXTEND-SHORTEST-PATHS is associative.
Show how to express the single-source shortest-paths problem as a product of matrices and a vector. Describe how evaluating this product corresponds to a Bellman-Ford-like algorithm (see Section 24.1).
A vector filled with 0 except that the source is 1.
Suppose we also wish to compute the vertices on shortest paths in the algorithms of this section. Show how to compute the predecessor matrix
$\prod$ from the completed matrix$L$ of shortest-path weights in$O(n^3)$ time.
If
We can also compute the vertices on shortest paths as we compute the shortestpath weights. Define
$\pi_{ij}^{(m)}$ as the predecessor of vertex$j$ on any minimum-weight path from$i$ to$j$ that contains at most$m$ edges. Modify the EXTEND-SHORTESTPATHS and SLOW-ALL-PAIRS-SHORTEST-PATHS procedures to compute the matrices$\prod^{(1)}, \prod^{(2)}, \dots, \prod^{(n-1)}$ as the matrices$L^{(1)}, L^{(2)}, \dots, L^{(n-1)}$ are computed.
If
The FASTER-ALL-PAIRS-SHORTEST-PATHS procedure, as written, requires us to store
$\lceil \lg (n - 1) \rceil$ matrices, each with$n^2$ elements, for a total space requirement of$\Theta(n^2 \lg n)$ . Modify the procedure to require only$\Theta(n^2)$ space by using only two$n \times n$ matrices.
def fast_all_pairs_shortest_paths(w):
n = len(w)
m = 1
while m < n - 1:
w = extend_shortest_paths(w, w)
m *= 2
return w
Modify FASTER-ALL-PAIRS-SHORTEST-PATHS so that it can determine whether the graph contains a negative-weight cycle.
If
Give an efficient algorithm to find the length (number of edges) of a minimum-length negative-weight cycle in a graph.
If