Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Find Components

(DSU or DFS)

Use Union-Find or DFS to find all connected components. Each component gets a unique ID. Nodes in the same component have the same ID. After identifying components, pick one representative node from each component. You will connect these representatives. If you find kk components, you need exactly k1k-1 edges.

The problem reduces to connecting kk isolated nodes into a single tree. Any spanning tree of kk nodes works.