日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构分析之——图

發(fā)布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构分析之——图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

無向圖,鄰接矩陣結(jié)構(gòu)

  • /* ?
  • ????????????????????圖 ?
  • 采用鄰接矩陣存儲 ?
  • DFS采用遞歸方案 ?
  • By______H4-breeze[在等帶mm來自習(xí)的時候] ?
  • Apr.28th.2012 ?
  • */?
  • ?
  • #include<iostream> ?
  • using?namespace?std; ?
  • ?
  • #define?VertexNum?10????????//定義頂點數(shù)量最多10個,#define不帶分號 ?
  • ?
  • //圖的相關(guān)數(shù)據(jù)結(jié)構(gòu) ?
  • typedef?struct{??????????????????????????????//圖的數(shù)據(jù)結(jié)構(gòu):頂點數(shù)據(jù)+邊數(shù)據(jù)+頂點數(shù)+邊數(shù) ?
  • ????char?vexs[VertexNum];?//頂點表 ?
  • ????int??edges[VertexNum][VertexNum]; ?
  • ????int?v,e;???????????????????????????????//頂點數(shù)+邊數(shù) ?
  • }Graph; ?
  • ?
  • //定義隊列數(shù)據(jù)結(jié)構(gòu),在BFS時用到 ?
  • typedef?struct?qNode{ ?
  • ????int?front; ?
  • ????int?rear; ?
  • ????int?count; ?
  • ????int?p[VertexNum]; ?
  • }queue; ?
  • ?
  • //全局變量,存放訪問信息 ?
  • int?visited[VertexNum]; ?
  • ?
  • void?CreatGraph(Graph?*G);??????????//創(chuàng)建一個圖 ?
  • void?FS(Graph?*G,int?choice);???????//對圖G實行深度優(yōu)先遍歷,choice為選擇遍歷方式 ?
  • void?dfs(Graph?*G,int?i);??????????//對圖G從頂點i出發(fā)實行DFS遍歷的具體算法,采用遞歸方案 ?
  • void?bfs(Graph?*G,int?i);??????????//對圖G從頂點i出發(fā)實行BFS遍歷的具體算法,采用隊列方案 ?
  • ?
  • //主函數(shù)體 ?
  • int?main(void) ?
  • { ?
  • ????Graph?*graph; ?
  • ????graph?=?(Graph?*)malloc(sizeof(Graph)); ?
  • ????CreatGraph(graph); ?
  • ????cout?<<?"----DFS----"?<<?endl; ?
  • ????FS(graph,0); ?
  • ????cout?<<?"----BFS----"?<<?endl; ?
  • ????FS(graph,1); ?
  • ????return?0; ?
  • } ?
  • ?
  • //創(chuàng)建圖 ?
  • void?CreatGraph(Graph?*G) ?
  • { ?
  • ????int?i,j,k; ?
  • ????cout?<<?"Input?number?of?vertex?and?number?of?edges"?<<?endl; ?
  • ????cin?>>?G->v?>>?G->e; ?
  • ????cout?<<?"Input?data?of?vertex"?<<?endl; ?
  • ????for(i?=?0;i?<?G->v;?i++) ?
  • ????{ ?
  • ????????cin?>>?G->vexs[i]; ?
  • ????} ?
  • ????for(i?=?0;?i?<?G->v;?i++) ?
  • ????????for(j?=?0;j?<?G->v;?j++) ?
  • ????????{ ?
  • ????????????G->edges[i][j]?=?0;??????????????//初始化為獨立頂點 ?
  • ????????} ?
  • ????cout?<<?"Input?information?of?edges.e.g:input?1?and?2?represents?vertex?1?connects?with?2"?<<?endl; ?
  • ????for(k?=?0;?k?<?G->v;?k++) ?
  • ????{ ?
  • ????????cin?>>?i?>>?j; ?
  • ????????G->edges[i-1][j-1]=1;??????????//設(shè)定各個頂點間的鏈接情況:1為相連;0為斷開 ?
  • ????} ?
  • } ?
  • ?
  • //遍歷初始化即方案選擇 ?
  • void?FS(Graph?*G,int?choice)?????????????????//從圖G的頂點i開始DFS ?
  • { ?
  • ????int?i?=?0; ?
  • ????for(i?=?0;i?<?G->v;?i++) ?
  • ????????visited[i]?=?0;?????????????//初始化所有頂點都沒有被訪問 ?
  • ????????????switch(choice) ?
  • ????????????{ ?
  • ????????????????case?0: ?
  • ????????????????????for(i?=?0;i?<?G->v;?i++) ?
  • ????????????????????????dfs(G,i);?????????????????//從第一個頂點開始訪問 ?
  • ????????????????????break; ?
  • ????????????????case?1: ?
  • ????????????????????for(i?=?0;i?<?G->v;?i++) ?
  • ????????????????????????bfs(G,i); ?
  • ????????????????????break; ?
  • ????????????} ?
  • } ?
  • ?
  • //DFS遍歷具體實現(xiàn)算法,遞歸方案 ?
  • void?dfs(Graph?*G,int?i) ?
  • { ?
  • ????if(visited[i]?==?0) ?
  • ????{ ?
  • ????????cout?<<?"Vertex?"?<<?i+1?<<?"?has?been?visited!"?<<?endl; ?
  • ????????visited[i]?=?1; ?
  • ????????for(int?j?=?0;j?<?G->v;?j++) ?
  • ????????{ ?
  • ????????????if(G->edges[i][j]?!=?0?&&?visited[j]?==?0) ?
  • ????????????????dfs(G,j);?????????//遞歸從與頂點i有連接的且尚未被訪問的頂點j開始訪問 ?
  • ????????}??? ?
  • ????} ?
  • } ?
  • ?
  • //BFS遍歷具體算法,隊列方案 ?
  • void?bfs(Graph?*G,int?i) ?
  • {??? ?
  • ????int?j,k; ?
  • ????queue?*q=(queue?*)malloc(sizeof(queue));???//新建一個隊列 ?
  • ????q->front?=?q->rear?=?q->count?=?0;?????????//空隊列 ?
  • ????//把當(dāng)前正在訪問的頂點進隊,以后會從這個頂點出發(fā)遍歷他的還沒有被訪問的鄰頂點 ?
  • ???????? ?
  • ????visited[i]?=?1; ?
  • ????cout?<<?"Vertex?"?<<?i+1?<<?"?has?been?visited!"?<<?endl; ?
  • ????q->p[q->rear]?=?i;??????????????????????????// ?
  • ????q->rear++; ?
  • ????q->count++;??????????????//進隊??rear永遠指向?qū)ξ?#xff0c;front永遠指向?qū)︻^ ?
  • ?
  • ????while(q->count?>?0)??????//最好不要用q->count?!=?0。因為可能為負數(shù),循環(huán)有問題 ?
  • ????{ ?
  • ????????j?=?q->p[q->front]; ?
  • ????????q->front++; ?
  • ????????q->count--;??????????????????//出隊 ?
  • ????????if(visited[j]?==?0) ?
  • ????????{ ?
  • ????????????for(k?=?0;?k?<?G->v;?k++) ?
  • ????????????{ ?
  • ????????????????if(G->edges[j][k]?!=?0?&&?visited[k]?==?0)???//邊存在且尚未被訪問 ?
  • ????????????????{ ?
  • ????????????????????cout?<<?"Vertex?"?<<?k+1?<<?"?has?been?visited!"?<<?endl; ?
  • ????????????????????visited[k]?=?1; ?
  • ?
  • ????????????????????q->p[q->rear]?=?k; ?
  • ????????????????????q->rear++; ?
  • ????????????????????q->count++;??????????//入隊 ?
  • ????????????????} ?
  • ????????????} ?
  • ????????} ?
  • ????} ?
  • }?
  • ?

    轉(zhuǎn)載于:https://blog.51cto.com/4893836/847327

    總結(jié)

    以上是生活随笔為你收集整理的数据结构分析之——图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。