图的最短路径算法的实现.doc

上传人:b****1 文档编号:235721 上传时间:2022-10-07 格式:DOC 页数:8 大小:28.50KB
下载 相关 举报
图的最短路径算法的实现.doc_第1页
第1页 / 共8页
图的最短路径算法的实现.doc_第2页
第2页 / 共8页
图的最短路径算法的实现.doc_第3页
第3页 / 共8页
图的最短路径算法的实现.doc_第4页
第4页 / 共8页
图的最短路径算法的实现.doc_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

图的最短路径算法的实现.doc

《图的最短路径算法的实现.doc》由会员分享,可在线阅读,更多相关《图的最短路径算法的实现.doc(8页珍藏版)》请在冰豆网上搜索。

图的最短路径算法的实现.doc

图的最短路径算法的实现

C语言

#include

#include

#include

#defineINF32767

#defineMAXV100

#defineBUFLEN1024

typedefstruct

{ charname[100];

charinfo[1000];

}VertexType;

typedefstruct

{VertexTypevexs[10];

intarcs[100][100];

intvexnum,arcnum;

}MGraph; //图结构

char**getFile(charfileName[],char*array[],int&count){

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

if(i%2==1){

ch[i][strlen(ch[i])-1]=0;

sscanf(ch[i],"%d",&arc);

}else{

sscanf(ch[i],"%d",&vex);

}

}

}

MGraphsetVertexTypeInfo(MGraphg,char*arrayVer[]){

intstr_count=0;

charbuf_ch[100];

char*ch[100];

char*tokenp;

for(inti=0;i

intstr_len=0;

tokenp=strtok(arrayVer[i],"");

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(inti1=2;i1

if(i1%2==1){

ch[i1][strlen(ch[i1])-1]=0;

strcpy(g.vexs[i1/2-1].info,ch[i1]);

}else{

strcpy(g.vexs[i1/2-1].name,ch[i1]);

}

}

returng;

}

//设置无向图的基本信息

MGraphsetMGraphInfo(MGraphg,char*arrayMGraph[],int&count){

intstr_count=0;

charbuf_ch[100];

char*ch[100];

char*tokenp;

for(inti4=g.vexnum+1;i4

intstr_len=0;

tokenp=strtok(arrayMGraph[i4],"");

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,"");

}

}

char*info[8];//需要匹配的字符串集合

for(inti2=0;i2

info[i2]=g.vexs[i2].name;

}

intG[50][50];//邻接矩阵初始化

for(inti3=0;i3

for(intj=0;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

if(k%3==0){

for(intm=0;m

if(strcmp(info[m],ch[k])==0){

templeft[templeft_count++]=m;

}

}

}elseif(k%3==1){

for(intm=0;m

if(strcmp(info[m],ch[k])==0){

tempright[tempright_count++]=m;

}

}

}elseif(k%3==2){

ch[k][strlen(ch[k])-1]=0;

sscanf(ch[k],"%d",&temp[temp_count++]);

}

}

for(inti5=0;i5

G[templeft[i5]][tempright[i5]]=temp[i5];

}

for(inti6=0;i6

for(intj=0;j

g.arcs[i6][j]=G[i6][j];

}

returng;

}

voidDispMat(MGraphg)

{

inti,j;

for(i=0;i

{

for(j=0;j

if(g.arcs[i][j]==INF)

printf("%5s","∞");

else

printf("%5d",g.arcs[i][j]);

printf("\n");

}

}

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{

printf("%s->",g.vexs[i].name);ppath(g,path,i,j);printf("%s",g.vexs[j].name);

printf("\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;i

for(j=0;j

{

A[i][j]=g.arcs[i][j];

path[i][j]=-1;

}

for(k=0;k

{

for(i=0;i

for(j=0;j

if(A[i][j]>(A[i][k]+A[k][j]))

{

A[i][j]=A[i][k]+A[k][j];

path[i][j]=k;

}

}

}

printf("最短路径为:

\n");

DisPath(g,A,path,p,q);//输出最短路径

}

intmain()

{

intvex,arc;

printf("欢迎来到江西理工大学\n");

printf("\n");

MGraphg; //图的定义

char*array[1]; //存储顶点和边数数据信息

char*arrayVer[10]; //存储地点信息

char*arrayMGraph[MAXV]; //存储关于图的信息

intcount=0;

charfileName[]="D:

\\数据结构\\shujujiegouyi\\graph.txt";

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1