zoukankan      html  css  js  c++  java
  • 判断联通子图的个数

    [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

    Example 1:

         0          3

         |          |

         1 --- 2    4

    Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

    Example 2:

         0           4

         |           |

         1 --- 2 --- 3

    Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

     Note:

    You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges

    这道题让我们求无向图中连通区域的个数,LeetCode中关于图Graph的题屈指可数,解法都有类似的特点,都是要先构建邻接链表Adjacency List来做。这道题的一种解法是利用DFS来做,思路是给每个节点都有个flag标记其是否被访问过,对于一个未访问过的节点,我们将结果自增1,因为这肯定是一个新的连通区域,然后我们通过邻接链表来遍历与其相邻的节点,并将他们都标记成已访问过,遍历完所有的连通节点后我们继续寻找下一个未访问过的节点,以此类推直至所有的节点都被访问过了,那么此时我们也就求出来了连通区域的个数。

    解法一:

     1 class Solution {
     2 public:
     3     int countComponents(int n, vector<pair<int, int> >& edges) {
     4         int res = 0;
     5         vector<vector<int> > g(n);
     6         vector<bool> v(n, false);
     7         for (auto a : edges) {
     8             g[a.first].push_back(a.second);
     9             g[a.second].push_back(a.first);
    10         }
    11         for (int i = 0; i < n; ++i) {
    12             if (!v[i]) {
    13                 ++res;
    14                 dfs(g, v, i);
    15             }
    16         }
    17         return res;
    18     }
    19     void dfs(vector<vector<int> > &g, vector<bool> &v, int i) {
    20         if (v[i]) return;
    21         v[i] = true;
    22         for (int j = 0; j < g[i].size(); ++j) {
    23             dfs(g, v, g[i][j]);
    24         }
    25     }
    26 };

    这道题还有一种比较巧妙的方法,不用建立邻接链表,也不用DFS,思路是建立一个root数组,下标和节点值相同,此时root[i]表示节点i属于group i,我们初始化了n个部分 (res = n),假设开始的时候每个节点都属于一个单独的区间,然后我们开始遍历所有的edge,对于一条边的两个点,他们起始时在root中的值不相同,这时候我们我们将结果减1,表示少了一个区间,然后更新其中一个节点的root值,使两个节点的root值相同,那么这样我们就能把连通区间的所有节点的root值都标记成相同的值,不同连通区间的root值不相同,这样也能找出连通区间的个数。

    解法二:

     1 class Solution {
     2 public:
     3     int countComponents(int n, vector<pair<int, int> >& edges) {
     4         int res = n;
     5         vector<int> root(n);
     6         for (int i = 0; i < n; ++i) root[i] = i;
     7         for (auto a : edges) {
     8             int x = find(root, a.first), y = find(root, a.second);
     9             if (x != y) {
    10                 --res;
    11                 root[y] = x;
    12             }
    13         }
    14         return res;
    15     }
    16     int find(vector<int> &root, int i) {
    17         while (root[i] != i) i = root[i];
    18         return i;
    19     }
    20 };
  • 相关阅读:
    webstorm
    web大文件上传(web应用---SSH框架)
    Webuploader 大文件分片上传
    百度Webuploader 大文件分片上传(.net接收)
    java使用WebUploader做大文件的分块和断点续传
    大文件上传插件webupload插件
    使用Webuploader大文件分片传输
    使用原生Java Web来实现大文件的上传
    Java实现浏览器端大文件分片上传
    怎样使用word2013发布csdn博客
  • 原文地址:https://www.cnblogs.com/mdumpling/p/9142154.html
Copyright © 2011-2022 走看看