Data Structures & AlgorithmsHardProduct-company MCQ pattern
Directed edges: A→B (2), A→C (5), C→B (−4), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the priority queue, is run from A. What distance does it report for D, and what is the true shortest distance?
- A. Reports 3; true distance 2
- B. Reports 2; true distance 1
- C. Reports 2; true distance 2
- D. Reports 3; true distance 3
Correct answer: A. Reports 3; true distance 2
Dijkstra pops A, then B (2), finalising it, then D = 3, then C (5); the edge C→B would give B = 1, but B is already final, so D stays 3. The real shortest path is A→C→B→D = 5 − 4 + 1 = 2.
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
- DFS is run on a directed graph with edges A→B, A→C, B→D, C→D, C→A, D→B, E→C, E→D, starting from A, taking vertices and neighbours in…
- Kahn's algorithm is run on the DAG with edges 5→2, 5→0, 4→0, 4→1, 2→3, 3→1, always removing the SMALLEST-numbered vertex of in-degree 0 (a…
- How many topological orderings does the DAG with edges A→B, A→C, B→D, C→D, D→E, B→F have?
- Prim's algorithm starts at A on the undirected graph with edges A–B (3), A–C (1), B–C (7), B–D (5), C–D (2), C–E (7), D–E (3), D–F (8)…
- An undirected graph on A, B, C, D has edges A–B, B–C, C–D, D–A of weight 1 and a diagonal A–C of weight 2. How many distinct minimum…
- Union-find on elements 0–7 uses union by size (the smaller tree's root points to the larger tree's root; on a tie the second root points…