Skip to content

Commit d88c5d2

Browse files
authored
Adding the solution for Redundant Connection (codedecks-in#136)
* Added a Leetcode solution for Valid Sudoku * Added Solution code and Editorial-readme * Added problem in readme * Added editorial for the problem * Added image for editorial * Added editorial Image
1 parent eecf894 commit d88c5d2

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Redundant Connection
2+
3+
> **Problem Link** : [Redundant Connection (LeetCode)](https://leetcode.com/problems/redundant-connection/) <br>
4+
> **Concepts Involved** : *Tree, Disjoint Union Set*
5+
6+
## Problem Statement
7+
We have been provided with a list of edges. These edges form a Tree. The list has one extra edge as well. We have to find that extra edge which if removed from the list makes the resulting list of edges a Tree of N nodes.
8+
9+
## Solution
10+
<p>
11+
We have been given with a list of edges. Now we need to remove one edge from the List so that the resultant list forms a Tree. Basically a Tree can be termed as a special Graph where every nodes are connected to one another directly or indirectly and there no loops present. As there is only one extra edge, then it must join two nodes which are already connected. And we neeed to find that particular edge.
12+
</p>
13+
<p>
14+
The basic idea is to use a Disjoint Union Sets here. We will iterate over the list of edges and for every edge <b>(u,v)</b> we will put u and v in a single set. If during the iteration we find an edge such that both the nodes u and v is in the same set, then we can say that it is an extra edge which can be removed. We can store that as our result and return it after the iteration.
15+
</p>
16+
17+
![Editorial Image](./images/image-1.png)
18+
19+
<p>
20+
The basic source code for the problem looks like :
21+
22+
```
23+
for each (edge in edge_list) :
24+
if(same_set(edge.u, edge.v)){
25+
extra_node_u = edge.u
26+
extra_node_v = edge.v
27+
}
28+
else{
29+
put_in_same_set(edge.u, edge.v)
30+
}
31+
}
32+
```
33+
</p>
34+
35+
## Complexity
36+
* The solution Time Complexity is : ***O(N)***
37+
* The solution Space Complexity is : ***O(N)***
38+
39+
> **Author's Note** : I would highly encourage readers to learn the Disjoint Set Union data structure before attempting the question from [here](https://www.geeksforgeeks.org/union-find/).
40.3 KB
Loading
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Problem Name : Redundant Connection
3+
* Concept Involved : Trees, Union Find
4+
*
5+
* Execution Time : 1 ms
6+
* Memory Consumed : 39 mb
7+
**/
8+
9+
class DUS{
10+
public int[] parent;
11+
public int n;
12+
13+
DUS(int n){
14+
this.n = n;
15+
parent = new int[n];
16+
for(int i=0; i<n; i++){
17+
parent[i] = i;
18+
}
19+
}
20+
21+
public int find(int u){
22+
if(parent[u] != u){
23+
parent[u] = find(parent[u]);
24+
}
25+
return parent[u];
26+
}
27+
28+
public void union(int u, int v){
29+
int pu = find(u);
30+
int pv = find(v);
31+
32+
if(pu != pv){
33+
parent[pv] = pu;
34+
}
35+
}
36+
}
37+
class Solution {
38+
public int[] findRedundantConnection(int[][] edges) {
39+
int[] res = new int[2];
40+
DUS dus = new DUS(edges.length+1);
41+
42+
for(int i=0; i<edges.length; i++){
43+
int u = edges[i][0];
44+
int v = edges[i][1];
45+
int pu = dus.find(u);
46+
int pv = dus.find(v);
47+
48+
if(pu != pv){
49+
dus.union(u,v);
50+
}
51+
else{
52+
res[0] = u;
53+
res[1] = v;
54+
}
55+
}
56+
57+
return res;
58+
}
59+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
236236
| 1028 | [Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | [C++](./C++/Recover-a-Tree-From-Preorder-Traversal.cpp) | _O(n)_ | _O(n)_ | Hard | Tree | |
237237
| 968 | [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras/) | [C++](./C++/Binary-Tree-Cameras.cpp) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Dynamic Programming |
238238
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Javascript](./JavaScript/98.Validate-Binary-Search-Tree.js) | _O(log(n))_ | _O(log(n))_ | Medium | Binary Tree |
239+
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Java](./Java/Redundant-Connection/redundant-connection.java) | _O(N)_ | _O(N)_ | Medium | Tree, Union Find |
239240

240241
<br/>
241242
<div align="right">

0 commit comments

Comments
 (0)