Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
How many pieces does the graph have?
Splits the graph into its connected pieces, one BFS per piece
Select an algorithm and generate steps to begin visualization
A connected component of an undirected graph is a maximal set of vertices in which every two are joined by a path. Every graph splits into components in exactly one way, and the graph is connected when there is only one. Finding the components is often the first step of a graph algorithm: it tells which vertices can reach each other at all.
Go through the vertices in order. Whenever a vertex has no label yet, it starts a new component: a breadth-first search from it labels every vertex it reaches with the same component number. A BFS never leaves its component and reaches all of it, so when its queue is empty the component is complete. Each vertex enters the queue once and each edge is looked at from both ends, so the whole run takes O(V + E) time. A depth-first search works the same way, and union-find gives the same answer while edges arrive one by one. For a directed graph, ignoring the directions gives the weakly connected components; strongly connected components need Tarjan's or Kosaraju's algorithm.
Connected components find isolated groups in social networks, islands in road and power networks, objects in images (connected-component labeling), and clusters of related records in data deduplication. Many algorithms, from minimum spanning forests to graph colouring, run on each component separately. It is one of the first applications of BFS and DFS taught in algorithms courses and a frequent interview question (number of islands, number of provinces).
Finding connected components is a loop of breadth-first searches. Each vertex that has no label yet starts a new component, and one BFS labels everything reachable from it.
ConnectedComponents(graph):
k = 0
for each vertex s, in any order:
if s has no label:
k = k + 1
label[s] = k
queue = [s]
while queue is not empty:
u = queue.removeFirst()
for each neighbor v of u:
if v has no label:
label[v] = k
queue.addLast(v)
return k, labelThe outer loop guarantees that every vertex is labelled, including isolated ones with no edges, and the inner BFS guarantees that two vertices get the same label exactly when a path joins them. Any traversal works in place of BFS. DFS gives the same partition, and union-find reaches it by processing the edges one at a time.
Label the components of the example this page loads: nine vertices that fall into four pieces of different shapes.
Example graph: Undirected edges A-B, A-C, B-C (a triangle), D-E, E-F (a path), and G-H (a single edge). Vertex I has no edges at all.
There are four components, {A, B, C}, {D, E, F}, {G, H} and {I}. A useful check: a forest with V vertices and E edges has exactly V - E components, which would be 9 - 6 = 3 here. The fourth comes from the triangle, whose third edge closes a cycle instead of joining two pieces. In general the number of components equals V - E plus the number of independent cycles.
Time: O(V + E) · Space: O(V)
Every vertex is enqueued exactly once over the whole run, because it is labelled when it is first reached and never enqueued again, and every adjacency list is scanned once, when its vertex is dequeued. The outer loop adds O(V) checks. The total is O(V + E) with adjacency lists. The label array and the queue are O(V). With union-find, the cost is O(E alpha(V)), where alpha is the inverse Ackermann function, less than 5 for any graph that fits in a computer. That is effectively linear and has the advantage that edges can arrive one at a time.
Connectivity questions come in several flavours. Pick the method that fits how the graph is given and what "connected" means for it.
| Alternative | Prefer it when | Cost |
|---|---|---|
| DFS labelling | Same result as BFS. Natural if you already have a DFS for other purposes, but recursion depth can be a problem on long paths. | O(V + E) |
| Union-find | Edges arrive over time, or you only need to ask whether two vertices are connected. It is also the core of Kruskal's algorithm. | O(E alpha(V)) |
| Tarjan or Kosaraju | The graph is directed and you need strongly connected components, where each vertex can reach every other. | O(V + E) |
| Biconnected components | You need to know which pieces survive the loss of any single vertex, not just which pieces are connected. | O(V + E) |
Related algorithms: Breadth-First Search, Depth-First Search, Tarjan's SCC Algorithm, Biconnected Components