VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-labelled-node-to-be-removed-from-undirected-graph-such-that-there-is-no-cycle/

⇱ Minimum labelled node to be removed from undirected Graph such that there is no cycle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum labelled node to be removed from undirected Graph such that there is no cycle

Last Updated : 12 Jul, 2025

Given an undirected graph of N nodes labelled from 1 to N, the task is to find the minimum labelled node that should be removed from the graph such that the resulting graph has no cycle. 

Note: If the initial graph has no cycle, i.e. no node needs to be removed, print -1.

Examples:  

Input: N = 5, edges[][] = {{5, 1}, {5, 2}, {1, 2}, {2, 3}, {2, 4}} 
Output:
Explanation: 
If node 1 is removed, the resultant graph has no cycle. Similarly, the cycle can be avoided by removing node 2 also. 
Since we have to find the minimum labelled node, the answer is 1.

Input: N = 5, edges[][] = {{4, 5}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}} 
Output: 4  

Naive Approach: The naive approach for this problem would be to remove each vertex individually and check whether the resulting graph has a cycle or not. The time complexity for this approach is quadratic. 

Efficient Approach: The idea is to apply depth-first search on the given graph and observing the dfs tree formed.  

  • A Back Edge is referred to as the edge that is not part of the constructed DFS tree and is an edge between some node v and one of the ancestors of v.
  • Clearly all those edges of the graph which are not a part of the DFS tree are back edges.
  • If there are no back edges in the graph, then the graph has no cycle. So, the answer will be -1 in this case.

If there are back edges in the graph, then we need to find the minimum edge. In order to do this, we need to check if the cycle is removed on removing a specific edge from the graph. Therefore, let v be a vertex which we are currently checking. Therefore, the following conditions must be followed by vertex v such that on removing, it would lead to no cycle:  

  • v must lie on the tree path connecting the endpoints of each back edge in the graph. 
    Proof: Suppose there exists some back edge x-y, such that v doesn’t lie on the tree path. If we remove v, we would still be able to traverse from x to y, and back to x via the back edge, indicating that the cycle is not removed.
  • The subtree of v must have at-most one back edge to any ancestor of v. 
    Proof: Let the subtree S has to back edges w-x and y-z such that w and y are in S and x and z are ancestors of v. If we remove v, clearly a cycle still exists consisting of the path between w to y, the path from x to z and the two back edges w-x and y-z, i.e. cycle is not removed.

Therefore, the idea is to keep a track of back edges, and an indicator for the number of back edges in the subtree of a node to any of its ancestors. To keep a track of back edges we will use a modified DFS graph colouring algorithm
In order to check if the subtree v has at-most one back edge to any ancestor of v or not, we implement dfs such that it returns the depth of two highest edges from the subtree of v. We maintain an array where every index 'i' in the array stores if the condition 2 from the above is satisfied by the node 'i' or not. Similarly, two arrays are implemented, one for the child and another for the parent to see if the node v lies on the tree path connecting the endpoints. 

Below is the implementation of the above approach: 


Output: 
1

 

Time Complexity: O(N + M), where N is the number of nodes and M is the number of edges.
Auxiliary Space: O(N + M).

Comment
Article Tags: