prim算法、dijkstra算法
prim算法实现最小生成树:
1、以0为整个最小生成树的根结点
2、找与根结点直接相连的结点的最小边权值
3、再以最小边相连的结点为根结点重复第2步骤
4、如果一个根结点相邻的结点被访问了那么就不再考虑该相邻的结点
5、返回访问的第二个结点继续重复2、3、4步直至所有的结点都被访问
dijkstra算法求最短路径:
1、distanceArray数组用来存储根结点结点到各个结点的距离
2、parentArray数组用来存储每一个结点的父结点
3、在prim算法上加入了dijkstra算法,只是在判断边的权值上不同而已,prims算法只是判断从根结点到(0)到另一个结点(A)的直接距离与间接的结点(B)与(A)结点边的距离,dijkstra算法判断的是从根结点(0)到另一个结点(A)的直接距离与间接距离从根结点(0)间接经过的结点(B…)到(A)结点的距离之和。如果从根结点(0)直接到另一个结点(A)的距离大于从根结点间接经过其他结点(B…)的边的距离之和那么distanceArray数组所存储的从根结点(0)到(A)结点的距离赋值为间接到(A)结点的距离
4、重复3、步骤直至所有的结点都被访问
#include <stdio.h>
#include <malloc.h>
#define MAX_DISTANCE 10000
typedef struct Net{
int** weights;
int numNodes;
} *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) {
//paraPtr为传进来的邻接矩阵表示的图,paraAlgorithm = 1 为Dijkstra算法,paraAlgorithm = 0 为prim算法
int i, j, minDistance, tempBestNode, resultCost;
//minDistance为一个结点到相邻结点的距离
//该图有6个结点编号为0 1 2 3 4 5
int source = 0;
int numNodes = paraPtr->numNodes;
int *distanceArray = (int*)malloc(numNodes * sizeof(int));//用来存储0结点到各个结点的距离的数组
int *parentArray = (int*)malloc(numNodes * sizeof(int));//用来存储每一个结点的父结点的下标
int *visitedArray = (int*)malloc(numNodes * sizeof(int)); //如果一个结点被访问过了那么就标注为1
//将0结点到各个结点的距离都初始化到distanceArray里面
//将每一个结点的父结点都初始化为0
//每一个结点都初始化为未访问
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;
}
if (minDistance > distanceArray[j]) {
minDistance = distanceArray[j];
tempBestNode = j;
}
}
visitedArray[tempBestNode] = 1;
//找出从tempBestNode结点到j结点的距离小于从0结点到j结点的距离的结点并把tempBestNode作为j结点的父结点
for (j = 0; j < numNodes; j++) {
//入果被访问就选找下一个结点
if (visitedArray[j]) {
continue;
}
//如果j结点与temoBestNode结点不相邻继续寻找下一个结点
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;
}
} else {
if (distanceArray[j] > paraPtr->weights[tempBestNode][j]) {
// Change the distance.
distanceArray[j] = paraPtr->weights[tempBestNode][j];
// Change the parent.
parentArray[j] = tempBestNode;
}
}
}
}
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: ");//从0结点开始经过多少距离可以到达下一个结点
for (i = 0; i < numNodes; i++) {
printf("%d (%d), ", i, distanceArray[i]);//从0结点到i结点的距离时disanceArray[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\r\n");
tempPtr = (int**)malloc(numNodes * sizeof(int*));
for (i = 0; i < numNodes; i ++) {
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_DISTANCE;//如果i,j结点不相邻距离表示最大
} 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;
}
运行结果
Preparing data
Data ready
=====Dijkstra algorithm=====
the parent of each node: -1, 0, 0, 0, 2, 2, From node 0, path length to all nodes are: 0 (0), 1 (6), 2 (1), 3 (5), 4 (7), 5 (5),
=====Prim algorithm=====
the parent of each node: -1, 2, 0, 5, 1, 2, cost of node 0 is 0, total = 0
cost of node 1 is 5, total = 5
cost of node 2 is 1, total = 6
cost of node 3 is 2, total = 8
cost of node 4 is 3, total = 11
cost of node 5 is 4, total = 15
Finally, the total cost is 15.
PS D:\Data Struction\weekSix\teacher>