zoukankan      html  css  js  c++  java
  • 数据结构/PTA-邻接矩阵存储图的深度优先遍历/图

    函数接口定义:

    void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );

    其中MGraph是邻接矩阵存储的图,定义如下:

    typedef struct GNode *PtrToGNode;
    struct GNode{
        int Nv;  /* 顶点数 */
        int Ne;  /* 边数   */
        WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
    };
    typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */

    函数DFS应从第V个顶点出发递归地深度优先遍历图Graph,遍历时用裁判定义的函数Visit访问每个顶点。当访问邻接点时,要求按序号递增的顺序。题目保证V是图中的合法顶点。

    裁判测试程序样例:

    #include <stdio.h>
    
    typedef enum {false, true} bool;
    #define MaxVertexNum 10  /* 最大顶点数设为10 */
    #define INFINITY 65535   /* ∞设为双字节无符号整数的最大值65535*/
    typedef int Vertex;      /* 用顶点下标表示顶点,为整型 */
    typedef int WeightType;  /* 边的权值设为整型 */
    
    typedef struct GNode *PtrToGNode;
    struct GNode{
        int Nv;  /* 顶点数 */
        int Ne;  /* 边数   */
        WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
    };
    typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
    bool Visited[MaxVertexNum]; /* 顶点的访问标记 */
    
    MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */
    
    void Visit( Vertex V )
    {
        printf(" %d", V);
    }
    
    void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );
    
    
    int main()
    {
        MGraph G;
        Vertex V;
    
        G = CreateGraph();
        scanf("%d", &V);
        printf("DFS from %d:", V);
        DFS(G, V, Visit);
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */

    输入样例:给定图如下

    5
    
     

    输出样例:

    DFS from 5: 5 1 3 0 2 4 6

    代码:

    void DFS( MGraph Graph, Vertex v, void (*Visit)(Vertex) )
    {
        Visited[v]=true;
        Visit(v);
        for(int i=0; i<Graph->Nv; i++)
        {
            if(Graph->G[v][i]==1&&!Visited[i])
            {
                DFS(Graph,i,Visit);
            }
        }
    }

    简单的递归

    需要注意的是题目代码中的这两句:bool Visited[MaxVertexNum]; /* 顶点的访问标记 */

                                                            MGraph CreateGraph(); /* 创建图并且将Visited初始化为false;裁判实现,细节不表 */

    附测试点:

     除此之外,来想一下MGraph CreatGraph里面究竟写了些什么

    MGraph CreateGraph()
    {
        int Nv, i, VertexNum;
        int v1, v2;
        Vertex V, W ;
        MGraph Graph;
        scanf("%d", &VertexNum);
        Graph = (MGraph)malloc(sizeof(struct GNode));
        Graph->Nv = VertexNum;
        Graph->Ne = 0;
        for(V = 0; V < Graph->Nv; V ++)
        {
            for(W = 0; W < Graph->Nv; W ++)
            {
                Graph->G[V][W] = INFINITY;
            }
        }
        scanf("%d", &Graph->Ne);
        if(Graph->Ne)
        {
            for(i = 0; i < Graph->Ne; i ++)
            {
                scanf("%d %d", &v1, &v2);
                Graph->G[v1][v2] = 1;
                Graph->G[v2][v1] = 1;
            }
        }
        return Graph;
    }
    
    void DFS( MGraph Graph, Vertex v, void (*Visit)(Vertex) )
    {
        Visited[v]=true;
        Visit(v);
        for(int i=0; i<Graph->Nv; i++)
        {
            if(Graph->G[v][i]==1&&!Visited[i])
            {
                DFS(Graph,i,Visit);
            }
        }
    }

    https://blog.csdn.net/qq_42623428/article/details/83998955

        int Nv, i, VertexNum;
        int v1, v2;
        Vertex V, W ;
    

    变量设置,分别设置了顶点数目、之后循环用的变量(V和W)

        MGraph Graph;
    scanf("%d", &VertexNum); Graph = (MGraph)malloc(sizeof(struct GNode)); Graph->Nv = VertexNum; Graph->Ne = 0;
        for(V = 0; V < Graph->Nv; V ++)
          {
              for(W = 0; W < Graph->Nv; W ++)
              {
                  Graph->G[V][W] = INFINITY;
              }
          }

    图的初始设置,建图申请空间,赋图的边数和点数;最后将所有权值赋极限

      scanf("%d", &Graph->Ne);
        if(Graph->Ne)
        {
            for(i = 0; i < Graph->Ne; i ++)
            {
                scanf("%d %d", &v1, &v2);
                Graph->G[v1][v2] = 1;
                Graph->G[v2][v1] = 1;
            }
        }

    输入内容,先输入边数,一条边对应两个顶点,以这两点为下标的数组赋1

  • 相关阅读:
    敏捷软件开发实践-Code Review Process(转)
    随笔1
    随笔
    随笔
    低级错误
    随笔
    随笔2
    随笔
    这以前写的代码
    蛋疼的vs
  • 原文地址:https://www.cnblogs.com/elegantcloud/p/13944532.html
Copyright © 2011-2022 走看看