Graph Theory37 sections · 1633 units
Open in Course

Worst Case: Chain Tree

(O(n²) disaster)

Consider a chain: 123.n1 \to 2 \to 3 \to. \to n. Each node has one child except the leaf. Node nn has set size 11. Node n1n-1 merges with nn, giving size 22. Node n2n-2 merges with n1n-1, giving size 33. You keep copying larger and larger sets.

Total operations: 1+2+3+.+n=O(n2)1 + 2 + 3 +. + n = O(n^2). For n=100,000n = 100,000, this is 55 billion operations. Your solution times out. The pattern happens whenever you always merge into the smaller set.