Graph Theory37 sections · 1633 units
Open in Course

Divide and Conquer on Trees

(The general pattern)

Find the centroid cc. Solve the problem for paths passing through cc. Remove cc and recursively solve for each component. Combine the results. This is the standard pattern for all centroid decomposition problems. The recursion depth is O(logn)O(\log n), and processing paths through each centroid takes O(n)O(n) or O(nlogn)O(n \log n) depending on the problem.

The total time depends on what you do at each centroid. This pattern applies to path counting, distance queries, and weighted path problems. You will see it repeatedly in the problems below.

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