Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
Directed edges: A→B (5), A→C (6), C→B (-3), B→D (3). 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 8; true distance 8
- B. Reports 6; true distance 8
- C. Reports 8; true distance 6
- D. Reports 6; true distance 6
Correct answer: C. Reports 8; true distance 6
Dijkstra finalises B before exploring the negative edge C→B, so it never lowers B's distance again even though C→B would give a shorter path; this gives a reported distance of 8 for D, while the true shortest distance (allowing the negative edge to be used) is 6. Dijkstra does not work correctly with negative edge weights.
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
- Keys 34, 69, 39, 53, 61, 76, 35 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges on…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [C, E, F]; B: [C, E]; C: [A, B, F]; D: [F]; E: [A…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B, C, F]; B: [A, C, D]; C: [A, B]; D: [B, E, F]; E…
- What are the time complexities of P and Q? c int P(int n) { if (n <= 1) return 1; return P(n/2) + P(n/2) + n; } int Q(int n) { if (n <= 1)…
- Undirected graph edges: E–D, E–C, A–C, D–C, B–A, B–E, B–D. Using a BFS 2-colouring, is this graph bipartite?
- Keys 69, 79, 89 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…