Minimize cost to connect the graph by connecting any pairs of vertices having cost at least 0 | Java program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; Update the minimum value such than it should be greater than 0 ; Update the minimum value such than it should be greater than 0 ; Function to minimize the cost to make the graph connected as per the given condition ; Stores the parent elements of sets ; Stores the rank of the sets ; Stores the minValue of the sets ; Update parent [ i ] to i ; Update minValue [ i ] to cost [ i - 1 ] ; Add i . first and i . second elements to the same set ; Stores the parent elements of the different components ; Insert parent of i to s ; Stores the minimum value from s ; Flag to mark if any minimum value is negative ; If minVal [ i ] is negative ; Mark flag as true ; Update the minimum value ; Stores minimum cost to add the edges ; If the given graph is connected or components minimum values not having any negative edge ; Update the minCost ; Print the value of minCost ; Print - 1 ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] minVal , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) { parent [ b ] = a ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ a ] = Math . min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ a ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ a ] = minVal [ b ] ; } else { minVal [ a ] = Math . max ( minVal [ a ] , minVal [ b ] ) ; } } else { parent [ a ] = b ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ b ] = Math . min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ b ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ b ] = minVal [ b ] ; } else { minVal [ b ] = Math . max ( minVal [ a ] , minVal [ b ] ) ; } } } static void findMinCost ( pair [ ] G , int [ ] cost , int N , int M ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] minVal = new int [ N + 1 ] ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = i ; minVal [ i ] = cost [ i - 1 ] ; } for ( pair i : G ) { Union ( parent , rank , minVal , i . first , i . second ) ; } HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { s . add ( Find ( parent , i ) ) ; } pair min = new pair ( 0 , Integer . MAX_VALUE ) ; boolean flag = false ; for ( int i : s ) { if ( minVal [ i ] < 0 ) { flag = true ; } if ( min . second > minVal [ i ] ) { min = new pair ( i , minVal [ i ] ) ; } } int minCost = 0 ; if ( ! flag || ( flag && s . size ( ) == 1 ) ) { for ( int i : s ) { if ( i != min . first ) { minCost += ( minVal [ i ] + min . second ) ; } } System . out . print ( minCost + "NEW_LINE"); } else { System . out . print ( - 1 + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 6 ; pair [ ] G = { new pair ( 3 , 1 ) , new pair ( 2 , 3 ) , new pair ( 2 , 1 ) , new pair ( 4 , 5 ) , new pair ( 5 , 6 ) , new pair ( 6 , 4 ) } ; int [ ] cost = { 2 , 1 , 5 , 3 , 2 , 9 } ; int M = G . length ; findMinCost ( G , cost , N , M ) ; } }