Graph Theory37 sections · 1633 units
Open in Course

Basic Implementation

(Computing tin and tout)

Start with a global timer at 00. Run DFS from the root. When entering node vv, set tin[v] = timer and increment. Recurse on children. When exiting, set tout[v] = timer without incrementing. Pseudocode:

timer := 0

function dfs(v, parent):
    tin[v] := timer
    timer := timer + 1
    for each child u of v:
        if u != parent then
            dfs(u, v)
    tout[v] := timer

After this DFS, you have entry and exit times for every node. All tintin values are unique (00 to n1n-1), while touttout values may repeat. All values stay within nn. This is part of the Euler Tour framework for converting tree structures into arrays.

This runs in O(n)O(n) time and uses O(n)O(n) space.