GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Recognize BFS as queue-based unweighted shortest paths

3 questions solved the same way, newest first. Back to Algorithms

How these are solved

Unit edge costs permit FIFO discovery in nondecreasing distance, giving BFS source-to-all shortest paths. 2006 asks the queue data structure for the Dijkstra specialization,2007 selects the algorithm,2014 asks what its tree computes.

1 mark · MCQ
Report issue

Graph Algorithms

Question 14, GATE 2014: Run BFS from W in a connected, undirected, unweighted graph and retain its tree edges in T. This tree supports which task? A shortest paths between every vertex pair; B shortest paths from W to every vertex; C shortest paths from W only to leaves of T; D a longest path in the graph.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Set up the reasoning. BFS assigns a vertex to the earliest reachable distance layer. When a vertex is first discovered, its parent is one layer closer to W, so following parents yields a path of exactly that shortest distance.

2. Derive the result. BFS explores vertices in increasing edge-distance from W. Parent pointers in the BFS tree therefore reconstruct a shortest path from W to every vertex. They need not preserve shortest paths between arbitrary other pairs. Thus B.

3. Check the interpretation. The guarantee is rooted at W and covers internal vertices as well as leaves. A non-tree edge may shorten the route between two other vertices, so one BFS tree is not an all-pairs shortest-path representation.

Original paper, page 32 · Answer key

2 marks · MCQ
Report issue

Single-source shortest paths in an unweighted graph

Graph Algorithms

Question 41, GATE 2007: In an unweighted undirected connected graph, which method most efficiently finds shortest paths from a source S to every other vertex?
Options:
(A) Dijkstra from S
(B) Warshall
(C) DFS from S
(D) BFS from S
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. All edges have equal cost, so distance is the number of edges. A FIFO queue processes every distance-d vertex before any distance-(d+1) vertex.

2. Derivation and interpretation. BFS starts at S and discovers vertices in increasing number of edges from S. When a vertex is first discovered, its level is its shortest-path distance; parent pointers recover paths. With adjacency lists, every vertex and edge is processed O(1) times, giving O(V+E). DFS does not generally find shortest paths; generic Dijkstra adds priority-queue overhead, and Warshall solves a more expensive all-pairs/reachability problem. Thus D is the appropriate algorithm.

Original paper, page 10

1 mark · MCQ
Report issue

Unweighted shortest paths

Graph Algorithms

Question 12, GATE 2006: Which data structure lets the shortest-path method on an unweighted graph run in linear time, as the unweighted specialization of Dijkstra’s algorithm?
Options:
(A) Queue
(B) Stack
(C) Heap
(D) B-tree
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. All edges have equal unit cost, so increasing distance order is breadth-first order. A FIFO queue processes each vertex and scans each adjacency list once, in O(V+E) time.

2. Thus A; a priority heap is unnecessary.

3. Teaching check: In an unweighted graph, every discovered neighbor is exactly one hop farther than its predecessor. A FIFO queue keeps frontier layers in nondecreasing distance order, which is the priority rule Dijkstra needs here. With adjacency lists the total scan cost is the sum of degrees, O(V+E).

Original paper, page 2