(1)从某个源点到其余各顶点的最短路径
Dijkstra的代码如下:
头文件:
#defineINFINITY10000
#defineMAX_VERTEX_NUM20
typedefintInfoType;
typedefcharVertexType;
typedefintVRType;

typedefenum...{DG,DN,UDG,UDN}GraphKind;

typedefstructArcCell...{
VRTypeadj;
InfoType*info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedefstruct...{
VertexTypevexs[MAX_VERTEX_NUM];
AdjMatrixarcs;
intvexnum,arcnum;
GraphKindkind;
}MGraph;

intweight[][6]=...{
...{0,INFINITY,10,INFINITY,30,100},
...{INFINITY,0,5,INFINITY,INFINITY,INFINITY},
...{INFINITY,INFINITY,0,50,INFINITY,INFINITY},
...{INFINITY,INFINITY,INFINITY,0,INFINITY,10},
...{INFINITY,INFINITY,INFINITY,20,0,60},
...{INFINITY,INFINITY,INFINITY,INFINITY,INFINITY,0}
};
//#defineINPUT1
源文件:
#include"graph_dj.h"
#include"stdio.h"
#include"stdlib.h"

voidcreateDG(MGraph&g)...{}
voidcreateDN(MGraph&g)...{}
voidcreateUDG(MGraph&g)...{}

intlocate(MGraphg,charname)...{
for(inti=0;i<g.vexnum;i++)...{
if(name==g.vexs[i])...{
returni;
}
}
return-1;
}

voidcreateUDN(MGraph&g)...{
#ifdefINPUT
printf("inputthenumberofvertexandarcs: ");
scanf("%d%d",&g.vexnum,&g.arcnum);
fflush(stdin);
#else
printf("inputthenumberofvertex: ");
scanf("%d",&g.vexnum);
fflush(stdin);
#endif
inti=0,j=0,k=0;
printf("inputthenameofvertex: ");

for(i=0;i<g.vexnum;i++)...{
scanf("%c",&g.vexs[i]);
fflush(stdin);
}

for(i=0;i<g.vexnum;i++)...{
for(j=0;j<g.vexnum;j++)...{
g.arcs[i][j].adj=INFINITY;
g.arcs[i][j].info=NULL;
}
}
#ifdefINPUT
charv1,v2;
intw;
printf("inputthe%darcsv1v2andweight: ",g.arcnum);
for(k=0;k<g.arcnum;k++)...{
scanf("%c%c%d",&v1,&v2,&w);
fflush(stdin);
i=locate(g,v1);
j=locate(g,v2);
g.arcs[i][j].adj=w;
}
#else
printf("nowconstructingthematrixofgraph... ");
for(i=0;i<g.vexnum;i++)...{
for(j=0;j<g.vexnum;j++)...{
g.arcs[i][j].adj=weight[i][j];
}
}
#endif
}

voidprintGraph(MGraphg)...{
for(inti=0;i<g.vexnum;i++)...{
for(intj=0;j<g.vexnum;j++)...{
printf("%d ",g.arcs[i][j].adj);
}
printf(" ");
}
}

voidcreateGragh(MGraph&g)...{
printf("pleaseinputthetypeofgraph: ");
scanf("%d",&g.kind);

switch(g.kind)...{
caseDG:
createDG(g);
break;
caseDN:
createDN(g);
break;
caseUDG:
createUDG(g);
break;
caseUDN:
createUDN(g);
printGraph(g);
break;
}
}

voiddijkstra(MGraphg)...{
int*dist=(int*)malloc(sizeof(int)*g.vexnum);
int*parent=(int*)malloc(sizeof(int)*g.vexnum);
int*final=(int*)malloc(sizeof(int)*g.vexnum);
inti,j,k,cost;
//initthearrays
for(i=0;i<g.vexnum;i++)...{
dist[i]=g.arcs[0][i].adj;
parent[i]=0;
final[i]=0;
}
//jointhestart0.
final[0]=1;

for(i=1;i<g.vexnum;i++)...{
//gettheminofdist
cost=INFINITY;
k=-1;
for(j=1;j<g.vexnum;j++)...{
if(dist[j]<cost&&final[j]==0)...{
cost=dist[j];
k=j;
}
}

if(k>0)...{
//jointheknode
final[k]=1;
//updatethedist
for(intt=1;t<g.vexnum;t++)...{
if(final[t]==0)...{
if(dist[t]>dist[k]+g.arcs[k][t].adj)...{
dist[t]=dist[k]+g.arcs[k][t].adj;
parent[t]=k;
}
}
}
}
}
//printthedistandparentarray
printf("theshortestpathfrom%cisasfollows: ",g.vexs[0]);
for(i=0;i<g.vexnum;i++)...{
printf("%cvia%c'sdistis%d ",
g.vexs[i],g.vexs[parent[i]],dist[i]);
}
}

voidmain()...{
MGraphg;
createGragh(g);
dijkstra(g);
}
程序的执行结果如下:
pleaseinputthetypeofgraph:
3
inputthenumberofvertex:
6
inputthenameofvertex:
a
b
c
d
e
f
nowconstructingthematrixofgraph...
010000101000030100
1000005100001000010000
10000100000501000010000
10000100001000001000010
10000100001000020060
10000100001000010000100000
theshortestpathfromaisasfollows:
aviaa'sdistis0
bviaa'sdistis10000
cviaa'sdistis10
dviae'sdistis50
eviaa'sdistis30
fviad'sdistis60
Pressanykeytocontinue
说明:Dijkstra算法的复杂度是O(n * n)。
(2)每一对顶点之间的最短路径
方法一:对每个顶点依次运行Dijkstra,复杂度O(n*n*n)。
方法二:floyd,复杂度O(n*n*n)。
头文件修改:

intweight[][3]=...{
...{0,4,11},
...{6,0,2},
...{3,INFINITY,0}
};

voidfloyd(MGraphg)...{
intdist[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
intparent[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
//initthearrays
inti,j;
for(i=0;i<g.vexnum;i++)...{
for(j=0;j<g.vexnum;j++)...{
dist[i][j]=g.arcs[i][j].adj;
parent[i][j]=i;
}
}
//computetheshortestpath
for(intk=0;k<g.vexnum;k++)...{
for(i=0;i<g.vexnum;i++)...{
for(j=0;j<g.vexnum;j++)...{
if(i!=j&&i!=k&&j!=k)...{
if(dist[i][j]>dist[i][k]+dist[k][j])...{
dist[i][j]=dist[i][k]+dist[k][j];
parent[i][j]=k;
}
}
}
}
}
//printtheshortestpathandparent
for(i=0;i<g.vexnum;i++)...{
for(j=0;j<g.vexnum;j++)...{
printf("%d(%c) ",dist[i][j],g.vexs[parent[i][j]]);
}
printf(" ");
}
}

voidmain()...{
MGraphg;
createGragh(g);
dijkstra(g);
floyd(g);
}
程序的运行结果如下:
pleaseinputthetypeofgraph:
3
inputthenumberofvertex:
3
inputthenameofvertex:
a
b
c
nowconstructingthematrixofgraph...
0411
602
3100000
theshortestpathfromaisasfollows:
aviaa'sdistis0
bviaa'sdistis4
cviab'sdistis6
0(a)4(a)6(b)
5(c)0(b)2(b)
3(c)7(a)0(c)
Pressanykeytocontinue
Dijkstra与Floyd算法详解
本文详细介绍了图论中的两种经典最短路径算法:Dijkstra算法和Floyd算法。Dijkstra算法用于寻找从源点到其他各顶点的最短路径,而Floyd算法则用于寻找图中所有顶点对之间的最短路径。文章提供了算法实现的代码示例,并展示了不同场景下的应用效果。
367

被折叠的 条评论
为什么被折叠?



