-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added Kruskal's Algorithm (more organized than the one present) #2218
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
Conversation
# create a new set with x as its member | ||
self.map[x] = Disjoint_Set_Tree_Node(x) | ||
|
||
def find_set(self, x): | ||
def find_set(self, x: int) -> Disjoint_Set_Tree_Node: |
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.
Missed the type hints in a few places, will add them
Could you review the PR please, @cclauss? |
if self.map[x] != self.map[x].parent: | ||
self.map[x].parent = self.find_set(self.map[x].parent.key) | ||
return self.map[x].parent |
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.
if self.map[x] != self.map[x].parent: | |
self.map[x].parent = self.find_set(self.map[x].parent.key) | |
return self.map[x].parent | |
self_map_x = self.map[x] | |
if self_map_x != self_map_x.parent: | |
self_map_x.parent = self.find_set(self_map_x.parent.key) | |
return self_map_x.parent |
OPTIONAL: You might gain a bit of performance...
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Loops
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'm keeping it as list comprehension, converting
[disjoint_set.make_set(node) for node in self.connections]
to
map(disjoint_set.make_set, self.connections.keys())
yields:
**********************************************************************
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 77, in __main__.GraphUndirectedWeighted.kruskal
Failed example:
mst = graph.kruskal()
Exception raised:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.GraphUndirectedWeighted.kruskal[7]>", line 1, in <module>
mst = graph.kruskal()
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 100, in kruskal
parentu = disjoint_set.find_set(u)
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 24, in find_set
elem_ref = self.map[x]
KeyError: 1
**********************************************************************
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 78, in __main__.GraphUndirectedWeighted.kruskal
Failed example:
assert 5 not in mst.connections[3]
Exception raised:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.GraphUndirectedWeighted.kruskal[8]>", line 1, in <module>
assert 5 not in mst.connections[3]
NameError: name 'mst' is not defined
**********************************************************************
1 items had failures:
2 of 9 in __main__.GraphUndirectedWeighted.kruskal
***Test Failed*** 2 failures.
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.
Cool!!
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 naming convention is a bit a bit off
# add a node ONLY if its not present in the graph | ||
if node not in self.connections: | ||
self.connections[node] = {} | ||
self.nodes += 1 |
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.
Under what circumstances is self.nodes != len(self.connections)?
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.
Yeah its always same. Should I remove it?
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.
Your call. You already have my approval so you can squash & merge whenever you want (as long as the tests are green). Well done!
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 have made the changes, but I cannot merge since I'm not a member
…lgorithms#2218) * Added Kruskal's Algorithm * Added Type Hints * fixup! Format Python code with psf/black push * Added Type Hints V2 * Implemented suggestions + uniform naming convention * removed redundant variable (self.nodes) * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.