Graph Theory37 sections · 1633 units
Open in Course

Walkthrough

(Example trace)

Tree: 121-2, 232-3, 343-4, 454-5. Target length: k=2k=2. Centroid is 33. Distances from 33: node 11 is 22, node 22 is 11, node 33 is 00, node 44 is 11, node 55 is 22. Pairs with sum 22: node 22 (distance 11) with node 44 (distance 11). That is one path: 2342-3-4.

Check child subtrees: subtree rooted at 22 contains {1,2}\{1,2\}, subtree at 44 contains {4,5}\{4,5\}. No pairs within a single subtree sum to 22. Recurse on components: each is small, no additional paths. Total: 11 path of length 22.