Minimum labelled node to be removed from undirected Graph such that there is no cycle | Python3 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
MAX = 100005 ; NEW_LINE totBackEdges = 0 NEW_LINE countAdj = [ 0 for i in range ( MAX ) ] NEW_LINE small = [ 0 for i in range ( MAX ) ] NEW_LINE isPossible = [ 0 for i in range ( MAX ) ] NEW_LINE depth = [ 0 for i in range ( MAX ) ] NEW_LINE adj = [ [ ] for i in range ( MAX ) ] NEW_LINE vis = [ 0 for i in range ( MAX ) ] NEW_LINE def change ( p , x ) : NEW_LINE INDENT if ( p [ 1 ] > x ) : NEW_LINE INDENT p [ 1 ] = x ; NEW_LINE DEDENT if ( p [ 0 ] > p [ 1 ] ) : NEW_LINE tmp = p [ 0 ] ; NEW_LINE p [ 0 ] = p [ 1 ] ; NEW_LINE p [ 1 ] = tmp ; NEW_LINE DEDENT def dfs ( v , p = - 1 , de = 0 ) : NEW_LINE INDENT global vis , totBackEdges NEW_LINE answer = [ 100000000 , 100000000 ] NEW_LINE depth [ v ] = de ; NEW_LINE vis [ v ] = 1 ; NEW_LINE isPossible [ v ] = 1 ; NEW_LINE for u in adj [ v ] : NEW_LINE INDENT if ( ( u ^ p ) != 0 ) : NEW_LINE INDENT if ( vis [ u ] == 0 ) : NEW_LINE INDENT x = dfs ( u , v , de + 1 ) ; NEW_LINE small [ v ] += small [ u ] ; NEW_LINE change ( answer , x [ 1 ] ) ; NEW_LINE change ( answer , x [ 0 ] ) ; NEW_LINE if ( x [ 1 ] < de ) : NEW_LINE INDENT isPossible [ v ] = 0 ; NEW_LINE DEDENT DEDENT elif ( vis [ u ] == 1 ) : NEW_LINE INDENT totBackEdges += 1 NEW_LINE countAdj [ v ] += 1 NEW_LINE countAdj [ u ] += 1 NEW_LINE small [ p ] += 1 NEW_LINE small [ u ] -= 1 NEW_LINE change ( answer , depth [ u ] ) ; NEW_LINE DEDENT DEDENT DEDENT vis [ v ] = 2 ; NEW_LINE return answer ; NEW_LINE DEDENT def minNodetoRemove ( n , edges ) : NEW_LINE INDENT for i in range ( len ( edges ) ) : NEW_LINE INDENT adj [ edges [ i ] [ 0 ] ] . append ( edges [ i ] [ 1 ] ) ; NEW_LINE adj [ edges [ i ] [ 1 ] ] . append ( edges [ i ] [ 0 ] ) ; NEW_LINE DEDENT global vis , totBackEdges NEW_LINE vis = [ 0 for i in range ( len ( vis ) ) ] NEW_LINE totBackEdges = 0 ; NEW_LINE for v in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( vis [ v ] == 0 ) : NEW_LINE INDENT dfs ( v ) ; NEW_LINE DEDENT DEDENT if ( totBackEdges == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT node = - 1 ; NEW_LINE for v in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( ( countAdj [ v ] + small [ v ] == totBackEdges ) and isPossible [ v ] != 0 ) : NEW_LINE INDENT node = v ; NEW_LINE DEDENT if ( node != - 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return node ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE edges = [ ] NEW_LINE edges . append ( [ 5 , 1 ] ) ; NEW_LINE edges . append ( [ 5 , 2 ] ) ; NEW_LINE edges . append ( [ 1 , 2 ] ) ; NEW_LINE edges . append ( [ 2 , 3 ] ) ; NEW_LINE edges . append ( [ 2 , 4 ] ) ; NEW_LINE print ( minNodetoRemove ( N , edges ) ) ; NEW_LINE DEDENT