#include <stdio.h>
#include <stdlib.h>
#define MAX_DISTANCE 10000
//定义图结构
typedef struct Net {
int** weights;
int numNOdes;
}*NetPtr;
//初始化图的各个节点
NetPtr initNet(int paraSize, int** paraData) {
int i, j;
NetPtr resultPtr = new struct Net;
resultPtr->numNOdes = paraSize; //节点数
//
resultPtr->weights = new int*[paraSize];
for (i = 0; i < paraSize; i++) {
resultPtr->weights[i] = new int [paraSize];
for (j = 0; j < paraSize; j++) {
resultPtr->weights[i][j] = paraData[i][j];//给每个节点赋上权值
}
}
return resultPtr;//初始化完成,返回这个图
}
//Prim和Dijkstra算法
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {
int i, j, minDistance, tempBestNode;
int resultCost=-1;
int source = 0;//源点
int numNodes = paraPtr->numNOdes;//节点个数
int* distanceArray = new int[numNodes];
int* parentArray = new int[numNodes];
//
int* visiteArray = new int[numNodes];//看每个节点是否都已经访问
//step.1 Initialize:Any node can be the source
for (i = 0; i < numNodes; i++) {
distanceArray[i] = paraPtr->weights[source][i];
parentArray[i] = source;
visiteArray[i] = 0;//初始化为0,代表都未进行访问
}
distanceArray[source] = 0;
parentArray[source] = -1;//源点没有母节点
visiteArray[source] = 1;
//step2.Main loops
tempBestNode = -1;
for (i = 0; i < numNodes - 1; i++) {
//step2.1 Find out the best next node
minDistance = MAX_DISTANCE;
for (j = 0; j < numNodes; j++) {
//This node is visited
if (visiteArray[j]) {
continue;
}
if (minDistance > distanceArray[j]) {
minDistance = distanceArray[j];
tempBestNode = j;//不断循环找出到源点权值最小的节点
}
}
visiteArray[tempBestNode] = 1;
//step2.2 Prepare for the next round
for (j = 0; j < numNodes; j++) {
//This node is visited
if (visiteArray[j]) {
continue;
}//如果是已经访问过的节点就不管
//This node cannot be reached
//以找到的节点为新源点
if (paraPtr->weights[tempBestNode][j] >= MAX_DISTANCE) {
continue;
}
//Attention : the difference between Dijkstra and Prim algorithms
if (paraAlgorithm == 0) {
if (distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]){
//change the distance
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
//change the parent
parentArray[j] = tempBestNode;
//Dijkstra算法
}
}
else {
if (distanceArray[j] > paraPtr->weights[tempBestNode][j]) {
//change the distance
distanceArray[j] = paraPtr->weights[tempBestNode][j];
//change the parent
parentArray[j] = tempBestNode;
}
}
//Prim算法
}
}
printf("the parent of each node: ");
for (i = 0; i < numNodes; i++) {
printf("%d ", parentArray[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]);
}
}
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);
}
printf("Finally, the total cost is %d.\r\n", resultCost);
}
//Output for debug
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 = new int* [numNodes];
for (i = 0; i < numNodes; i++) {
tempPtr[i] = new int[numNodes];
}
for (i = 0; i < numNodes; i++) {
for (j = 0; j < numNodes; j++) {
if (myGraph[i][j] == 0) {
tempPtr[i][j] = MAX_DISTANCE;//0代表距离无穷大
}
else {
tempPtr[i][j] = myGraph[i][j];
}
}
}
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算法可用于构建最小生成树,我们只想要构建一棵树,使得每个节点都能到达任意一个节点,在操作时只关心所有边之和最小即可,Prim算法计算时只考虑与相邻节点的权值
Dijkstra算法可用于构建单源点的最短路径树,要让源点到其他任何点的距离都为最短,在每一步都只考虑路径最短的节点,但与Prim算法不同的是,Dijkstra算法要加上之前的权值