684-Redundant Connection

本文介绍了一种算法,用于从一个包含N个节点的无向图中找到并移除一条冗余边,使得剩余部分构成一棵树。通过两种方法实现:深度优先搜索(DFS)和并查集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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

Note:

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

问题描述

在这个问题中, 树是没有环的连通的无向图

给定的输入为有N个节点(值互不相同, 1, 2, …N)的树, 其中有一条边是多余的。多余的边不是已有的边。

图的边由二维数组给出。每条边为[u, v], 其中u < v, 表示u到v的无向边

返回多余的边, 使得移除这条边后, 剩下的图为一个有N个节点的树。若存在多个解, 返回二维数组中的最后一个。
返回的边同样以[u, v]的形式表示, u < v.


问题分析

使用dfs或者并查集来做

若加入一条边之前, 该边的两个顶点已经连通, 那么这条边即为结果。否则, 将这条边添加到图中(若以并查集来做, 就是union)


解法1(dfs)

class Solution {
    Set<Integer> seen = new HashSet();
    int MAX_EDGE_VAL = 1000;

    public int[] findRedundantConnection(int[][] edges) {
        ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];
        for (int i = 0; i <= MAX_EDGE_VAL; i++) {
            graph[i] = new ArrayList();
        }
        for (int[] edge: edges) {
            seen.clear();
            if (!graph[edge[0]].isEmpty() && !graph[edge[1]].isEmpty() &&
                    dfs(graph, edge[0], edge[1])) {
                return edge;
            }
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        throw new AssertionError();
    }
    //判断source和target是否连通
    public boolean dfs(ArrayList<Integer>[] graph, int source, int target) {
        if (!seen.contains(source)) {
            seen.add(source);
            if (source == target) return true;
            for (int nei: graph[source]) {
                if (dfs(graph, nei, target)) return true;
            }
        }
        return false;
    }

解法2(并查集)

class Solution {
  public int[] findRedundantConnection(int[][] edges) {
        // max size 1000, num from 1 to 1000
        int[] parent = new int[1001];

        for(int[] edge: edges){
            int a = find(parent, edge[0]);
            int b = find(parent, edge[1]);
            if(a == b) return edge;
            // union
            parent[b] = a;
        }

        return new int[2];
    }
    private int find(int[] parent, int f) {
        if(parent[f] == 0){
            parent[f] = f;
            return f;
        }

        while(f != parent[f]){
            // path compression
            parent[f] = parent[parent[f]];  
            f = parent[f];
        }

        return f;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值