Minimum labelled node to be removed from undirected Graph such that there is no cycle | C # implementation to find the minimum labelled node to be removed such that there is no cycle in the undirected graph ; Variables to store if a node V has at - most one back edge and store the depth of the node for the edge ; Function to swap the pairs of the graph ; If the second value is greater than x ; Put the pair in the ascending order internally ; Function to perform the DFS ; Initialise with the large value ; Storing the depth of this vertex ; Mark the vertex as visited ; Iterating through the graph ; If the node is a child node ; If the child node is unvisited ; Move to the child and increase the depth ; increase according to algorithm ; If the node is not having exactly K backedges ; If the child is already visited and in current dfs ( because colour is 1 ) then this is a back edge ; Increase the countAdj values ; Colour this vertex 2 as we are exiting out of dfs for this node ; Function to find the minimum labelled node to be removed such that there is no cycle in the undirected graph ; Construct the graph ; Mark visited as false for each node ; Apply dfs on all unmarked nodes ; If no backedges in the initial graph this means that there is no cycle So , return - 1 ; Iterate through the vertices and return the first node that satisfies the condition ; Check whether the count sum of small [ v ] and count is the same as the total back edges and if the vertex v can be removed ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static int totBackEdges ; static int [ ] countAdj = new int [ MAX ] ; static int [ ] small = new int [ MAX ] ; static int [ ] isPossible = new int [ MAX ] ; static int [ ] depth = new int [ MAX ] ; static ArrayList adj = new ArrayList ( ) ; static int [ ] vis = new int [ MAX ] ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void change ( ref pair p , int x ) { if ( p . second > x ) p . second = x ; if ( p . first > p . second ) { int tmp = p . first ; p . first = p . second ; p . second = tmp ; } } static pair dfs ( int v , int p = - 1 , int de = 0 ) { pair answer = new pair ( 100000000 , 100000000 ) ; depth [ v ] = de ; vis [ v ] = 1 ; isPossible [ v ] = 1 ; foreach ( int u in ( ArrayList ) adj [ v ] ) { if ( ( u ^ p ) != 0 ) { if ( vis [ u ] == 0 ) { pair x = dfs ( u , v , de + 1 ) ; small [ v ] += small [ u ] ; change ( ref answer , x . second ) ; change ( ref answer , x . first ) ; if ( x . second < de ) isPossible [ v ] = 0 ; } else if ( vis [ u ] == 1 ) { totBackEdges ++ ; countAdj [ v ] ++ ; countAdj [ u ] ++ ; small [ p ] ++ ; small [ u ] -- ; change ( ref answer , depth [ u ] ) ; } } } vis [ v ] = 2 ; return answer ; } static int minNodetoRemove ( int n , ArrayList edges ) { for ( int i = 0 ; i < edges . Count ; i ++ ) { ( ( ArrayList ) adj [ ( ( pair ) edges [ i ] ) . first ] ) . Add ( ( ( pair ) edges [ i ] ) . second ) ; ( ( ArrayList ) adj [ ( ( pair ) edges [ i ] ) . second ] ) . Add ( ( ( pair ) edges [ i ] ) . first ) ; } Array . Fill ( vis , 0 ) ; totBackEdges = 0 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( vis [ v ] == 0 ) dfs ( v ) ; } if ( totBackEdges == 0 ) return - 1 ; int node = - 1 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( ( countAdj [ v ] + small [ v ] == totBackEdges ) && isPossible [ v ] != 0 ) { node = v ; } if ( node != - 1 ) break ; } return node ; } static void Main ( ) { int N = 5 ; ArrayList edges = new ArrayList ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) { adj . Add ( new ArrayList ( ) ) ; } edges . Add ( new pair ( 5 , 1 ) ) ; edges . Add ( new pair ( 5 , 2 ) ) ; edges . Add ( new pair ( 1 , 2 ) ) ; edges . Add ( new pair ( 2 , 3 ) ) ; edges . Add ( new pair ( 2 , 4 ) ) ; Console . Write ( minNodetoRemove ( N , edges ) ) ; } }