Graph Theory37 sections · 1633 units
Open in Course

Space Complexity

(Memory usage)

You store the original tree (O(n)O(n) space for adjacency lists) and the decomposition tree (O(n)O(n) space for parent pointers). Distance arrays during DFS use O(n)O(n) space. If you store distances from every centroid to all nodes (for distance queries), that is O(nlogn)O(n \log n) space because each node appears in O(logn)O(\log n) centroid subtrees.

For most problems, O(nlogn)O(n \log n) space is acceptable with modern memory limits. If space is tight, recompute distances on-the-fly instead of storing them.