Detect when a graph has been broken into two or more connected components
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
An undirected graph is "broken" when vertices that used to be reachable from each other are now split into separate connected components. This matters in routing, network monitoring, and dependency analysis because a disconnected graph often means some part of the system is no longer usable.
Count Components with DFS or BFS
The simplest test is to count connected components. If the count is greater than 1, the graph is disconnected. A depth-first search or breadth-first search works well because each vertex and edge is visited at most once.
With an adjacency list, the algorithm is:
- Build neighbors for every vertex.
- Keep a
visitedset. - Start a traversal from each unvisited vertex.
- Increment a counter every time a new traversal starts.
That counter is the number of connected components.
If you only need a yes or no answer, you can stop early as soon as the second component is found.
This approach is O(V + E) time and O(V + E) space, which is usually the right baseline solution.
Detect Whether Removing One Edge Breaks the Graph
Sometimes the graph is currently connected, and the real question is whether a specific failure will split it. In graph theory, an edge with that property is a bridge. For a single edge check, a direct approach is to remove that edge and count components again.
This is fine for one-off checks, but it becomes expensive if you need to test many edges because you repeat a full traversal every time. In that case, compute all bridges once with Tarjan's algorithm. Tarjan's method also runs in O(V + E) time, but it returns every edge whose removal disconnects the graph.
The key idea is to track:
- '
disc[node]: when a node was first visited' - '
low[node]: the earliest reachable discovery time from that node'
If a child cannot reach an ancestor of its parent, then the edge between parent and child is a bridge. That is the efficient choice for repeated failure analysis.
Practical Modeling Details
The exact definition of "broken" depends on the graph model:
- In an undirected graph, connected components are the standard answer.
- In a directed graph, you must decide between weakly connected and strongly connected components.
- If some vertices are isolated but expected, you may only care whether a specific subset remains connected.
A monitoring system for servers, for example, may treat a graph as broken only if two active data centers can no longer reach each other. A graph processing library cannot guess that business rule, so define it explicitly before coding.
Common Pitfalls
- Forgetting isolated vertices. A vertex with no edges is still its own component, so looping only over adjacency keys can undercount.
- Mixing directed and undirected logic. If the graph is directed, reusing undirected traversal code may hide the real failure mode.
- Recomputing from scratch for every edge query. That works for small inputs, but a bridge-finding algorithm is much better when many removals must be tested.
- Mutating shared adjacency structures carelessly. Removing an edge in place can affect later checks if the original graph is needed again.
Summary
- A graph is broken when it has more than one connected component.
- DFS and BFS detect disconnection in
O(V + E)time. - For a single failure check, remove the edge and recount components.
- For many failure checks, compute bridges once instead of repeating full traversals.
- Be explicit about whether the graph is undirected, directed, or only partially relevant.
Related reading
- Detecting consecutive integers in a list
- Detecting duplicates in an array using divide and conquer
- Detecting HTML table orientation based only on table data
- Detecting if a string has unique characters comparing my solution to Cracking the Coding Interview?
- Detect when RecyclerView reaches the bottom most position while scrolling
- Detecting Singularities in a Graph
- Detecting self crossing in closed Bezier curves
- Determine if 2 lists have the same elements, regardless of order?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.