Prim 算法:
用于构建最小生成树——即树中所有边的权值之和最小。
①任取一顶点(或题中已告知顶点),去掉所有边
②选择一个与当前顶点集合距离最近的顶点,并将该顶点和相应的边加入进来,同时不形成回路
③重复②,直至图中所有顶点都并入。
Dijkstra 算法:
用于构建单元点的最短路径树——即树中某个点到任何其他点的距离都是最短的。
求解过程:
对于网N=(V,E),将N中的顶点分成两组。
第一组S:已求出的最短路径的终点集合(初始时只包含源点v)。
第二组V-S:尚未求出的最短路径的顶点集合(初始时为V-{Vo})。
算法将按各顶点与v间最短路径长度递增的次序,逐个将集合V-S中的顶点加入集合S中去。在这个过程中,总保持从到集合S中各顶点的路径长度始终不大于到集合V-S中各顶点的路径长度。
代码:
#include <stdio.h>
#include <cstdlib>
#define max_size 10000
//二维数组,即邻接矩阵
typedef struct Net {
int** weights;
int numNodes;
}*NetPtr;
//给定一个数组,根据这个数组初始化邻接矩阵
NetPtr initNet(int paraSize, int** paraData) {
int i, j;
NetPtr resultPtr = new Net;
resultPtr->numNodes = paraSize;
resultPtr->weights = (int**)malloc(paraSize * sizeof(int*));
for (i = 0; i < paraSize; i++) {
if (resultPtr->weights != NULL)
resultPtr->weights[i] = new int[paraSize];
for (j = 0; j < paraSize; j++) {
if (resultPtr->weights != NULL)
resultPtr->weights[i][j] = paraData[i][j];
}
}
return resultPtr;
}
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {
int i, j, minDistance = 0, tempBestNode, resultCost = 0;
int source = 0;
int numNodes = paraPtr->numNodes;
int* distanceArray = (int*)malloc(numNodes * sizeof(int) + 4);
int* parentArray = (int*)malloc(numNodes * sizeof(int) + 4);
int* visitedArray = (int*)malloc(numNodes * sizeof(int) + 4);
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_size;
for (j = 0; j < numNodes; j++) {
//如果该节点已经被访问,就跳过
if (visitedArray[j]) {
continue;
}
if (minDistance > distanceArray[j]) {
minDistance = distanceArray[j];
tempBestNode = j;
}
}
visitedArray[tempBestNode] = 1;
for (j = 0; j < numNodes; j++) {
// 节点被访问过
if (visitedArray[j]) {
continue;
}
// 没有路径到达该节点
if (paraPtr->weights[tempBestNode][j] >= max_size) {
continue;
}
//Dijkstra algorithm
if (paraAlgorithm == 0) {
if (distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]) {
// 改变距离
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
// 改变父节点
parentArray[j] = tempBestNode;
}
}
else {
if (distanceArray[j] > paraPtr->weights[tempBestNode][j]) {
// 改变距离大小
distanceArray[j] = distanceArray[j] = paraPtr->weights[tempBestNode][j];
// 改变父节点
parentArray[j] = tempBestNode;
}
}
}
printf("the parent of each node: ");
//Prim algorithm
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);
}
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\n");
tempPtr = (int**)malloc(numNodes * sizeof(int*));
for (i = 0; i < numNodes; i++) {
if (tempPtr != NULL)
tempPtr[i] = (int*)malloc(numNodes * sizeof(int));
}
for (i = 0; i < numNodes; i++) {
for (j = 0; j < numNodes; j++) {
if (myGraph[i][j] == 0) {
tempPtr[i][j] = max_size;
}
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=====\n");
dijkstraOrPrim(tempNetPtr, 0);
printf("====Prim algorithm====\n");
dijkstraOrPrim(tempNetPtr, 1);
}
int main(){
testPrim();
return 1;
}
结果: