DFS Edge Classification
Four Edge Types
During DFS on a directed graph, every edge is classified based on the colour of the target vertex at the moment the edge is first explored from . There are four classifications:
- Tree edge: is WHITE. The edge is added to the DFS forest; becomes a child of in the depth-first tree. Discovered during:
DFS-VISIT(u)→v.colour == WHITE→ callDFS-VISIT(v). - Back edge: is GREY. is an ancestor of in the current DFS tree. Indicates a directed cycle exists. Example: .
- Forward edge: is BLACK and is a descendant of in the DFS forest (but not a direct child; direct children are tree edges). Connects a vertex to a non-child descendant.
- Cross edge: is BLACK and is neither an ancestor nor a descendant of . Either connects different subtrees of the same DFS tree, or connects vertices in different DFS trees entirely.
The classification depends on DFS exploration order: the same edge might be classified differently if adjacency lists are processed in a different order.
Timestamp-Based Characterisation
The timestamps give precise, colour-independent criteria. For edge :
- Tree or forward edge iff . The target’s interval is nested inside the source’s. To distinguish tree from forward: for tree edges, was discovered via this specific edge; for forward edges, was discovered through a different path but is still a descendant.
- Back edge iff . The target was discovered before the source and finishes after the source. This is the hallmark of cycles.
- Cross edge iff . The entire interval of lies before the entire interval of .
Properties Through an Example Walk
Run DFS on this directed graph: , , , , , , , , , , .
Discovery and finish times (assuming adjacency lists in numerical order, starting from 1):
| Vertex | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | |
| 16 | 15 | 10 | 7 | 12 | 11 | 14 | 13 |
Classify each edge:
- : tree (2 white). : forward (3 black, descendant: , but 3 discovered via 2, not 1).
- : tree. : tree.
- : tree.
- : forward (5 discovered later via 3, but is descendant of 4).
- : tree. : tree.
- : tree.
- : back (6 is grey when exploring ; also , is not assigned yet but — back edge).
- : back (4 is grey when exploring ; or by timestamps: and is not yet assigned before finishes, but — confirms ancestor relationship).
The presence of back edges and confirms cycles in the graph.
Undirected Graphs
In an undirected graph, DFS processes each edge in both directions (once from each endpoint). This restricts the possible classifications:
- Tree edges occur when the neighbour is white.
- Back edges occur when the neighbour is already discovered but is not the immediate parent (the vertex from which we just arrived). The edge explored in reverse is trivially a back edge under the directed definition; in the undirected context, we treat this as the reverse of the tree edge and not a cycle-indicating back edge.
Forward and cross edges cannot occur in undirected graphs. Proof: if were a forward edge (descendant), then exploring from would encounter as a back edge; but the symmetric relationship means the edge would already be part of the DFS tree. Cross edges would imply exploring from first then from later, but if , the second exploration would encounter the other vertex immediately, making it a back edge.
Conclusion for undirected graphs: Every edge is either a tree edge or a back edge. A back edge (to a non-parent ancestor) indicates an undirected cycle.
Cycle Detection
- Directed graph is a DAG iff DFS produces no back edges. Algorithm: run DFS; if any edge is explored with coloured GREY, report “contains cycle”. Time: .
- Undirected graph has a cycle iff any non-tree back edge exists.
This is a fundamental result: DFS provides linear-time cycle detection for both directed and undirected graphs.
Summary
| Edge Type | Target Colour | Timestamp Condition | Significance |
|---|---|---|---|
| Tree | WHITE | Part of DFS forest | |
| Back | GREY | Indicates cycle | |
| Forward | BLACK, descendant | Non-tree shortcut to descendant | |
| Cross | BLACK, unrelated | Between subtrees or trees |
| Graph type | Allowed edge types |
|---|---|
| Directed | All four types |
| Undirected | Tree + Back only |
| Application | Detail |
|---|---|
| Cycle detection in directed graphs | Back edge cycle, |
| Acyclicity test | No back edges in DFS |
Past paper questions: y2026p1q10 (DAG and topological sort via DFS).