ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

图的深度遍历(C语言)邻接矩阵表示

2022-02-25 13:01:18  阅读:182  来源: 互联网

标签:遍历 vis int 邻接矩阵 C语言 访问 v1 顶点


 

知识讲解:

图的遍历分为两种,深度遍历与广度遍历。这里讨论深度遍历。

以上图为例讨论图(图片来自《算法笔记》)的深度遍历:

设图形的顶点数为n。

先从顶点v0开始,用一个数组vis[n]来表示该顶点是否被访问,如果未被访问,vis[顶点编号]=0,否则为1.从v0开始访问,则vis[0]=1,v0可以连通v1与v2,即下一个访问的顶点是v1(遍历二维数组时1比2先访问到),再更新vis数组,然后再从v1访问v3,到v3时发现没有办法继续访问了,再退回上一个顶点v1(v1仍存在未被访问的岔道),从v1访问没有被访问过的v4,再从v4出发访问v5,到v5时无法再访问其余的顶点,回退至上一个存在未被访问岔道的顶点v0,访问未被访问的v2,至此所有顶点均被访问,遍历结束。

遍历要用递归实现;

代码实现:

 

int vis[1005],n;

int a[1005][1005];

void DFS(int v){

vis[v]=1;//该顶点被访问

for(int i=1;i<=n;i++)

     {

if(a[v][i]==1&&vis[i]==0)//与之有连接的点是否被访问过

{

DFS(i);

         }

     }

}

void DFSG()

{

for(int i=0;i<n;i++){

if(vis[i]==0){

DFS(i);}

}

}

 

 

 

例题研究:

1013 Battle Over Cities (25 分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:
3 2 3
1 2
1 3
1 2 3

 

Sample Output:
1
0
0

 

来源网站:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

思路分析:

这道题的题意是,一个图如果删除一个顶点,添加几条边能保证其他点的联通。

先介绍概念连通体:连通体就是一个最大连通的子图(可以是一个点,两个相连的点)。

根据题意,我们需要讨论图要形成连通图的条件:假如某个图有n个连通体,v0,v1……vn,如果它们要连通,至少需要n-1条边相连,这里特别注意将整个连通体视作一个整体,也可以将其视为一整个顶点。

这道题可以先通过深度遍历找到连通体(深度遍历是在不断形成最大连通体),判断连通体的数量(即每次遍历到底后又回到有未访问岔口的顶点的时候计数器+1);需要添加的边就是连通体数量-1.这里需特别注意,删除一个点不能在图上真的删除它,只能在遍历到被删除的点时就返回。

代码实现:

#include <stdio.h>
#include <string.h>
int vis[1005],n,m,k,delet;
int a[1005][1005];
void DFS(int v){
    if(v==delet) return;
    vis[v]=1;
     for(int i=1;i<=n;i++)
     {
         if(a[v][i]==1&&vis[i]==0){
             DFS(i);
         }
     }
}
int main()
{
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            a[i][j]=-1;
            a[j][i]=-1;
        }
    }
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        a[x][y]=1;
        a[y][x]=1;
    }
    while(k--){
       
        scanf("%d",&delet);
        int block=0;
        //vis[delet]=1;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        if(vis[i]==0&&i!=delet){
            DFS(i);
            block++;
        }
    }
    printf("%d\n",block-1);
    }
    return 0;
}

 

 

  

 

 

 

标签:遍历,vis,int,邻接矩阵,C语言,访问,v1,顶点
来源: https://www.cnblogs.com/littlepotato2002/p/15935563.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有