Number of Connected Components in an Undirected Graph — Union Find / BFS
Count connected components by Union Find (decrement count on each successful union) or by BFS / DFS (increment count for each unvisited node). Both run in near-linear time.
1582 articles
Count connected components by Union Find (decrement count on each successful union) or by BFS / DFS (increment count for each unvisited node). Both run in near-linear time.
Find the edge that closes a cycle when added to an n-node, n-edge graph. The first edge whose two endpoints share a Union Find root is the answer — a five-line solve.
Enumerate every path from node 0 to node n - 1 in a DAG. Classic DFS plus backtracking — push on enter, pop on exit, snapshot on hitting the target.
LC 743 Network Delay Time asks for the time a signal takes to reach all n nodes from source k. Single-source shortest path via Dijkstra gives every distance in O(E log V); the answer is the maximum among them.
LC 787 Cheapest Flights Within K Stops asks for the cheapest route from src to dst using at most k intermediate stops. Bellman-Ford with exactly k+1 relaxation rounds handles the stop constraint cleanly without Dijkstra getting confused by the layered state.