Dijkstra算法

算法简介:是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。

算法特点:从起始点开始,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。

具体步骤:

设置两点顶点的集合U和T,集合U中存放已找到最短路径的顶点,集合T中存放当前还未找到的最短路径的顶点。

初始状态时,集合U中只包含源点,设为v0 。

然后从集合T TT中选择到源点v0路径长度最短的顶点u加入到集合U中。

集合U中每加入一个新的顶点u都要修改源点v0 带集合T中剩余顶点的当前最短路径值,集合T中各顶点的新的当前最短路径长度值,为原来的当前最短路径长度值与从源点过顶点u到达该顶点的带权路径长度中的较小者。

回到3,此过程不断重复,直到集合T中的顶点全部加入到集合U中为止。
 

代码如下:

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

#define MAX_DISTANCE 1000
/**
 * Int matrix multiplex.
 * The dimension of the first matrix is [paraM][paraN]
 * The dimension of the second matrix is [paraN][paraK]
 */
int** matrixMultiplex(int** paraFirstMatrix, int** paraSecondMatrix, int paraM, int paraN, int paraK)
{
	int i, j, k; //Cope with different compilers.
	//Step 1. Allocate space.
	int** resultMatrix = (int**)malloc(sizeof(int*) * paraM);
	for (i = 0; i < paraM; i++)
	{
		resultMatrix[i] = (int*)malloc(sizeof(int) * paraN);
	}//Of for i

	//Step 2. Now multiply.
	for (i = 0; i < paraM; i++)
	{
		for (k = 0; k < paraK; k++)
		{
			resultMatrix[i][k] = 0; //Initialize
			for (j = 0; j < paraN; j++) {
				resultMatrix[i][k] += paraFirstMatrix[i][j] * paraSecondMatrix[j][k];
			}//Of for j
		}//Of for k
	}//Of for i

	return resultMatrix;
}//Of matrixMultiplex

/**
 * Output the array.
 */
void outputArray(int* paraArray, int paraSize)
{
	for (int i = 0; i < paraSize; i++)
	{
		printf("%d ", paraArray[i]);
	}//Of for i

	printf("\r\n");
}//Of outputArray

/**
 * Output the matrix.
 */
void outputMatrix(int** paraMatrix, int paraM, int paraN)
{
	for (int i = 0; i < paraM; i++)
	{
		printf("\r\n");
		for (int j = 0; j < paraN; j++)
		{
			printf("%d ", paraMatrix[i][j]);
		}//Of for j
	}//Of for i
	printf("\r\n");
}//Of outputMatrix

/**
 * Generate a matrix with the given rows, columns and vaule.
 */
int** generateMatrix(int paraRows, int paraColumns, int paraValue)
{
	int** resultMatrix = (int**)malloc(sizeof(int*) * paraRows);
	for (int i = 0; i < paraRows; i++)
	{
		resultMatrix[i] = (int*)malloc(sizeof(int) * paraColumns);
		for (int j = 0; j < paraColumns; j++)
		{
			resultMatrix[i][j] = paraValue;
		}//Of for j
	}//Of for i

	return resultMatrix;
}//Of generateMatrix

/**
 * Get a sample adjacent matrix.
 */
int** generateSampleAdjacentMatrix()
{
	int** resultMatrix = generateMatrix(5, 5, 0);

	resultMatrix[0][1] = 1;
	resultMatrix[0][2] = 1;
	resultMatrix[1][0] = 1;
	resultMatrix[2][0] = 1;
	resultMatrix[2][3] = 1;
	resultMatrix[3][2] = 1;
	resultMatrix[3][4] = 1;
	resultMatrix[4][3] = 1;

	return resultMatrix;
}//Of generateSampleAdjacentMatrix

/**
 * Shortest path.
 */
void shortestPath(int** paraNet, int paraSize, int paraStart)
{
	//Step 1. Initialize.
	int i, j;
	int* tempParentArray = (int*)malloc(sizeof(int) * paraSize);
	int* tempDistanceArray = (int*)malloc(sizeof(int) * paraSize);
	bool* tempVisitedArray = (bool*)malloc(sizeof(bool) * paraSize);
	int tempBestNode;
	for (i = 0; i < paraSize; i++)
	{
		tempParentArray[i] = paraStart;
		tempDistanceArray[i] = paraNet[paraStart][i];
		tempVisitedArray[i] = false;
	}//Of for i
	tempParentArray[paraStart] = -1;
	tempDistanceArray[paraStart] = 0;
	tempVisitedArray[paraStart] = true;

	printf("The parent array: ");
	outputArray(tempParentArray, paraSize);
	printf("The distance array: ");
	outputArray(tempDistanceArray, paraSize);

	//Step 2. Now compute.
	int tempMinDistance;
	for (i = 0; i < paraSize - 1; i++)
	{
		//Step 2.1 Find out the best next node.
		tempMinDistance = MAX_DISTANCE;
		for (j = 0; j < paraSize; j++)
		{
			//This node is visited.
			if (tempVisitedArray[j])
			{
				continue;
			}//Of if

			if (tempMinDistance > tempDistanceArray[j])
			{
				tempMinDistance = tempDistanceArray[j];
				tempBestNode = j;
			}//Of if
		}//Of for j

		tempVisitedArray[tempBestNode] = true;

		//Step 2.2 Prepare for the next round.
		for (j = 0; j < paraSize; j++)
		{
			//This node is visited.
			if (tempVisitedArray[j])
			{
				continue;
			}//Of if

			//This node cannot be reached.
			if (paraNet[tempBestNode][j] >= MAX_DISTANCE)
			{
				continue;
			}//Of if

			if (tempDistanceArray[j] > tempDistanceArray[tempBestNode] + paraNet[tempBestNode][j])
			{
				//Change the distance.
				tempDistanceArray[j] = tempDistanceArray[tempBestNode] + paraNet[tempBestNode][j];
				//Change the parent.
				tempParentArray[j] = tempBestNode;
			}//Of if
		}//Of for j

		//For test
		printf("The parent array: ");
		outputArray(tempParentArray, paraSize);
		printf("The distance array: ");
		outputArray(tempDistanceArray, paraSize);

	}//Of for i

	printf("Finally.\r\nThe parent array: ");
	outputArray(tempParentArray, paraSize);
	printf("The distance array: ");
	outputArray(tempDistanceArray, paraSize);
}//Of shortestPath

/**
 * Get a sample adjacent matrix.
 */
int** generateSampleNet()
{
	int** resultMatrix = generateMatrix(5, 5, MAX_DISTANCE);

	resultMatrix[0][1] = 1;
	resultMatrix[0][2] = 1;
	resultMatrix[1][0] = 1;
	resultMatrix[2][0] = 1;
	resultMatrix[2][3] = 1;
	resultMatrix[3][2] = 1;
	resultMatrix[3][4] = 1;
	resultMatrix[4][3] = 1;

	return resultMatrix;
}//Of generateSampleNet

/**
 * Test functions.
 */
void main()
{
	printf("Hello, world!\r\n");

	int** tempMatrix1 = generateSampleAdjacentMatrix();

	outputMatrix(tempMatrix1, 5, 5);

	int** tempMatrix2 = matrixMultiplex(tempMatrix1, tempMatrix1, 5, 5, 5);

	outputMatrix(tempMatrix2, 5, 5);

	int** tempMatrix3 = matrixMultiplex(tempMatrix2, tempMatrix1, 5, 5, 5);

	outputMatrix(tempMatrix3, 5, 5);

	//Shortest path test.
	int** tempNet = generateSampleNet();
	shortestPath(tempNet, 5, 0);
}//Of main

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值