Posts Redundant Connection
Post
Cancel

Redundant Connection

Problem Description

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

Solution

snapshot

find() -> get the “leader” of that set/group
union() -> combine the sets/groups, which means, one of the leaders can be made as a child of another or vise-versa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
    public int[] findRedundantConnection(int[][] edges) {
        
        //A tree has (nodes-1) edges. There is one extra edge which is creating the cycle
        int n = edges.length;

        //Initialize: we will assume that each node's parent is itself
        int[] parent = new int[n+1];
        for(int i=1; i<parent.length; i++){
            parent[i] = i;
        }

        for(int i=0; i<edges.length; i++){

            int a = edges[i][0];
            int b = edges[i][1];

            if(find(parent, a) == find(parent, b)){
                return edges[i];
            }else{
                union(parent, find(parent, a), find(parent, b));
            }

        }

        return new int[]{};

    }

    public int find(int[] parent, int n){
        if(parent[n] == n) return n;
        return find(parent, parent[n]);
    }

    public void union(int[] parent, int u, int v){
        if(u <= v){
            parent[v] = u;
        }else{
            parent[v] = u;
        }
    }


}

OPTIMIZATION

We can optimize the find(x) method to O(1) -

See: Minimal Spanning Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
int find(int x, int[] leader){

    if(leader[x] == x) return x;

    while(leader[x] != x){
        x = leader[x];
    }

    leader[x] = find(leader[x], leader);

    return leader[x];

}
This post is licensed under CC BY 4.0 by the author.