Maximum number of bridges in a path of a given graph | Java 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
import java . util . * ; class GFG { static int N = ( int ) 1e5 + 5 ; static Vector < Integer > [ ] v = new Vector [ N ] ; static Vector < Integer > [ ] g = new Vector [ N ] ; static boolean [ ] vis = new boolean [ N ] ; static int [ ] in = new int [ N ] ; static int [ ] low = new int [ N ] ; static int [ ] parent = new int [ N ] ; static int [ ] rnk = new int [ N ] ; static Vector < pair > bridges = new Vector < > ( ) ; static int n , m ; static int timer = 0 ; static int diameter ; static class pair { 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 ; in [ a ] = low [ a ] = timer ++ ; for ( int i : v [ a ] ) { if ( i == par ) continue ; if ( vis [ i ] ) low [ a ] = Math . min ( low [ a ] , in [ i ] ) ; else { dfsBridges ( i , a ) ; low [ a ] = Math . min ( low [ a ] , low [ i ] ) ; if ( in [ 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 ; for ( int i : 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 . isEmpty ( ) ) return 0 ; int head = - 1 ; for ( pair i : 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 Vector < Integer > ( ) ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; 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 ( ) ; System . out . print ( ans + "NEW_LINE"); } }