当前位置: 代码迷 >> 综合 >> 图的深度遍历 (邻接矩阵)
  详细解决方案

图的深度遍历 (邻接矩阵)

热度:48   发布时间:2023-11-24 11:39:14.0

知识讲解:

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

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

设图形的顶点数为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;
}