1.代码如下
#include <stdio.h>
#include <malloc.h>
#define MAX_DISTANCE 10000
/*
** the structure of the net
*/
typedef struct Net{
int** weights;
int numNodes;
} Net,*NetPtr;
/*
** initialize a net
*/
NetPtr initNet(int paraSize, int** paraData)
{
int i,j;
NetPtr resultPtr=(NetPtr)malloc(sizeof(Net));
resultPtr->numNodes=paraSize;
resultPtr->weights=(int**)malloc(sizeof(int*)*paraSize);
for(i=0;i<paraSize;i++)
{
resultPtr->weights[i]=(int*)malloc(sizeof(int)*paraSize);
for(j=0;j<paraSize;j++)
{
resultPtr->weights[i][j]=paraData[i][j];
}
}
return resultPtr;
}
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm)
{
int i,j;
int source=0;
int numNodes=paraPtr->numNodes;
int* distanceArray=(int*)malloc(sizeof(int)*numNodes);
int* parentArray=(int*)malloc(sizeof(int)*numNodes);
int* visitedArray=(int*)malloc(sizeof(int)*numNodes);
//initialize
for(i=0;i<numNodes;i++)
{
distanceArray[i]=paraPtr->weights[source][i];
parentArray[i]=source;
visitedArray[i]=0;
}
parentArray[source]=-1;//no parent nodes
distanceArray[source]=0;//the distance from itself is zero
visitedArray[source]=1;//1 stand for that the node is visited
int tempBestNode=-1;
int resultCost;
for(i=0;i<numNodes-1;i++)
{
//find the next best node
int 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++)
{
//ignore the nodes visited
if(visitedArray[j])
{
continue;
}
//ignore the nodes unconnected. MAX_DISTANCE means no connection;
if(paraPtr->weights[tempBestNode][j]>=MAX_DISTANCE)
{
continue;
}
//paraAlgorithm = 0 --> dijkstra,eles --> prim
if(paraAlgorithm==0)
{
if(distanceArray[j]>distanceArray[tempBestNode]+paraPtr->weights[tempBestNode][j])
{
//if the distance from a to c is larger than a to tempbestnode(the node which has the shortest distance from a)to c
//then change the distance recorded
distanceArray[j]=distanceArray[tempBestNode]+paraPtr->weights[tempBestNode][j];
parentArray[j]=tempBestNode;
}
}
else
{
if(distanceArray[j]>paraPtr->weights[tempBestNode][j])
{
//find out the shorest side to node 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;
}
/**
* construct a sample net
*/
NetPtr constructSampleNet()
{
int i,j;
int numNodes=6;
int** tempPtr=(int**)malloc(sizeof(int*)*numNodes);
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},
};
printf("Preparing data\r\n");
for(i=0;i<numNodes;i++)
{
tempPtr[i]=(int*)malloc(sizeof(int)*numNodes);
}
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 0;
}
2.图示
3.代码说明
1)用0来说明结点与结点间无连接,距离换为MAX_SIZE,即10000(结点到自身的距离也为0,此时也换为10000,distanceArray[source]=0这句代码会使到自身的距离为0)
2)用parentArray数组来记录它的前一个结点,用visitedArray数组来标记一个结点是否已经被访问
3)dijkstra算法是求并且记录到结点的最短距离,Prim算法则是求并记录最短边,这两个算法的代码竟然可以只有几行不同
4.运行结果
Preparing data
Data ready
====Dijkstra algorithm====
the parent of each node: -1, 0, 0, 0, 2, 2, From node 0, path length to all nodes are: 0 (0), 1 (6), 2 (1), 3 (5), 4 (7), 5 (5),
====Prim algorithm====
the parent of each node: -1, 2, 0, 5, 1, 2, cost of node 0 is 0, total = 0
cost of node 1 is 5, total = 5
cost of node 2 is 1, total = 6
cost of node 3 is 2, total = 8
cost of node 4 is 3, total = 11
cost of node 5 is 4, total = 15
Finally, the total cost is 15.
--------------------------------
Process exited after 0.02702 seconds with return value 0
请按任意键继续. . .