LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

In DFS, why is an edge's own label checked first, and not only the label of the vertex at its far end?

Every undirected edge sits in the incidence lists of both its ends, so DFS meets it twice. The second time, the edge leads to a vertex that is already visited, and a check on that vertex alone would relabel the edge as a back edge, turning every discovery edge into a false cycle. Checking that the edge itself is still UNEXPLORED makes DFS examine each edge exactly once.

Take the first step from A to B. DFS(A) labels A–B a discovery edge and calls DFS(B). Among B's incident edges is A–B again, and its far end A is VISITED. If only vertex labels counted, DFS(B) would now label A–B a back edge, as if A–B and B–A formed a cycle. Because A–B already carries the label DISCOVERY, DFS skips it.

The same check underlies the running time: each edge is examined once, from whichever end reaches it first, and only looked at from the other.

Implementations without edge labels need another way to recognise the edge back to the parent, usually by passing the parent vertex into the recursive call and skipping it. That shortcut misreads parallel edges: two different edges between A and B do form a cycle, and skipping the parent vertex hides it, while edge labels see the second edge as UNEXPLORED and report it as a back edge.

From Quiz: ADS / Graph Traversals: Depth-First and Breadth-First Search | Updated: Nov 27, 2026