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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图的深度优先和广度优先算法(DFS递归与非递归)

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图的深度优先和广度优先算法(DFS递归与非递归) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

無向圖——鄰接矩陣的深度優先和廣度優先算法實現

測試環境:VS2008(C)

?

[cpp:showcolumns]?view plaincopyprint? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  • #include?"stdafx.h"??
  • #include?<stdlib.h>??
  • #include?<malloc.h>??
  • #define?INFINITY?INT_MAX??
  • #define?MAX_VEX?20??
  • #define?VRType?int??
  • #define?VertexType?char??
  • #define?InfoType?int??
  • int?*visited;??
  • /********************************/??
  • /****??????圖的結構定義??????****/??
  • /********************************/??
  • typedef?enum??
  • {??
  • ????DG,???
  • ????DN,??
  • ????UDG,??
  • ????UDN??
  • }GraphKind;??
  • struct?_ArcCell??
  • {??
  • ????VRType?adj;?/*?note?weight?*/??
  • ????InfoType?*info;??
  • };??
  • typedef?struct?_ArcCell?ArcCell,?AdjMatrix[MAX_VEX][MAX_VEX];??
  • struct?_MGraph??
  • {??
  • ????VertexType?vexs[MAX_VEX];??
  • ????AdjMatrix?arcs;??
  • ????int?vexnum,?arcnum;??
  • ????GraphKind?kind;??
  • };??
  • typedef?struct?_MGraph?MGraph;??
  • /********************************/??
  • /****??????棧的結構定義??????****/??
  • /********************************/??
  • struct?_node??
  • {??
  • ????int?ivex;??
  • ????struct?_node?*next;??
  • };??
  • typedef?struct?_node?node,?*pnode;??
  • struct?_stack??
  • {??
  • ????int?size;??
  • ????pnode?ptop;??
  • };??
  • typedef?struct?_stack?stack,?*pstack;??
  • /********************************/??
  • /****??????堆的結構定義??????****/??
  • /********************************/??
  • struct?_queue??
  • {??
  • ????pnode?front;??
  • ????pnode?rear;??
  • };??
  • typedef?struct?_queue?queue,?*pqueue;??
  • /********************************/??
  • /****?????????棧的實現???????****/??
  • /********************************/??
  • pstack?init_stack(int?size)??
  • {??
  • ????pnode?pn=NULL;??
  • ????pstack?ps=NULL;??
  • ????pn=(pnode)malloc(sizeof(node));??
  • ????ps=(pstack)malloc(sizeof(stack));??
  • ????pn->ivex=-1;??
  • ????pn->next=NULL;??
  • ????ps->size=size;??
  • ????ps->ptop=pn;??
  • ????return?ps;??
  • }??
  • int?empty_stack(pstack?ps)??
  • {??
  • ????if(ps->ptop->next==NULL)??
  • ????????return?1;??
  • ????else??
  • ????????return?0;??
  • }??
  • void?push_stack(pstack?ps,?int?ivex)??
  • {??
  • ????pnode?pn=NULL;??
  • ????pn=(pnode)malloc(sizeof(node));??
  • ????pn->ivex=ivex;??
  • ????pn->next=ps->ptop;??
  • ????ps->ptop=pn;??
  • }??
  • int?pop_stack(pstack?ps)??
  • {??
  • ????int?ivex=-1;??
  • ????pnode?pn=NULL;??
  • ????if(!empty_stack(ps))??
  • ????{??
  • ????????pn=ps->ptop;??
  • ????????ps->ptop=ps->ptop->next;??
  • ????????ivex=pn->ivex;??
  • ????????free(pn);??
  • ????}??
  • ????return?ivex;??
  • }??
  • /********************************/??
  • /****?????????堆的實現???????****/??
  • /********************************/??
  • queue?init_queue()??
  • {??
  • ????pnode?pn=NULL;??
  • ????queue?qu;??
  • ????pn=(pnode)malloc(sizeof(node));??
  • ????pn->next;??
  • ????pn->ivex=-1;??
  • ????qu.front=qu.rear=pn;??
  • ????return?qu;??
  • }??
  • int?empty_queue(queue?qu)??
  • {??
  • ????if(qu.front==qu.rear)??
  • ????????return?1;??
  • ????else??
  • ????????return?0;??
  • }??
  • void?en_queue(queue?qu,?int?ivex)??
  • {??
  • ????pnode?pn=NULL;??
  • ????pn=(pnode)malloc(sizeof(node));??
  • ????pn->ivex=ivex;??
  • ????pn->next=qu.rear->next;??
  • ????qu.rear=pn;??
  • }??
  • int?de_queue(queue?qu)??
  • {??
  • ????int?ivex=-1;??
  • ????pnode?pn=NULL;??
  • ????if(!empty_queue(qu))??
  • ????{??
  • ????????pn=qu.front;??
  • ????????qu.front=qu.front->next;??
  • ????????ivex=pn->ivex;??
  • ????????free(pn);??
  • ????}??
  • ????return?ivex;??
  • }??
  • /********************************/??
  • /****?????????圖的實現???????****/??
  • /********************************/??
  • int?LocateVex(MGraph?g,?char?ch)??
  • {??
  • ????int?i;??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????if(ch==g.vexs[i])??
  • ????????????return?i;??
  • ????return?-1;??
  • }??
  • MGraph?Create_UDG()??
  • {??
  • ????int?i,?j,?w,?p1,?p2;??
  • ????char?ch1,?ch2;??
  • ????MGraph?g;??
  • ????printf("Enter?vexnum?arcnum:?");??
  • ????scanf("%d?%d",?&g.vexnum,?&g.arcnum);??
  • ????getchar();??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????for(j=1;?j<=g.vexnum;?j++)??
  • ????????????g.arcs[i][j].adj=g.arcs[j][i].adj=INFINITY;?/*?UDG?should?define?i-j?and?j-i?*/??
  • ????printf("Enter?%d?vex.../n",?g.vexnum);??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????{??
  • ????????printf("vex?%d:?",?i);??
  • ????????scanf("%c",?&g.vexs[i]);??
  • ????????getchar();??
  • ????}??
  • ????printf("Enter?%d?arc.../n",?g.arcnum);??
  • ????for(i=1;?i<=g.arcnum;?i++)??
  • ????{??
  • ????????printf("arc?%d:?",?i);??
  • ????????scanf("%c?%c?%d",?&ch1,?&ch2,?&w);??
  • ????????getchar();??
  • ????????p1=LocateVex(g,?ch1);??
  • ????????p2=LocateVex(g,?ch2);??
  • ????????g.arcs[p1][p2].adj=g.arcs[p2][p1].adj=w;??
  • ????}??
  • ????return?g;??
  • }??
  • int?FirstVex(MGraph?g,?int?i)??
  • {??
  • ????int?k;??
  • ????if(i>=1?&&?i<=g.vexnum)??
  • ????????for(k=1;?k<=g.vexnum;?k++)??
  • ????????????if(g.arcs[i][k].adj!=INFINITY)??
  • ????????????????return?k;??
  • ????return?-1;??
  • }??
  • int?NextVex(MGraph?g,?int?i,?int?j)??
  • {??
  • ????int?k;??
  • ????if(i>=1?&&?i<=g.vexnum?&&?j>=1?&&?j<=g.vexnum)??
  • ????????for(k=j+1;?k<=g.vexnum;?k++)??
  • ????????????if(g.arcs[i][k].adj!=INFINITY)??
  • ????????????????return?k;??
  • ????return?-1;??
  • }??
  • void?DFS(MGraph?g,?int?i)??
  • {??
  • ????int?j;??
  • ????if(!visited[i])??
  • ????{??
  • ????????visited[i]=1;??
  • ????????printf("%3c",?g.vexs[i]);??
  • ????????for(j=FirstVex(g,?i);?j>=1;?j=NextVex(g,?i,?j))??
  • ????????????if(!visited[j])??
  • ????????????????DFS(g,?j);??
  • ????}??
  • }??
  • void?DFS_Graph(MGraph?g)??
  • {??
  • ????int?i;??
  • ????visited=(int?*)malloc((g.vexnum+1)*sizeof(int));??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????visited[i]=0;??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????if(!visited[i])??
  • ????????????DFS(g,?i);??
  • }??
  • void?DFS2_Graph(MGraph?g)??
  • {??
  • ????int?i,?j,?k;??
  • ????pstack?ps=NULL;??
  • ????ps=init_stack(g.vexnum);??
  • ????visited=(int?*)malloc((g.vexnum+1)*sizeof(int));??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????visited[i]=0;??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????if(!visited[i])??
  • ????????{??
  • ????????????visited[i]=1;??
  • ????????????printf("%3c",?g.vexs[i]);??
  • ????????????push_stack(ps,?i);??
  • ????????????k=i;??
  • ????????????while?(!empty_stack(ps))??
  • ????????????{?????????
  • ????????????????for(j=FirstVex(g,?k);?j>=1;?j=NextVex(g,?k,?j))??
  • ????????????????{??
  • ????????????????????if(!visited[j])??
  • ????????????????????{??
  • ????????????????????????visited[j]=1;??
  • ????????????????????????printf("%3c",?g.vexs[j]);??
  • ????????????????????????push_stack(ps,?j);?/*?push?all?visited?ivex?*/??
  • ????????????????????????k=j;?/*?newer?node?*/??
  • ????????????????????}?????????????????????
  • ????????????????}??
  • ????????????????k=pop_stack(ps);??
  • ????????????}??
  • ????????}??
  • }??
  • void?BFS_Graph(MGraph?g)??
  • {??
  • ????int?i,?j,?k;??
  • ????queue?qu;??
  • ????qu=init_queue();??
  • ????visited=(int?*)malloc((g.vexnum+1)*sizeof(int));??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????visited[i]=0;??
  • ????for(i=1;?i<=g.vexnum;?i++)??
  • ????????if(!visited[i])??
  • ????????{??
  • ????????????visited[i]=1;??
  • ????????????printf("%3c",?g.vexs[i]);??
  • ????????????en_queue(qu,?i);??
  • ????????????while?(!empty_queue(qu))??
  • ????????????{??
  • ????????????????k=de_queue(qu);??
  • ????????????????for(j=FirstVex(g,?k);?j>=1;?j=NextVex(g,?k,?j))??
  • ????????????????????if(!visited[j])??
  • ????????????????????{??
  • ????????????????????????visited[j]=1;??
  • ????????????????????????printf("%3c",?g.vexs[j]);??
  • ????????????????????????en_queue(qu,?j);??
  • ????????????????????}??
  • ????????????}??
  • ????????}??
  • }??
  • /********************************/??
  • /****??????????主函數????????****/??
  • /********************************/??
  • int?_tmain(int?argc,?_TCHAR*?argv[])??
  • {??
  • ????MGraph?g;??
  • ????g=Create_UDG();??
  • ????printf("/nDFS:?");???
  • ????DFS_Graph(g);???????/*?recursion?*/??
  • ????printf("/nDFS:?");???
  • ????DFS2_Graph(g);??????/*?non?recursion?*/??
  • ????printf("/nBFS:?");??
  • ????BFS_Graph(g);??
  • ????printf("/n");??
  • ????return?0;??
  • }??
  • ?

    運行結果:

    ??????

    ?

    原文鏈接:http://blog.csdn.net/sunboy_2050/article/details/5684519

    轉載于:https://www.cnblogs.com/10jschen/archive/2012/08/15/2639636.html

    總結

    以上是生活随笔為你收集整理的图的深度优先和广度优先算法(DFS递归与非递归)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。