圖的最小生成樹和最短路徑_第1頁
圖的最小生成樹和最短路徑_第2頁
圖的最小生成樹和最短路徑_第3頁
圖的最小生成樹和最短路徑_第4頁
圖的最小生成樹和最短路徑_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

/采用圖的鄰接矩陣方式查找圖的最小生成樹和最短路徑的完整程序代碼:#include<iostream>#include<vector>#include<set>#include<map>#include<string>#include<functional>#include<numeric>#include<algorithm>#include<math.h>usingnamespacestd;#defineMAXNUM26#defineMAXVALUE32767//矩?陣¨?領(lǐng)¢¨?接¨?圖a?的ì?定?§義°?typedefstruct{charVertex[MAXNUM];//vertexintEdges[MAXNUM][MAXNUM];//edgeintIstrave[MAXNUM];//flagintVertex_num;//numberofvertexintEdges_num;//numberofedgesintGlaphtype;//0:nodirection;1:direction}Maxtrixgraph;//創(chuàng)???建?§矩?陣¨?領(lǐng)¢¨?接¨?矩?陣¨?voidCreatmatrixgraph<Maxtrixgraph*G>{inti,j,k,weight;charstart,end;cout<<"pleaseinputtheallvertexinformation:"<<endl;for<i=0;i<G->Vertex_num;i++> {cout<<"the"<<i+1<<"vertex:";cin>>G->Vertex[i]; }cout<<endl<<"pleaseinputtheedgesandvalue:"<<endl;for<k=0;k<G->Edges_num;k++> {cout<<"the"<<k+1<<"edge:";cin>>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; }}//輸o?出?鄰¢¨2接¨?矩?陣¨?voidOutmatrix<Maxtrixgraph*G>{inti,j;cout<<"Thevertexisfollow:"<<endl;for<j=0;j<G->Vertex_num;j++>cout<<""<<G->Vertex[j];cout<<endl<<"Theedgesmatrixisfollow:"<<endl;for<i=0;i<G->Vertex_num;i++> {cout<<G->Vertex[i];for<j=0;j<G->Vertex_num;j++> {if<G->Edges[i][j]==MAXVALUE>cout<<"#";elsecout<<""<<G->Edges[i][j]; }cout<<endl; }}//下?面?介¨|紹|¨1圖a?的ì?鄰¢¨2接¨?表à¨a創(chuàng)???建?§方¤?法¤?§typedefstructedgenode{intVertex;//vertexnumberintweight;//quanstructedgenode*next;//thenextedgenode}EdgeNode;//圖a?的ì?鄰¢¨2接¨?鏈¢??表à¨a定?§義°?typedefstruct{EdgeNode*Adjlist[MAXNUM];intvertex_num;intedge_num;intGraphtype;}ListGraph;//生|¨2成¨|圖a?的ì?鄰¢¨2接¨?表à¨avoidCreatgraph<ListGraph*G>{inti,weight,start,end;EdgeNode*s;for<i=0;i<G->vertex_num;i++>//clearthenodepointerG->Adjlist[i]=NULL;for<i=0;i<G->edge_num;i++>//inputthe2nodeofeachedgeandweight {cout<<"the"<<i+1<<"edge:";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>>;s->next=G->Adjlist[end];s->Vertex=start;s->weight=weight;G->Adjlist[end]=s; } }}//輸o?出?鄰¢¨2接¨?表à¨avoidOutList<ListGraph*G>{inti;EdgeNode*s;for<i=0;i<G->vertex_num;i++> {cout<<"Vertex:"<<i<<"";s=G->Adjlist[i];while<s> {cout<<"->"<<s->Vertex<<"<"<<s->weight<<">";s=s->next; }cout<<endl; }}//圖a?的ì?廣?度¨¨優(yōu)??先¨¨搜?說|ì遍à¨|歷¤¨2typedefstruct{intData[MAXNUM];inthead;inttail;}Sequeue;voidQueueinit<Sequeue*Q>{Q->head=0;Q->tail=0;}intQueuepush<Sequeue*Q,intch>{if<<Q->tail+1>%MAXNUM==Q->head>return0;Q->Data[Q->tail]=ch;Q->tail=<Q->tail+1>%MAXNUM;return1;}intQueuepop<Sequeue*Q,int*ch>{if<Q->head==Q->tail>return0; *ch=Q->Data[Q->head];Q->head=<Q->head+1>%MAXNUM;return1;}intQueueIsempty<Sequeue*Q>{if<Q->head==Q->tail>return1;elsereturn0;}//廣?度¨¨優(yōu)??先¨¨遍à¨|歷¤¨2函?¥數(shù)oyvoidBFSM<Maxtrixgraph*G,intk>{//startfromvertexkinti,j;SequeueQ;Queueinit<&Q>;G->Istrave[k]=1;cout<<"->"<<G->Vertex[k];Queuepush<&Q,k>;while<!QueueIsempty<&Q>> {Queuepop<&Q,&i>;for<j=0;j<G->Vertex_num;j++>if<G->Edges[i][j]!=MAXVALUE&&!G->Istrave[j]> {cout<<"->"<<G->Vertex[j];G->Istrave[j]=1;Queuepush<&Q,j>; } }}//廣?度¨¨優(yōu)??先¨¨搜?索??算?法¤?§的ì?測a試o?程¨?序¨°voidBFSTraverse<Maxtrixgraph*G>{for<inti=0;i<G->Vertex_num;i++>G->Istrave[i]=0;cout<<"廣?度¨¨優(yōu)??先¨¨遍à¨|歷¤¨2節(jié)¨2點(diǎn)ì?:êo"<<endl;for<inti=0;i<G->Vertex_num;i++> {if<!G->Istrave[i]>BFSM<G,i>; }cout<<endl;}//圖a?的ì?深|?度¨¨優(yōu)??先¨¨搜?索??遍à¨|歷¤¨2算?法¤?§voidDFSM<Maxtrixgraph*G,inti>{//startfromvertexiG->Istrave[i]=1;cout<<"->"<<G->Vertex[i];for<intj=0;j<G->Vertex_num;j++> {if<G->Edges[i][j]!=MAXVALUE&&!G->Istrave[j]>DFSM<G,j>; }}//圖a?的ì?深|?度¨¨優(yōu)??先¨¨搜?索??遍à¨|歷¤¨2的ì?測a試o?程¨?序¨°voidDFSTraverse<Maxtrixgraph*G>{for<inti=0;i<G->Vertex_num;i++>G->Istrave[i]=0;cout<<"深|?度¨¨優(yōu)??先¨¨遍à¨|歷¤¨2節(jié)¨2點(diǎn)ì?:êo"<<endl;for<inti=0;i<G->Vertex_num;i++> {if<!G->Istrave[i]>DFSM<G,i>; }cout<<endl;}//圖a?的ì?最á?小?生|¨2成¨|樹o??程¨?序¨°voidSmalltree<MaxtrixgraphG>{inti,j,k,min,sum=0;intweight[MAXNUM];chartmpvertex[MAXNUM];tmpvertex[0]='0';weight[0]=MAXVALUE;for<i=1;i<G.Vertex_num;i++>//saveoneline {weight[i]=G.Edges[0][i];if<weight[i]==MAXVALUE>tmpvertex[i]='-1';//notadjacencynodeelsetmpvertex[i]=G.Vertex[0]; }for<i=1;i<G.Vertex_num;i++> {min=weight[0];k=i;for<j=1;j<G.Vertex_num;j++> {if<weight[j]<min&&tmpvertex[j]!=0> {min=weight[j];k=j; } }sum+=min;cout<<"<"<<tmpvertex[k]<<","<<G.Vertex[k]<<">,";//coutoneedgeofthesmalltreetmpvertex[k]=0;weight[k]=MAXVALUE;for<j=0;j<G.Vertex_num;j++>//rechoosesmallestedge {if<G.Edges[k][j]<weight[j]&&tmpvertex[j]!=0> {weight[j]=G.Edges[k][j];tmpvertex[j]=G.Vertex[k]; } } }cout<<"Thetotalweightvalueis:"<<sum<<endl;}//圖a?的ì?最á?短¨?路?¤徑?程¨?序¨°<從?¨?頂£¤點(diǎn)ì?v0開a始o(jì)?>voidSmallroad<MaxtrixgraphG,intv0>{//Dijkstramethodintweight[MAXNUM];//起e點(diǎn)ì?到ì?各??頂£¤點(diǎn)ì?的ì?路?¤徑?長?è度¨¨intpath[MAXNUM];//起e點(diǎn)ì?到ì?任¨?一°?頂£¤點(diǎn)ì?的ì?路?¤徑?集?¥合?inttmpvertex[MAXNUM];//最á?短¨?路?¤徑?的ì?終?點(diǎn)ì?集?¥合?inti,j,k,min;v0--;//vertexstartfrom0for<i=0;i<G.Vertex_num;i++> {weight[i]=G.Edges[v0][i];if<weight[i]<MAXVALUE&&weight[i]>0>path[i]=v0;tmpvertex[i]=0; }tmpvertex[v0]=1;//將?起e點(diǎn)ì?v0添?¨a加¨?到ì?集?¥合?U中Dweight[v0]=0;//將?起e點(diǎn)ì?V0的ì?權(quán)¨?§值|ì設(shè)|¨¨為a0for<i=0;i<G.Vertex_num;i++> {min=MAXVALUE;k=v0;for<j=0;j<G.Vertex_num;j++>if<tmpvertex[j]==0&&weight[j]<min> {min=weight[j];k=j; }tmpvertex[k]=1;for<j=0;j<G.Vertex_num;j++>if<tmpvertex[j]==0&&weight[k]+G.Edges[k][j]<weight[j]> {weight[j]=weight[k]+G.Edges[k][j];path[j]=k; } }cout<<"頂£¤點(diǎn)ì?"<<G.Vertex[v0]<<"到ì?各??頂£¤點(diǎn)ì?的ì?最á?短¨?路?¤徑?為a〔ê?§終?點(diǎn)ì?<源??點(diǎn)ì?ê?:êo"<<endl;for<i=0;i<G.Vertex_num;i++> {if<tmpvertex[i]==1> {k=i;while<k!=v0> {j=k;cout<<G.Vertex[k]<<"<";k=path[k]; }cout<<G.Vertex[k]<<endl; }elsecout<<G.Vertex[i]<<"<-"<<G.Vertex[v0]<<"hasnoway."<<endl; }}主函數(shù)調(diào)用:#include"stdafx.h"#include"example32_graph.h"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論