Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2
Directed weighted graph on vertices A, B, C, D with edges D→C (-4); A→B (-1); D→B (6); C→B (2); A→D (-2); D→A (5); C→A (-2); B→A (6). Source A. Run Bellman–Ford for exactly n−1 = 3 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. 8
- B. 1
- C. 2
- D. 4
Correct answer: B. 1
After n−1 passes 1 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
- Undirected graph on vertices A, B, C, D, E, F, G with edges A–C (1), A–E (1), A–F (1), A–G (1), B–D (1), B–G (1), C–D (1), D–E (1), D–F…
- Directed acyclic graph on vertices A, B, C, D, E, F, G, H (edges go from earlier to later letters) with weights A→C (4), A→E (3), A→F (2)…
- Undirected simple graph on vertices A, B, C, D, E, F, G with edges A–B (1), A–G (1), B–F (1), B–G (1), C–E (1), C–F (1), C–G (1), D–E (1)…
- Directed weighted graph on vertices E, C, A, B, D (source E, target D) with edges: E→C (2); E→A (3); A→C (-3); C→D (3); A→D (3); E→D (18)…
- A trie keeps a counter at each node, and every word of the list W = ['acb', 'c', 'bcb', 'c', 'bc', 'a', 'aac'] is inserted once per…
- On an 6 × 6 chessboard with rows and columns numbered 0 to 5, a knight starts at (3, 5) and must reach (5, 0) (as (row, column)). A knight…