Graph Theory37 sections · 1633 units
Open in Course

Lessons Learned

(Path counting with centroids)

Centroid decomposition turns all-pairs path counting into single-source distance counting. You process each node O(logn)O(\log n) times instead of checking O(n)O(n) paths for each node. The subtract step prevents double-counting paths within subtrees.

This is standard in divide-and-conquer: include everything, then subtract what you will handle recursively. You can apply this pattern to other path properties: sums, XOR, products, or any property that depends on the path as a whole.