Sub topic 4
Tree, Back, Forward, and Cross Edges
This chapter assumes you can read recursive depth-first search (DFS) in Python and that you know discovery and finish times from the previous chapter. When DFS explores a directed graph, it puts every edge into one of four classes. The classes tell you where cycles are. Tarjan's algorithm depends on that information later.
The four edge types
Suppose DFS is at vertex u and examines the edge u → v. The edge's class depends on the state of v at that moment:
| State of `v` | Extra test | Edge type | Meaning |
|---|---|---|---|
| Undiscovered (white) | none | Tree | DFS moves to `v`. `v` becomes a child of `u`. |
| Discovered, not finished (gray) | none | Back | `v` is an ancestor of `u` that is still on the call stack. |
| Finished (black) | `disc[u] < disc[v]` | Forward | `v` is a descendant of `u` that DFS already reached by another path. |
| Finished (black) | `disc[u] > disc[v]` | Cross | `v` is in a subtree or DFS tree that is already done. |
A self-loop u → u counts as a back edge, because u is gray when DFS examines it.
Classifying edges in code
def classify_edges(n, adj):
WHITE, GRAY, BLACK = 0, 1, 2
color = [WHITE] * n
disc = [0] * n
fin = [0] * n
edges = []
time = 0
def dfs(u):
nonlocal time
color[u] = GRAY
disc[u] = time
time += 1
for v in adj[u]:
if color[v] == WHITE:
edges.append((u, v, "tree"))
dfs(v)
elif color[v] == GRAY:
edges.append((u, v, "back"))
elif disc[u] < disc[v]:
edges.append((u, v, "forward"))
else:
edges.append((u, v, "cross"))
color[u] = BLACK
fin[u] = time
time += 1
for u in range(n):
if color[u] == WHITE:
dfs(u)
return edges
adj = [[1, 2], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
for e in classify_edges(8, adj):
print(e)Output:
(0, 1, 'tree')
(1, 2, 'tree')
(2, 0, 'back')
(2, 3, 'tree')
(3, 4, 'tree')
(4, 5, 'tree')
(5, 3, 'back')
(0, 2, 'forward')
(6, 5, 'cross')
(6, 7, 'tree')
(7, 6, 'back')The edge 0 → 2 is a forward edge. DFS had already reached vertex 2 through vertex 1 by the time it examined this edge. The edge 6 → 5 is a cross edge because the first DFS tree finished before DFS started at vertex 6.
Why the classes matter for SCCs
- Back edges mean cycles. A directed graph has a cycle if and only if some DFS finds a back edge. Each back edge closes a cycle, and every vertex on a cycle belongs to the same strongly connected component (SCC).
- Forward edges add no new reachability. The tree path already connects
utov, so forward edges never change the components. - Cross edges need care. In a directed graph, a cross edge always points to a vertex with a smaller discovery time. Its target may be in a different SCC, as
6 → 5is. It may also be in the same SCC if a path leads back. Tarjan's algorithm separates the two cases by checking whether the target is still on its stack.
Common errors
- Testing
disc[v] != Nonealone. This merges back, forward, and cross edges into one case. - Deciding between forward and cross by finish times. At the moment DFS examines the edge,
uhas no finish time yet. Compare discovery times instead. - Treating every edge to a visited vertex as a cycle. Only gray targets mean a cycle.
Exercise
The graph has edges 0→1, 1→2, 0→2, 2→1, and 3→2. Adjacency lists follow that order, and DFS starts at 0 and then at 3. Classify every edge.
Show answer
0→1 is a tree edge, 1→2 is a tree edge, 2→1 is a back edge (1 is gray), 0→2 is a forward edge (2 is finished and disc[0] < disc[2]), and 3→2 is a cross edge (2 is finished and disc[3] > disc[2]).
Exercise
Why can't a cross edge u→v in a directed DFS have disc[u] < disc[v]?
Show answer
If v were white when DFS examined the edge, the edge would be a tree edge. So if disc[u] < disc[v] and v is already finished, DFS must have discovered v while u was still gray. That makes v a descendant of u, so the edge is a forward edge.