C语言实现图的邻接矩阵存储结构及深度优先遍历和广度优先遍历

来源:互联网 发布:珠海金控 知乎 编辑:程序博客网 时间:2024/06/10 01:07

DFS的核心思想在于对访问的邻接节点进行递归调用;BFS的核心思想在于建立了一个邻接节点的队列。

在Dev C++中调试运行通过。

用下图进行了测试。


#include <stdio.h>  #define MaxVertexNum 50  #define QueueSize 50  typedef enum{FALSE,TRUE}shifou;  shifou visited[MaxVertexNum];                                                     typedef char VertexType;   //顶点是字符型typedef int EdgeType;   //边是整型typedef struct    //图的邻接矩阵存储结构{      VertexType vexs[MaxVertexNum];  //顶点向量      EdgeType edges[MaxVertexNum][MaxVertexNum];     //邻接矩阵      int vexnum,arcnum;    //图中当前的顶点数和边数  }MGraph;  /* 邻接矩阵的建立*/ void CreateGraph(MGraph *G)  {       int i,j,k;      char ch1,ch2;      printf("请输入顶点数和边数(输入格式为:顶点数,边数):");      scanf("%d,%d",&(G->vexnum),&(G->arcnum));      printf("请输入顶点名称(输入格式为:a,b,c...):");      for(i=0;i<G->vexnum;i++)      {         getchar();       scanf("%c",&(G->vexs[i]));      }      for(i=0;i<G->vexnum;i++)          for(j=0;j<G->vexnum;j++)              G->edges[i][j]=0;          printf("请输入每条边对应的两个顶点名称(输入格式为:a,b):\n");          for(k=0;k<G->arcnum;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[j][i]=1;            G->edges[i][j]=1;          }  }  /* 深度优先遍历 */ void Depth(MGraph *G,int i)  {      int j;      printf("%c\n",G->vexs[i]);   //访问顶点vi      visited[i]=TRUE;              for(j=0;j<G->vexnum;j++)           //依次搜索vi邻接点          if(G->edges[i][j]==1 && !visited[j])              Depth(G,j);  }  void Depthsearch(MGraph *G)  {      int i;      for(i=0;i<G->vexnum;i++)          visited[i]=FALSE;         for(i=0;i<G->vexnum;i++)          if(!visited[i])               Depth(G,i);  }  /*广度优先遍历*/ typedef struct {      int front;      int rear;      int count;      int data[QueueSize];  }AQueue;   void InitQueue(AQueue *Q)  {      Q->front=Q->rear=0;      Q->count=0;  }  int QueueEmpty(AQueue *Q)  {      return Q->count!=QueueSize;  }  int QueueFull(AQueue *Q)  {      return Q->count==QueueSize;  }  void EnQueue(AQueue *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(AQueue *Q)  {      int temp;      if(QueueEmpty(Q))      {           printf("Queue underflow");          return 0;      }      else     {          temp=Q->data[Q->front];          Q->count--;          Q->front=(Q->front+1)%QueueSize;          return temp;      }  }  void Breadth(MGraph *G,int k)  {       int i,j;      AQueue 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->vexnum;j++)              if(G->edges[i][j]==1&&!visited[j])              {                  printf("%c\n",G->vexs[j]);                  visited[j]=TRUE;                  EnQueue(&Q,j);              }      }  }  void Breadthsearch(MGraph *G)  {      int i;      for (i=0;i<G->vexnum;i++)          visited[i]=FALSE;      for (i=0;i<G->vexnum;i++)          if (!visited[i])               Breadth(G,i);  }  void main()  {      MGraph G;      CreateGraph(&G);    printf("深度优先搜索结果为:");    printf("\n");      Depthsearch(&G);     printf("广度优先搜索结果为:");    printf("\n");      Breadthsearch(&G);  } 
运行结果如下图所示。




阅读全文
0 0
原创粉丝点击