数据结构18-Prim算法和Dijkstra算法

Prim算法和Dijkstra算法的区别:


        Prim算法解决连通无向有权图中最小生成树问题,而Dijkstra算法解决源点到目标点的最短路径问题。Prim算法中,“距离最短”是指未访问的结点到已经访问的所有结点距离最小,即将已经访问的结点视为一个整体,将距离最小的结点加入到已访问的集合中;而在Dijkstra算法中,“距离最短”是指所有未访问结点(通过已访问的结点)到源点距离最小。

在代码实现上这两者区别不大

if(pAlgorithm == 0){//D算法 
				if(distanceArray[j] > distanceArray[tempBestNode] + pPtr->weights[tempBestNode][j]){
					distanceArray[j] = distanceArray[tempBestNode] + pPtr->weights[tempBestNode][j];
					parentArray[j] = tempBestNode;
				} 
			}else{//P算法 
				if(distanceArray[j] > pPtr->weights[tempBestNode][j]){
					distanceArray[j] = pPtr->weights[tempBestNode][j];
					parentArray[j] = tempBestNode;
				}

完整代码

#include <stdio.h>
#include <malloc.h>

#define MAXDISTANCE 10000

typedef struct Net{
	int** weights;
	int numNodes;
}*NetPtr;

NetPtr iniNet(int pSize,int** pData){
	int i,j;
	NetPtr resultPtr = (NetPtr)malloc(sizeof(Net));
	resultPtr->numNodes = pSize;
	
	resultPtr->weights = (int**)malloc(pSize * sizeof(int*));
	for(i = 0;i < pSize;i++){
		resultPtr->weights[i] = (int*)malloc(pSize * sizeof(int));
		for(j = 0;j < pSize;j++){
			resultPtr->weights[i][j] = pData[i][j];
		}
	}
	
	return resultPtr;
}

int dijkstraOrPrim(NetPtr pPtr,int pAlgorithm){
	int i,j,miniDistance,tempBestNode,resultCost;
	int source = 0;
	int numNodes = pPtr->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] = pPtr->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++){
		miniDistance = MAXDISTANCE;
		for(j = 0;j < numNodes;j++){
			if(visitedArray[j]){
				continue;
			}
			
			if(miniDistance > distanceArray[j]){
				miniDistance = distanceArray[j];
				tempBestNode = j;
			}
		}
		
		visitedArray[tempBestNode] = 1;
		
		for(j = 0;j < numNodes;j++){
			if(visitedArray[j]){
				continue;
			}
			
			if(pPtr->weights[tempBestNode][j] >= MAXDISTANCE){
				continue;
			}
			
			if(pAlgorithm == 0){//D算法 
				if(distanceArray[j] > distanceArray[tempBestNode] + pPtr->weights[tempBestNode][j]){
					distanceArray[j] = distanceArray[tempBestNode] + pPtr->weights[tempBestNode][j];
					parentArray[j] = tempBestNode;
				} 
			}else{//P算法 
				if(distanceArray[j] > pPtr->weights[tempBestNode][j]){
					distanceArray[j] = pPtr->weights[tempBestNode][j];
					parentArray[j] = tempBestNode;
				}
			}
		}
	}
	
	printf("the parent of each node: ");
	for (i = 0; i < numNodes; i++) {
		printf("%d, ", parentArray[i]);
	}
	
	if(pAlgorithm == 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);
	}
	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] = MAXDISTANCE;
			} else {
				tempPtr[i][j] = myGraph[i][j];
			}
		}
	}
 
	printf("Data ready\r\n");
	
	NetPtr resultNetPtr = iniNet(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;
}

运行结果

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值