Sub topic 8
Discovery and Finish Times
This chapter assumes you can write recursive DFS and that you know how the outer loop over all vertices builds a DFS forest. Here we add two numbers to every vertex. They record when DFS first reaches the vertex and when it has finished with it.
Two Timestamps per Vertex
Keep one global counter, time, and add 1 to it at two moments for each vertex:
- Discovery time
d[v]: the moment DFS first entersv. This is the pre-order position, since you would record it before exploring any neighbors. - Finish time
f[v]: the moment DFS returns fromv, after every vertex reachable fromvthrough unvisited paths has been fully explored. This is the post-order position.
Both events share the same counter, so for a graph with V vertices the timestamps run from 1 to 2V, and d[v] < f[v] always holds.
def dfs_times(graph):
discovery = {}
finish = {}
time = 0
def visit(node):
nonlocal time
time += 1
discovery[node] = time
for neighbor in graph[node]:
if neighbor not in discovery:
visit(neighbor)
time += 1
finish[node] = time
for node in graph:
if node not in discovery:
visit(node)
return discovery, finish
graph = {"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": [], "E": ["D"]}
d, f = dfs_times(graph)
for node in graph:
print(node, d[node], f[node])
# A 1 8
# B 2 5
# C 6 7
# D 3 4
# E 9 10The discovery dictionary also serves as the visited set. A vertex counts as visited once it has a discovery time.
Reading the Intervals
Treat each vertex as the interval [d[v], f[v]]:
| Vertex | Discovery | Finish | Interval |
|---|---|---|---|
| A | 1 | 8 | [1, 8] |
| B | 2 | 5 | [2, 5] |
| D | 3 | 4 | [3, 4] |
| C | 6 | 7 | [6, 7] |
| E | 9 | 10 | [9, 10] |
The Parenthesis Property
For any two vertices u and v, their intervals are either nested or disjoint. They never partially overlap. Write each discovery as "(" and each finish as ")" and the result is a balanced string of parentheses: (A (B (D D) B) (C C) A) (E E).
The reason is the call stack. If v is discovered while u is still active, then v's call sits above u's call on the stack and must return first. That gives d[u] < d[v] < f[v] < f[u], and it means v is a descendant of u in the DFS forest. If neither is discovered while the other is active, their intervals do not overlap at all.
This gives an O(1) ancestor test: u is an ancestor of v exactly when u's interval contains v's interval.
Common Errors
- Two separate counters for discovery and finish. The parenthesis property then breaks, because the two kinds of numbers can no longer be compared.
- Forgetting
nonlocal time. Python raisesUnboundLocalErrorontime += 1insidevisit. - Recording the finish time before the neighbor loop. That records discovery twice. The finish time must come after every recursive call has returned.
- Iterative DFS that marks a vertex finished when it is popped. In the stack version from the earlier chapter, popping a vertex is its discovery. To get a correct finish time you need an explicit "exit" marker on the stack.
Exercise
In some DFS run, d[u] = 4, f[u] = 11, d[v] = 6, and f[v] = 13. Is this possible?
Show answer
No. The intervals [4, 11] and [6, 13] partially overlap. v was discovered while u was active, so v's call must return before u's call does, which would force f[v] < f[u]. Two DFS intervals must be either nested or disjoint.
Exercise
Run dfs_times on {"X": ["Y"], "Y": ["Z"], "Z": [], "W": ["X"]}. What are the discovery and finish times for each vertex?
Show answer
X 1/6, Y 2/5, Z 3/4, W 7/8. DFS starts at X because it comes first in the dictionary. When W is visited, X has already been discovered, so W finishes immediately.