Maximum number of bridges in a path of a given graph | C # program to find the maximum number of bridges in any path of the given graph ; Stores the nodes and their connections ; Store the tree with Bridges as the edges ; Stores the visited nodes ; For finding bridges ; for Disjoint Set Union ; For storing actual bridges ; Stores the number of nodes and edges ; For finding bridges ; Function to find root of the component in which A lies ; Doing path compression ; Function to do union between a and b ; If both are already in the same component ; If both have same rank , then increase anyone 's rank ; Function to find bridges ; Initialize in time and low value ; Update the low value of the parent ; Perform DFS on its child updating low if the child has connection with any ancestor ; Bridge found ; Otherwise ; Find union between parent and child as they are in same component ; Function to find diameter of the tree for storing max two depth child ; Finding max two depth from its children ; Update diameter with the sum of max two depths ; Return the maximum depth ; Function to find maximum bridges bwtween any two nodes ; DFS to find bridges ; If no bridges are found ; Iterate over all bridges ; Find the endpoints ; Generate the tree with bridges as the edges ; Update the head ; Return the diameter ; Driver Code ; Graph = > 1 -- -- 2 -- -- 3 -- -- 4 | | 5 -- -- 6
using System ; using System . Collections . Generic ; class GFG { static int N = ( int ) 1e5 + 5 ; static List < int > [ ] v = new List < int > [ N ] ; static List < int > [ ] g = new List < int > [ N ] ; static bool [ ] vis = new bool [ N ] ; static int [ ] init = new int [ N ] ; static int [ ] low = new int [ N ] ; static int [ ] parent = new int [ N ] ; static int [ ] rnk = new int [ N ] ; static List < pair > bridges = new List < pair > ( ) ; static int n , m ; static int timer = 0 ; static int diameter ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } static int find_set ( int a ) { if ( parent [ a ] == a ) return a ; return parent [ a ] = find_set ( parent [ a ] ) ; } static void union_set ( int a , int b ) { int x = find_set ( a ) , y = find_set ( b ) ; if ( x == y ) return ; if ( rnk [ x ] == rnk [ y ] ) rnk [ x ] ++ ; if ( rnk [ y ] > rnk [ x ] ) swap ( x , y ) ; parent [ y ] = x ; } static void dfsBridges ( int a , int par ) { vis [ a ] = true ; init [ a ] = low [ a ] = timer ++ ; foreach ( int i in v [ a ] ) { if ( i == par ) continue ; if ( vis [ i ] ) low [ a ] = Math . Min ( low [ a ] , init [ i ] ) ; else { dfsBridges ( i , a ) ; low [ a ] = Math . Min ( low [ a ] , low [ i ] ) ; if ( init [ a ] < low [ i ] ) bridges . Add ( new pair ( i , a ) ) ; else union_set ( i , a ) ; } } } static int dfsDiameter ( int a , int par ) { int x = 0 , y = 0 ; foreach ( int i in g [ a ] ) { if ( i == par ) continue ; int mx = dfsDiameter ( i , a ) ; if ( mx > x ) { y = x ; x = mx ; } else if ( mx > y ) y = mx ; } diameter = Math . Max ( diameter , x + y ) ; return x + 1 ; } static int findMaxBridges ( ) { for ( int i = 0 ; i <= n ; i ++ ) { parent [ i ] = i ; rnk [ i ] = 1 ; } dfsBridges ( 1 , 0 ) ; if ( bridges . Count == 0 ) return 0 ; int head = - 1 ; foreach ( pair i in bridges ) { int a = find_set ( i . first ) ; int b = find_set ( i . second ) ; g [ a ] . Add ( b ) ; g [ b ] . Add ( a ) ; head = a ; } diameter = 0 ; dfsDiameter ( head , 0 ) ; return diameter ; } public static void Main ( String [ ] args ) { n = 6 ; m = 6 ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; v [ 1 ] . Add ( 2 ) ; v [ 2 ] . Add ( 1 ) ; v [ 2 ] . Add ( 3 ) ; v [ 3 ] . Add ( 2 ) ; v [ 2 ] . Add ( 5 ) ; v [ 5 ] . Add ( 2 ) ; v [ 5 ] . Add ( 6 ) ; v [ 6 ] . Add ( 5 ) ; v [ 6 ] . Add ( 3 ) ; v [ 3 ] . Add ( 6 ) ; v [ 3 ] . Add ( 4 ) ; v [ 4 ] . Add ( 4 ) ; int ans = findMaxBridges ( ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }