迪克斯特拉算法
迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法的策略,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。
普里姆算法
普里姆算法,图论中的一种算法,可在加权连通图里搜索最小生成树。意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小。该算法于1930年由捷克数学家沃伊捷赫·亚尔尼克(英语:Vojtěch Jarník)发现;并在1957年由美国计算机科学家罗伯特·普里姆(英语:Robert C. Prim)独立发现;1959年,艾兹格·迪科斯彻再次发现了该算法。因此,在某些场合,普里姆算法又被称为DJP算法、亚尔尼克算法或普里姆-亚尔尼克算法。
#include <stdio.h> #include <malloc.h> #define MAX_DISTANCE 1000 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) { //算法 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)); //申请空间 for (i = 0; i < numNodes; i++) { distanceArray[i] = paraPtr->weights[source][i]; //赋权值 parentArray[i] = source; visitedArray[i] = 0; } distanceArray[source] = 0; //距离为0 parentArray[source] = -1; //父母为-1 没有父母 visitedArray[source] = 1; //已经见过了 ture tempBestNode = -1; for (i = 0; i < numNodes - 1; i++) { minDistance = MAX_DISTANCE; //找距离最小的,先把距离拉成最大 for (j = 0; j < numNodes; j++) { if (visitedArray[j]) { continue; //拜访过了,直接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_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; } } else { if (distanceArray[j] > paraPtr->weights[tempBestNode][j]) { //普里姆算法 distanceArray[j] = paraPtr->weights[tempBestNode][j]; parentArray[j] = tempBestNode; } } } } printf("the parent of each node: "); for (i = 0; i < numNodes; i++) { printf("%d, ", parentArray[i]); } if (paraAlgorithm == 0) { printf("\nFrom 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("\r\ncost of node %d is %d, total = %d", i, distanceArray[i], resultCost); } printf("\nFinally, 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; } 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.
原本的图:

Prim算法:

Dijkstra 算法:

完毕;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/48131.html