Data Structures & AlgorithmsHardICPC regional · GATE CS top band
Directed weighted graph on vertices A, B, C, D, E with edges listed in this exact order: B→E (7); E→A (6); B→D (3); B→C (3); B→A (4); A→B (4); E→B (-3). Bellman–Ford from A makes passes over the edge list in that order, relaxing in place (each edge is tried once per pass). Counting the passes from the first, how many passes change at least one distance?
- A. 2
- B. 7
- C. 3
- D. 4
Correct answer: A. 2
The distances settle after 2 passes that change something; one more pass is needed only to confirm nothing changes, and that final pass is not counted.
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
- Array A = [5, 20, 23, 4, 3, 30, 2, 24]. How many index pairs i < j satisfy (A[i] XOR A[j]) < 19? (A trie with subtree counts answers this…
- Distinct words ['ccc', 'aaa', 'cb', 'bbbc', 'ba', 'ca', 'bb'] are inserted into an empty trie (one node per character, shared prefixes…
- Array [2, 14, 21, 11, 1, 17] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at…
- A 6 × 6 grid (rows top to bottom, '#' = land, '.' = water): .#.... / ..#.#. / ##...# / #...#. / ##...# / ..##.#. Land cells form an island…
- Undirected weighted graph on vertices A, B, C, D, E, F with edges A–D (3), A–E (8), B–C (6), B–D (6), B–E (8), B–F (7), C–F (3), D–F (7)…
- Undirected graph on vertices A, B, C, D, E, F with edges, in this exact order: A–B (1), A–D (1), A–F (1), B–E (1), A–E (1), A–C (1)…