Uh oh!
There was an error while loading. Please reload this page.
Add disjoint set - #1194
Conversation
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
luoheng23
commented
Sep 22, 2019
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: defas_python_set(node: Node) ->set:
python_set=set()
whileTrue:
python_set.add(node.data)
ifnode==node.p:
# assert node.rank == 0, f"{python_set} root node has zero rank: {node.rank}"returnpython_setnode=node.pif__name__=="__main__":
test_disjoint_set()
vertex= [Node(i) foriinrange(6)]
forvinvertex:
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])
fornodeinvertex:
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: |
Uh oh!
There was an error while loading. Please reload this page.
cclauss
commented
Sep 22, 2019
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. |
luoheng23
commented
Sep 22, 2019
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. deffind_python_set(node: Node) ->set:
""" Return a Python Standard Library set that contains i. """sets= ({0, 1, 2}, {3, 4, 5})
forsinsets:
ifnode.datains:
returnsraiseValueError(f"{node.data} is not in {sets}")
deftest_disjoint_set():
""" >>> test_disjoint_set() """vertex= [Node(i) foriinrange(6)]
forvinvertex:
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])
fornode0invertex:
fornode1invertex:
iffind_python_set(node0).isdisjoint(find_python_set(node1)):
assertfind_set(node0) !=find_set(node1)
else:
assertfind_set(node0) ==find_set(node1)Please put a link to the Wikipedia article in a comment at the top of the file and then we can land this. Thanks for your patience. |
luoheng23
commented
Sep 23, 2019
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.