在PAT程序设计题目中看到图数据结构相关的,特地学习了图的创建和最小生成树的方法(Prim算法)。
图的创建,首先是结构体:
typedef struct tagGraph
{
VertexType point[MatrixSize]; //顶点value数组
WeightType matrix[MatrixSize][MatrixSize]; //路径权重矩阵表
int vertexNumber; //顶点数量
int edgeNumber; //路径数量
}Graph,*pGraph;
VertexType是int类型,WeightType是int类型;point数组代表每个顶点的存储数据值大小,matrix二维数组存储着所有顶点之间的路径距离,其中,同个顶点视为距离为0,没有连接的两个点距离视为无穷大(65535表示)。vertexNumber代表顶点数量,也是矩阵二维数组的宽和高,edgeNumber代表存在有效路径的数量。
最小生成树算法里,有两个临时数组shortestCost、mst,shortestCost数组存储了当前生成树此时距离各个未加入到生成树的顶点的最短距离,对于已加入生成树的顶点,最短距离均是0(代表无效),比如生成树初始建立只有一个顶点0时,shortestCost[3]代表的就是顶点0到顶点3的距离,如果生成树不断壮大,顶点1、2都被加进来,那么shortestCost[3]代表顶点0、1、2中离3最近的那个点到3号顶点的距离,如果他们三个都没有和3直接连线,那么暂时使用无穷大65535表示,等待以后更新。
mst数组则存储了那些拥有最短距离的顶点的序号,上个例子里,mst[3]代表顶点0、1、2的生成树里离顶点3最近的那个顶点序号(可能是0、1、2的任一个),如果他们三个都没有和3直接连线,那么mst[3]暂时使用初始值0,等待以后更新。
随着生成树不断壮大,最后生成树收纳所有的点,两个数组也不断的更新,最短路径的结果也得到了。
代码实现如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MatrixSize 100
#define INFINITE 65535
typedef int WeightType;
typedef int VertexType;
typedef struct tagGraph
{
VertexType point[MatrixSize]; //顶点value数组
WeightType matrix[MatrixSize][MatrixSize]; //路径权重矩阵表
int vertexNumber; //顶点数量
int edgeNumber; //路径数量
}Graph,*pGraph;
int CreateGraph( Graph* p)
{
int v1,v2,distance;
for ( int i = 0; i < p->vertexNumber; i++ )
{
for ( int j = 0; j < p->vertexNumber; j++ )
{
if ( i == j ) p->matrix[i][j] = 0;
else p->matrix[i][j] = INFINITE;
}
}
for ( int i = 0; i < p->edgeNumber; i++ )
{
printf("Please Input the pair of two cities and their Distance:\n");
getchar();
scanf("%d %d %d", &v1, &v2, &distance );
p->matrix[v1][v2] = distance;
p->matrix[v2][v1] = distance;
}
return 0;
}
//最小生成树Prim算法
int CalcPath( Graph* p)
{
//当前生成树到各个点的最短直达距离数组
int* shortestCost = new int[p->vertexNumber];
//最短直达距离的起点序号数组(当前生成树中每个可以做到最短直达距离的那个点)
int* mst = new int[p->vertexNumber];
memset( shortestCost, 0, sizeof(int)*(p->vertexNumber) );
memset( mst, 0, sizeof(int)*(p->vertexNumber) );
for ( int i = 0; i < p->vertexNumber; i++ )
{
shortestCost[i] = p->matrix[0][i];
mst[i] = 0;
}
for ( int i = 1; i < p->vertexNumber; i++ )
{
int shortestLength = INFINITE;
int shortestId = 0;
//先在shortestCost中选出最小的
for ( int j = 1; j < p->vertexNumber; j++ )
{
if ( shortestCost[j] < shortestLength && shortestCost[j] > 0 )
{
shortestLength = shortestCost[j];
shortestId = j;
}
}
printf("V%d-->V%d = %d\n", mst[shortestId], shortestId, shortestLength );
shortestCost[shortestId] = 0;
for ( int k = 1; k < p->vertexNumber; k++ )
{
if ( p->matrix[shortestId][k] < shortestCost[k] && p->matrix[shortestId][k] > 0 )
{
shortestCost[k] = p->matrix[shortestId][k];
mst[k] = shortestId;
}
}
}
delete []shortestCost;
delete []mst;
return 0;
}
int PrintMatrixResult( Graph* p )
{
for ( int i = 0; i < p->vertexNumber; i++ )
{
for ( int j = 0; j < p->vertexNumber; j++ )
{
printf( "%d ",p->matrix[i][j] );
}
printf("\n");
}
return 0;
}
void ParseValueOfPoint( Graph* graph, char* buf )
{
int index = 0;
char buffer[200] = {0};
strcpy( buffer, buf );
char* p = buffer;
p = strtok( buffer, " " );
while ( p != NULL )
{
graph->point[index++] = atoi( p );
p = strtok( NULL, " " );
}
}
int main()
{
int currentCity = 0;
int destCity = 0;
char buffer[200] = {0};
Graph graph;
printf("Please Input the Number of Cityies/the Number of path/\nyour Current City/your Destination:\n");
scanf("%d %d %d %d", &graph.vertexNumber, &graph.edgeNumber, ¤tCity, &destCity );
printf("Please Input the Number of Emergency Rescue in Every City:\n");
getchar();
scanf("%[^\n]",buffer);
ParseValueOfPoint( &graph, buffer );
CreateGraph(&graph);
PrintMatrixResult(&graph);
CalcPath(&graph);
printf("\n");
return 0;
}