GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 12(a)

Initialize all-pairs shortest-path distances

Algorithms · Graph Algorithms · Full GATE CSE 2002 paper

GATE 2002, Q12(a) Descriptive
Report issue

Graph Algorithms

Question 12(a), 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 initialization line so that the algorithm is correct.
Question 12(a), 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 initialization line 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: For i≠j: ∞ if A[i,j]=0, otherwise 1; for i=j: 0 (no official key was published)

1. Fix the graph interpretation. The adjacency entries are binary: A[i,j]=1 means a directed edge i→j of length 1, and A[i,j]=0 means that edge is absent.

2. Handle distinct vertices before any intermediate vertices are allowed. If i≠j and A[i,j]=1, the one-edge path has length 1, so initialize P[i,j]=1. If i≠j and A[i,j]=0, no direct path is available yet, so initialize P[i,j]=∞. Zero would incorrectly claim a free path between disconnected vertices.

3. Handle the diagonal separately. Standard shortest-path distance permits the empty path, so P[i,i]=0 whether or not A[i,i] contains a self-loop. A length-1 self-loop cannot beat that empty path.

4. Fill the original two blanks with expressions that incorporate these cases, without changing the printed outer condition:

if A[i,j] = 0 then P[i,j] = (0 if i = j else ∞)
              else P[i,j] = (0 if i = j else 1);

Equivalently, initialize off-diagonal entries to ∞/1 and then explicitly set each P[i,i] to 0.

5. Check a small graph. If the only edge is 1→2, initialization gives rows [0,1] and [∞,0]. This correctly represents the direct path, lack of a reverse path, and both empty paths.

6. State the convention caveat. The abbreviated fills “∞,1” alone make a diagonal entry the length of a shortest nonempty closed walk, or ∞ if none exists, rather than the standard distance 0. That abbreviation suffices only if diagonal results are excluded or that other convention is intended. The conditional fill above is correct for every vertex pair under the standard convention and respects the template’s two blanks.

Original paper, page 8