From 5d02103b273dffb2637264c0fc2136ca4fd41b57 Mon Sep 17 00:00:00 2001 From: Jogendra Singh <58473917+Joe-Sin7h@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:24:38 +0530 Subject: [PATCH] Fixed #4764 (#4779) * Fixed #4764 * Fixes #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 a93b89621..bf5ab415d 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