Skip to content

Added Borůvka's algorithm.#4645

Merged
l3str4nge merged 14 commits intoTheAlgorithms:masterfrom
srkchowdary2000:master
Aug 23, 2021
Merged

Added Borůvka's algorithm.#4645
l3str4nge merged 14 commits intoTheAlgorithms:masterfrom
srkchowdary2000:master

Conversation

@srkchowdary2000
Copy link
Copy Markdown
Contributor

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}.

@ghost ghost added awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html labels Aug 21, 2021
Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.


class Graph:
"""Class Graph."""
def __init__(self, num_of_nodes):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: num_of_nodes

self.m_edges = []
self.m_component = {}

def add_edge(self, u, v, weight):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: add_edge. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: v

Please provide type hint for the parameter: weight


self.m_edges.append([u, v, weight])

def find_component(self, u):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: find_component. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

return u
return self.find_component(self.m_component[u])

def set_component(self, u):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: set_component. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

for k in self.m_component.keys():
self.m_component[k] = self.find_component(k)

def union(self, component_size, u, v):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: union. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: component_size

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: v


print(self.m_component)

def boruvka(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: boruvka. If the function does not return a value, please provide the type hint as: def function() -> None:

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

class Graph:
"""Class Graph."""

def __init__(self, num_of_nodes) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide type hint for the parameter: num_of_nodes

self.m_edges = []
self.m_component = {}

def add_edge(self, u, v, weight) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: v

Please provide type hint for the parameter: weight


self.m_edges.append([u, v, weight])

def find_component(self, u) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

return u
return self.find_component(self.m_component[u])

def set_component(self, u) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

for k in self.m_component.keys():
self.m_component[k] = self.find_component(k)

def union(self, component_size, u, v) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide type hint for the parameter: component_size

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: v

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label Aug 21, 2021
Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

self.m_edges = []
self.m_component = {}

def add_edge(self, u: int, v: int, weight: int) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: u

Please provide descriptive name for the parameter: v


self.m_edges.append([u, v, weight])

def find_component(self, u: int) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: u

return u
return self.find_component(self.m_component[u])

def set_component(self, u: int) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: u

for k in self.m_component.keys():
self.m_component[k] = self.find_component(k)

def union(self, component_size: list, u: int, v: int) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: u

Please provide descriptive name for the parameter: v

@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

@ghost ghost removed the require descriptive names This PR needs descriptive function and/or variable names label Aug 21, 2021
@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

This a Borůvka's algorithm, a simple greedy algorithm to find the Minimum spanning tree of a grap. It is also the oldest know graph algorithm, works as efficient as Kruskal's and Prim's algorithm.
Please review my code contribution.
@l3str4nge

@ghost ghost added the tests are failing Do not merge until tests pass label Aug 21, 2021
Copy link
Copy Markdown
Member

@l3str4nge l3str4nge left a comment

Choose a reason for hiding this comment

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

Please fix the tests :)

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Aug 21, 2021
@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Aug 21, 2021
@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Hi, I have Fixed the Tests. Can you please review the code. Thank you.
@l3str4nge

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Final Changes for Test Case Errors.

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Test Case Errors Solved.

@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Hi, Can you please Review my Code.
@l3str4nge

@l3str4nge
Copy link
Copy Markdown
Member

Please reformat with black because of precommit build

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Final

@l3str4nge
Copy link
Copy Markdown
Member

Not really :)

@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Even Spaces are not acceptable, is it? 😔
R

@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Rules are Strict. 😂

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Precommiting seems difficult than writing Algorithm. Now Final Review Please.
@l3str4nge

Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Reduced Each Line Length, Code Formatting.
@l3str4nge

…panning tree. Solved Test Cases Errors.Removed WhiteSpaces.
Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Removed WhiteSpaces.

@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Final Review Please.
@l3str4nge

@ghost ghost removed the tests are failing Do not merge until tests pass label Aug 23, 2021


class Graph:
"""Class Graph."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unnecessary comment.

def set_component(self, u_node: int) -> None:
"""Finds the component index of a given node"""

if self.m_component[u_node] == u_node:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be just self.m_component[u_node] != u_node: and then we don't need else

component_size[u_node] += component_size[v_node]
self.set_component(v_node)

print(self.m_component)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove this print statement

num_of_components = self.m_v

while num_of_components > 1:
for i in range(len(self.m_edges)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Calling len() each iteration is inefficient.

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Aug 23, 2021
@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Aug 23, 2021
Copy link
Copy Markdown
Contributor Author

@srkchowdary2000 srkchowdary2000 left a comment

Choose a reason for hiding this comment

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

Requested code changes done. Please review my code.
@l3str4nge

Copy link
Copy Markdown
Member

@l3str4nge l3str4nge left a comment

Choose a reason for hiding this comment

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

Nice 🚀

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Aug 23, 2021
@l3str4nge l3str4nge merged commit 4ed7c7f into TheAlgorithms:master Aug 23, 2021
@srkchowdary2000
Copy link
Copy Markdown
Contributor Author

Can I write python code from scratch for Singular Vector Decompositions and Principal Component Analysis?

Thank you So Much.
@l3str4nge

shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Added Borůvka's algorithm.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Solved Test Cases Errors.Removed WhiteSpaces.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Code Changes.

* Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Code Changes.
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.

2 participants