Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS
Bellman–Ford is run from source J on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: C→J (4), A→C (2), J→C (7), D→A (3), J→Z (8), Z→C (1), C→D (8), A→Z (2), A→J (9), Z→A (6), Z→D (2), C→Z (1). 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. 12
- B. 3
- C. 4
- D. 2
Correct answer: B. 3
The last pass changes no distance, so it is counted; the passes before it changed something, 2 of them. The bound n − 1 = 4 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
- Each cell of a grid with 3 rows and 3 columns has an arrow: R = right, L = left, U = up, D = down. ULU LUR ULU From a cell you may move to…
- A tree has vertices A, D, E, J, L, Q, T, W, X and weighted edges (weights in brackets): L–T (2), T–D (6), D–W (9), T–Q (6), Q–J (7), L–X…
- Each cell of a grid with 3 rows and 4 columns has an arrow: R = right, L = left, U = up, D = down. DLUL RUUR DLRD From a cell you may move…
- A directed graph with no directed cycles has arcs u→v (weight) : A→U (8), Z→U (1), Z→I (1), E→U (-2), E→I (2), U→C (4), U→I (5), C→I (-2)…
- A connected undirected graph has the vertices F, G, O, P, R, U, Y and the edges P–O, P–G, P–F, O–U, O–R, R–G, R–Y, Y–F. How many bridges…
- 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…