Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band
Directed weighted graph on vertices A, B, C, D, E with edges A→C (4); C→E (-2); D→C (-4); D→B (2); D→E (-1); D→A (1); E→D (6); E→C (-2); A→D (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. 4
- C. 9
- D. 3
Correct answer: D. 3
After n−1 passes 3 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 simple graph on vertices A, B, C, D, E, F, G with edges A–B (1), A–E (1), A–G (1), B–C (1), B–F (1), B–G (1), C–F (1), C–G (1)…
- A chain of 6 matrices M1 to M6 has dimensions given by the sequence p = [20, 15, 18, 8, 2, 3, 20], where Mi has size p[i−1] × p[i]…
- Strings a = "bccbc" and b = "bbacaa". The edit distance allows insert, delete and substitute, each costing 1 (the standard Levenshtein…
- 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→C (9)…
- Undirected weighted graph on vertices A, B, C, D with edges A–B (3), A–C (2), A–D (1), B–C (2), B–D (2), C–D (1). How many distinct…
- Keys [2, 42, 28, 49, 8, 35] are inserted, in this order, into an empty AVL tree using the standard rebalancing (left, right, left–right…