Graph Theory37 sections · 1633 units
Open in Course

Chain Decomposition DFS

(Assigning nodes to chains)

Run a DFS that assigns each node to a chain. Keep a counter chain_idchain\_id that increments each time you start a new chain. At each node vv, if vv is the start of a chain (no heavy edge from parent, or vv is root), assign chain[v] = chain_id and increment chain_idchain\_id.

Then recurse on the heavy child first with the same chain ID, then on light children with new chain IDs. This takes O(n)O(n) time and labels every node with its chain. The heavy child continues the parent's chain. Light children start new chains.

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