Graph Theory37 sections · 1633 units
Open in Course

Euler Tour - Construction

(DFS with recording)

To build an Euler tour, run DFS from the root. Maintain an array tour and a counter timer = 0. When you enter node v, set tour[timer] = v, record first[v] = timer, and increment timer. After visiting each child, record v again with timer++.

function eulerTour(v, d):
    first[v] = timer
    tour[timer] = v
    tourDepth[timer] = d
    timer += 1
    for each child c of v:
        eulerTour(c, d + 1)
        tour[timer] = v
        tourDepth[timer] = d
        timer += 1

Time: O(n)O(n). Space: O(n)O(n) for the arrays.