Sub topic 7
Classifying DFS Edges
This chapter assumes you can read Python with recursion and nested functions, and that you know how depth-first search (DFS) visits vertices. It also assumes the directed and undirected cycle definitions from earlier chapters. Here you will learn the vocabulary that the next chapter, three-color DFS, depends on.
Why Classify Edges
When DFS runs on a graph, each edge it examines connects the current vertex u to a neighbor v. The relationship between u and v at that moment tells you something about the graph's structure. One kind of edge, the back edge, means a cycle exists. The other kinds are harmless. A cycle detector is really an edge classifier that stops at the first back edge.
The Four Edge Types
DFS builds a forest. Each tree in the forest starts at a vertex the outer loop picked. Every edge u → v falls into one of four types based on where v sits in that forest when the edge is examined.
| Type | State of `v` when `u → v` is examined | Meaning |
|---|---|---|
| Tree | Not yet discovered | DFS moves to `v` through this edge |
| Back | Discovered, not finished (an ancestor of `u`, or `u` itself) | Points up the current path |
| Forward | Finished, and discovered after `u` | Shortcut to a descendant already explored |
| Cross | Finished, and discovered before `u` | Points to another branch or an earlier tree |
A vertex is "discovered, not finished" while it is still on the recursion stack. That is the only state in which an edge can close a loop back onto the current path. A self-loop u → u counts as a back edge, because u is on the stack when the edge is examined.
Classifying Edges in Code
Discovery and finish timestamps are enough to tell the four types apart.
def classify_edges(adj):
n = len(adj)
disc = [-1] * n
fin = [-1] * n
time = 0
result = []
def dfs(u):
nonlocal time
disc[u] = time
time += 1
for v in adj[u]:
if disc[v] == -1:
result.append((u, v, "tree"))
dfs(v)
elif fin[v] == -1:
result.append((u, v, "back"))
elif disc[u] < disc[v]:
result.append((u, v, "forward"))
else:
result.append((u, v, "cross"))
fin[u] = time
time += 1
for s in range(n):
if disc[s] == -1:
dfs(s)
return result
adj = [[1, 2], [2], [0], [2]]
for edge in classify_edges(adj):
print(edge)Output:
(0, 1, 'tree')
(1, 2, 'tree')
(2, 0, 'back')
(0, 2, 'forward')
(3, 2, 'cross')The Back-Edge Theorem
A directed graph has a cycle if and only if DFS finds at least one back edge. If a back edge u → v exists, then v is an ancestor of u, so the tree path from v to u plus the edge u → v forms a cycle. The proof in the other direction goes like this. In any cycle, let v be the first vertex DFS discovers. Every other vertex on the cycle becomes a descendant of v, so the cycle edge that enters v is a back edge.
Undirected Graphs Are Simpler
In an undirected DFS, forward and cross edges cannot occur. If v had already finished when u was explored, DFS would have reached u from v through their shared edge first. Every edge is therefore a tree edge or a back edge. The only subtlety is that the tree edge you arrived on looks like a back edge to your parent. Earlier chapters handled this by skipping the parent edge.
Common Errors
- Using one
visitedflag for directed graphs. This mixes up "on the stack" and "finished", so forward and cross edges get reported as cycles. - Starting DFS from a single vertex. Back edges in unreached components are never examined.
- Resetting state between outer-loop starts. Finished vertices must stay finished, or cross edges get misread.
Exercise
In the graph with edges 0 → 1, 1 → 2, and 0 → 2, DFS starts at 0 and visits neighbors in list order. Classify each edge and say whether the graph has a cycle.
Show answer
0 → 1 is a tree edge, 1 → 2 is a tree edge, and 0 → 2 is a forward edge because 2 has finished and was discovered after 0. There is no back edge, so the graph is acyclic.
Exercise
Why can an undirected DFS never produce a cross edge?
Show answer
Suppose v finished before u was discovered, and the edge {u, v} exists. While exploring v, DFS would have examined that edge and discovered u as a descendant of v. That contradicts the assumption, so the edge must be a tree edge or a back edge.