Implementation of Depth Traversal of Undirected Graph Represented by Adjacency Matrix

Implementation of Depth Traversal of Undirected Graph Represented by Adjacency Matrix

C language

Realization of Undirected Graph Represented by Adjacency Matrix

1. Define the adjacency matrix structure

#define VerType int //定义顶点信息的类型
#define MVNum 10 //定义数组的最大长度
typedef int ArcType; //假设边的权值类型为整型
//定义邻接矩阵结构
typedef struct{
    
    
    //定义顶点表
    VerType vexs[MVNum];
    //定义邻接矩阵的边关系
    ArcType arcs[MVNum][MVNum];
    //定义顶点、弧/边的数量
    int vexnum,arcnum;
}AMGraph;

2. Functions to create an adjacency matrix

Creating Undirected Networks with Adjacency Matrix Notation, Algorithm Ideas

  • Enter the total number of vertices and total number of edges
  • The information of the input points in turn is stored in the vertex table
    *Initialize the adjacency matrix, so that each weight is initialized to 0
  • Construct an adjacency matrix

The space complexity of the adjacency matrix is ​​O(n^2)

//创建邻接矩阵
void CreateUND(AMGraph *G){
    
    
    //输入顶点数和边数
    printf("please input the vexnum and arcnum:\n");
    scanf("%d %d",&(G->vexnum),&(G->arcnum));
    //初始化顶点表
    printf("please input the info of vexs matrix:\n");
    for (int i = 0; i < G->vexnum; i++)
    {
    
    
        scanf("%d",&(G->vexs[i]));
    }
    //初始化邻接矩阵
    for (int i = 0; i < G->vexnum; i++)
    {
    
    
        for (int j = 0; j < G->vexnum; j++)
        {
    
    
            G->arcs[i][j]= 0;//边的权值均为0
        }
    }

    //根据边/弧赋值矩阵
    for (int k = 0; k < G->arcnum; k++)
    {
    
       
        // 一条边所依附的两个点
        //输入点和权值
        VerType v1,v2;
        int w;
        printf("please input the value of point v1 and point v2 and w:\n");
        scanf("%d %d %d",&v1,&v2,&w);
        
        //找寻这两个点的下标
        int i = LocateG(*G,v1);
        int j = LocateG(*G,v2);

        if ((i!=-1) && (j!=-1))
        {
    
    
            G->arcs[i][j] = w;
            G->arcs[j][i] = w;
        } 
    }
}

//定位元素下标位置的函数
int LocateG(AMGraph p,VerType v){
    
    
    for (int i = 0; i < p.vexnum; i++)
    {
    
    
        if(p.vexs[i]==v) return i;
    }
    return -1;
}

3. Depth traversal

method

  • After visiting a certain starting point v of the graph, starting from v, visit any critical vertex w1 of it;
  • Starting from w1, visit the vertex w2 that is adjacent to w1 but has not been visited yet;
  • Then proceed from w2 to conduct a similar visit;
  • This continues until all adjacent points have been visited.
  • Then, take a step back, go back to the vertex that was just visited before and start again, and perform a visit similar to the above;
  • If not, take another step back and search. Repeat the above process until all vertices in the connected graph have been visited
void DFS(AMGraph G,int v,int visited[]){
    
    
    //访问第v+1个顶点,1<= v+1 <=vexnum
    visited[v] = 1;
    printf("%d\t",G.vexs[v]);//输出访问顶点的值
    //依次检查邻接矩阵v所在的行
    for (int w = 0; w < G.vexnum; w++)
    {
    
    
        if ((G.arcs[v][w]!=0) && (visited[w]==0))
        {
    
    
            DFS(G,w,visited);
        }
    }
}

4. Run the test

int main(){
    
    
    AMGraph G;
    AMGraph *p = &G;

    //创建邻接矩阵
    CreateUND(p);

    //输出邻接矩阵
    printf("\t");
    for (int i = 0; i < G.vexnum; i++)  printf("%d\t",i+1);
    printf("\n");
    for (int i = 0; i < G.vexnum; i++)
    {
    
       
        printf("%d\t",i+1);
        for (int j = 0; j <G.vexnum; j++)
        {
    
    
            printf("%d\t",G.arcs[i][j]);
        }
        printf("\n");
    }
    //深度优先遍历图
    //声明并初始化数组visited
    int visited[G.vexnum];
    for (int i = 0; i < G.vexnum; i++)
    {
    
    
        visited[i] = 0;
    }
    //输入起始点在顶点表中的下标0<=k<vexnum
    printf("please input the index of initial point:\n");
    int k;
    scanf("%d",&k);
    DFS(G,k,visited);
    
    return 0;
}

The graph to be established is shown in the figure.
insert image description here
The input and output results of the operation are as follows:

Guess you like

Origin blog.csdn.net/weixin_44848852/article/details/119649675