Data Structures & AlgorithmsHardProduct-company placement
Directed edges: A→B (6), A→C (6), C→B (-5), 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 7; true distance 2
- B. Reports 7; true distance 7
- C. Reports 2; true distance 2
- D. Reports 2; true distance 7
Correct answer: A. Reports 7; 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 7 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 38, 13, 87, 73, 18, 97, 3, 68, 72, 98 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain)…
- A trie is built from the words {den, deed, do, cat, cap, dot}. Using the trie (walk the prefix's characters, then count all words in the…
- A binary tree has preorder traversal [11, 83, 45, 67, 61, 47, 95] and inorder traversal [11, 45, 47, 61, 67, 83, 95]. Reconstructing the…
- Keys 41, 31, 21 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 38, 48, 58 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Undirected graph edges: B–D, C–A, E–B, E–A, D–E, C–B, A–B. Using a BFS 2-colouring, is this graph bipartite?