Introduction to Connectivity

Can A reach B? Same component?

Can node A reach node B? In a static graph, run DFS once: O(V+E)O(V + E). Simple.

But what if edges arrive over time and you need to answer connectivity queries after each one? DFS from scratch each time is too slow.

Union-Find answers "are these connected?" in nearly O(1)O(1) time. It handles dynamic edge additions efficiently. I'll show you when DFS suffices, when you need Union-Find, and how to apply path compression and union by rank. These two tools cover most connectivity problems you'll encounter.

941af795-f9eb-4bb5-97c5-f1c971965b44 (1) (1).png