Sub topic 4
Tree, Back, Forward, and Cross Edges
This chapter assumes you already know recursive depth-first search (DFS) on an adjacency list. It also assumes you know the discovery time d[u] and finish time f[u] that DFS gives each vertex, which the previous chapter covered. Here we use those timestamps to sort every edge of a directed graph into one of four classes. Both SCC algorithms later in the tutorial rely on these classes. They are especially important for Tarjan's algorithm, whose correctness depends on which kind of edge it is looking at.
The Four Edge Classes
When DFS at vertex u examines an edge u → v, the state of v at that moment decides the class. DFS keeps each vertex in one of three states. White means not yet discovered. Gray means discovered but not finished, so the vertex is still on the recursion stack. Black means finished.
| Class | State of `v` when edge is examined | Meaning in the DFS forest |
|---|---|---|
| Tree edge | White | DFS descends from `u` to `v`; `v` becomes `u`'s child |
| Back edge | Gray | `v` is an ancestor of `u` (or `u` itself, for a self-loop) |
| Forward edge | Black, and `d[u] < d[v]` | `v` is a non-child descendant of `u`, already finished |
| Cross edge | Black, and `d[v] < d[u]` | `v` is in another subtree or an earlier tree, already finished |
Gray vertices always form one path: the current recursion stack, from the root of the current DFS tree down to u. An edge to a gray vertex therefore points back up that path, and that closes a cycle.
A Worked Example
Take this graph on six vertices, with neighbors examined in the listed order:
adj = [[1, 2], [2], [0, 3], [1], [3, 5], [4]]DFS from vertex 0, then from the first remaining white vertex (4), produces the trace below. Each node label shows d/f.
Step through the interesting edges:
2 → 0is examined while 0, 1, and 2 are all gray. Vertex 0 is gray, so this is a back edge.3 → 1is a back edge too, because 1 is still gray when 3 looks at it.0 → 2is examined only after 2 has finished. Vertex 2 is black andd[0] = 1 < d[2] = 3, so 2 is a descendant reached by another route. That makes it a forward edge.4 → 3is examined in the second DFS tree. Vertex 3 is black andd[3] = 4 < d[4] = 9, so this is a cross edge.
The SCCs here are {0, 1, 2, 3} and {4, 5}. The back edges hold each component together. The single cross edge 4 → 3 runs between the two components.
Classifying Edges in Code
The function below runs DFS with explicit colors and records every edge's class. It returns a list rather than a dictionary, so parallel edges each get their own entry.
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
time += 1
disc[u] = time
color[u] = GRAY
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
time += 1
fin[u] = time
for u in range(n):
if color[u] == WHITE:
dfs(u)
return edges, disc, fin
adj = [[1, 2], [2], [0, 3], [1], [3, 5], [4]]
edges, disc, fin = classify_edges(6, adj)
for u, v, kind in edges:
print(f"{u} -> {v}: {kind}")
print("disc:", disc) # disc: [1, 2, 3, 4, 9, 10]
print("fin: ", fin) # fin: [8, 7, 6, 5, 12, 11]Output:
0 -> 1: tree
1 -> 2: tree
2 -> 0: back
2 -> 3: tree
3 -> 1: back
0 -> 2: forward
4 -> 3: cross
4 -> 5: tree
5 -> 4: back
disc: [1, 2, 3, 4, 9, 10]
fin: [8, 7, 6, 5, 12, 11]Classifying After the Fact with Intervals
You don't need colors during the search if you keep the timestamps. By the parenthesis theorem, the intervals [d, f] of any two vertices are either nested or disjoint. Each class matches one interval pattern:
| Class | Interval relationship for edge `u → v` |
|---|---|
| Tree or forward | `d[u] < d[v] < f[v] < f[u]` (v nested inside u) |
| Back | `d[v] ≤ d[u] < f[u] ≤ f[v]` (u nested inside v) |
| Cross | `d[v] < f[v] < d[u] < f[u]` (v entirely before u) |
Timestamps alone cannot separate tree edges from forward edges, because both have nested intervals. To tell them apart you need the parent array, or the classification recorded during the search.
The cross-edge row has a consequence worth remembering. In a directed graph, a cross edge always points from a later-discovered vertex to one that has already finished. An edge never points "forward in time" to a vertex in a disjoint interval. If DFS reached u while v was still white and u → v existed, DFS would have taken that edge as a tree edge.
Facts That Matter for SCCs
A directed graph has a cycle if and only if DFS finds a back edge. A back edge u → v plus the tree path from v down to u forms a cycle. In the other direction, take any cycle and let v be its first discovered vertex. By the white-path theorem, every other vertex on the cycle becomes a descendant of v. The cycle edge entering v then comes from a descendant while v is gray, so it is a back edge.
Classes depend on the traversal order, but cycle detection does not. If you start the example at vertex 4, the edge 4 → 3 becomes a tree edge and no cross edge appears. Change the neighbor order of 0 to [2, 1] and 0 → 2 becomes a tree edge, while 0 → 1 is never examined as anything but a tree edge. Whether any back edge exists stays the same, because that depends only on whether the graph has a cycle.
Undirected graphs have only tree and back edges. Forward and cross edges are directed phenomena. Don't carry intuitions from undirected DFS over to this setting.
Cross edges can stay inside an SCC. In the graph 0 → 1, 0 → 2, 1 → 0, 2 → 1, DFS visits 1 first, returns, then visits 2. When it sees 2 → 1, vertex 1 is black and was discovered earlier, so the edge is a cross edge. Yet 2 → 1 → 0 → 2 is a cycle, so 1 and 2 share a component. Other cross edges, like 4 → 3 in the main example, point into a component that is already complete. Tarjan's algorithm needs to tell these two cases apart. Its on-stack marker does exactly that, as a later chapter shows.
Forward edges carry no new reachability information. A forward edge u → v duplicates a tree path that already exists from u to v, so it can never be the edge that merges two components.
Common Errors
- Mixing up the order test. The forward edge test is
disc[u] < disc[v]. Comparing finish times doesn't work, becausefin[u]is not yet assigned when the edge is examined. - Checking
color[v] == BLACKfor back edges. Back edges go to gray vertices. Black vertices give forward or cross edges. - Expecting a fixed classification. Tests that compare edge classes must also fix the start vertex and the neighbor order.
- Recursing on large graphs. Python's default recursion limit of about 1000 breaks
classify_edgeson long paths. The chapter on iterative DFS covers how to fix this.
Exercise
Run DFS on adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]], starting at vertex 0 and taking neighbors in list order. Classify every edge.
Show answer
Discovery/finish times: 0 is 1/12, 1 is 2/11, 2 is 3/10, 3 is 4/9, 4 is 5/8, 5 is 6/7, 6 is 13/16, 7 is 14/15. Tree edges: 0→1, 1→2, 2→3, 3→4, 4→5, 6→7. Back edges: 2→0, 5→3, 7→6. Cross edge: 6→5 (5 is black and d[5]=6 < d[6]=13). There are no forward edges.
Exercise
In the graph 0→1, 0→2, 1→0, 2→1 (neighbors in that order, DFS from 0), what class is 2→1, and are 1 and 2 in the same SCC? What does this imply about using "cross edge" as a signal that an edge leaves the current component?
Show answer
2→1 is a cross edge, because 1 is black and d[1] < d[2]. Yet 1 and 2 are in the same SCC via the cycle 2→1→0→2. So a cross edge does not by itself mean the edge leaves the component. You also need to know whether the target's component is still open, which is what Tarjan's on-stack check tracks.
Exercise
Explain why a directed graph with no back edges in some DFS has every SCC of size one.
Show answer
No back edge means the graph is acyclic, because any cycle forces a back edge in every DFS. In an acyclic graph, two distinct vertices cannot reach each other, since mutual reachability would create a cycle. So every vertex is its own SCC.