Graph Theory37 sections · 1633 units
Open in Course

Decision Question 2

(One root vs all roots)

Second decision: Does the problem ask for an answer from one specific root, or do you need answers for all nodes as roots?

If it is 'from node 11' or 'from the root', use subtree DP or diameter (if it is about distances). One root means one DFS.

If it is 'for each node' or 'for all possible roots', you need rerooting. Running DFS from each node separately is O(n2)O(n^2). Rerooting is O(n)O(n).

Diameter is special: it finds the farthest pair without checking all roots. Two BFS or one DP.

Example: Distance sum from node 11 = subtree DP. Distance sum for all nodes = rerooting.