图---邻接矩阵 建立,深度遍历,广度遍历

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

    图的存储方式可以用邻接矩阵来表示,我们假定顶点序号从0开始,即图G的顶点集的一般形式是V(G)={v0,vi,…,Vn-1}。

以下代码测试过,为图的邻接矩阵表示方式。

[cpp]
view plain
copy
print
?

  1. /************************************************************************/  
  2. /* 图的邻接矩阵存储结构                                                  */  
  3. /************************************************************************/  
  4. #include <stdio.h>  
  5. #define MaxVertexNum 100  
  6. #define QueueSize 30  
  7. typedef enum{FALSE,TRUE}Boolean;  
  8. Boolean visited[MaxVertexNum];  
  9. typedef char VertexType;  
  10. typedef int EdgeType;  
  11. typedef struct    
  12. {  
  13.     VertexType vexs[MaxVertexNum];  //顶点表  
  14.     EdgeType edges[MaxVertexNum][MaxVertexNum];     //邻接矩阵,可看做边表  
  15.     int n,e;    //图中当前的顶点数和边数  
  16. }MGraph;  
  17. /************************************************************************/  
  18. /* 邻接矩阵的建立                                                        */  
  19. /************************************************************************/  
  20. void CreateMGraph(MGraph *G)  
  21. {   
  22.     int i,j,k;  
  23.     char ch1,ch2;  
  24.     printf(“请输入顶点数和边数(输入格式为:顶点数,边数):/n”);  
  25.     scanf(“%d,%d”,&(G->n),&(G->e));  
  26.     printf(“请输入顶点信息(顶点号<CR>)每个顶点以回车作为结束:/n”);  
  27.     for(i=0;i<G->n;i++)  
  28.     {  
  29.         getchar();scanf(“%c”,&(G->vexs[i]));  
  30.     }  
  31.     for(i=0;i<G->n;i++)  
  32.         for(j=0;j<G->n;j++)  
  33.             G->edges[i][j]=0;  
  34.         printf(“请输入每条边对应的两个顶点的序号(输入格式为:i,j):/n”);  
  35.         for(k=0;k<G->e;k++)  
  36.         {  
  37.             getchar();  
  38.             printf(“请输入第%d条边的顶点序号:”,k+1);  
  39.             scanf(“%c,%c”,&ch1,&ch2);  
  40.             for(i=0;ch1!=G->vexs[i];i++);  
  41.             for(j=0;ch2!=G->vexs[j];j++);  
  42.             G->edges[i][j]=1;  
  43.         }  
  44. }  
  45. /************************************************************************/  
  46. /* 深度优先遍历(深度优先搜索)                                         */  
  47. /************************************************************************/  
  48. void DFSM(MGraph *G,int i)  
  49. {  
  50.     int j;  
  51.     printf(“深度优先遍历结点: 结点%c/n”,G->vexs[i]);   //访问顶点vi  
  52.     visited[i]=TRUE;          
  53.     for(j=0;j<G->n;j++)           //依次搜索vi邻接点  
  54.         if(G->edges[i][j]==1 && !visited[j])  
  55.             DFSM(G,j);  
  56. }  
  57. void DFSTraverseM(MGraph *G)  
  58. {  
  59.     int i;  
  60.     for(i=0;i<G->n;i++)  
  61.         visited[i]=FALSE;     
  62.     for(i=0;i<G->n;i++)  
  63.         if(!visited[i])   
  64.             DFSM(G,i);  
  65. }  
  66. /************************************************************************/  
  67. /* 广度优先遍历(广度优先搜索)                                         */  
  68. /************************************************************************/  
  69. typedef struct  
  70. {  
  71.     int front;  
  72.     int rear;  
  73.     int count;  
  74.     int data[QueueSize];  
  75. }CirQueue;  
  76. void InitQueue(CirQueue *Q)  
  77. {  
  78.     Q->front=Q->rear=0;  
  79.     Q->count=0;  
  80. }  
  81. int QueueEmpty(CirQueue *Q)  
  82. {  
  83.     return Q->count=QueueSize;  
  84. }  
  85. int QueueFull(CirQueue *Q)  
  86. {  
  87.     return Q->count==QueueSize;  
  88. }  
  89. void EnQueue(CirQueue *Q,int x)  
  90. {   
  91.     if (QueueFull(Q))  
  92.         printf(“Queue overflow”);  
  93.     else  
  94.     {   
  95.         Q->count++;  
  96.         Q->data[Q->rear]=x;  
  97.         Q->rear=(Q->rear+1)%QueueSize;  
  98.     }  
  99. }  
  100. int DeQueue(CirQueue *Q)  
  101. {  
  102.     int temp;  
  103.     if(QueueEmpty(Q))  
  104.     {   
  105.         printf(“Queue underflow”);  
  106.         return NULL;  
  107.     }  
  108.     else  
  109.     {  
  110.         temp=Q->data[Q->front];  
  111.         Q->count–;  
  112.         Q->front=(Q->front+1)%QueueSize;  
  113.         return temp;  
  114.     }  
  115. }  
  116. void BFSM(MGraph *G,int k)  
  117. {   
  118.     int i,j;  
  119.     CirQueue Q;  
  120.     InitQueue(&Q);  
  121.     printf(“广度优先遍历结点: 结点%c/n”,G->vexs[k]);  
  122.     visited[k]=TRUE;  
  123.     EnQueue(&Q,k);  
  124.     while (!QueueEmpty(&Q))  
  125.     {  
  126.         i=DeQueue(&Q);  
  127.         for (j=0;j<G->n;j++)  
  128.             if(G->edges[i][j]==1&&!visited[j])  
  129.             {  
  130.                 printf(“广度优先遍历结点:%c/n”,G->vexs[j]);  
  131.                 visited[j]=TRUE;  
  132.                 EnQueue(&Q,j);  
  133.             }  
  134.     }  
  135. }  
  136. void BFSTraverseM(MGraph *G)  
  137. {  
  138.     int i;  
  139.     for (i=0;i<G->n;i++)  
  140.         visited[i]=FALSE;  
  141.     for (i=0;i<G->n;i++)  
  142.         if (!visited[i])   
  143.             BFSM(G,i);  
  144. }  
  145. /************************************************************************/  
  146. /* 主函数调用                                                           */  
  147. /************************************************************************/  
  148. int main()  
  149. {  
  150.     MGraph G;  
  151.     CreateMGraph(&G);  
  152.     DFSTraverseM(&G);  
  153.     BFSTraverseM(&G);  
  154.     return 0;  
  155. }  

/************************************************************************//* 图的邻接矩阵存储结构                                                  *//************************************************************************/#include <stdio.h>#define MaxVertexNum 100#define QueueSize 30typedef enum{FALSE,TRUE}Boolean;Boolean visited[MaxVertexNum];typedef char VertexType;typedef int EdgeType;typedef struct  { VertexType vexs[MaxVertexNum]; //顶点表 EdgeType edges[MaxVertexNum][MaxVertexNum];  //邻接矩阵,可看做边表 int n,e; //图中当前的顶点数和边数}MGraph;/************************************************************************//* 邻接矩阵的建立                                                        *//************************************************************************/void CreateMGraph(MGraph *G){  int i,j,k; char ch1,ch2; printf(“请输入顶点数和边数(输入格式为:顶点数,边数):/n”); scanf(“%d,%d”,&(G->n),&(G->e)); printf(“请输入顶点信息(顶点号<CR>)每个顶点以回车作为结束:/n”); for(i=0;i<G->n;i++) {  getchar();scanf(“%c”,&(G->vexs[i])); } for(i=0;i<G->n;i++)  for(j=0;j<G->n;j++)   G->edges[i][j]=0;  printf(“请输入每条边对应的两个顶点的序号(输入格式为:i,j):/n”);  for(k=0;k<G->e;k++)  {   getchar();   printf(“请输入第%d条边的顶点序号:”,k+1);   scanf(“%c,%c”,&ch1,&ch2);   for(i=0;ch1!=G->vexs[i];i++);   for(j=0;ch2!=G->vexs[j];j++);   G->edges[i][j]=1;  }}/************************************************************************//* 深度优先遍历(深度优先搜索)                                         *//************************************************************************/void DFSM(MGraph *G,int i){ int j; printf(“深度优先遍历结点: 结点%c/n”,G->vexs[i]); //访问顶点vi visited[i]=TRUE;   for(j=0;j<G->n;j++)   //依次搜索vi邻接点  if(G->edges[i][j]==1 && !visited[j])   DFSM(G,j);}void DFSTraverseM(MGraph *G){ int i; for(i=0;i<G->n;i++)  visited[i]=FALSE;  for(i=0;i<G->n;i++)  if(!visited[i])    DFSM(G,i);}/************************************************************************//* 广度优先遍历(广度优先搜索)                                         *//************************************************************************/typedef struct{ int front; int rear; int count; int data[QueueSize];}CirQueue;void InitQueue(CirQueue *Q){ Q->front=Q->rear=0; Q->count=0;}int QueueEmpty(CirQueue *Q){ return Q->count=QueueSize;}int QueueFull(CirQueue *Q){ return Q->count==QueueSize;}void EnQueue(CirQueue *Q,int x){  if (QueueFull(Q))  printf(“Queue overflow”); else {   Q->count++;  Q->data[Q->rear]=x;  Q->rear=(Q->rear+1)%QueueSize; }}int DeQueue(CirQueue *Q){ int temp; if(QueueEmpty(Q)) {   printf(“Queue underflow”);  return NULL; } else {  temp=Q->data[Q->front];  Q->count–;  Q->front=(Q->front+1)%QueueSize;  return temp; }}void BFSM(MGraph *G,int k){  int i,j; CirQueue Q; InitQueue(&Q); printf(“广度优先遍历结点: 结点%c/n”,G->vexs[k]); visited[k]=TRUE; EnQueue(&Q,k); while (!QueueEmpty(&Q)) {  i=DeQueue(&Q);  for (j=0;j<G->n;j++)   if(G->edges[i][j]==1&&!visited[j])   {    printf(“广度优先遍历结点:%c/n”,G->vexs[j]);    visited[j]=TRUE;    EnQueue(&Q,j);   } }}void BFSTraverseM(MGraph *G){ int i; for (i=0;i<G->n;i++)  visited[i]=FALSE; for (i=0;i<G->n;i++)  if (!visited[i])    BFSM(G,i);}/************************************************************************//* 主函数调用                                                           *//************************************************************************/int main(){ MGraph G; CreateMGraph(&G); DFSTraverseM(&G); BFSTraverseM(&G); return 0;}

测试结构如下(含测试数据):

《图---邻接矩阵 建立,深度遍历,广度遍历》

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

《图---邻接矩阵 建立,深度遍历,广度遍历》

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/jhfyuf/article/details/83922138
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞