Hello everyone,
Can anyone please help me figure out what I miss or did wrong to my code for not to compile?
below are the code I did and I just couldn't figure it out what I missed. Thanks.
#include <iostream>
typedef struct NodeType {
char name; // the node's name
int weight; // the node's current weight
int pi; // the node's parent
} Node;
int graph[6][6] = {
{0,1,2,0,0,0},
{1,0,5,9,0,10},
{2,5,0,3,0,0},
{0,9,3,0,3,0},
{0,0,0,3,0,2},
{0,10,0,0,2,0}
};
Node node[6];
bool visited[6];
int getMinNode()
{
int index = -1;
for (int i=0; i<6; i++)
{
if (!visited[i])
{
index = i;
break;
}
}
for (int j=i+1; j<6; j++)
{
if (node[j].weight < node[index].weight) index = j;
}
return index;
}
void printResult(char *message) {
printf("%s\n", message);
for (int i=0; i<6; i++) {
if (node[i].pi >= 0) {
int parent = node[i].pi;
printf("%c -- %c \n", node[i].name, node[parent].name);
}
}
}
void main() { // Prim's algorithm
// Initialize the 6 nodes
for (int i=0; i<6; i++) {
node[i].name = 'A'+i;
node[i].pi = -1; // no parent
node[i].weight = 10000; // 10000 as infinity
visited[i] = false;
}
// Start from node 'A'
node[0].weight = 0;
int index = getMinNode();
// Prim's algorithm
while (index >= 0) {
visited[index] = true;
for (int j=0; j<6; j++) {
if (j==index) continue;
if (graph[j][index] == 0) continue;
int weight = node[index].weight + graph[j][index];
if (weight < node[j].weight) {
node[j].weight = weight;
node[j].pi = index;
}
}
index = getMinNode();
}
// Print out the result
printResult("Final Minimum Spanning Tree:");
}