From 995b1ec5295cd7c58e48ab881c7bf2a8b2f106cd Mon Sep 17 00:00:00 2001 From: Joe-Sin7h Date: Fri, 24 Sep 2021 13:03:17 +0530 Subject: [PATCH 1/2] Fixed #4764 --- data_structures/disjoint_set/disjoint_set.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/data_structures/disjoint_set/disjoint_set.py b/data_structures/disjoint_set/disjoint_set.py index a93b89621c4a..462b0e366231 100644 --- a/data_structures/disjoint_set/disjoint_set.py +++ b/data_structures/disjoint_set/disjoint_set.py @@ -26,7 +26,10 @@ def union_set(x, y): disjoint set tree will be more flat. """ x, y = find_set(x), find_set(y) - if x.rank > y.rank: + if x==y: + return + + elif x.rank > y.rank: y.parent = x else: x.parent = y From a0638ffe3ca369cb112fd44b44532dd51b9a6050 Mon Sep 17 00:00:00 2001 From: Jogendra Singh <58473917+Joe-Sin7h@users.noreply.github.com> Date: Fri, 24 Sep 2021 13:12:54 +0530 Subject: [PATCH 2/2] Fixes #4764 --- data_structures/disjoint_set/disjoint_set.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/disjoint_set/disjoint_set.py b/data_structures/disjoint_set/disjoint_set.py index 462b0e366231..bf5ab415d5e4 100644 --- a/data_structures/disjoint_set/disjoint_set.py +++ b/data_structures/disjoint_set/disjoint_set.py @@ -26,7 +26,7 @@ def union_set(x, y): disjoint set tree will be more flat. """ x, y = find_set(x), find_set(y) - if x==y: + if x == y: return elif x.rank > y.rank: