684. Redundant Connection

In this problem, a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.

Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
  1
 / \
2 - 3
Example 2:
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
    |   |
    4 - 3

方法一:深度优先寻找
主要思想就是,建立一个hash表,把点和连接它的边都建立起来。然后把每一条边逐一添加进去,形成最后完成的图。在这个过程中,如果一条边(v1, v2)在添加之前,v2就可以通过深度优先搜索找到v1。那么证明v1和v2本来就有连接路径,新添加的这个边,只会导致形成环路。
深度优先搜索本身的问题,注意对已经访问过的点的标记。

class Solution {
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        unordered_map<int, vector<int>> hash;
        unordered_set<int> isVisited;

        for (auto e : edges) {
            isVisited.clear();
            if (hash.find(e[0]) != hash.end() && hash.find(e[1]) != hash.end() && DFS(e[0], e[1], hash, isVisited)) {
                return e;
            }
            hash[e[0]].push_back(e[1]);
            hash[e[1]].push_back(e[0]);
        }

        return {-1, -1};
    }
    bool DFS(int v1, int v2, unordered_map<int, vector<int>>& hash, unordered_set<int>& isVisited) {
        if (isVisited.find(v1) == isVisited.end()) {
            if (v1 == v2) return true;
            isVisited.emplace(v1);
            for (auto v : hash[v1]) {
                if (DFS(v, v2, hash, isVisited)) return true;
            }
        }
        return false;
    }
};

方法二:union-find
find 本身是通过递归找到每个连通区域的leading pixel。这里在写find的时候用了path compression,当我们找到leading pixel之后,直接将这个点的parent改成最后那个leading pixel,这样的话在判断两个点是否属于同一个集合的时候会快很多。

class DSU {
public:
    vector<int> parent;

    DSU (int n) {
        for (int i = 0; i <= n; i++) {
            parent.push_back(i);
        }
    }

    int find (int x) {
        if (parent[x] != x) parent[x] = find(parent[x]);
        return parent[x];
    }

    bool Union (int x, int y) {
        if (find(x) == find(y)) return false;
        parent[find(x)] = find(y);
        return true;
    }

};

class Solution {
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        int maxedges = 1000;
        DSU dsu(maxedges);

        for (auto e : edges) {
            if (!dsu.Union(e[0], e[1])) return e;
        }

        return {-1, -1};
    }
};

加速选项二:
除了path compression,还可以加上ranking的方法。对每个母结点的rank 进行记录,将新添加的边的关系依附到更大的连通分量上,有助于比较。

方法一深度优先的复杂度是O(N*N).每个点都要遍历所有的点,方法二调用了N次Union,所以假设Union 复杂度是O(a(N))的话,那么方法二复杂度是O(N*a(N))。可以证明,方法二接近O(N).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值