Graph Algorithms
![Question 70, GATE 2003: G is a directed n-vertex graph. A path may repeat vertices; a simple path has no repeated vertex. Initialize A[j,k]=1 when edge (j,k) exists, otherwise 0. Run in-place:
for i=1,...,n:
for j=1,...,n:
for k=1,...,n:
A[j,k]=max(A[j,k], A[j,i]+A[i,k])
Which is necessarily true at termination?
A. A[j,k]≤n
B. If A[j,j]≥n−1, G has a Hamiltonian cycle
C. Whenever a path j→k exists, A[j,k] is its longest-path length
D. Whenever a path j→k exists, every simple j→k path has at most A[j,k] edges](../../assets/questions/CS-2003-Q70-1.webp)
![Question 70, GATE 2003: G is a directed n-vertex graph. A path may repeat vertices; a simple path has no repeated vertex. Initialize A[j,k]=1 when edge (j,k) exists, otherwise 0. Run in-place:
for i=1,...,n:
for j=1,...,n:
for k=1,...,n:
A[j,k]=max(A[j,k], A[j,i]+A[i,k])
Which is necessarily true at termination?
A. A[j,k]≤n
B. If A[j,j]≥n−1, G has a Hamiltonian cycle
C. Whenever a path j→k exists, A[j,k] is its longest-path length
D. Whenever a path j→k exists, every simple j→k path has at most A[j,k] edges](../../assets/questions/CS-2003-Q70-2.webp)
Answer & solution
Answer: D (no official key was published)
1. Initially every one-edge path has its length represented by A[j,k]≥1. Updates only increase entries, so established lower bounds persist.
2. For a simple path with internal vertices, take its highest-numbered internal vertex i. Splitting there gives two subpaths whose internal vertices have smaller indices. Inductively their lengths are bounded above by A[j,i] and A[i,k] before processing i.
3. The update therefore makes A[j,k] at least the sum of those lengths, the length of the original path. This proves D for every simple path.
4. This is not a correct longest-simple-path algorithm: it never checks that the joined paths are disjoint. Cycles can repeatedly inflate values, and zero is an unsafe unreachable marker for maximizing lengths. Therefore exactness, an n bound and Hamiltonian-cycle inference do not follow.
Solution: The recurrence A[j,k]=max(A[j,k],A[j,i]+A[i,k]) gives option D. Induct on the largest-index internal vertex of a simple path: direct edges start at one; splitting a path at its largest-index internal vertex combines previously established lower bounds for the two pieces. Monotone updates cannot destroy them. Thus the final A[j,k] is at least the length of every simple j-to-k path. It need not equal a longest-path length: cycles and zero values used for nonexistent edges can inflate entries, so the other claims fail. For example, a directed two-vertex cycle with an isolated third vertex can produce values larger than n and large diagonal entries although there is no Hamiltonian cycle. Neither the claimed n bound nor the exact-longest-path interpretation is justified.