数据结构-Prim算法与 Dijkstra 算法

本文介绍了图论中的两种经典算法——迪杰斯特拉算法和普里姆算法。迪杰斯特拉算法用于寻找加权有向图中从指定起点到所有其他顶点的最短路径,而普里姆算法则用于找到加权连通图的最小生成树。文中提供了C语言实现这两种算法的示例代码,并通过一个样例网络进行测试,展示了算法的运行过程和结果。
摘要由CSDN通过智能技术生成

里姆算法Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树。意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小

迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法策略,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。

#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) {
	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 = %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;
			} 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;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值