zoukankan      html  css  js  c++  java
  • 图的深度优先和广度优先算法(DFS递归与非递归)

    无向图——邻接矩阵的深度优先和广度优先算法实现

    测试环境:VS2008(C)

    [cpp:showcolumns] view plaincopyprint?
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. #include "stdafx.h"  
    2. #include <stdlib.h>  
    3. #include <malloc.h>  
    4. #define INFINITY INT_MAX  
    5. #define MAX_VEX 20  
    6. #define VRType int  
    7. #define VertexType char  
    8. #define InfoType int  
    9. int *visited;  
    10. /********************************/  
    11. /****      图的结构定义      ****/  
    12. /********************************/  
    13. typedef enum  
    14. {  
    15.     DG,   
    16.     DN,  
    17.     UDG,  
    18.     UDN  
    19. }GraphKind;  
    20. struct _ArcCell  
    21. {  
    22.     VRType adj; /* note weight */  
    23.     InfoType *info;  
    24. };  
    25. typedef struct _ArcCell ArcCell, AdjMatrix[MAX_VEX][MAX_VEX];  
    26. struct _MGraph  
    27. {  
    28.     VertexType vexs[MAX_VEX];  
    29.     AdjMatrix arcs;  
    30.     int vexnum, arcnum;  
    31.     GraphKind kind;  
    32. };  
    33. typedef struct _MGraph MGraph;  
    34. /********************************/  
    35. /****      栈的结构定义      ****/  
    36. /********************************/  
    37. struct _node  
    38. {  
    39.     int ivex;  
    40.     struct _node *next;  
    41. };  
    42. typedef struct _node node, *pnode;  
    43. struct _stack  
    44. {  
    45.     int size;  
    46.     pnode ptop;  
    47. };  
    48. typedef struct _stack stack, *pstack;  
    49. /********************************/  
    50. /****      堆的结构定义      ****/  
    51. /********************************/  
    52. struct _queue  
    53. {  
    54.     pnode front;  
    55.     pnode rear;  
    56. };  
    57. typedef struct _queue queue, *pqueue;  
    58. /********************************/  
    59. /****         栈的实现       ****/  
    60. /********************************/  
    61. pstack init_stack(int size)  
    62. {  
    63.     pnode pn=NULL;  
    64.     pstack ps=NULL;  
    65.     pn=(pnode)malloc(sizeof(node));  
    66.     ps=(pstack)malloc(sizeof(stack));  
    67.     pn->ivex=-1;  
    68.     pn->next=NULL;  
    69.     ps->size=size;  
    70.     ps->ptop=pn;  
    71.     return ps;  
    72. }  
    73. int empty_stack(pstack ps)  
    74. {  
    75.     if(ps->ptop->next==NULL)  
    76.         return 1;  
    77.     else  
    78.         return 0;  
    79. }  
    80. void push_stack(pstack ps, int ivex)  
    81. {  
    82.     pnode pn=NULL;  
    83.     pn=(pnode)malloc(sizeof(node));  
    84.     pn->ivex=ivex;  
    85.     pn->next=ps->ptop;  
    86.     ps->ptop=pn;  
    87. }  
    88. int pop_stack(pstack ps)  
    89. {  
    90.     int ivex=-1;  
    91.     pnode pn=NULL;  
    92.     if(!empty_stack(ps))  
    93.     {  
    94.         pn=ps->ptop;  
    95.         ps->ptop=ps->ptop->next;  
    96.         ivex=pn->ivex;  
    97.         free(pn);  
    98.     }  
    99.     return ivex;  
    100. }  
    101. /********************************/  
    102. /****         堆的实现       ****/  
    103. /********************************/  
    104. queue init_queue()  
    105. {  
    106.     pnode pn=NULL;  
    107.     queue qu;  
    108.     pn=(pnode)malloc(sizeof(node));  
    109.     pn->next;  
    110.     pn->ivex=-1;  
    111.     qu.front=qu.rear=pn;  
    112.     return qu;  
    113. }  
    114. int empty_queue(queue qu)  
    115. {  
    116.     if(qu.front==qu.rear)  
    117.         return 1;  
    118.     else  
    119.         return 0;  
    120. }  
    121. void en_queue(queue qu, int ivex)  
    122. {  
    123.     pnode pn=NULL;  
    124.     pn=(pnode)malloc(sizeof(node));  
    125.     pn->ivex=ivex;  
    126.     pn->next=qu.rear->next;  
    127.     qu.rear=pn;  
    128. }  
    129. int de_queue(queue qu)  
    130. {  
    131.     int ivex=-1;  
    132.     pnode pn=NULL;  
    133.     if(!empty_queue(qu))  
    134.     {  
    135.         pn=qu.front;  
    136.         qu.front=qu.front->next;  
    137.         ivex=pn->ivex;  
    138.         free(pn);  
    139.     }  
    140.     return ivex;  
    141. }  
    142. /********************************/  
    143. /****         图的实现       ****/  
    144. /********************************/  
    145. int LocateVex(MGraph g, char ch)  
    146. {  
    147.     int i;  
    148.     for(i=1; i<=g.vexnum; i++)  
    149.         if(ch==g.vexs[i])  
    150.             return i;  
    151.     return -1;  
    152. }  
    153. MGraph Create_UDG()  
    154. {  
    155.     int i, j, w, p1, p2;  
    156.     char ch1, ch2;  
    157.     MGraph g;  
    158.     printf("Enter vexnum arcnum: ");  
    159.     scanf("%d %d", &g.vexnum, &g.arcnum);  
    160.     getchar();  
    161.     for(i=1; i<=g.vexnum; i++)  
    162.         for(j=1; j<=g.vexnum; j++)  
    163.             g.arcs[i][j].adj=g.arcs[j][i].adj=INFINITY; /* UDG should define i-j and j-i */  
    164.     printf("Enter %d vex.../n", g.vexnum);  
    165.     for(i=1; i<=g.vexnum; i++)  
    166.     {  
    167.         printf("vex %d: ", i);  
    168.         scanf("%c", &g.vexs[i]);  
    169.         getchar();  
    170.     }  
    171.     printf("Enter %d arc.../n", g.arcnum);  
    172.     for(i=1; i<=g.arcnum; i++)  
    173.     {  
    174.         printf("arc %d: ", i);  
    175.         scanf("%c %c %d", &ch1, &ch2, &w);  
    176.         getchar();  
    177.         p1=LocateVex(g, ch1);  
    178.         p2=LocateVex(g, ch2);  
    179.         g.arcs[p1][p2].adj=g.arcs[p2][p1].adj=w;  
    180.     }  
    181.     return g;  
    182. }  
    183. int FirstVex(MGraph g, int i)  
    184. {  
    185.     int k;  
    186.     if(i>=1 && i<=g.vexnum)  
    187.         for(k=1; k<=g.vexnum; k++)  
    188.             if(g.arcs[i][k].adj!=INFINITY)  
    189.                 return k;  
    190.     return -1;  
    191. }  
    192. int NextVex(MGraph g, int i, int j)  
    193. {  
    194.     int k;  
    195.     if(i>=1 && i<=g.vexnum && j>=1 && j<=g.vexnum)  
    196.         for(k=j+1; k<=g.vexnum; k++)  
    197.             if(g.arcs[i][k].adj!=INFINITY)  
    198.                 return k;  
    199.     return -1;  
    200. }  
    201. void DFS(MGraph g, int i)  
    202. {  
    203.     int j;  
    204.     if(!visited[i])  
    205.     {  
    206.         visited[i]=1;  
    207.         printf("%3c", g.vexs[i]);  
    208.         for(j=FirstVex(g, i); j>=1; j=NextVex(g, i, j))  
    209.             if(!visited[j])  
    210.                 DFS(g, j);  
    211.     }  
    212. }  
    213. void DFS_Graph(MGraph g)  
    214. {  
    215.     int i;  
    216.     visited=(int *)malloc((g.vexnum+1)*sizeof(int));  
    217.     for(i=1; i<=g.vexnum; i++)  
    218.         visited[i]=0;  
    219.     for(i=1; i<=g.vexnum; i++)  
    220.         if(!visited[i])  
    221.             DFS(g, i);  
    222. }  
    223. void DFS2_Graph(MGraph g)  
    224. {  
    225.     int i, j, k;  
    226.     pstack ps=NULL;  
    227.     ps=init_stack(g.vexnum);  
    228.     visited=(int *)malloc((g.vexnum+1)*sizeof(int));  
    229.     for(i=1; i<=g.vexnum; i++)  
    230.         visited[i]=0;  
    231.     for(i=1; i<=g.vexnum; i++)  
    232.         if(!visited[i])  
    233.         {  
    234.             visited[i]=1;  
    235.             printf("%3c", g.vexs[i]);  
    236.             push_stack(ps, i);  
    237.             k=i;  
    238.             while (!empty_stack(ps))  
    239.             {         
    240.                 for(j=FirstVex(g, k); j>=1; j=NextVex(g, k, j))  
    241.                 {  
    242.                     if(!visited[j])  
    243.                     {  
    244.                         visited[j]=1;  
    245.                         printf("%3c", g.vexs[j]);  
    246.                         push_stack(ps, j); /* push all visited ivex */  
    247.                         k=j; /* newer node */  
    248.                     }                     
    249.                 }  
    250.                 k=pop_stack(ps);  
    251.             }  
    252.         }  
    253. }  
    254. void BFS_Graph(MGraph g)  
    255. {  
    256.     int i, j, k;  
    257.     queue qu;  
    258.     qu=init_queue();  
    259.     visited=(int *)malloc((g.vexnum+1)*sizeof(int));  
    260.     for(i=1; i<=g.vexnum; i++)  
    261.         visited[i]=0;  
    262.     for(i=1; i<=g.vexnum; i++)  
    263.         if(!visited[i])  
    264.         {  
    265.             visited[i]=1;  
    266.             printf("%3c", g.vexs[i]);  
    267.             en_queue(qu, i);  
    268.             while (!empty_queue(qu))  
    269.             {  
    270.                 k=de_queue(qu);  
    271.                 for(j=FirstVex(g, k); j>=1; j=NextVex(g, k, j))  
    272.                     if(!visited[j])  
    273.                     {  
    274.                         visited[j]=1;  
    275.                         printf("%3c", g.vexs[j]);  
    276.                         en_queue(qu, j);  
    277.                     }  
    278.             }  
    279.         }  
    280. }  
    281. /********************************/  
    282. /****          主函数        ****/  
    283. /********************************/  
    284. int _tmain(int argc, _TCHAR* argv[])  
    285. {  
    286.     MGraph g;  
    287.     g=Create_UDG();  
    288.     printf("/nDFS: ");   
    289.     DFS_Graph(g);       /* recursion */  
    290.     printf("/nDFS: ");   
    291.     DFS2_Graph(g);      /* non recursion */  
    292.     printf("/nBFS: ");  
    293.     BFS_Graph(g);  
    294.     printf("/n");  
    295.     return 0;  
    296. }  

    运行结果:

          

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

  • 相关阅读:
    ABP dynamic API
    SQL语句 还原未知逻辑名称数据库
    SQL 还原数据库
    使用开源项目集锦
    webp性能测评
    js通过继承实现私有函数
    js组合继承和寄生组合式继承比较
    JS中循环绑定遇到的问题及解决方法
    JS学习之事件流
    OpenGL利用模板测试实现不规则裁剪
  • 原文地址:https://www.cnblogs.com/10jschen/p/2639636.html
Copyright © 2011-2022 走看看