Let be a directed graph with vertices. A path from to in is a sequence of vertices () such that for all in through . A simple path is a path in which no vertex appears more than once.
Let be an array initialized as follows:
Consider the following algorithm:
for i=1 to n
for j=1 to n
for k=1 to n
A[j,k] = max(A[j,k], A[j,i] + A[i,k]);
Which of the following statements is necessarily true for all and after termination of the above algorithm?
View worked solution
Answer: D
The given algorithm is a variation of the Floyd-Warshall algorithm, where the min operation is replaced by max. Since all initial edge weights are , any update of the form computes the length of some walk from to .
Let be any simple path from to of length . Since is a simple path, all its vertices are distinct. We can show by induction on the maximum index of the intermediate vertices that the algorithm will eventually find a walk of length at least :
- Let be the vertex in the intermediate set of with the largest index.
- The subpaths from to and from to only use intermediate vertices with indices strictly smaller than .
- By induction, before the outer loop reaches step , we have and .
- At step , the algorithm updates , ensuring .
Since only increases in subsequent steps, the final value of is at least the length of any simple path from to . Thus, every simple path from to contains at most edges.
Free practice · Your answers on this page are not saved.
