-
Notifications
You must be signed in to change notification settings - Fork 313
Added make_root in DSU
#342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,9 @@ class DisjointSetForest(object): | |
| >>> dst.union(1, 2) | ||
| >>> dst.find_root(2).key | ||
| 1 | ||
| >>> dst.make_root(2) | ||
| >>> dst.find_root(2).key | ||
| 2 | ||
|
|
||
| References | ||
| ========== | ||
|
|
@@ -74,3 +77,18 @@ def union(self, key1, key2): | |
|
|
||
| y_root.parent = x_root | ||
| x_root.size += y_root.size | ||
|
|
||
| def make_root(self, key): | ||
| """ | ||
| Finds the set to which the key belongs | ||
| and makes it as the root of the set. | ||
| """ | ||
| if self.tree.get(key, None) is None: | ||
| raise KeyError("Invalid key, %s"%(key)) | ||
|
|
||
| current_root = self.find_root(key) | ||
| if current_root.key != key: | ||
| key_set = self.tree[key] | ||
| current_root.parent = key_set | ||
| key_set.size = current_root.size | ||
|
||
| key_set.parent = key_set | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature looks good. Could you share an example (book/lecture notes examples will work) where this is used/required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presently, I don't remember the exact the example but, we can consider this example https://cp-algorithms.com/data_structures/disjoint_set_union.html#toc-tgt-10 wherein we generally make root which Is largest element in set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. That's great. Could you solve one such problem using DSU API in
pydatastructsand show the code here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will trying finding the exact problem I solved using this method and show that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this is the problem where I used the above method: https://codeforces.com/contest/1133/problem/F2, and the submission of mine can be found at: https://codeforces.com/contest/1133/submission/109674863