#include<stdio.h>//包含图文件及图的类型定义
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef char VertexType[4];
typedef char InfoPtr;
typedef int VRType;
#define INFINITY 100000//定义一个无限大的值
#define MAXSIZE 100//最大顶点个数
typedef int PathMatrix[MAXSIZE][MAXSIZE];//定义一个保存最短路径的二维数组
typedef int ShortPathLength[MAXSIZE];//定义一个保存从顶点v0到顶点v的最短距离数组
typedef enum {DG,DN,UG,UN} GraphKind;//图的类型:有向图,有向网,无向图,无向网
typedef struct
{
VRType adj;//对于无权图,用1表示相邻,0表示不相邻;对于带权图,存储权值
InfoPtr *info;//与弧或边相关的信息
}ArcNode,AdjMatrix[MAXSIZE][MAXSIZE];
typedef struct//图的类型定义
{
VertexType vex[MAXSIZE];//用于存储顶点
AdjMatrix arc;//邻接矩阵,存储边或弧的信息
int vexnum,arcnum;//顶点数和边的数目
GraphKind kind;//图的类型
}MGraph;
typedef struct//添加一个存储网的行、列和权值的类型定义
{
int row;
int col;
int weight;
}GNode;
void CreateGraph(MGraph *N,GNode *value,int vnum,int arcnum,VertexType *ch);
void DisplayGraph(MGraph N);
void Dijkstra(MGraph N,int v0,PathMatrix path,ShortPathLength dist);
void main()
{
int i,vnum=6, arcnum=9;
MGraph N;
GNode value[]={{0,1,30},{0,2,60},{0,4,150},{0,5,40},{1,2,40},{1,3,100},{2,3,50},{3,4,30}
,{4,5,10}};
VertexType ch[]={"v0","v1","v2","v3","v4","v5"};
PathMatrix path;//用二维数组存放最短路径所经过的顶点
ShortPathLength dist;//用一维数组存放最短路径长度
CreateGraph(&N, value, vnum, arcnum, ch);//创建有向网
DisplayGraph(N);//输出有向网
Dijkstra(N,0,path,dist);
printf("%s到各顶点的最短路径长度:\n",N.vex[0]);
for(i=0;i<N.vexnum;i++)
if(i!=0)
printf("%s-%s:%d\n",N.vex[0],N.vex[i],dist[i]);
}
void CreateGraph(MGraph *N,GNode *value,int vnum,int arcnum,VertexType *ch)
//采用邻接矩阵表示法创建有向网
{
int i, j, k, w, InfoFlag, len;
char s[MAXSIZE];
VertexType v1,v2;
N->vexnum=vnum;
N->arcnum=arcnum;
for(i=0;i<vnum;i++)//将各个顶点赋值给vex域
strcpy(N->vex[i],ch[i]);
for(i=0;i<N->vexnum;i++)//初始化邻接矩阵
for(j=0;j<N->vexnum;j++)
{
N->arc[i][j].adj=INFINITY;
N->arc[i][j].info=NULL;//弧的初始化信息为空
}
for(k=0;k<arcnum;k++)
{
i=value[k].row;
j=value[k].col;
N->arc[i][j].adj=value[k].weight;
}
N->kind =DN;//图的类型为有向网
}
void DisplayGraph(MGraph N)//输出邻接矩阵存储表示的图N
{
int i,j;
printf("有向网具有%d个顶点%d条弧,顶点依次是:",N.vexnum,N.arcnum);
for(i=0;i<N.vexnum;++i)//输出网的顶点
printf("%s",N.vex[i]);
printf("\n有向网N的:\n");
printf("序号i=");
for(i=0;i<N.vexnum;i++)
printf("%8d",i);
printf("\n");
for(i=0;i<N.vexnum;i++)
{
printf("%8d",i);
for(j=0;j<N.vexnum;j++)
printf("%8d",N.arc[i][j].adj);
printf("\n");
}
}
void Dijkstra(MGraph N,int v0,PathMatrix path,ShortPathLength dist)
{
int v , w ,i ,k, min;
int final[MAXSIZE];
for(v=0;v<N.vexnum ;v++)
{
final[v] = 0;
dist[v]=N.arc [v0][v].adj ;
for(w=0;w<N.vexnum ;w++)
path[v][w] = 0;
if(dist[v]<INFINITY)
{
path[v][v0] = 1;
path[v][v] = 1;
}
}
dist[v0] = 0;
final[v0] = 1;
for(i = 1; i<N.vexnum ;i++ )
{
min = INFINITY;
for(w=0;w<N.vexnum ;w++)
if(!final[w] && dist[w]<min)
{
v=w;
min = dist[w];
}
final[v]=1;
for(w=0;w<N.vexnum ;w++)
if(!final[w] && min<INFINITY && N.arc[v][w].adj < INFINITY && (min+N.arc[v][w].adj <dist[w]))
{
dist[w] = min + N.arc[v][w].adj ;
for(k=0;k<N.vexnum ;k++)
{
path[w][k] = path[v][k];
}
path[w][w]=1;
}
}
}
最短路径(迪杰斯特拉算法)
最新推荐文章于 2022-03-19 16:01:13 发布