Graph Theory37 sections · 1633 units
Open in Course

Intro

(Solving path queries fast)

You can answer subtree queries in O(logn)O(\log n) with Euler tours and segment trees.

But what about path queries? Given two nodes uu and vv, find the maximum value on the path between them. The naive approach visits every node on the path. In a line graph with nn nodes, paths can have length n1n-1, making each query O(n)O(n).

With qq queries, that is O(nq)O(nq) total. In this section, I'll show you Heavy-Light Decomposition, a technique that splits any tree into O(logn)O(\log n) chains. This turns path queries into O(logn)O(\log n) range queries on a segment tree, giving O(qlog2n)O(q \log^2 n) total time.

1770082777478-y8vfxypyb18.jpg