Data Structures & AlgorithmsHardGATE CS · ICPC regional
Bellman–Ford is run from source T on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: P→Y (9), Y→P (6), T→Y (5), H→Y (7), P→H (9). 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. 5
- B. 2
- 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
- A directed graph has the vertices A, G, N, S, T, Y, Z and the arcs N→A, N→S, T→N, T→Z, T→Y, T→S, A→N, A→Y, Z→N, Z→T, Z→Y, Z→S, G→N, G→A…
- A tree has the vertices C, I, N, P, T, U, X, Y, Z and the edges N–Z, N–Y, N–U, Y–I, I–X, N–C, Y–T, T–P. What is the minimum number of…
- A tree has the vertices A, B, C, H, L, V, W and the edges C–W, W–H, H–B, B–A, C–L, A–V. A centroid is a vertex whose removal leaves no…
- Cities M, R, T, V, W are joined by roads with these travel costs: W–M 7, W–R 14, W–V 12, W–T 8, M–R 19, M–V 20, M–T 5, R–V 15, R–T 13, V–T…
- A directed acyclic graph has the vertices J, K, L, Q, R, S, X, Y and the directed edges S→R, S→Y, K→Y, L→K, R→Q, Y→X, K→X, J→Q, Y→J, J→X…
- Consider this recursive function: python def f(n): if n <= 0: return f(n - 1) if n % 2 == 0: f(n - 2) How many times is f called in total…