Graph Traversal (BFS and DFS) · Algorithms
16 questions · asked in 11 of 27 years (2000–2026)
MCQ 12 · MSQ 2 · NAT 21-mark 12 · 2-mark 4
2000Questions per year2026
Questions 1–5 of 16
Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph as input, where and are the discovery time and finishing time, respectively, of the vertex .
Suppose that the input directed graph is a directed acyclic graph (DAG). For an edge , which of the following options will NEVER be correct?
Suppose that the input directed graph is a directed acyclic graph (DAG). For an edge , which of the following options will NEVER be correct?
One or more options are correct.
Answer: (B), (D)
DFS on a DAG gives discovery and finish time intervals . These follow the standard parenthesis theorem: for any two vertices, the intervals are either fully nested (one inside the other) or fully disjoint. They can never partly overlap.
For an edge in a DAG, DFS treats it as one of two kinds. It is a tree/forward edge if is discovered while exploring from , so the interval sits inside and we get . It is a cross edge if was already fully explored before started, so the intervals are disjoint and do not overlap, giving . Since the input is a DAG, there are no back edges, so the intervals can never partly overlap (interleave).
- : this is valid nesting, so it can happen (tree/forward edge).
- : these are valid disjoint intervals, so it can happen (cross edge).
- : this would mean started before , but the whole subtree of nests inside . Then the edge would go from a later, nested vertex back to an earlier, ancestor vertex that is still open. This is exactly a back edge, so it is impossible in a DAG.
- : this is a partly overlapping (interleaved) interval. The parenthesis theorem says it can never happen in any DFS on any graph, whatever the edge type.
Answer: and can NEVER happen.
For an edge in a DAG, DFS treats it as one of two kinds. It is a tree/forward edge if is discovered while exploring from , so the interval sits inside and we get . It is a cross edge if was already fully explored before started, so the intervals are disjoint and do not overlap, giving . Since the input is a DAG, there are no back edges, so the intervals can never partly overlap (interleave).
- : this is valid nesting, so it can happen (tree/forward edge).
- : these are valid disjoint intervals, so it can happen (cross edge).
- : this would mean started before , but the whole subtree of nests inside . Then the edge would go from a later, nested vertex back to an earlier, ancestor vertex that is still open. This is exactly a back edge, so it is impossible in a DAG.
- : this is a partly overlapping (interleaved) interval. The parenthesis theorem says it can never happen in any DFS on any graph, whatever the edge type.
Answer: and can NEVER happen.
Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph is/are TRUE?
One or more options are correct.
Answer: (B), (C), (D)
Check each claim about BFS and DFS on an undirected simple graph:
- (A) "A DFS tree is a shortest path tree": false — DFS goes as deep as it can along one branch first, so it gives no shortest-path guarantee. For example, in a triangle , DFS from can go , so is at depth though its distance from is . Only BFS gives shortest paths here.
- (B) "Every non-tree edge w.r.t. a DFS tree is a forward/back edge": true — in an undirected graph, DFS can never produce a cross edge. Every non-tree edge joins an ancestor and a descendant. Seen from the descendant it is a back edge, and seen from the ancestor it is a forward edge.
- (C) "Non-tree BFS edge (u,v): distances differ by at most 1": true — BFS explores level by level. Any edge can only join vertices in the same level or in adjacent levels.
- (D) "Both BFS and DFS can find connected components": true — if you run either search again and again from each vertex not yet visited, the graph is correctly split into its connected components.
Answer: (B), (C), (D)
- (A) "A DFS tree is a shortest path tree": false — DFS goes as deep as it can along one branch first, so it gives no shortest-path guarantee. For example, in a triangle , DFS from can go , so is at depth though its distance from is . Only BFS gives shortest paths here.
- (B) "Every non-tree edge w.r.t. a DFS tree is a forward/back edge": true — in an undirected graph, DFS can never produce a cross edge. Every non-tree edge joins an ancestor and a descendant. Seen from the descendant it is a back edge, and seen from the ancestor it is a forward edge.
- (C) "Non-tree BFS edge (u,v): distances differ by at most 1": true — BFS explores level by level. Any edge can only join vertices in the same level or in adjacent levels.
- (D) "Both BFS and DFS can find connected components": true — if you run either search again and again from each vertex not yet visited, the graph is correctly split into its connected components.
Answer: (B), (C), (D)
Consider the following algorithm someAlgo that takes an undirected graph as input.
1. Let be any vertex in . Run BFS on starting at . Let be a vertex in at maximum distance from as given by the BFS.
2. Run BFS on again with as the starting vertex. Let be the vertex at maximum distance from as given by the BFS.
3. Output the distance between and in .
The output of for the tree shown in the given figure is __________. (Answer in integer)
1. Let be any vertex in . Run BFS on starting at . Let be a vertex in at maximum distance from as given by the BFS.
2. Run BFS on again with as the starting vertex. Let be the vertex at maximum distance from as given by the BFS.
3. Output the distance between and in .
The output of for the tree shown in the given figure is __________. (Answer in integer)
Answer: 6
This is the double-BFS method for the diameter of a tree (the longest path between two vertices). For a tree, the vertex farthest from any start vertex is always one end of a longest path, so the second BFS gives the diameter.
Name the vertices in the figure. Let be the upper-left vertex of degree 4 and the lower-centre vertex of degree 4, joined by an edge. From : one branch goes down-left through a vertex to a leaf ; another goes up-right to a vertex with two leaves; the rest are single leaves. From : two single leaves, and a branch to a fork vertex . From : one leaf, and a path to the far-right leaf .
Longest path: . This has 7 vertices, so 6 edges. The path from either leaf of , , also has 6 edges, and no path is longer.
Step 1: BFS from any ends at a farthest vertex . This is always a tip of a longest branch, for example (or or a leaf of ).
Step 2: BFS from reaches and the leaves of at distance 6, and nothing is farther. So the output is 6.
Answer: 6
Name the vertices in the figure. Let be the upper-left vertex of degree 4 and the lower-centre vertex of degree 4, joined by an edge. From : one branch goes down-left through a vertex to a leaf ; another goes up-right to a vertex with two leaves; the rest are single leaves. From : two single leaves, and a branch to a fork vertex . From : one leaf, and a path to the far-right leaf .
Longest path: . This has 7 vertices, so 6 edges. The path from either leaf of , , also has 6 edges, and no path is longer.
Step 1: BFS from any ends at a farthest vertex . This is always a tip of a longest branch, for example (or or a leaf of ).
Step 2: BFS from reaches and the leaves of at distance 6, and nothing is farther. So the output is 6.
Answer: 6
Let . Let denote the powerset of . Consider an undirected graph whose vertex set is . For any is an edge in if and only if (i) , and (ii) either or . For any vertex in , the set of all possible orderings in which the vertices of can be visited in a Breadth First Search (BFS) starting from is denoted by .
If denotes the empty set, then the cardinality of is ____.
If denotes the empty set, then the cardinality of is ____.
Answer: 5040
is a subset of every set, so is adjacent to all the other vertices.
BFS starting from visits all 7 neighbours at level 1. These 7 can come in any order, so the count is .
Answer: 5040
BFS starting from visits all 7 neighbours at level 1. These 7 can come in any order, so the count is .
Answer: 5040
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time of Depth First Search on G, when G is represented as an adjacency matrix?
Answer: (C)
When the graph is stored as an adjacency matrix, the neighbours of a vertex are found by reading its full row of n entries. DFS repeats this scan for each vertex, so the total work is .
Answer:
Answer:
Keep practising
Topics in Algorithms
SortingAsymptotic NotationRecurrence RelationGreedy TechniqueDynamic ProgrammingDivide and Conquer
Graph Traversal (BFS and DFS)You are here
Minimum Spanning TreeShortest Path