Prim算法
图论中的一种算法,可在加权连通图里搜索最小生成树。意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小
Dijkstra 算法
是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止
代码如下:
#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;
}
if (paraAlgorithm == 0) {
if (distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]){
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
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;
}
运行结果: