Graph Theory37 sections · 1633 units
Open in Course

Space Improvement

(Reducing memory usage)

After merging child sets into the parent, you can delete the child sets if you do not need them later. Most problems do not revisit nodes after processing. In C++, explicitly call .clear() on child sets after merging, or use std::move which invalidates the source automatically.

This keeps total memory close to O(n)O(n) instead of O(nlogn)O(n \log n). You are only storing active sets on the current DFS path, not all historical sets. Memory stays linear in practice.