Data Structures & AlgorithmsHardHCLTech · Wipro pattern
Directed edges: A→B (4), 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 5; true distance 5
- B. Reports 2; true distance 2
- C. Reports 5; true distance 2
- D. Reports 2; true distance 5
Correct answer: C. Reports 5; true distance 2
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 5 for D, while the true shortest distance (allowing the negative edge to be used) is 2. 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 43, 46, 99, 82, 78, 23, 40, 44, 60, 34 are inserted via separate chaining (h(k) = k mod 5, new keys appended to the end of the chain)…
- A binary tree has preorder traversal [21, 13, 54, 28, 40, 34] and inorder traversal [13, 21, 28, 34, 40, 54]. Reconstructing the tree…
- 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); } int Q(int n) { if (n <= 1)…
- Keys 30, 11, 59, 70, 43, 35 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain) into a table of…
- Undirected graph edges: A–C, A–D, D–C, C–B. Using a BFS 2-colouring, is this graph bipartite?
- 0/1 knapsack, capacity 11. Items (weight, value): (4, 6), (1, 7), (6, 3), (2, 2). What is the maximum total value?