Minimize cost to connect the graph by connecting any pairs of vertices having cost at least 0 | Python 3 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 sys NEW_LINE def Find ( parent , a ) : NEW_LINE INDENT if parent [ parent [ a ] ] != parent [ a ] : NEW_LINE INDENT parent [ a ] = Find ( parent , parent [ a ] ) NEW_LINE DEDENT return parent [ a ] NEW_LINE DEDENT def Union ( parent , rank , minVal , a , b ) : NEW_LINE INDENT a = Find ( parent , a ) NEW_LINE b = Find ( parent , b ) NEW_LINE if ( rank [ a ] == rank [ b ] ) : NEW_LINE INDENT rank [ a ] += 1 NEW_LINE DEDENT if ( rank [ a ] > rank [ b ] ) : NEW_LINE INDENT parent [ b ] = a NEW_LINE if ( minVal [ a ] >= 0 and minVal [ b ] >= 0 ) : NEW_LINE INDENT minVal [ a ] = min ( minVal [ a ] , minVal [ b ] ) NEW_LINE DEDENT elif ( minVal [ a ] >= 0 and minVal [ b ] < 0 ) : NEW_LINE INDENT minVal [ a ] = minVal [ a ] NEW_LINE DEDENT elif ( minVal [ a ] < 0 and minVal [ b ] >= 0 ) : NEW_LINE INDENT minVal [ a ] = minVal [ b ] NEW_LINE DEDENT else : NEW_LINE INDENT minVal [ a ] = max ( minVal [ a ] , minVal [ b ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT parent [ a ] = b NEW_LINE if ( minVal [ a ] >= 0 and minVal [ b ] >= 0 ) : NEW_LINE INDENT minVal [ b ] = min ( minVal [ a ] , minVal [ b ] ) NEW_LINE DEDENT elif ( minVal [ a ] >= 0 and minVal [ b ] < 0 ) : NEW_LINE INDENT minVal [ b ] = minVal [ a ] NEW_LINE DEDENT elif ( minVal [ a ] < 0 and minVal [ b ] >= 0 ) : NEW_LINE INDENT minVal [ b ] = minVal [ b ] NEW_LINE DEDENT else : NEW_LINE INDENT minVal [ b ] = max ( minVal [ a ] , minVal [ b ] ) NEW_LINE DEDENT DEDENT DEDENT def findMinCost ( G , cost , N , M ) : NEW_LINE INDENT parent = [ 0 for i in range ( N + 1 ) ] NEW_LINE rank = [ 0 for i in range ( N + 1 ) ] NEW_LINE minVal = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE minVal [ i ] = cost [ i - 1 ] NEW_LINE DEDENT for i in G : NEW_LINE INDENT Union ( parent , rank , minVal , i [ 0 ] , i [ 1 ] ) NEW_LINE DEDENT s = set ( ) NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT s . add ( Find ( parent , i ) ) NEW_LINE DEDENT min1 = [ 0 , sys . maxsize ] NEW_LINE flag = False NEW_LINE for i in s : NEW_LINE INDENT if ( minVal [ i ] < 0 ) : NEW_LINE INDENT flag = True NEW_LINE DEDENT if ( min1 [ 1 ] > minVal [ i ] ) : NEW_LINE INDENT min1 = [ i , minVal [ i ] ] NEW_LINE DEDENT DEDENT minCost = 0 NEW_LINE if ( flag == False or ( flag and len ( s ) == 1 ) ) : NEW_LINE INDENT for i in s : NEW_LINE INDENT if ( i != min1 [ 0 ] ) : NEW_LINE INDENT minCost += ( minVal [ i ] + min1 [ 1 ] ) NEW_LINE DEDENT DEDENT print ( minCost ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE G = [ [ 3 , 1 ] , [ 2 , 3 ] , [ 2 , 1 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 4 ] ] NEW_LINE cost = [ 2 , 1 , 5 , 3 , 2 , 9 ] NEW_LINE M = len ( G ) NEW_LINE findMinCost ( G , cost , N , M ) NEW_LINE DEDENT