-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Add disjoint set #1194
Conversation
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.
This is supercool! Congratulations and thanks for sharing.
A nice addition would be a function like:
def as_python_set(node: Node) -> set:
And an addition test, as_python_set(vertex[0]).is_disjoint(as_python_set(vertex[3]))
https://docs.python.org/3.7/library/stdtypes.html#frozenset.isdisjoint
Thank you for your advice. I have updated the code. |
We should be able to get the whole set by starting at the leaf node. Put this towards the bottom of your file: def as_python_set(node: Node) -> set:
python_set = set()
while True:
python_set.add(node.data)
if node == node.p:
# assert node.rank == 0, f"{python_set} root node has zero rank: {node.rank}"
return python_set
node = node.p
if __name__ == "__main__":
test_disjoint_set()
vertex = [Node(i) for i in range(6)]
for v in vertex:
make_set(v)
union_set(vertex[0], vertex[1])
union_set(vertex[1], vertex[2])
union_set(vertex[3], vertex[4])
union_set(vertex[4], vertex[5])
for node in vertex:
print(node.data, as_python_set(node)) This might help to see issues to fix. Ideally, we would like as_python_set(vertex[2]) to be {0, 1, 2}. The output currently is:
|
Perhaps we are not creating parents with the current code but are creating children instead. ;-) My sense is that rank is not really helping us and could safely be dropped. Goal: Build a Python set from either the root node of a union_set or the leaf node of a union_set. |
I don't think we need a node-to-set method. |
Sorry for being so dense. You are correct. It works perfectly. Here is how I tested it. def find_python_set(node: Node) -> set:
"""
Return a Python Standard Library set that contains i.
"""
sets = ({0, 1, 2}, {3, 4, 5})
for s in sets:
if node.data in s:
return s
raise ValueError(f"{node.data} is not in {sets}")
def test_disjoint_set():
"""
>>> test_disjoint_set()
"""
vertex = [Node(i) for i in range(6)]
for v in vertex:
make_set(v)
union_set(vertex[0], vertex[1])
union_set(vertex[1], vertex[2])
union_set(vertex[3], vertex[4])
union_set(vertex[3], vertex[5])
for node0 in vertex:
for node1 in vertex:
if find_python_set(node0).isdisjoint(find_python_set(node1)):
assert find_set(node0) != find_set(node1)
else:
assert find_set(node0) == find_set(node1) Please put a link to the Uncyclopedia article in a comment at the top of the file and then we can land this. Thanks for your patience. |
Thank you for your reply. |
This reverts commit 01601e6.
* Add disjoint set * disjoint set: add doctest, make code more Pythonic * disjoint set: replace x.p with x.parent * disjoint set: add test and refercence
No description provided.