Minimum Product Spanning Tree | A Python3 program for getting minimum product spanning tree The program is for adjacency matrix representation of the graph ; Number of vertices in the graph ; A utility function to find the vertex with minimum key value , from the set of vertices not yet included in MST ; Initialize min value ; A utility function to print the constructed MST stored in parent [ ] and print Minimum Obtaiable product ; Function to construct and print MST for a graph represented using adjacency matrix representation inputGraph is sent for printing actual edges and logGraph is sent for actual MST operations ; Array to store constructed MST ; Key values used to pick minimum ; weight edge in cut To represent set of vertices not ; Always include first 1 st vertex in MST Make key 0 so that this vertex is ; Picked as first vertex First node is always root of MST ; The MST will have V vertices ; Pick the minimum key vertex from the set of vertices not yet included in MST ; Add the picked vertex to the MST Set ; Update key value and parent index of the adjacent vertices of the picked vertex . Consider only those vertices which are not yet included in MST ; logGraph [ u ] [ v ] is non zero only for adjacent vertices of m mstSet [ v ] is false for vertices not yet included in MST . Update the key only if logGraph [ u ] [ v ] is smaller than key [ v ] ; Print the constructed MST ; Method to get minimum product spanning tree ; Constructing logGraph from original graph ; Applyting standard Prim 's MST algorithm  on Log graph. ; Driver code ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
import math NEW_LINE V = 5 NEW_LINE def minKey ( key , mstSet ) : NEW_LINE INDENT min = 10000000 NEW_LINE min_index = 0 NEW_LINE for v in range ( V ) : NEW_LINE INDENT if ( mstSet [ v ] == False and key [ v ] < min ) : NEW_LINE INDENT min = key [ v ] NEW_LINE min_index = v NEW_LINE DEDENT DEDENT return min_index NEW_LINE DEDENT def printMST ( parent , n , graph ) : NEW_LINE INDENT print ( " Edge ▁ Weight " ) NEW_LINE minProduct = 1 NEW_LINE for i in range ( 1 , V ) : NEW_LINE INDENT print ( " { } ▁ - ▁ { } ▁ { } ▁ " . format ( parent [ i ] , i , graph [ i ] [ parent [ i ] ] ) ) NEW_LINE minProduct *= graph [ i ] [ parent [ i ] ] NEW_LINE DEDENT print ( " Minimum ▁ Obtainable ▁ product ▁ is ▁ { } " . format ( minProduct ) ) NEW_LINE DEDENT def primMST ( inputGraph , logGraph ) : NEW_LINE INDENT parent = [ 0 for i in range ( V ) ] NEW_LINE key = [ 10000000 for i in range ( V ) ] NEW_LINE mstSet = [ False for i in range ( V ) ] NEW_LINE key [ 0 ] = 0 NEW_LINE parent [ 0 ] = - 1 NEW_LINE for count in range ( 0 , V - 1 ) : NEW_LINE INDENT u = minKey ( key , mstSet ) NEW_LINE mstSet [ u ] = True NEW_LINE for v in range ( V ) : NEW_LINE INDENT if ( logGraph [ u ] [ v ] > 0 and mstSet [ v ] == False and logGraph [ u ] [ v ] < key [ v ] ) : NEW_LINE INDENT parent [ v ] = u NEW_LINE key [ v ] = logGraph [ u ] [ v ] NEW_LINE DEDENT DEDENT DEDENT printMST ( parent , V , inputGraph ) NEW_LINE DEDENT def minimumProductMST ( graph ) : NEW_LINE INDENT logGraph = [ [ 0 for j in range ( V ) ] for i in range ( V ) ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT if ( graph [ i ] [ j ] > 0 ) : NEW_LINE INDENT logGraph [ i ] [ j ] = math . log ( graph [ i ] [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT logGraph [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT primMST ( graph , logGraph ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT graph = [ [ 0 , 2 , 0 , 6 , 0 ] , [ 2 , 0 , 3 , 8 , 5 ] , [ 0 , 3 , 0 , 0 , 7 ] , [ 6 , 8 , 0 , 0 , 9 ] , [ 0 , 5 , 7 , 9 , 0 ] , ] NEW_LINE minimumProductMST ( graph ) NEW_LINE DEDENT