最短路径算法是一个经典且实用的算法。最短路径应用于各个导航软件之中,高德地图,百度地图等。那么怎么才能通过代码得出最短路径呢。
要知道最短路径算法,是一步一步把起始点和各个点的最短路径全部算出来的。
构建完图之后,从起始点出发,遍历起始点为头的边,将每个节点的前身和路径和存储下来。
比如从V0 出发,先将v0的visited改为true。假设V0和V2,V3,V4相连,则V2,V3,V4的前身就是V0,路径就是各个边的权值,其他没有与他相连的边的路径和可以记为正无穷(至于为什么记为正无穷后面会提到)。然后从visited为false的点中找到路径和最短(小)的一个点,将他和他的前身相连,然后把这个点visited改为true;此时就可以从这个新的点出发,计算与他相连的点的路径和,而这个路径和等于这个点原本的路径和加上与他相连的边的权值。如果新的路径和的值比原来的值小就把以小的值替换掉原来的那个值。(这就是为什么一开始要记为正无穷)。这样又有一组新的路径和,找出最小的顶点,重复上面的步骤直到visited都为true;此时每个节点的路径和就为起始点到这个节点的最短路径。
下面是代码
#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
下面是结果展示
这里将1000当做正无穷,给出每次连接节点的结果,最后得到每个节点路径的最小值