[C] 邻接矩阵存储图的深度优先遍历 →→→→→进入此内容的聊天室

来自 , 2019-09-10, 写在 C, 查看 129 次.
URL http://www.code666.cn/view/e9412ee5
  1. #include <stdio.h>
  2. typedef enum {false, true} bool;
  3. #define MaxVertexNum 10  /* 最大顶点数设为10 */
  4. #define INFINITY 65535   /* ∞设为双字节无符号整数的最大值65535*/
  5. typedef int Vertex;      /* 用顶点下标表示顶点,为整型 */
  6. typedef int WeightType;  /* 边的权值设为整型 */
  7.  
  8. typedef struct GNode *PtrToGNode;
  9. struct GNode{
  10.     int Nv;  /* 顶点数 */
  11.     int Ne;  /* 边数   */
  12.     WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
  13. };
  14. typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
  15. bool Visited[MaxVertexNum]; /* 顶点的访问标记 */
  16.  
  17. MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */
  18.  
  19. void Visit( Vertex V )
  20. {
  21.     printf(" %d", V);
  22. }
  23.  
  24. void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
  25. {
  26.     Vertex i;
  27.     Visit(V) ;
  28.     Visited[V] = 1;
  29.     for( i = 0; i < Graph->Nv ; i ++)
  30.     {
  31.         if(Graph->G[V][i] ==1&&!Visited[i])
  32.           DFS(Graph, i, Visit);  
  33.     }
  34.     return 0;
  35. }
  36.  
  37. int main()
  38. {
  39.     MGraph G;
  40.     Vertex V;
  41.  
  42.     G = CreateGraph();
  43.     scanf("%d", &V);
  44.     printf("DFS from %d:", V);
  45.     DFS(G, V, Visit);
  46.  
  47.     return 0;
  48. }

回复 "邻接矩阵存储图的深度优先遍历"

这儿你可以回复上面这条便签

captcha