欢迎来到天天文库
浏览记录
ID:39483010
大小:38.34 KB
页数:10页
时间:2019-07-04
《图的最小生成树和最短路径》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、采用图的邻接矩阵方式查找图的最小生成树和最短路径的完整程序代码:#include#include#include#include#include#include#include#include#includeusingnamespacestd;#defineMAXNUM26#defineMAXVALUE32767//矩?阵¨®领¢¨¬接¨®图ª?的Ì?定¡§义
2、°?typedefstruct{charVertex[MAXNUM];//vertexintEdges[MAXNUM][MAXNUM];//edgeintIstrave[MAXNUM];//flagintVertex_num;//numberofvertexintEdges_num;//numberofedgesintGlaphtype;//0:nodirection;1:direction}Maxtrixgraph;//创ä¡ä建¡§矩?阵¨®领¢¨¬接¨®矩?阵¨®voidCreatmatrixgraph
3、(Maxtrixgraph*G){inti,j,k,weight;charstart,end;cout<<"pleaseinputtheallvertexinformation:"<Vertex_num;i++){cout<<"the"<>G->Vertex[i];}cout<Edges_num;k++){cout<<"
4、the"<>start>>end>>weight;i=0;j=0;while(G->Vertex[i]!=start){i++;}while(G->Vertex[j]!=end){j++;}G->Edges[i][j]=weight;if(G->Glaphtype==0)G->Edges[j][i]=weight;}}//输º?出?邻¢¨²接¨®矩?阵¨®voidOutmatrix(Maxtrixgraph*G){inti,j;cout<<"Thevertexisfollo
5、w:"<Vertex_num;j++)cout<<""<Vertex[j];cout<Vertex_num;i++){cout<Vertex[i];for(j=0;jVertex_num;j++){if(G->Edges[i][j]==MAXVALUE)cout<<"#";elsecout<<""<Edges[i][j];}cout<6、ndl;}}//下?面?介¨¦绍¦¨¹图ª?的Ì?邻¢¨²接¨®表À¨ª创ä¡ä建¡§方¤?法¤¡§typedefstructedgenode{intVertex;//vertexnumberintweight;//quanstructedgenode*next;//thenextedgenode}EdgeNode;//图ª?的Ì?邻¢¨²接¨®链¢¡ä表À¨ª定¡§义°?typedefstruct{EdgeNode*Adjlist[MAXNUM];intvertex_num;intedge_num;intG7、raphtype;}ListGraph;//生¦¨²成¨¦图ª?的Ì?邻¢¨²接¨®表À¨ªvoidCreatgraph(ListGraph*G){inti,weight,start,end;EdgeNode*s;for(i=0;ivertex_num;i++)//clearthenodepointerG->Adjlist[i]=NULL;for(i=0;iedge_num;i++)//inputthe2nodeofeachedgeandweight{cout<<"the"<8、ge:";cin>>start>>end>>weight;s=(EdgeNode*)malloc(sizeof(EdgeNode));s->next=G->Adjlist[start];s->Vertex=end;s->weight=weight;G->Adjlist[start]=s;if(G->Graphtype==0){s=(EdgeNode*)malloc(sizeof(EdgeNode))
6、ndl;}}//下?面?介¨¦绍¦¨¹图ª?的Ì?邻¢¨²接¨®表À¨ª创ä¡ä建¡§方¤?法¤¡§typedefstructedgenode{intVertex;//vertexnumberintweight;//quanstructedgenode*next;//thenextedgenode}EdgeNode;//图ª?的Ì?邻¢¨²接¨®链¢¡ä表À¨ª定¡§义°?typedefstruct{EdgeNode*Adjlist[MAXNUM];intvertex_num;intedge_num;intG
7、raphtype;}ListGraph;//生¦¨²成¨¦图ª?的Ì?邻¢¨²接¨®表À¨ªvoidCreatgraph(ListGraph*G){inti,weight,start,end;EdgeNode*s;for(i=0;ivertex_num;i++)//clearthenodepointerG->Adjlist[i]=NULL;for(i=0;iedge_num;i++)//inputthe2nodeofeachedgeandweight{cout<<"the"<8、ge:";cin>>start>>end>>weight;s=(EdgeNode*)malloc(sizeof(EdgeNode));s->next=G->Adjlist[start];s->Vertex=end;s->weight=weight;G->Adjlist[start]=s;if(G->Graphtype==0){s=(EdgeNode*)malloc(sizeof(EdgeNode))
8、ge:";cin>>start>>end>>weight;s=(EdgeNode*)malloc(sizeof(EdgeNode));s->next=G->Adjlist[start];s->Vertex=end;s->weight=weight;G->Adjlist[start]=s;if(G->Graphtype==0){s=(EdgeNode*)malloc(sizeof(EdgeNode))
此文档下载收益归作者所有