迪杰斯特拉和普利姆算法

1.代码:

#include<iostream>
using namespace std;
#define N 10000
typedef struct Net {
	int** weight;
	int numNodes;
}Net, * NetPtr;
NetPtr initNet(int paraSize, int** paraData) {
	NetPtr resultPtr = (NetPtr)malloc(sizeof(Net));
	resultPtr->numNodes = paraSize;
	resultPtr->weight = (int**)malloc(sizeof(int*) * paraSize);
	for (int i = 0; i < paraSize; i++) {
		resultPtr->weight[i] = (int*)malloc(sizeof(int) * paraSize);
		for (int j = 0; j < paraSize; j++) {
			resultPtr->weight[i][j] = paraData[i][j];
		}
	}
	return resultPtr;
}
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {
	int source = 0;
	int numNodes = paraPtr->numNodes;
	int minDistance, tempBestNode, resultCost;
	int* distanceArray = (int*)malloc(sizeof(int) * numNodes);
	int* parentArray = (int*)malloc(sizeof(int) * numNodes);
	int* visitArray = (int*)malloc(sizeof(int) * numNodes);
	for (int i = 0; i < numNodes; i++) {
		distanceArray[i] = paraPtr->weight[source][i];
		parentArray[i] = source;
		visitArray[i] = 0;
	}
	distanceArray[source] = 0;
	parentArray[source] = -1;
	visitArray[source] = 1;
	tempBestNode = -1;
	for (int i = 0; i < numNodes - 1; i++) {
		minDistance = N;
		for (int j = 0; j < numNodes; j++) {
			if (visitArray[j])
				continue;
			if (minDistance > distanceArray[j]) {
				minDistance = distanceArray[j];
				tempBestNode = j;
			}
		}
		visitArray[tempBestNode] = 1;
		for (int j = 0; j < numNodes; j++) {
			if (visitArray[j] || paraPtr->weight[tempBestNode][j] >= N)
				continue;
			if (!paraAlgorithm) {
				if (distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weight[tempBestNode][j]) {
					distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weight[tempBestNode][j];
					parentArray[j] = tempBestNode;
				}
			}
			else {
				if (distanceArray[j] > paraPtr->weight[tempBestNode][j]) {
					distanceArray[j] = paraPtr->weight[tempBestNode][j];
					parentArray[j] = tempBestNode;
				}
			}
		}
	}
	printf("the parent of each node: ");
	for (int i = 0; i < numNodes; i++) {
		printf("%d, ", parentArray[i]);
	}
	if (paraAlgorithm == 0) {
		printf("From node 0, path length to all nodes are: ");
		for (int i = 0; i < numNodes; i++) {
			printf("%d (%d), ", i, distanceArray[i]);
		}
	}
	else {
		resultCost = 0;
		for (int 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 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 (int i = 0; i < numNodes; i++) {
		tempPtr[i] = (int*)malloc(numNodes * sizeof(int));
	}
	for (int i = 0; i < numNodes; i++) {
		for (int j = 0; j < numNodes; j++) {
			if (myGraph[i][j] == 0) {
				tempPtr[i][j] = N;
			}
			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 0;
}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值