Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Directed weighted graph on vertices A, B, C, D, E with edges A→C (6); A→E (2); C→A (-2); C→B (1); D→A (1); E→C (5); C→E (3); E→D (2); D→E (-4). Source A. Run Bellman–Ford for exactly n−1 = 4 passes (each pass tries every edge once). Then, using the distances after those passes and no further updates, how many edges (u→v, w) still satisfy dist[u] + w < dist[v] (with dist[u] finite)?
- A. 2
- B. 9
- C. 3
- D. 5
Correct answer: D. 5
After n−1 passes 5 edges still improve, which means a negative cycle is reachable from the source. Checking one pass early, or counting only negative edges, gives different numbers.
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
- Rooted tree with root 0 and vertices 0 to 8; the edges are 0–1, 1–2, 2–3, 1–4, 1–5, 4–6, 4–7, 6–8 (parent–child). Every edge has length 1…
- Undirected weighted graph on vertices A, B, C, D, E, F; its edges, in this listed order, are: B–E (3), C–E (4), B–C (8), E–F (2), D–F (7)…
- Array [28, 17, 3, 22, 8, 23, 10] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element…
- Bipartite graph with left side {A, B, C}, right side {D, E, F, G} and edges A–D, A–E, B–F, B–G, C–D. What is the size of a maximum…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→B (4)…
- Points A(8,5), B(3,9), C(3,5), D(2,7), E(3,2) in the plane. The cost of joining two points is the Manhattan distance |x1−x2| + |y1−y2|…