来源王英S同学
来源王英S同学
来源王英S同学
在使用图的邻接矩阵ADT的基础上,设计Dijkstra算法,用以解决单源最短路径问题,并以文本形式输出从源点到其余各个顶点的路径以及路径长度。将此算法加入到邻接矩阵ADT中,在邻接矩阵ADT中提供一个公有的成员函数Dijkstra。
提示:
(1)单源最短路径问题:已知有向带权图(简称有向网)G=(V,E),找出从某个源点s∈V到V中其余各顶点的最短路径。限定权值均为正整数。
(2)目的: 设一有向图G=(V, E),已知各边的权值,以某指定点v0为源点,求从v0到图的其余各点的最短路径。限定各边上的权值大于或等于0。应按路径“长度” 递增的次序,逐步产生最短路径。
(3)Dijkstra算法的基本步骤:设V0是起始源点,U = 已求得最短路径终点集合。V-U = 未确定最短路径的顶点的集合,初始时 U ={V0}。
1)“长度”最短的最短路径是边数为1的长度最小的路径。
2)下一条“长度”最短的路径:
① Vi ∈ V - U ,先求出V0 到Vi 中间只经 U 中结点的最短路径;
② 上述最短路径中长度最小者即为下一条长度最短的路径;
③ 将所求最短路径的终点加入U 中;
3)重复2)直到求出所有的最短路径。
(4)实现方法:
1)图用带权邻接矩阵存储ad[][];
2)数组dist[]存放当前找到的从源点V0到每个终点的最短路径长度,其初态为图中直接路径权值;
3)数组pre[]表示从V0到各终点的最短路径上,此顶点的前一顶点的序号;若从V0到某终点无路径,则用-1作为其前一顶点的序号。
参考函数原型:
(1)//Dijkstra算法(成员函数)
template<class TypeOfVer, class TypeOfEdge>
bool adjmatrix_graph<TypeOfVer, TypeOfEdge>::Dijkstra( int u, TypeOfEdge *dist, int *pre); // u:源点的位序
(2)辅助函数
//最短路径输出(用户函数)
template<class TypeOfVer, class TypeOfEdge>
void searchPath(TypeOfVer *ver, int *prev, TypeOfEdge *dist, int v, int u); // ver:输入的顶点集 v:源点的位序 u:终点的位序
输入说明 :
第一行:图的类型
第二行:顶点数
第三行:顶点集
第四行:无边标记
第五行:边数
第六行:边集
第七行:权集
第八行:源点位序
输出说明 :
第一行:顶点集
空行
第二行:图的邻接矩阵
空行
第三行:dist数组的初值
第四行:pre数组的初值
空行
第五行:dist数组的值
第六行:pre数组的值
空行
第七行:源点到其余各顶点的最短路径及最短路径长度(输出格式参见测试数据)
DN
7
V1 V2 V3 V4 V5 V6 V7
99
10
0 1
0 2
0 4
0 6
1 5
1 6
2 3
3 4
4 5
5 6
13 8 30 32 9 7 5 6 2 17
0
--------------
V1 V2 V3 V4 V5 V6 V7
99 13 8 99 30 99 32
99 99 99 99 99 9 7
99 99 99 5 99 99 99
99 99 99 99 6 99 99
99 99 99 99 99 2 99
99 99 99 99 99 99 17
99 99 99 99 99 99 99
0 13 8 99 30 99 32
-1 0 0 -1 0 -1 0
0 13 8 13 19 21 20
-1 0 0 2 3 4 1
<(0,V1),(1,V2)>,13
<(0,V1),(2,V3)>,8
<(0,V1),(2,V3),(3,V4)>,13
<(0,V1),(2,V3),(3,V4),(4,V5)>,19
<(0,V1),(2,V3),(3,V4),(4,V5),(5,V6)>,21
<(0,V1),(1,V2),(6,V7)>,20
#include<iostream>
#include<map>
using namespace std;
#define MVNum 101
#define MaxInt 32767
typedef string VerTexType;
typedef int ArcType;
int flag,v;
typedef struct
{
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum,arcnum;
} AMGraph;
void f(int v, int s,AMGraph G,int t[101],bool end=0)
{
if(s == v)
{
cout<<'('<<s<<','<<G.vexs[s]<<')'<<',';
return;
}
f(v,t[s],G,t,0);
cout<<'('<<s<<','<<G.vexs[s]<<')';
if(end==0)
cout<<',';
}
void GreateUDN(AMGraph &G)
{
int x[101]= {0},y[101]= {0},cost[101];
cin>>G.vexnum;
for(int i=0; i<G.vexnum; i++)
{
cin>>G.vexs[i];
}
cin>>flag;
cin>>G.arcnum;
for(int i=0; i<G.vexnum; i++)
{
for(int j=0; j<G.vexnum; j++)
G.arcs[i][j]=flag;
}
for(int i=0; i<G.arcnum; i++)
{
cin>>x[i]>>y[i];
}
for(int i=0; i<G.arcnum; i++)
{
cin>>cost[i];
}
cin>>v;
for(int i=0; i<G.arcnum; i++)
{
G.arcs[x[i]][y[i]]=cost[i];
}
for(int i=0; i<G.vexnum-1; i++)
{
cout<<G.vexs[i]<<' ';
}
cout<<G.vexs[G.vexnum-1];
cout<<endl<<endl;
for(int i=0; i<G.vexnum; i++)
{
for(int j=0; j<G.vexnum; j++)
cout<<G.arcs[i][j]<<' ';
cout<<endl;
}
cout<<endl;
}
void Dijkstra(AMGraph g,int v)
{
int S[101],dist[101]= {0},Path[101],mindis,i,j,u=0;
for(i=0; i<g.vexnum; i++)
{
S[i]=0;
dist[i]=g.arcs[v][i];
if(dist[i]!=flag&&dist[i])
Path[i]=v;
else
Path[i]=-1;
}
dist[v]=0;
Path[v]=-1;
for(i=0; i<g.vexnum; i++)
{
cout<<dist[i]<<' ';
}
cout<<endl;
for(i=0; i<g.vexnum; i++)
{
cout<<Path[i]<<' ';
}
cout<<endl<<endl;
S[v]=1;
for(i=1; i<g.vexnum; ++i)
{
mindis=MaxInt;
for(j=0; j<g.vexnum; ++j)
{
if(!S[j]&&dist[j]<mindis)
{
u=j;
mindis=dist[j];
}
}
S[u]=1;
for(int w=0; w<g.vexnum; ++w)
{
if(!S[w]&&(dist[u]+g.arcs[u][w]<dist[w]))
{
dist[w]=dist[u]+g.arcs[u][w];
Path[w]=u;
}
}
}
for(i=0; i<g.vexnum; i++)
{
cout<<dist[i]<<' ';
}
cout<<endl;
for(i=0; i<g.vexnum; i++)
{
cout<<Path[i]<<' ';
}
cout<<endl<<endl;
for(int i=0; i<g.vexnum; i++)
{
if(i==v)
continue;
cout<<'<';
f(v,i,g,Path,1);
cout<<'>'<<','<<dist[i]<<endl;
}
}
int main()
{
string ss;
cin>>ss;
AMGraph G;
GreateUDN(G);
Dijkstra(G,v);
return 0;
}