Sub topic 9
Classifying Edges During DFS
This chapter assumes you know how recursive DFS works, how the DFS forest is built when you launch a search from every unvisited vertex, and how discovery and finish times are recorded. All examples use directed graphs stored as adjacency-list dictionaries.
The Four Edge Types
When DFS examines an edge (u, v), the state of v at that moment tells you how the edge relates to the DFS forest. There are four possibilities.
| Edge type | State of `v` when `(u, v)` is examined | Meaning |
|---|---|---|
| Tree | Not yet discovered | DFS reaches `v` through this edge, so it becomes part of the forest |
| Back | Discovered but not finished | `v` is an ancestor of `u` and still on the recursion path |
| Forward | Finished, and `disc[u] < disc[v]` | `v` is a descendant of `u` that was already reached by another route |
| Cross | Finished, and `disc[u] > disc[v]` | `v` is in a branch or tree that DFS already completed |
Back edges carry the most information. A back edge closes a loop from a vertex back up to one of its ancestors. The next chapter builds on that fact.
The Decision Procedure
Classifying Edges in Code
The implementation adds one branch per edge type to the timestamped DFS from the previous chapter.
def classify_edges(graph):
disc, fin = {}, {}
kinds = {}
time = 0
def visit(u):
nonlocal time
time += 1
disc[u] = time
for v in graph[u]:
if v not in disc:
kinds[(u, v)] = "tree"
visit(v)
elif v not in fin:
kinds[(u, v)] = "back"
elif disc[u] < disc[v]:
kinds[(u, v)] = "forward"
else:
kinds[(u, v)] = "cross"
time += 1
fin[u] = time
for u in graph:
if u not in disc:
visit(u)
return kinds
graph = {"A": ["B", "C", "D"], "B": ["D"], "C": ["A"], "D": [], "E": ["D"]}
for edge, kind in classify_edges(graph).items():
print(edge, kind)
# ('A', 'B') tree
# ('B', 'D') tree
# ('A', 'C') tree
# ('C', 'A') back
# ('A', 'D') forward
# ('E', 'D') crossHere is how the trace produces each label. DFS goes A → B → D, and D finishes. After returning to A, DFS takes the tree edge to C. From C, the edge back to A finds A discovered but unfinished, so it is a back edge. A then examines (A, D). D is already finished and was discovered after A, which makes this a forward edge. Finally a new tree starts at E. D is finished and was discovered before E, so (E, D) is a cross edge.
Common Errors
- Using only a visited set. A single set cannot tell "on the current path" apart from "fully finished," so back edges look the same as forward and cross edges. You need either the finish set or the three-color scheme.
- Expecting the labels to be fixed. The labels depend on the order you visit vertices and neighbors. Reordering adjacency lists can change a forward edge into a tree edge. Only the presence or absence of back edges stays the same.
- Applying all four types to undirected graphs. In an undirected DFS, every edge is either a tree edge or a back edge. The edge back to the immediate parent is the same edge you just used, so skip it, or every edge will look like a back edge.
Exercise
In the graph {"P": ["Q", "R"], "Q": ["R"], "R": ["Q"]}, DFS starts at P and visits neighbors in listed order. Classify every edge.
Show answer
(P, Q) is a tree edge. (Q, R) is a tree edge. (R, Q) is a back edge, because Q is still unfinished when R examines it. (P, R) is a forward edge, because R is finished and was discovered after P.
Exercise
Why can a cross edge never appear in a DFS of an undirected graph?
Show answer
If (u, v) is an undirected edge and v finished before u was discovered, then v would have explored the edge to u and discovered u as its descendant first. That contradicts v finishing before u was discovered, so every non-tree edge connects a vertex to one of its ancestors.