2204. Distance to a Cycle in Undirected Graph - Detailed Explanation
Problem Statement
You are given an undirected graph with n nodes and exactly n edges. Such a graph is guaranteed to have a single cycle with one or more trees (or “branches”) attached to it. For every node in the graph, your task is to calculate its shortest distance (in number of edges) to any node that belongs to the cycle.
For nodes that are part of the cycle, the distance is 0. For nodes attached to the cycle by some path, the distance is the minimum number of edges to traverse from that node to reach any cycle node.
Example 1
-
Input:
- n = 4
- Edges: [[0,1], [1,2], [2,3], [3,0]]
-
Explanation:
The graph forms a perfect cycle where every node is part of the cycle. -
Output: [0, 0, 0, 0]
Example 2
- Input:
- n = 6
- Edges: [[0,1], [1,2], [2,0], [2,3], [3,4], [2,5]]
- Explanation:
Nodes 0, 1, and 2 form the cycle. Nodes 3 and 5 are directly attached to node 2, and node 4 is attached to node 3. - Output: [0, 0, 0, 1, 2, 1]
- Detail:
-
Nodes 0, 1, 2 → distance 0 (they are on the cycle)
-
Node 3 → distance 1 (direct neighbor of node 2)
-
Node 5 → distance 1 (direct neighbor of node 2)
-
Node 4 → distance 2 (node 4 → node 3 → node 2)
-
- Detail:
Example 3
- Input:
- n = 7
- Edges: [[0,1], [1,2], [2,0], [2,3], [3,4], [4,5], [2,6]]
- Explanation:
The cycle is between nodes 0, 1, and 2. Node 3 is connected to the cycle via node 2; node 4 via node 3; node 5 via node 4; and node 6 is directly connected to node 2. - Output: [0, 0, 0, 1, 2, 3, 1]
Constraints
-
The graph has n nodes and n edges.
-
The graph is connected.
-
3 ≤ n ≤ 10⁵ (for example; actual constraint limits may vary).
Hints
-
Cycle Identification:
Since the graph has exactly one cycle, consider methods to identify the cycle first. One effective strategy is to repeatedly “peel off” the leaves (nodes with degree 1). The remaining nodes after this process are the ones on the cycle. -
Distance Calculation:
Once you know the cycle nodes, you can use a multi-source Breadth-First Search (BFS) starting from all cycle nodes simultaneously. This will allow you to calculate the shortest distance for every node from the cycle.
Approaches
Approach 1: Brute Force (Not Recommended)
Explanation
A brute force method would be to, for each node, try to run a DFS or BFS to determine if it is on a cycle or calculate its distance to the cycle by exploring all possible paths. For every node not on the cycle, you would try all paths to see when a cycle node is reached.
Drawbacks
-
Inefficient: The repeated searches for each node can lead to high time complexity, especially on larger graphs.
-
Complexity: In the worst case, the approach could end up exploring many overlapping subtrees, leading to redundant computations.
Approach 2: Optimal - Peeling Leaves and Multi-Source BFS
This approach is efficient and works in two phases.
Phase 1: Identifying Cycle Nodes (Peeling Leaves)
-
Initialization:
- Create an array (or list) to record the degree of each node.
- Use an adjacency list to represent the graph.
-
Peel Off Leaves:
-
Start by adding all nodes with a degree of 1 (leaves) to a queue.
-
Iteratively remove a node from the queue. For each removed leaf, reduce the degree count of its neighbors by 1.
-
If any neighbor becomes a leaf (degree becomes 1) after removal, enqueue it.
-
Continue until no leaves remain. The remaining nodes will be the ones on the cycle.
-
Phase 2: Calculating Distance Using Multi-Source BFS
-
Initialization:
- Create a distance array (or list) initialized with -1 for every node.
- For every node identified as part of the cycle, set its distance to 0 and add it to the BFS queue.
-
Multi-Source BFS:
-
Process nodes from the queue.
-
For each neighboring node, if its distance has not been set (i.e., -1), update it as
distance[current] + 1
and add it to the queue. -
Continue until all reachable nodes are processed.
-
Complexity Analysis
-
Time Complexity: O(n)
- Peeling off leaves is O(n) since each edge is processed at most once.
- BFS is also O(n) as each edge is traversed once.
-
Space Complexity: O(n)
-
The adjacency list, degree array, and distance array all require O(n) space.
-
Code Solutions
Python Implementation
Java Implementation
Step-by-Step Walkthrough and Visual Example
Consider Example 2 with 6 nodes:
Edges:
- Cycle: 0–1, 1–2, 2–0
- Branches: 2–3, 3–4, 2–5
Phase 1: Identify the Cycle
- Build the Graph:
- Node 0: Degree 2 (neighbors: 1, 2)
- Node 1: Degree 2 (neighbors: 0, 2)
- Node 2: Degree 4 (neighbors: 0, 1, 3, 5)
- Node 3: Degree 2 (neighbors: 2, 4)
- Node 4: Degree 1 (leaf)
- Node 5: Degree 1 (leaf)
- Peel Leaves:
- Initial leaves: nodes 4 and 5 (mark them as not on cycle).
- Removing node 4: decrease the degree of node 3 (now degree becomes 1) → node 3 becomes a leaf.
- Removing node 5: decrease the degree of node 2 (node 2’s degree is reduced but remains ≥2 because it is still connected to 0 and 1).
- Removing node 3: decrease the degree of node 2.
- The nodes that remain unpeeled are 0, 1, and 2. These nodes form the cycle.
Phase 2: Compute Distances Using BFS
- Initialize:
- Set distances for nodes 0, 1, 2 to 0 and enqueue them.
- BFS Traversal:
- Dequeue a cycle node (say node 0) and look at its neighbors. Its neighbors are already at distance 0 (or will be updated later).
- Continue with nodes 1 and 2.
- For node 2’s neighbors: nodes 3 becomes distance 1 since it wasn’t assigned a distance before; for node 3, the neighbor node 4 becomes distance 2.
- Similarly, for node 2’s neighbor node 5, set distance 1.
The resulting distances are:
-
Nodes 0, 1, 2 → 0
-
Node 3 → 1
-
Node 4 → 2
-
Node 5 → 1
Common Mistakes
-
Not Handling the Degree Updates Correctly:
Failing to properly decrement the degree of a node’s neighbors when peeling off leaves may result in an incorrect identification of cycle nodes. -
Incorrect BFS Initialization:
Forgetting to start the BFS from all cycle nodes (i.e., nodes with distance 0) can lead to an erroneous distance calculation. -
Edge Assumptions:
Some may mistakenly assume the graph is a tree and try a simple DFS from a random node, missing the critical cycle identification step.
Edge Cases
-
All Nodes Form a Cycle:
When every node is part of the cycle (for example, a simple cycle with no branches), the peeling process will not remove any node, and all distances remain 0. -
Long Chain Branches:
Branches with many nodes require the BFS to propagate the distance correctly; testing on a long path attached to the cycle is important.
Alternative Variations
-
Distance from Each Node to a Given Set:
Instead of a cycle, you might be asked to compute the distance from every node to a specific set of nodes. -
Cycle Detection Only:
A variation could ask only to determine which nodes are part of the cycle without computing distances. -
Weighted Graph Variant:
If edges have weights, you would typically use Dijkstra’s algorithm for the multi-source shortest path.
Related Problems
GET YOUR FREE
Coding Questions Catalog
