-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create alternate_disjoint_set.py #2302
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3c058b3
Create alternate_disjoint_set.py
anubabajide d15e3b7
Update alternate_disjoint_set.py
anubabajide 8bc8ba6
Update alternate_disjoint_set.py
anubabajide 1f44ae6
Formatted with Black
anubabajide 604b064
More formatting
anubabajide 427acc3
Formatting on line 28
anubabajide 58a7db5
Error in Doctest
anubabajide e83f714
Doctest Update in alternate disjoint set
anubabajide a4ac86c
Fixed build error
anubabajide 57cec27
Fixed doctest
anubabajide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Implements a disjoint set using Lists and some added heuristics for efficiency | ||
Union by Rank Heuristic and Path Compression | ||
""" | ||
|
||
|
||
class DisjointSet: | ||
def __init__(self, set_counts: list) -> None: | ||
""" | ||
Initialize with a list of the number of items in each set | ||
and with rank = 1 for each set | ||
""" | ||
self.set_counts = set_counts | ||
self.max_set = max(set_counts) | ||
num_sets = len(set_counts) | ||
self.ranks = [1] * num_sets | ||
self.parents = list(range(num_sets)) | ||
|
||
def merge(self, src: int, dst: int) -> bool: | ||
""" | ||
Merge two sets together using Union by rank heuristic | ||
Return True if successful | ||
Merge two disjoint sets | ||
>>> A = DisjointSet([1, 1, 1]) | ||
>>> A.merge(1, 2) | ||
True | ||
>>> A.merge(0, 2) | ||
True | ||
>>> A.merge(0, 1) | ||
False | ||
""" | ||
src_parent = self.get_parent(src) | ||
dst_parent = self.get_parent(dst) | ||
|
||
if src_parent == dst_parent: | ||
return False | ||
|
||
if self.ranks[dst_parent] >= self.ranks[src_parent]: | ||
self.set_counts[dst_parent] += self.set_counts[src_parent] | ||
self.set_counts[src_parent] = 0 | ||
self.parents[src_parent] = dst_parent | ||
if self.ranks[dst_parent] == self.ranks[src_parent]: | ||
self.ranks[dst_parent] += 1 | ||
joined_set_size = self.set_counts[dst_parent] | ||
else: | ||
self.set_counts[src_parent] += self.set_counts[dst_parent] | ||
self.set_counts[dst_parent] = 0 | ||
self.parents[dst_parent] = src_parent | ||
joined_set_size = self.set_counts[src_parent] | ||
|
||
self.max_set = max(self.max_set, joined_set_size) | ||
return True | ||
|
||
def get_parent(self, disj_set: int) -> int: | ||
""" | ||
Find the Parent of a given set | ||
>>> A = DisjointSet([1, 1, 1]) | ||
>>> A.merge(1, 2) | ||
True | ||
>>> A.get_parent(0) | ||
0 | ||
>>> A.get_parent(1) | ||
2 | ||
""" | ||
if self.parents[disj_set] == disj_set: | ||
return disj_set | ||
self.parents[disj_set] = self.get_parent(self.parents[disj_set]) | ||
return self.parents[disj_set] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.