-
-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Add disjoint set #1194
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
Add disjoint set #1194
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,62 @@ | ||
| class node: | ||
| def __init__(self, data): | ||
| self.data = data | ||
|
|
||
|
|
||
| def makeSet(x): | ||
luoheng23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| make x as a set. | ||
| """ | ||
| # rank is the distance from x to its' parent | ||
| # root's rank is 0 | ||
| x.rank = 0 | ||
| # p is x's parent | ||
| x.p = x | ||
luoheng23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def unionSet(x, y): | ||
luoheng23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| union two sets. | ||
| set with bigger rank should be parent, so that the | ||
| disjoint set tree will be more flat. | ||
| """ | ||
| x, y = findSet(x), findSet(y) | ||
| if x.rank > y.rank: | ||
| y.p = x | ||
| else: | ||
| x.p = y | ||
| if x.rank == y.rank: | ||
| y.rank += 1 | ||
|
|
||
|
|
||
| def findSet(x): | ||
luoheng23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| return the parent of x | ||
| """ | ||
| if x != x.p: | ||
| x.p = findSet(x.p) | ||
| return x.p | ||
|
|
||
|
|
||
| def test_disjoint_set(): | ||
luoheng23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vertex = [node(i) for i in range(6)] | ||
| for v in vertex: | ||
| makeSet(v) | ||
| unionSet(vertex[0], vertex[1]) | ||
| unionSet(vertex[1], vertex[2]) | ||
| unionSet(vertex[3], vertex[4]) | ||
| unionSet(vertex[3], vertex[5]) | ||
| # now there are two sets: {1, 2, 3}, {4, 5, 6} | ||
| assert findSet(vertex[0]) == findSet(vertex[1]) | ||
| assert findSet(vertex[1]) == findSet(vertex[2]) | ||
| assert findSet(vertex[2]) != findSet(vertex[3]) | ||
| assert findSet(vertex[3]) == findSet(vertex[4]) | ||
| assert findSet(vertex[4]) == findSet(vertex[5]) | ||
|
|
||
|
|
||
| def main(): | ||
| test_disjoint_set() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
luoheng23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.