Skip to content

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

Merged
merged 8 commits into from
Aug 12, 2020

Conversation

ruppysuppy
Copy link
Member

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

# 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:
Copy link
Member Author

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

@ruppysuppy
Copy link
Member Author

Could you review the PR please, @cclauss?

Comment on lines 24 to 26
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

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.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!!

Copy link
Member Author

@ruppysuppy ruppysuppy left a 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
Copy link
Member

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)?

Copy link
Member Author

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?

Copy link
Member

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!

Copy link
Member Author

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

@cclauss cclauss merged commit aa46639 into TheAlgorithms:master Aug 12, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants