GATE / CS ARCHIVE

GATE CSE 2018 / QUESTION 30

DFS and BFS edge restrictions

Algorithms · Graph Algorithms · Full GATE CSE 2018 paper

GATE 2018, Q30 2 marks · MCQ
Report issue

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