Prim 算法与 Dijkstra 算法

文章介绍了Prim算法和Dijkstra算法在图论中的应用。Prim算法用于构建最小生成树,从一个顶点开始逐步添加边,确保不形成回路且总权重最小。Dijkstra算法则用于找到单源最短路径,通过不断扩展已知最短路径的顶点集来更新其他顶点的最短路径。代码示例展示了如何实现这两个算法,并在邻接矩阵表示的图上进行操作。
摘要由CSDN通过智能技术生成

Prim 算法:

用于构建最小生成树——即树中所有边的权值之和最小。

①任取一顶点(或题中已告知顶点),去掉所有边

②选择一个与当前顶点集合距离最近的顶点,并将该顶点和相应的边加入进来,同时不形成回路

③重复②,直至图中所有顶点都并入。

 Dijkstra 算法: 

用于构建单元点的最短路径树——即树中某个点到任何其他点的距离都是最短的。

求解过程:
对于网N=(V,E),将N中的顶点分成两组。
第一组S:已求出的最短路径的终点集合(初始时只包含源点v)。
第二组V-S:尚未求出的最短路径的顶点集合(初始时为V-{Vo})。
算法将按各顶点与v间最短路径长度递增的次序,逐个将集合V-S中的顶点加入集合S中去。在这个过程中,总保持从到集合S中各顶点的路径长度始终不大于到集合V-S中各顶点的路径长度。
 

代码:

#include <stdio.h>
#include <cstdlib>
 
#define max_size 10000
 
//二维数组,即邻接矩阵
typedef struct Net {
	int** weights;
	int numNodes;
}*NetPtr;
 
//给定一个数组,根据这个数组初始化邻接矩阵
NetPtr initNet(int paraSize, int** paraData) {
	int i, j;
	NetPtr resultPtr = new Net;
	resultPtr->numNodes = paraSize;
	resultPtr->weights = (int**)malloc(paraSize * sizeof(int*));
	for (i = 0; i < paraSize; i++) {
		if (resultPtr->weights != NULL)
			resultPtr->weights[i] = new int[paraSize];
		for (j = 0; j < paraSize; j++) {
			if (resultPtr->weights != NULL)
				resultPtr->weights[i][j] = paraData[i][j];
		}
	}
 
	return resultPtr;
}
 
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {
	int i, j, minDistance = 0, tempBestNode, resultCost = 0;
	int source = 0;
	int numNodes = paraPtr->numNodes;
	int* distanceArray = (int*)malloc(numNodes * sizeof(int) + 4);
	int* parentArray = (int*)malloc(numNodes * sizeof(int) + 4);
	int* visitedArray = (int*)malloc(numNodes * sizeof(int) + 4);
 
	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_size;
		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_size) {
				continue;
			}
 
			//Dijkstra algorithm
			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] = distanceArray[j] = paraPtr->weights[tempBestNode][j];
					// 改变父节点
					parentArray[j] = tempBestNode;
				}
			}
		}
 
		printf("the parent of each node: ");
		//Prim algorithm
		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\n");
 
		tempPtr = (int**)malloc(numNodes * sizeof(int*));
		for (i = 0; i < numNodes; i++) {
			if (tempPtr != NULL)
				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_size;
				}
				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=====\n");
		dijkstraOrPrim(tempNetPtr, 0);
		printf("====Prim algorithm====\n");
		dijkstraOrPrim(tempNetPtr, 1);
	}
 
int main(){
		testPrim();
		return 1;
	}

结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值