Skip to content

684. Redundant Connection

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

You are given a graph that started as a tree with n nodes labeled from 1 to 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 graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

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 input.

Example 1:

img

Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]

Example 2:

img

Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]

Constraints:

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ai < bi <= edges.length
  • ai != bi
  • There are no repeated edges.
  • The given graph is connected.

Solution:

class Solution {
    static class UnionFind {
        private int[] parent;
        private int[] rank;

        public UnionFind(int size) {
            parent = new int[size];
            rank = new int[size];
            for (int i = 0; i < size; i++) {
                parent[i] = i;
                rank[i] = 1;
            }
        }

        public int find(int u) {
            if (parent[u] != u) {
                parent[u] = find(parent[u]); // Path compression
            }
            return parent[u];
        }

        public boolean union(int u, int v) {
            int rootU = find(u);
            int rootV = find(v);

            if (rootU != rootV) {
                if (rank[rootU] > rank[rootV]) {
                    parent[rootV] = rootU;
                } else if (rank[rootU] < rank[rootV]) {
                    parent[rootU] = rootV;
                } else {
                    parent[rootV] = rootU;
                    rank[rootU]++;
                }
                return true;
            }
            return false;
        }
    }

    public int[] findRedundantConnection(int[][] edges) {
        int n = edges.length;
        UnionFind uf = new UnionFind(n + 1); // Initialize UnionFind with n + 1 because nodes are 1-indexed

        for (int[] edge : edges) {
            int u = edge[0];
            int v = edge[1];
            if (!uf.union(u, v)) {
                return edge; // If u and v are already connected, this edge is redundant
            }
        }
        return new int[0]; // This line should never be reached because there is always one redundant edge


    }
}
// TC: O(n)
// SC: O(n)
class Solution {
    public int[] findRedundantConnection(int[][] edges) {
        int n = edges.length;

        List<List<Integer>> adj = new ArrayList<List<Integer>>();

        for (int i = 0; i < n; i++){
            adj.add(new ArrayList<>());
        }

        for (int[] edge : edges){
            int u = edge[0] - 1;
            int v = edge[1] - 1;

            boolean[] visited = new boolean[n];
            // check if there is already a path between the two nodes before adding a
            // and edge, this way we can identify the additional edge that needs to be removed
            if (dfs(adj, u, v, visited)){
                return edge;
            }

            adj.get(u).add(v); // otherwise add to the grapn
            adj.get(v).add(u);
        }

        return new int[0]; // return empty list

    }

    private boolean dfs(List<List<Integer>> adj, int u, int v, boolean[] visited){
        if (u == v){
            return true;
        }

        visited[u] = true;

        for (int next : adj.get(u)){
            if (!visited[next]){
                if (dfs(adj, next, v, visited)){
                    return true;
                }
            }
        }

        return false;
    }
}
// TC: O(N^2)
// SC: O(N)

https://youtu.be/wU6udHRIkcc