GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Apply undirected traversal edge and layer restrictions

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

How these are solved

For undirected BFS, queue depth is nondecreasing and endpoints of any edge differ in depth by at most1. For undirected DFS, non-tree edges join ancestor and descendant rather than unrelated branches.

1 mark · MSQ
Report issue

Graph Algorithms

Question 29, GATE 2025: For BFS and DFS on an undirected simple graph G, choose all true statements.
A. A DFS tree is always a shortest-path tree.
B. Every DFS non-tree edge connects a vertex and an ancestor/descendant, described as a forward/back edge.
C. Endpoints of a BFS non-tree edge have source distances differing by at most one.
D. Either repeated BFS or repeated DFS can find all connected components.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C, D

A is false. In a triangle with edges s–u,u–v,s–v, DFS may choose tree edges s–u and u–v. Its tree distance from s to v is then 2 even though the graph contains a length-1 path s–v. DFS prioritizes depth, not distance.

B is true for undirected DFS. If an edge from a currently explored vertex reaches an unvisited vertex, DFS follows it before finishing that subtree. Consequently an undirected non-tree edge cannot join two completely separate DFS subtrees; its endpoints have an ancestor-descendant relationship. It is usually called a back edge when oriented toward the ancestor. The question's “forward/back” wording refers to that relationship, not to a separate undirected cross-edge type.

C follows from shortest-path distances. For any edge u–v, a shortest route to u followed by that edge shows d(s,v)≤d(s,u)+1. Reversing the roles gives d(s,u)≤d(s,v)+1. BFS tree levels equal these distances, so their difference is at most one.

D is true: traverse from an unvisited vertex, mark its entire component, and repeat while any vertex remains unvisited. This works with either BFS or DFS, including isolated vertices. Select B,C,D.

Original paper, page 23 · Answer key

2 marks · MCQ
Report issue

DFS and BFS edge restrictions

Graph Algorithms

Question 30, GATE 2018: For a simple undirected graph, I says DFS has no cross edge between unrelated vertices; II says every graph edge joins BFS depths differing by exactly 1. Which must hold? A I only; B II only; C both; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Method

If an undirected DFS edge led to a still-unvisited vertex, DFS would explore it before returning, making that vertex a descendant. If the vertex is already on the recursion stack it is an ancestor; an edge to a completed unrelated subtree would have discovered the current vertex earlier. For BFS, shortest-path distances differ by at most 1 across an edge. A triangle rooted at one vertex places the other two at depth 1 and still has an edge between them, disproving “exactly”.

Solution

In undirected DFS, an edge to an undiscovered vertex is explored before the current DFS call completes, so every non-tree edge joins an ancestor and descendant; no cross edges, making I true. BFS edges can connect equal-depth vertices, for example the edge joining the two non-root vertices of a triangle. The necessary inequality is |i−j|≤1, not equality 1. Thus only I, A.

Original paper, page 12 · Answer key

2 marks · MCQ
Report issue

Graph Algorithms

Question 51, GATE 2015: G is a simple undirected graph, s a source, d(x) shortest distance from s, and T a BFS tree. For an edge (u,v) not in T, which cannot equal d(u)−d(v)? A −1; B 0; C 1; D 2.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

An edge gives a one-step extension of a shortest path: d(u)≤d(v)+1 and d(v)≤d(u)+1. Combining gives −1≤d(u)−d(v)≤1. BFS non-tree edges can connect two vertices on the same level or consecutive levels; being non-tree does not force equal levels. Distances must be finite, so interpret u,v as vertices in the explored source component.

Solution: For any edge joining reachable vertices u, v, a shortest path to v followed by that edge proves d(u)≤d(v)+1, and symmetrically d(v)≤d(u)+1. Hence their distances differ by at most 1, so 2 is impossible, D. The restriction to a non-tree edge does not change the bound; same-level and adjacent-level non-tree edges can occur. Distances are understood within the source component, since subtraction of two infinite unreachable distances is not defined.

Original paper, page 19 · Answer key

2 marks · MCQ
Report issue

BFS order and shortest-path distances

Graph Algorithms

Question 2.14, GATE 2001: In BFS from r in an undirected unweighted graph, vertex u is visited before v. Must d(r,u)<d(r,v), >, ≤, or none of these?
Question 2.14, GATE 2001: In BFS from r in an undirected unweighted graph, vertex u is visited before v. Must d(r,u)<d(r,v), >, ≤, or none of these?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Breadth-first search’s FIFO queue processes all vertices at distance k before any vertex at distance k+1. Thus dequeue/discovery order is nondecreasing in distance from r.

Step 2: Two distinct vertices in the same layer can occur in either order, so visiting u first does not imply a strict inequality. A star centered at r supplies such an equal-distance example.

Therefore d(r,u)≤d(r,v), C, for vertices reached by this BFS. Unreachable vertices are not visited by the traversal from r.

Original paper, page 5