Data Structures & AlgorithmsModerateGATE CS · ICPC regional
Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (5), 1→4 (2), 2→1 (2), 2→3 (3), 2→4 (6), 3→1 (1), 3→4 (10), 4→2 (4), 4→3 (7). dist[i][i] = 0, dist[i][j] is the arc weight, or ∞ when there is no arc. The outer loop runs over k = 1, 2, … in that order. After the iterations for k = 1 through 2 have finished, what is dist[2][1]?
- A. 2
- B. 4
- C. 1
- D. 3
Correct answer: A. 2
Only intermediate vertices 1 to 2 are allowed in the middle of paths at this point, which gives 2. Stopping one iteration earlier gives 2, and the final distance is 2.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A connected undirected graph has the vertices A, I, P, T, V, Y and the edges P–V, P–T, P–A, V–I, V–Y, I–A. Parallel edges are allowed when…
- On an 6×6 chessboard, rows and columns are numbered 0 to 5, and squares are written (row, column). A knight moves in the usual L-shape…
- An undirected graph has the vertices D, K, R, S, X, Y and the edges Y–R, S–X, D–S, Y–D, Y–S, R–K, R–X, D–R, R–S, D–X. What is the minimum…
- A connected undirected graph has the vertices E, H, O, P, V, W, X and weighted edges (weights in brackets): E–P (4), O–W (16), X–H (8)…
- A directed acyclic graph has the vertices I, K, O, Q, T, U, X, Z and the directed edges K→Z, U→K, U→Z, T→K, X→I, X→K, O→K, T→I, X→U, X→O…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→D (9), S→A (2), S→B (9), S→T (3), A→D (7), A→T…