一、代码
#include<stdio.h>
#include<malloc.h>
#define MAX_DISTANCE 10000
/**
* net 的数据结构
*/
typedef struct Net{
int** weights;
int numNodes;
} Net, *NetPtr;
/**
* 初始化
*/
NetPtr initNet(int paraSize, int** paraData){
int i, j;
NetPtr resultPtr = (NetPtr)malloc(sizeof(NetPtr));
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];
}// of for j
}//o for i
return resultPtr;
} //of initNet
/**
* 最短路径问题--Dijkstra 算法
* 生成最小数--Prim 算法
*/
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));
//step 1. 初始化, 任何结点都可以是起点
for(i = 0; i < numNodes; i ++){
distanceArray[i] = paraPtr->weights[source][i];
parentArray[i] = source;
visitedArray[i] = 0;
} //of for i
distanceArray[source] = 0;
parentArray[source] = -1;
visitedArray[source] = 1;
//step 2. main loops
for(i =0; i < numNodes; i ++){
//step2.1 找最好的结点
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;
//step 2.2 准备下一次循环
for(j = 0; j < numNodes; j ++){
//如果节点已经被访问过
if(visitedArray[j]){
continue;
} //of if
//结点将不能到达
if(paraPtr->weights[tempBestNode][j] >= MAX_DISTANCE){
continue;
} //of if
//prim 和 Disjkstra 的区别
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 lenth 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
//step 3. 输出
printf("\r\n");
return resultCost;
} //of dijkstraOrPrim
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;
}//of constructSampleNet
/**
* 测试prim算法
*/
void teatPrim(){
NetPtr tempNetPtr = constructSampleNet();
printf("====Dijkstra algotithm=====\r\n");
dijkstraOrPrim(tempNetPtr, 0);
printf("====Prim algorithm====\r\n");
dijkstraOrPrim(tempNetPtr, 1);
} //of testPrim
int main(){
teatPrim();
return 0;
}//of main
二、运行结果
三、代码说明
1)Prim 算法:生成最小树,可以运用于城市之间修路的最小投入问题,在每次选择最小边时可能存在同样权值的表面可以选择,此时任选一条即可。
2)Dijkstra算法:与Prim算法有相似之处但又不同,是一个按路径长度递增的次序产生最短路径的算法,要求所有边的权重都为非负值。首先找一个源点,然后从该源点出发找一条权重最小的路径,然后又以该路径的另一端点为源点,继续向下找点。