数据结构 图 最短路径

本文详细介绍了图论中的两种经典最短路径算法:Dijkstra算法和Floyd算法。Dijkstra算法用于寻找从源点到其他各顶点的最短路径,而Floyd算法则用于寻找图中所有顶点对之间的最短路径。文章提供了算法实现的代码示例,并展示了不同场景下的应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)从某个源点到其余各顶点的最短路径

Dijkstra的代码如下:

头文件:

#defineINFINITY10000
#defineMAX_VERTEX_NUM20

typedef
intInfoType;
typedef
charVertexType;
typedef
intVRType;

typedef
enum...{DG,DN,UDG,UDN}GraphKind;

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

typedef
struct...{
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
theshortestpathfroma
isasfollows:
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
theshortestpathfroma
isasfollows:
aviaa
'sdistis0
bviaa'sdistis4
cviab'sdistis6
0(a)4(a)6(b)
5(c)0(b)2(b)
3(c)7(a)0(c)
Pressanykeyto
continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值