栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构与算法:13.Prim算法与Dijkstra算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构与算法:13.Prim算法与Dijkstra算法

文章目录
  • 定义
  • 代码
    • 结构的定义
    • 初始化网格
    • 算法函数
    • 测试函数
    • 完整代码以及运行结果

定义

普里姆算法(Prim):对顶点操作,在最小生成树的顶点集U和待处理顶点集V-U中,不断地寻找最短边(代价最小变),找到后将对应顶点加入集合U,直到所有顶点均处理完毕(V-U里没有剩余顶点)。
迪杰斯特拉(Dijkstra):它先确定起点的最短路径(自己到自己)再从起点周围寻找⼀个离起点最近的点,并确定它现在到起点的的路径就是最短路径。然后再从已确定的两个点周围寻找离起点最近的点,并确定它现在到起点的路径就是最短路径。

代码 结构的定义
// 结构的定义 
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;
}
算法函数
// Dijstra算法和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)); 

	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;

		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("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 = %drn", i, distanceArray[i], resultCost);
		} 
		printf("Finally, the total cost is %d.rn ", resultCost);
	}

	printf("rn");

	return resultCost;
}
测试函数
// 测试算法 
void testPrim() {
	NetPtr tempNetPtr = constructSampleNet();
	printf("=====Dijkstra algorithm=====rn");
	dijkstraOrPrim(tempNetPtr, 0);
	printf("=====Prim algorithm=====rn");
	dijkstraOrPrim(tempNetPtr, 1);
}
完整代码以及运行结果
#include 
#include 

#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;
}

// Dijstra算法和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)); 

	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;

		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("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 = %drn", i, distanceArray[i], resultCost);
		} 
		printf("Finally, the total cost is %d.rn ", resultCost);
	}

	printf("rn");

	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 datarn");
		
	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 readyrn");
	
	NetPtr resultNetPtr = initNet(numNodes, tempPtr);
	return resultNetPtr;


// 测试算法 
void testPrim() {
	NetPtr tempNetPtr = constructSampleNet();
	printf("=====Dijkstra algorithm=====rn");
	dijkstraOrPrim(tempNetPtr, 0);
	printf("=====Prim algorithm=====rn");
	dijkstraOrPrim(tempNetPtr, 1);
}

int main(){
	testPrim();
	return 1;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号