zoukankan      html  css  js  c++  java
  • 图及其遍历

    图——深度优先和广度优先算法

    无向图用二维邻接矩阵表示

    测试环境:VC 6.0 (C)

    [cpp:showcolumns] view plaincopyprint?
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. #include <stdio.h>  
    2. #include <malloc.h>  
    3. #include <stdlib.h>  
    4. #define INFINITY 32767  
    5. #define MAX_VEX 20  
    6. #define QUEUE_SIZE (MAX_VERTEX+1)  
    7. #define DataType char  /* vertext's info  */  
    8. int *visited; /* Node: visited flag with dynamic array, good idea ! */  
    9. /* init queue for bfs */  
    10. struct _node  
    11. {  
    12.     int v_num;  
    13.     struct _node *next;  
    14. };  
    15. typedef struct _node node, *pnode;  
    16. struct _queue  
    17. {  
    18.     pnode front;  
    19.     pnode rear;  
    20. };  
    21. typedef struct _queue queue, *pqueue;  
    22. struct _graph  
    23. {  
    24.     DataType *vexs;  
    25.     int arcs[MAX_VEX][MAX_VEX];  
    26.     int vexnum, arcnum;  
    27. };  
    28. typedef struct _graph graph, *pgraph;  
    29. /* operation of queue */  
    30. queue init_queue()  
    31. {  
    32.     queue qu;  
    33.     qu.front=qu.rear=(pnode)malloc(sizeof(node));  
    34.     if(qu.front == NULL)  
    35.         exit(1);  
    36.     qu.rear->next=NULL;  
    37.     return qu;  
    38. }  
    39. void en_queue(pqueue pqu, int v_num)  
    40. {  
    41.     pnode pn;  
    42.     pn=(pnode)malloc(sizeof(node));  
    43.     if(pqu->front == NULL)  
    44.         exit(1);  
    45.     pn->v_num=v_num;  
    46.     pn->next=NULL;  
    47.     pqu->rear->next=pn;  
    48.     pqu->rear=pqu->rear->next;  
    49. }  
    50. int isempty_queue(pqueue pqu)  
    51. {  
    52.     if(pqu->front == pqu->rear)  
    53.         return 1;  
    54.     else  
    55.         return 0;  
    56. }  
    57. int de_queue(pqueue pqu)  
    58. {  
    59.     pnode pn;  
    60.     int d;  
    61.     if(isempty_queue(pqu))  
    62.         return -1;  
    63.     pn=pqu->front;  
    64.     d=pn->v_num;  
    65.     pqu->front=pn->next;  
    66.     free(pn);  
    67.     return d;  
    68. }  
    69. int locate(graph g, DataType data)  
    70. {  
    71.     int i;  
    72.     for(i=0;i<g.vexnum;i++)  
    73.         if(g.vexs[i] == data)  
    74.             return i;  
    75.      return -1;  
    76. }  
    77. graph create_graph()  
    78. {  
    79.     int i,j,w, s1,s2;  
    80.     DataType ch1,ch2,tmp;  
    81.     graph g;  
    82.     printf("g sizeof: %d/n"sizeof(g));  
    83.     printf("Enter vexnum arcnum:");  
    84.     scanf("%d %d", &g.vexnum, &g.arcnum);  
    85.     tmp=getchar();  
    86.     g.vexs=(DataType *)malloc(sizeof(DataType));  
    87.     if(g.vexs == NULL)  
    88.         exit(1);  
    89.     printf("Enter %d vertext,please.../n", g.vexnum);  
    90.     for(i=0;i<g.vexnum;i++)  
    91.     {  
    92.         printf("vex %d: ", i);  
    93.         scanf("%c", &g.vexs[i]);  
    94.         tmp=getchar();  
    95.         //visited[i]=0;  
    96.     }  
    97.     for(i=0;i<g.vexnum;i++)  
    98.         for(j=0;j<g.vexnum;j++)  
    99.             g.arcs[i][j]=INFINITY;  
    100.      printf("Enter %d arcs:/n", g.arcnum);  
    101.      for(i=0;i<g.arcnum;i++)  
    102.      {  
    103.         printf("arc %d: ", i);  
    104.         scanf("%c %c %d", &ch1, &ch2, &w);  
    105.         tmp=getchar();  
    106.         s1=locate(g, ch1);  
    107.         s2=locate(g, ch2);  
    108.         g.arcs[s1][s2]=g.arcs[s2][s1]=w; /* NOTE: weight */  
    109.      }  
    110.      return g;  
    111. }  
    112. int firstvex_graph(graph g, int k)  
    113. {  
    114.     int i;  
    115.     if(k>=0 && k<g.vexnum)  
    116.         for(i=0;i<g.vexnum;i++)  
    117.             if(g.arcs[k][i] != INFINITY)  
    118.                 return i;  
    119.      return -1;  
    120. }  
    121. int nextvex_graph(graph g, int i, int j)  
    122. {  
    123.     int k;  
    124.     if(i>=0 && i<g.vexnum && j>=0 && j<g.vexnum)  
    125.         for(k=j+1; k<g.vexnum; k++)  
    126.             if(g.arcs[i][k] != INFINITY)  
    127.                 return k;  
    128.      return -1;  
    129. }  
    130. void dfs(graph g, int k)  
    131. {  
    132.     int i;  
    133.     if(k == -1)  
    134.     {  
    135.         for(i=0;i<g.vexnum;i++)  
    136.             if(!visited[i])  
    137.                 dfs(g,i);  
    138.      }  
    139.      else  
    140.      {  
    141.         visited[k]=1;  
    142.         printf("%c ", g.vexs[k]);  
    143.         for(i=firstvex_graph(g,k);i>=0;i=nextvex_graph(g,k,i))  
    144.             if(!visited[i])  
    145.                 dfs(g,i);  
    146.      }  
    147. }  
    148. void bfs(graph g)  
    149. {  
    150.     int i,j,k;  
    151.     queue qu;  
    152.     qu=init_queue();  
    153.     for(i=0;i<g.vexnum;i++)  
    154.         if(!visited[i])  
    155.         {  
    156.             visited[i] =1;  
    157.             printf("%c ", g.vexs[i]);  
    158.             en_queue(&qu, i);  
    159.             while(!isempty_queue(&qu))  
    160.             {  
    161.                 k=de_queue(&qu);  
    162.                 for(j=firstvex_graph(g,k); j>=0;j=nextvex_graph(g,k,j))  
    163.                     if(!visited[j])  
    164.                     {  
    165.                         visited[j]=1;  
    166.                         printf("%c ", g.vexs[j]);  
    167.                         en_queue(&qu, j);  
    168.                     }  
    169.             }  
    170.         }  
    171. }  
    172. void main()  
    173. {  
    174.     int i;  
    175.     graph g;  
    176.     g=create_graph();  
    177.     visited=(int *)malloc(g.vexnum*sizeof(int));  
    178.     for(i=0;i<g.vexnum;i++)  
    179.         visited[i]=0;  
    180.     printf("/n/n dfs:");  
    181.     dfs(g,-1);  
    182.     for(i=0;i<g.vexnum;i++)  
    183.         visited[i]=0;  
    184.     printf("/n bfs:");  
    185.     bfs(g);  
    186.       
    187.     if(visited)  
    188.         free(visited);  
    189.     printf("/n");  
    190. }  

    运行结果:

          

    ======================================================

    图 ——深度优先

    测试环境: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 MAX_VEX 20  
    5. #define INFINITY 65535  
    6. int *visited;  
    7. struct _node  
    8. {  
    9.     int vex_num;  
    10.     struct _node *next;  
    11. };  
    12. typedef struct _node node, *pnode;  
    13. struct _graph  
    14. {  
    15.     char *vexs;  
    16.     int arcs[MAX_VEX][MAX_VEX];  
    17.     int vexnum, arcnum;  
    18. };  
    19. typedef struct _graph graph, *pgraph;  
    20. int locate(graph g, char ch)  
    21. {  
    22.     int i;  
    23.     for(i=1; i<=g.vexnum; i++)  
    24.         if(g.vexs[i]==ch)  
    25.             return i;  
    26.     return -1;  
    27. }  
    28. graph create_graph()  
    29. {  
    30.     int i, j, w, p1, p2;  
    31.     char ch1, ch2;  
    32.     graph g;  
    33.     printf("Enter vexnum arcnum: ");  
    34.     scanf("%d %d", &g.vexnum, &g.arcnum);  
    35.     getchar();  
    36.     for(i=1; i<=g.vexnum; i++)  
    37.         for(j=1; j<g.vexnum; j++)  
    38.             g.arcs[i][j]=INFINITY;  
    39.     g.vexs=(char *)malloc(sizeof(char));  
    40.     printf("Enter %d vexnum.../n", g.vexnum);  
    41.     for(i=1; i<=g.vexnum; i++)  
    42.     {  
    43.         printf("vex %d: ", i);  
    44.         scanf("%c", &g.vexs[i]);  
    45.         getchar();  
    46.     }  
    47.     printf("Enter %d arcnum.../n", g.arcnum);  
    48.     for(i=1; i<=g.arcnum; i++)  
    49.     {  
    50.         printf("arc %d: ", i);  
    51.         scanf("%c %c %d", &ch1, &ch2, &w);  
    52.         getchar();  
    53.         p1=locate(g, ch1);  
    54.         p2=locate(g, ch2);  
    55.         g.arcs[p1][p2]=g.arcs[p2][p1]=w;  
    56.     }  
    57.     return g;  
    58. }  
    59. int firstvex_graph(graph g, int i)  
    60. {  
    61.     int k;  
    62.     if(i>=1 && i<=g.vexnum)  
    63.         for(k=1; k<=g.vexnum; k++)  
    64.             if(g.arcs[i][k]!=INFINITY)  
    65.                 return k;  
    66.     return -1;  
    67. }  
    68. int nextvex_graph(graph g, int i, int j)  
    69. {  
    70.     int k;  
    71.     if(i>=1 && i<=g.vexnum && j>=1 && j<=g.vexnum)  
    72.         for(k=j+1; k<=g.vexnum; k++)  
    73.             if(g.arcs[i][k]!=INFINITY)  
    74.                 return k;  
    75.     return -1;  
    76. }  
    77. void dfs(graph g, int i)  
    78. {  
    79.     int k, j;  
    80.     if(!visited[i])  
    81.     {  
    82.         visited[i]=1;  
    83.         printf("%c", g.vexs[i]);  
    84.         for(j=firstvex_graph(g, i); j>=1; j=nextvex_graph(g, i, j))  
    85.             if(!visited[j])  
    86.                 dfs(g, j);  
    87.     }  
    88. }  
    89. void dfs_graph(graph g)  
    90. {  
    91.     int i;  
    92.     visited=(int *)malloc((g.vexnum+1)*sizeof(int));  
    93.     for(i=1; i<=g.vexnum; i++)  
    94.         visited[i]=0;  
    95.     for(i=1; i<g.vexnum; i++)  
    96.         if(!visited[i])  
    97.             dfs(g, i);  
    98. }  
    99. int _tmain(int argc, _TCHAR* argv[])  
    100. {  
    101.     graph g;  
    102.     g=create_graph();  
    103.     dfs_graph(g);  
    104.     printf("/n");  
    105.     return 0;  
    106. }  

    ======================================================

    图 ——广度优先

    测试环境: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 MAX_VEX 20  
    5. #define INFINITY 65535  
    6. int *visited;  
    7. struct _node  
    8. {  
    9.     int data;  
    10.     struct _node *next;  
    11. };  
    12. typedef struct _node node, *pnode;  
    13. struct _queue  
    14. {  
    15.     pnode front;  
    16.     pnode rear;  
    17. };  
    18. typedef struct _queue queue, *pqueue;  
    19. queue init_queue()  
    20. {  
    21.     pnode pn=NULL;  
    22.     queue qu;  
    23.     pn=(pnode)malloc(sizeof(node));  
    24.     if(pn==NULL)  
    25.         printf("init queue, malloc is fail.../n");  
    26.     pn->data=-1;  
    27.     pn->next=NULL;  
    28.     qu.front=qu.rear=pn;  
    29.     return qu;  
    30. }  
    31. int empty_queue(queue qu)  
    32. {  
    33.     if(qu.rear==qu.front)  
    34.         return 0;  
    35.     else  
    36.         return 1;  
    37. }  
    38. void en_queue(pqueue pqu, int data)  
    39. {  
    40.     pnode pn=NULL;  
    41.     if(pqu->rear==NULL)  
    42.         return;  
    43.     pn=(pnode)malloc(sizeof(node));  
    44.     pn->data=data;  
    45.     pn->next=pqu->rear->next;  
    46.     pqu->rear->next=pn;  
    47.     pqu->rear=pn;  
    48. }  
    49. int de_queue(pqueue pqu)  
    50. {  
    51.     int data;  
    52.     pnode pn=NULL;  
    53.     if(pqu->front->next==NULL)  
    54.         return -1;  
    55.       
    56.     pn=pqu->front->next;  
    57.     pqu->front=pqu->front->next;  
    58.     data=pn->data;  
    59.     free(pn);  
    60.     return data;  
    61. }  
    62. struct _graph  
    63. {  
    64.     char *vexs;  
    65.     int arcs[MAX_VEX][MAX_VEX];  
    66.     int vexnum, arcnum;  
    67. };  
    68. typedef _graph graph, *pgraph;  
    69. int locate(graph g, char ch)  
    70. {  
    71.     int i;  
    72.     for(i=1; i<=g.vexnum; i++)  
    73.         if(g.vexs[i]==ch)  
    74.             return i;  
    75.     return -1;  
    76. }  
    77. graph create_graph()  
    78. {  
    79.     int i, j, w, p1, p2;  
    80.     char ch1, ch2;  
    81.     graph g;  
    82.     printf("Enter vexnum arcnum: ");  
    83.     scanf("%d %d", &g.vexnum, &g.arcnum);  
    84.     getchar();  
    85.     for(i=1; i<=g.vexnum; i++)  
    86.         for(j=1; j<g.vexnum; j++)  
    87.             g.arcs[i][j]=INFINITY;  
    88.     g.vexs=(char *)malloc((g.vexnum+1)*sizeof(char));  
    89.     printf("Enter %d vexnum.../n", g.vexnum);  
    90.     for(i=1; i<=g.vexnum; i++)  
    91.     {  
    92.         printf("vex %d: ", i);  
    93.         scanf("%c", &g.vexs[i]);  
    94.         getchar();  
    95.     }  
    96.     printf("Enter %d arcnum.../n", g.arcnum);  
    97.     for(i=1; i<=g.arcnum; i++)  
    98.     {  
    99.         printf("arc %d: ", i);  
    100.         scanf("%c %c %d", &ch1, &ch2, &w);  
    101.         getchar();  
    102.         p1=locate(g, ch1);  
    103.         p2=locate(g, ch2);  
    104.         g.arcs[p1][p2]=g.arcs[p2][p1]=w;  
    105.     }  
    106.     return g;  
    107. }  
    108. int firstvex_graph(graph g, int i)  
    109. {  
    110.     int k;  
    111.     if(i>=1 && i<=g.vexnum)  
    112.         for(k=1; k<=g.vexnum; k++)  
    113.             if(g.arcs[i][k]!=INFINITY)  
    114.                 return k;  
    115.     return -1;  
    116. }  
    117. int nextvex_graph(graph g, int i, int j)  
    118. {  
    119.     int k;  
    120.     if(i>=1 && i<=g.vexnum && j>=1 && j<=g.vexnum)  
    121.         for(k=j+1; k<=g.vexnum; k++)  
    122.             if(g.arcs[i][k]!=INFINITY)  
    123.                 return k;  
    124.     return -1;  
    125. }  
    126. void bfs(graph g)  
    127. {  
    128.     int i, ivex, inextvex;  
    129.     visited=(int *)malloc((g.vexnum+1)*sizeof(int));  
    130.     for(i=1; i<=g.vexnum; i++)  
    131.         visited[i]=0;  
    132.     queue qu=init_queue();  
    133.     for(i=1; i<=g.vexnum; i++)  
    134.     {  
    135.         if(!visited[i])  
    136.         {  
    137.             visited[i]=1;  
    138.             printf("%c", g.vexs[i]);  
    139.             en_queue(&qu, i);  
    140.         }  
    141.           
    142.         while(!empty_queue(qu))  
    143.         {  
    144.             ivex=de_queue(&qu);  
    145.             for(inextvex=firstvex_graph(g, ivex); inextvex>=1; inextvex=nextvex_graph(g, ivex, inextvex))  
    146.                 if(!visited[inextvex])  
    147.                 {  
    148.                     visited[inextvex]=1;  
    149.                     printf("%c", g.vexs[inextvex]);  
    150.                     en_queue(&qu, inextvex);  
    151.                 }  
    152.         }  
    153.     }  
    154. }  
    155. int _tmain(int argc, _TCHAR* argv[])  
    156. {  
    157.     graph g;  
    158.     g=create_graph();  
    159.     bfs(g);  
    160.     printf("/n");  
    161.     return 0;  
    162. }  

     

    ======================================================

    图 ——深度优先和广度优先算法2(网摘)

    本文引用网址:http://bbs.bccn.net/thread-155311-1-1.html(编程论坛)

    看到本算法在网上转载较多,比较流行,且能直接运行

    但发现大多转载中,也把DFS与BFS正好写反了,对此本文已修正

    此外,本算法混用了C与C++,不够单纯,申请的指针空间也未及时释放

    测试环境:VC 6.0 (C)

    [cpp:showcolumns] view plaincopyprint?
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. #include <stdio.h>  
    2. #include <malloc.h>  
    3. #define INFINITY 32767  
    4. #define MAX_VEX 20   
    5. #define QUEUE_SIZE (MAX_VEX+1)   
    6. bool *visited;    
    7. typedef struct  
    8. {  
    9.     char *vexs;                    //顶点向量  
    10.     int arcs[MAX_VEX][MAX_VEX];    //邻接矩阵  
    11.     int vexnum,arcnum;             //图的当前顶点数和弧数  
    12. }Graph;  
    13. //队列类  
    14. class Queue  
    15. {  
    16.     public:  
    17.     void InitQueue(){  
    18.     base=(int *)malloc(QUEUE_SIZE*sizeof(int));  
    19.     front=rear=0;  
    20. }  
    21. void EnQueue(int e)  
    22. {  
    23.     base[rear]=e;  
    24.     rear=(rear+1)%QUEUE_SIZE;  
    25. }  
    26. void DeQueue(int &e)  
    27. {  
    28.     e=base[front];  
    29.     front=(front+1)%QUEUE_SIZE;  
    30. }  
    31. public:  
    32.     int *base;  
    33.     int front;  
    34.     int rear;  
    35. };  
    36. //图G中查找元素c的位置  
    37. int Locate(Graph G,char c)  
    38. {  
    39.     for(int i=0;i<G.vexnum;i++)  
    40.         if(G.vexs[i]==c)   
    41.             return i;  
    42.       
    43.     return -1;  
    44. }  
    45. //创建无向网  
    46. void CreateUDN(Graph &G){  
    47.     int i,j,w,s1,s2;  
    48.     char a,b,temp;  
    49.     printf("输入顶点数和弧数: ");  
    50.     scanf("%d%d",&G.vexnum,&G.arcnum);  
    51.     temp=getchar(); //接收回车  
    52.     G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配顶点数目  
    53.     printf("输入%d个顶点./n",G.vexnum);  
    54.     for(i=0;i<G.vexnum;i++) //初始化顶点  
    55.     {   
    56.         printf("输入顶点%d: ",i);  
    57.         scanf("%c",&G.vexs[i]);  
    58.         temp=getchar(); //接收回车   
    59.     }  
    60.     for(i=0;i<G.vexnum;i++) //初始化邻接矩阵  
    61.         for(j=0;j<G.vexnum;j++)  
    62.             G.arcs[i][j]=INFINITY;  
    63.     printf("输入%d条弧./n",G.arcnum);  
    64.     for(i=0;i<G.arcnum;i++)  
    65.     { //初始化弧  
    66.         printf("输入弧%d: ",i);  
    67.         scanf("%c %c %d",&a,&b,&w); //输入一条边依附的顶点和权值  
    68.         temp=getchar(); //接收回车  
    69.         s1=Locate(G,a);  
    70.         s2=Locate(G,b);  
    71.         G.arcs[s1][s2]=G.arcs[s2][s1]=w;  
    72.     }  
    73. }  
    74. //图G中顶点k的第一个邻接顶点  
    75. int FirstVex(Graph G,int k)  
    76. {  
    77.     if(k>=0 && k<G.vexnum) //k合理  
    78.         for(int i=0;i<G.vexnum;i++)  
    79.             if(G.arcs[k][i]!=INFINITY) return i;  
    80.   return -1;  
    81. }  
    82. //图G中顶点i的第j个邻接顶点的下一个邻接顶点  
    83. int NextVex(Graph G,int i,int j)  
    84. {  
    85.     if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum) //i,j合理  
    86.         for(int k=j+1;k<G.vexnum;k++)  
    87.             if(G.arcs[i][k]!=INFINITY)   
    88.                 return k;  
    89.   return -1;  
    90. }  
    91. //深度优先遍历  
    92. void DFS(Graph G,int k)  
    93. {  
    94.     int i;  
    95.     if(k==-1) //第一次执行DFS时,k为-1  
    96.     {  
    97.         for(i=0;i<G.vexnum;i++)  
    98.             if(!visited[i])   
    99.                 DFS(G,i); //对尚未访问的顶点调用DFS  
    100.     }  
    101.     else  
    102.     {   
    103.         visited[k]=true;  
    104.         printf("%c ",G.vexs[k]); //访问第k个顶点  
    105.         for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i))  
    106.             if(!visited[i]) //对k的尚未访问的邻接顶点i递归调用DFS  
    107.                 DFS(G,i);   
    108.     }  
    109. }  
    110. //广度优先遍历  
    111. void BFS(Graph G)  
    112. {  
    113.     int k;  
    114.     Queue Q; //辅助队列Q  
    115.     Q.InitQueue();  
    116.     for(int i=0;i<G.vexnum;i++)  
    117.         if(!visited[i]) //i尚未访问  
    118.         {   
    119.             visited[i]=true;  
    120.             printf("%c ",G.vexs[i]);  
    121.              Q.EnQueue(i); //i入列  
    122.             while(Q.front!=Q.rear)  
    123.             {  
    124.                 Q.DeQueue(k); //队头元素出列并置为k  
    125.                 for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w))  
    126.                     if(!visited[w]) //w为k的尚未访问的邻接顶点  
    127.                     {   
    128.                         visited[w]=true;  
    129.                         printf("%c ",G.vexs[w]);  
    130.                         Q.EnQueue(w);  
    131.                     }  
    132.             }  
    133.         }  
    134. }  
    135. //主函数  
    136. void main(){  
    137.     int i;  
    138.     Graph G;  
    139.     CreateUDN(G);  
    140.     visited=(bool *)malloc(G.vexnum*sizeof(bool));   
    141.     printf("/n深度优先遍历: ");   
    142.     for(i=0;i<G.vexnum;i++)  
    143.         visited[i]=false;  
    144.     DFS(G,-1); /* NODE: DFS */  
    145.     printf("/n广度优先遍历: ");   
    146.     for(i=0;i<G.vexnum;i++)  
    147.         visited[i]=false;  
    148.     BFS(G); /* NODE: BFS */  
    149.     printf("/n程序结束./n");  
    150. }  

    运行结果:

         

    ======================================================

    [cpp:showcolumns] view plaincopyprint?
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. #include <iostream.h>   
    2. #include <stdlib.h>   
    3. #define INFINITY 0   
    4. #define MAX_VERTEX_NUM 10 //最大顶点数   
    5. #define MAX_EDGE_NUM 40 //最大边数   
    6. typedef enum {DG,DN,UDG,UDN}Graphkind;   
    7. typedef char VertexType; //顶点数据类型   
    8. typedef struct ArcCell   
    9. {   
    10. int adj; //无权图,1或0表示相邻否;带权图则是权值。   
    11. //int *info;   
    12. }ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];   
    13. typedef struct   
    14. {   
    15. VertexType vexs[MAX_VERTEX_NUM]; //顶点向量   
    16. AdjMatrix arcs; //邻接矩阵   
    17. int vexnum,arcnum; //图的当前顶点数和弧数。   
    18. Graphkind kind;   
    19. }MGraph;   
    20. int LocateVex(MGraph G,VertexType v1)   
    21. {   
    22. int i;   
    23. for(i=0;i<G.vexnum;i++)   
    24. if(G.vexs[i]==v1)   
    25. return i;   
    26. return -1;   
    27. }   
    28. int CreatUDN(MGraph &G)   
    29. // 采用数组表示法,构造无向网 G   
    30. {   
    31. VertexType v1,v2;   
    32. int w,j;   
    33. cout<<"输入图的顶点数"<<endl;   
    34. cin>>G.vexnum;   
    35. cout<<"输入图的弧数"<<endl;   
    36. cin>>G.arcnum;   
    37. for(int i=0;i<G.vexnum;i++)   
    38. {   
    39. cout<<"输入顶点向量"<<endl;   
    40. cin>>G.vexs[i];   
    41. }   
    42. for(i=0;i<G.vexnum;i++)   
    43. for(j=0;j<G.vexnum;j++)   
    44. {   
    45. G.arcs[i][j].adj=INFINITY;   
    46. }   
    47. for(int k=0;k<G.arcnum;++k) //构造邻接矩阵   
    48. {   
    49. cout<<"输入边依附的两个顶点"<<endl;   
    50. cin>>v1>>v2;   
    51. cout<<"输入此边的权值"<<endl;   
    52. cin>>w;   
    53. i=LocateVex(G,v1);   
    54. j=LocateVex(G,v2);   
    55. G.arcs[i][j].adj=w;   
    56. G.arcs[j][i].adj=G.arcs[i][j].adj;   
    57. }   
    58. return 1;   
    59. }   
    60. void dispMGraph(MGraph G)   
    61. {   
    62. cout<<"图的邻接矩阵图是:"<<endl;   
    63. for(int i=0;i<G.vexnum;i++)   
    64. {   
    65. for(int j=0;j<G.vexnum;j++)   
    66. cout<<" "<<G.arcs[i][j].adj;   
    67. cout<<endl;   
    68. }   
    69. }   
    70. void main()   
    71. {   
    72. MGraph G;   
    73. CreatUDN(G);   
    74. dispMGraph(G);   
    75. }   
    76. // 邻接表 表示:   
    77. #include <iostream.h>   
    78. #include <stdlib.h>   
    79. #define MAX_VERTEX_NUM 20 //最大顶点数   
    80. #define MAX_EDGE_NUM 40 //最大边数   
    81. int visited[ MAX_VERTEX_NUM];   
    82. typedef int VertexType ; //顶点数据类型   
    83. typedef struct ArcNode   
    84. {   
    85. int adjvex;   
    86. int weight;   
    87. struct ArcNode *nextarc;   
    88. }ArcNode;   
    89. typedef struct VNode   
    90. {   
    91. VertexType data;   
    92. ArcNode *firstarc;   
    93. }VNode,AdjList[MAX_VERTEX_NUM];   
    94. typedef struct   
    95. {   
    96. AdjList vertices;   
    97. int vexnum,arcnum;   
    98. int kind;   
    99. }ALGraph;   
    100. void CreateDG(ALGraph &G)   
    101. {   
    102. int i,j,k;   
    103. ArcNode *p;   
    104. cout<<"创建一个图:"<<endl;   
    105. cout<<"顶点数:"; cin>>G.vexnum;cout<<endl;   
    106. cout<<"边数:"; cin>>G.arcnum; cout<<endl;   
    107. for(i=0;i<G.vexnum;i++)   
    108. {   
    109. G.vertices[i].data=i;   
    110. G.vertices[i].firstarc=NULL;   
    111. }   
    112. for(k=0;k<G.arcnum;k++)   
    113. {   
    114. cout<<"请输入第"<<k+1<<"条边:";   
    115. cin>>i>>j;   
    116. p=(ArcNode*)malloc(sizeof(ArcNode));   
    117. p->adjvex=j;   
    118. p->nextarc=G.vertices[i].firstarc;   
    119. G.vertices[i].firstarc=p;   
    120. }   
    121. }   
    122. void Disp(ALGraph G)   
    123. {   
    124. int i,j;   
    125. ArcNode *p;   
    126. cout<<"输出图为:"<<endl;   
    127. for(i=0;i<G.vexnum;i++)   
    128. {   
    129. p=G.vertices[i].firstarc;   
    130. j=0;   
    131. while(p!=NULL)   
    132. {   
    133. cout<<"("<<i<<","<<p->adjvex<<")";   
    134. p=p->nextarc;   
    135. j=1;   
    136. }   
    137. if(j==1)   
    138. cout<<endl;   
    139. }   
    140. }   
    141. void dfs(ALGraph G,int v) //深度优先遍历   
    142. {   
    143. ArcNode *p;   
    144. cout<<v<<" ";   
    145. visited[v]=1;   
    146. p=G.vertices[v].firstarc;   
    147. while(p!=NULL)   
    148. if(!visited[p->adjvex])   
    149. dfs(G,p->adjvex);   
    150. p=p->nextarc;   
    151. }   
    152. return ;   
    153. }   
    154. void dfs1(ALGraph G)   
    155. {   
    156. int i;   
    157. for(i=0;i<G.vexnum;i++)   
    158. if(visited[i]==0)   
    159. dfs(G,i);   
    160. }   
    161. void main()   
    162. {   
    163. ALGraph G;   
    164. CreateDG(G);   
    165. int v;   
    166. Disp(G);   
    167. cout<<"输入顶点:";   
    168. cin>>v;   
    169. cout<<"深度优先序列:";   
    170. dfs1(G);   
    171. cout<<endl;   

    原文链接:http://blog.csdn.net/Sunboy_2050/article/details/5645828

  • 相关阅读:
    【gtest/gmock】警告与报错集合
    【gtest/gmock】gmock:Mock的常用方法
    【C++容器】vector 和 list 的区别
    【C++百科】C++标准库到底是什么?
    【C++】设置、改变、获取系统环境变量:setenv & putenv & getenv
    【C++调试】error: 编译报错合集
    【C++调试】 warning: 编译警告合集
    Linux常用命令
    tcpdump及wireshark组合使用
    Vim快捷键
  • 原文地址:https://www.cnblogs.com/10jschen/p/2639631.html
Copyright © 2011-2022 走看看