Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded Kruskal's Algorithm (more organized than the one present) #2218
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: |
This comment has been minimized.
This comment has been minimized.
|
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 |
Comment on lines
24
to
26
This comment has been minimized.
This comment has been minimized.
cclauss
Aug 11, 2020
Member
Suggested change
| 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
This comment has been minimized.
This comment has been minimized.
ruppysuppy
Aug 12, 2020
Author
Contributor
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.|
Cool!! |
|
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 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cclauss
Aug 12, 2020
Member
Your call. You already have my approval so you can squash & merge whenever you want (as long as the tests are green). Well done!
This comment has been minimized.
This comment has been minimized.
ruppysuppy
Aug 12, 2020
Author
Contributor
I have made the changes, but I cannot merge since I'm not a member
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ruppysuppy commentedJul 21, 2020
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}.