Minimum Bipartite Groups | Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Drivers Code ; Adjacency List ; Adding edges to List
function height ( s , adj , visited ) { visited [ s ] = 1 ; var h = 0 ; adj [ s ] . forEach ( child => { if ( visited [ child ] == 0 ) { h = Math . max ( h , 1 + height ( child , adj , visited ) ) ; } } ) ; return h ; } function minimumGroups ( adj , N ) { var visited = Array ( N + 1 ) . fill ( 0 ) ; var groups = - 1000000000 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( visited [ i ] == 0 ) { var comHeight ; comHeight = height ( i , adj , visited ) ; groups = Math . max ( groups , comHeight ) ; } } return groups ; } function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } var N = 5 ; var adj = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 4 , 3 ) ; document . write ( minimumGroups ( adj , N ) ) ;