#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
12-10
1178
12-09
821
12-10
2565