GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 12(b)

Complete the Floyd–Warshall update for the given loop order

Algorithms · Graph Algorithms · Full GATE CSE 2002 paper

GATE 2002, Q12(b) Descriptive
Report issue

Graph Algorithms

Question 12(b), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the line in the algorithm’s triple loop so that the algorithm is correct.
Question 12(b), GATE 2002: Complete a template computing all-pairs shortest-path lengths in a directed graph G. Its n×n adjacency matrix A has A[i,j]=1 for an edge i→j and 0 otherwise. Initialization loops over i=1..n and j=1..n and executes:
if A[i,j]=0 then P[i,j]=____ else P[i,j]=____;
The algorithm then has nested loops, in this exact order: for i=1..n, for j=1..n, for k=1..n, executing:
P[____,____] = min{____,____};
Copy and complete the line in the algorithm’s triple loop so that the algorithm is correct.
Tap to enlarge
Shared context

For a directed unweighted graph, binary adjacency A[i,j] equals 1 for an edge i → j and 0 otherwise. Initialize a distance table P and run the printed loops in the order i outermost, then j, then k. Standard shortest-path distances permit an empty path of length 0.

Answer & solution

Answer: P[j,k] = min(P[j,k], P[j,i] + P[i,k]) (no official key was published)

1. Respect the printed loop order: i is outermost, j is next, and k is innermost. In Floyd–Warshall the outer loop must identify the newly allowed intermediate vertex. Therefore i is that intermediate, j is the source, and k the destination.

2. State the invariant. Before outer iteration i, P[j,k] is the shortest distance from j to k using only vertices 1,…,i−1 as internal vertices. Initialization supplies paths with no internal vertices: 0 on the diagonal, 1 for a direct edge, and ∞ otherwise.

3. Split paths into two exhaustive cases. A shortest path allowed after iteration i either avoids i internally, in which case its best length is the old P[j,k], or passes through i, in which case it can be split into a j→i portion and an i→k portion. Their best combined length is P[j,i]+P[i,k]. Nonnegative unit weights let us choose a shortest path without repeated positive-length cycles.

4. Take the better candidate and fill the line as:

P[j,k] = min(P[j,k], P[j,i] + P[i,k]);

For example, if the current j→k value is ∞ while P[j,i]=2 and P[i,k]=3, the update discovers a path of length 5. If P[j,k] were already 4, the minimum would retain 4.

5. Verify in-place and final correctness. During iteration i, the zero diagonal means an update through i does not lower the distances into or out of i; the in-place recurrence therefore preserves the invariant. After i=n, all vertices are allowed internally, so the table gives every shortest-path distance. Interpret ∞+x as ∞, or guard finite arithmetic against overflow.

6. Avoid a loop-order substitution error. The familiar form P[i,j]=min(P[i,j],P[i,k]+P[k,j]) assumes k is outermost. Under the source’s i,j,k loop order, using that form would generally be incorrect. Exhaustive comparison with BFS on every directed three-vertex graph, including self-loops, verifies the completed initialization and the stated j,k update.

Original paper, page 8