一.代码
#include <stdio.h>
#include <malloc.h>
#define MAX_DISTANCE 10000
typedef struct Net {
int** weights;
int numNodes;
} Net, *NetPtr;
NetPtr initNet(int paraSize, int** paraData) {
int i, j;
NetPtr resultPtr = (NetPtr)malloc(sizeof(Net));
resultPtr -> numNodes = paraSize;
resultPtr->weights = (int**)malloc(paraSize * sizeof(int*));
for (i = 0; i < paraSize; i ++) {
resultPtr -> weights[i] = (int*)malloc(paraSize * sizeof(int));
for (j = 0; j < paraSize; j ++) {
resultPtr -> weights[i][j] = paraData[i][j];
}
}
return resultPtr;
}
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {
int i, j, minDistance, tempBestNode, resultCost;
int source = 0;
int numNodes = paraPtr->numNodes;
int *distanceArray = (int*)malloc(numNodes * sizeof(int));
int *parentArray = (int*)malloc(numNodes * sizeof(int));
int *visitedArray = (int*)malloc(numNodes * sizeof(int));
for (i = 0; i < numNodes; i++) {
distanceArray[i] = paraPtr->weights[source][i];
parentArray[i] = source;
visitedArray[i] = 0;
}
distanceArray[source] = 0;
parentArray[source] = -1;
visitedArray[source] = 1;
tempBestNode = -1;
for (i = 0; i < numNodes - 1; i++) {
minDistance = MAX_DISTANCE;
for (j = 0; j < numNodes; j++) {
if (visitedArray[j]) {
continue;
}//of if
if (minDistance > distanceArray[j]) {
minDistance = distanceArray[j];
tempBestNode = j;
} of if
} // of for j
visitedArray[tempBestNode] = 1;
for (j = 0; j < numNodes; j++) {
if (visitedArray[j]) {
continue;
} // of if
if (paraPtr->weights[tempBestNode][j] >= MAX_DISTANCE) {
continue;
} // of if
if (paraAlgorithm == 0) {
if (distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]) {
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
parentArray[j] = tempBestNode;
} // of if
} else {
if (distanceArray[j] > paraPtr->weights[tempBestNode][j]) {
distanceArray[j] = paraPtr->weights[tempBestNode][j];
parentArray[j] = tempBestNode;
} // of if
}//of if
} // of for j
} // of for i
printf("the parent of each node: ");
for (i = 0; i < numNodes; i++) {
printf("%d, ", parentArray[i]);
} // of for i
if (paraAlgorithm == 0) {
printf("From node 0, path length to all nodes are: ");
for (i = 0; i < numNodes; i++) {
printf("%d (%d), ", i, distanceArray[i]);
} // of for i
} else {
resultCost = 0;
for (i = 0; i < numNodes; i++) {
resultCost += distanceArray[i];
printf("cost of node %d is %d, total = %d\r\n", i, distanceArray[i], resultCost);
}//of for i
printf("Finally, the total cost is %d.\r\n ", resultCost);
}//of if
printf("\r\n");
return resultCost;
}
NetPtr constructSampleNet() {
int i, j;
int myGraph[6][6] = {
{0, 6, 1, 5, 0, 0},
{6, 0, 5, 0, 3, 0},
{1, 5, 0, 5, 6, 4},
{5, 0, 5, 0, 0, 2},
{0, 3, 6, 0, 0, 6},
{0, 0, 4, 2, 6, 0}};
int** tempPtr;
int numNodes = 6;
printf("Preparing data\r\n");
tempPtr = (int**)malloc(numNodes * sizeof(int*));
for (i = 0; i < numNodes; i ++) {
tempPtr[i] = (int*)malloc(numNodes * sizeof(int));
}//of for i
for (i = 0; i < numNodes; i ++) {
for (j = 0; j < numNodes; j ++) {
if (myGraph[i][j] == 0) {
tempPtr[i][j] = MAX_DISTANCE;
} else {
tempPtr[i][j] = myGraph[i][j];
}//of if
}//of for j
}//of for i
printf("Data ready\r\n");
NetPtr resultNetPtr = initNet(numNodes, tempPtr);
return resultNetPtr;
}
void testPrim() {
NetPtr tempNetPtr = constructSampleNet();
printf("=====Dijkstra algorithm=====\r\n");
dijkstraOrPrim(tempNetPtr, 0);
printf("=====Prim algorithm=====\r\n");
dijkstraOrPrim(tempNetPtr, 1);
}
int main() {
testPrim();
return 1;
}
二.运行结果
三.图示
四.心得体会
①.Prim算法的主要目的是在加权连通图中找到一棵最小生成树。通过逐步添加权重最小的边,确保最终得到的树的总权重是最小的。在算法的执行过程中,需要维护两个集合:一个集合包含已加入最小生成树的顶点,另一个集合包含尚未加入的顶点。每次选择距离已生成树最近的顶点加入,确保了最小生成树的性质。
②.