图的最短路径算法的实现Word格式文档下载.doc
《图的最短路径算法的实现Word格式文档下载.doc》由会员分享,可在线阅读,更多相关《图的最短路径算法的实现Word格式文档下载.doc(8页珍藏版)》请在冰豆网上搜索。
FILE*file;
charbuf[BUFLEN];
intlen=0;
//文件读取的长度
file=fopen(fileName,"
rt"
);
//打开graph.txt的信息
if(file==NULL) //文件为空的处理办法
{
printf("
Cannotopenfilestrikeanykeyexit!
\n"
exit
(1);
}
while(fgets(buf,BUFLEN,file))
len=strlen(buf);
array[count]=(char*)malloc(len+1);
if(!
array[count])
break;
strcpy(array[count++],buf);
}
fclose(file);
returnarray;
}
voidgetInfo(int&
vex,int&
arc,char*array){
charbuf_ch[100];
char*ch[100];
char*tokenp;
intstr_count=0,str_len=0;
tokenp=strtok(array,"
"
strcpy(buf_ch,tokenp);
while(tokenp!
=NULL)
str_len=strlen(tokenp);
ch[str_count]=(char*)malloc(str_len+1);
strcpy(ch[str_count++],tokenp);
tokenp=strtok(NULL,"
}
for(inti=0;
i<
str_count;
i++){
if(i%2==1){
ch[i][strlen(ch[i])-1]=0;
sscanf(ch[i],"
%d"
&
arc);
}else{
vex);
}
}
MGraphsetVertexTypeInfo(MGraphg,char*arrayVer[]){
intstr_count=0;
for(inti=0;
g.vexnum+1&
&
arrayVer[g.vexnum];
intstr_len=0;
tokenp=strtok(arrayVer[i],"
strcpy(buf_ch,tokenp);
while(tokenp!
{
str_len=strlen(tokenp);
ch[str_count]=(char*)malloc(str_len+1);
strcpy(ch[str_count++],tokenp);
tokenp=strtok(NULL,"
for(inti1=2;
i1<
i1++){
if(i1%2==1){
ch[i1][strlen(ch[i1])-1]=0;
strcpy(g.vexs[i1/2-1].info,ch[i1]);
strcpy(g.vexs[i1/2-1].name,ch[i1]);
returng;
//设置无向图的基本信息
MGraphsetMGraphInfo(MGraphg,char*arrayMGraph[],int&
for(inti4=g.vexnum+1;
i4<
count;
i4++){
tokenp=strtok(arrayMGraph[i4],"
char*info[8];
//需要匹配的字符串集合
for(inti2=0;
i2<
g.vexnum;
i2++){
info[i2]=g.vexs[i2].name;
intG[50][50];
//邻接矩阵初始化
for(inti3=0;
i3<
i3++)
for(intj=0;
j<
j++)
G[i3][j]=INF;
inttemp[100]={0};
//存储距离信息
inttemp_count=0;
//距离计数器
inttempleft[100]={0};
//起始地址的代号信息
inttempleft_count=0;
//起始地址计数器
inttempright[100]={0};
//终点地址的代号信息
inttempright_count=0;
//终点地址的计数器
for(intk=0;
k<
k++){
if(k%3==0){
for(intm=0;
m<
m++){
if(strcmp(info[m],ch[k])==0){
templeft[templeft_count++]=m;
}
}
}elseif(k%3==1){
tempright[tempright_count++]=m;
}
}
}elseif(k%3==2){
ch[k][strlen(ch[k])-1]=0;
sscanf(ch[k],"
temp[temp_count++]);
for(inti5=0;
i5<
temp_count;
i5++){
G[templeft[i5]][tempright[i5]]=temp[i5];
}
for(inti6=0;
i6<
i6++) //建立图的邻接矩阵
j++){
g.arcs[i6][j]=G[i6][j];
}
voidDispMat(MGraphg)
{
inti,j;
for(i=0;
i++)
for(j=0;
if(g.arcs[i][j]==INF)
printf("
%5s"
"
∞"
else
%5d"
g.arcs[i][j]);
voidppath(MGraphg,intpath[][MAXV],inti,intj)
intk;
k=path[i][j];
if(k==-1)return;
ppath(g,path,i,k);
printf("
%s->
"
g.vexs[k].name);
ppath(g,path,k,j);
voidDisPath(MGraphg,intA[][MAXV],intpath[][MAXV],inti,intj)
if(A[i][j]==INF)
if(i!
=j)
printf("
从%s到%s没有路径\n"
g.vexs[i].name,g.vexs[j].name);
else{
g.vexs[i].name);
ppath(g,path,i,j);
printf("
%s"
g.vexs[j].name);
\t路径长度为:
%d\n"
A[i][j]);
}
voidFloyd(MGraphg,intp,intq) //弗洛伊德算法
intA[MAXV][MAXV],path[MAXV][MAXV];
inti,j,k,n=g.vexnum;
for(i=0;
n;
i++)
j++)
{
A[i][j]=g.arcs[i][j];
path[i][j]=-1;
for(k=0;
k++)
for(i=0;
for(j=0;
if(A[i][j]>
(A[i][k]+A[k][j]))
{
A[i][j]=A[i][k]+A[k][j];
path[i][j]=k;
}
}
最短路径为:
DisPath(g,A,path,p,q);
//输出最短路径
intmain()
intvex,arc;
欢迎来到江西理工大学\n"
\n"
MGraphg;
//图的定义
char*array[1];
//存储顶点和边数数据信息
char*arrayVer[10];
//存储地点信息
char*arrayMGraph[MAXV];
//存储关于图的信息
intcount=0;
charfileName[]="
D:
\\数据结构\\shujujiegouyi\\graph.txt"
;