Module 8 · Graphs and Trees · Union Find and MST

Number of connected components

Start with every node as its own component. An edge whose ends have different roots joins two components, so the count drops by one; an edge between nodes with the same root changes nothing.

In the forest an arrow goes from each node to its parent; rank counts the nodes in each root's tree.