using System;
class GFG {
static int V = 5;
static int minKey(int[] key, Boolean[] mstSet)
{
int min = int.MaxValue, min_index = 0;
for (int v = 0; v < V; v++) {
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
}
return min_index;
}
static void printMST(int[] parent, int n, int[, ] graph)
{
Console.Write("Edge Weight\n");
int minProduct = 1;
for (int i = 1; i < V; i++) {
Console.Write("{0} - {1} {2} \n",
parent[i], i, graph[i, parent[i]]);
minProduct *= graph[i, parent[i]];
}
Console.Write("Minimum Obtainable product is {0}\n",
minProduct);
}
static void primMST(int[, ] inputGraph, double[, ] logGraph)
{
int[] parent = new int[V];
int[] key = new int[V];
Boolean[] mstSet = new Boolean[V];
for (int i = 0; i < V; i++) {
key[i] = int.MaxValue;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++)
{
if (logGraph[u, v] > 0
&& mstSet[v] == false
&& logGraph[u, v] < key[v]) {
parent[v] = u;
key[v] = (int)logGraph[u, v];
}
}
}
printMST(parent, V, inputGraph);
}
static void minimumProductMST(int[, ] graph)
{
double[, ] logGraph = new double[V, V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (graph[i, j] > 0) {
logGraph[i, j] = Math.Log(graph[i, j]);
}
else {
logGraph[i, j] = 0;
}
}
}
primMST(graph, logGraph);
}
public static void Main(String[] args)
{
int[, ] 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 },
};
minimumProductMST(graph);
}
}