Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS
Bellman–Ford is run from source S on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: S→E (7), M→S (5), O→M (7), M→E (6), E→O (4), O→S (3). One pass relaxes every arc once, in the listed order. Passes are repeated until a complete pass changes no distance. Counting the final pass that changes nothing, how many passes run in total?
- A. 2
- B. 6
- C. 4
- D. 3
Correct answer: D. 3
The last pass changes no distance, so it is counted; the passes before it changed something, 2 of them. The bound n − 1 = 3 counts only the passes needed in the worst case.
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
- Cities J, O, Q, V, X, Y are joined by roads with these travel costs: X–J 4, X–Q 5, X–Y 7, X–V 6, X–O 2, J–Q 8, J–Y 4, J–V 5, J–O 9, Q–Y…
- An undirected graph has the vertices C, E, F, L, U, X, Z and the edges L–C, C–U, X–L, E–C, Z–L, E–U, F–L, Z–C, E–Z, X–U. How many simple…
- An undirected graph has the vertices F, I, M, Q, T and the edges I–Q, Q–M, I–F, Q–F, I–T, T–Q, T–M, I–M, T–F. What is the minimum number…
- 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 connected graph on the vertices H, K, L, V has the edges L–K, V–H, H–K, V–K, L–H, V–L. How many spanning trees does it have?…
- A directed acyclic graph has the vertices A, E, G, V, X, Z and the edges G→E, G→V, X→G, X→A, A→Z, Z→E. A topological order lists every…