Graph Theory37 sections · 1633 units
Open in Course

Path Queries - Implementation

(BIT for prefix sums)

Use BIT (Fenwick tree) for efficient prefix sum queries and point updates. Pseudocode:

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

Complexity: O(nlogn)O(n \log n) build, O(logn)O(\log n) per query.

Space complexity is O(n)O(n) for the data structures used.