Skip to content

Strongly connected components #2114

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 11 commits into from
Jun 17, 2020

Conversation

nikalosa
Copy link
Contributor

Describe your change:

Algorithm for finding out strongly connected components in a graph

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

@TravisBuddy
Copy link

Hey @nikalosa,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 631e3d40-ae34-11ea-b5ce-3bd953fd8c80

@l3str4nge
Copy link
Member

Please fix also flake8 errors: https://travis-ci.com/github/TheAlgorithms/Python/builds/171253892

@nikalosa
Copy link
Contributor Author

nikalosa commented Jun 15, 2020

Please fix also flake8 errors: https://travis-ci.com/github/TheAlgorithms/Python/builds/171253892

That error has already fixed after the first commit.

"""

n = len(graph)

Copy link
Member

Choose a reason for hiding this comment

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

graph_size could be more readable instead n


# describe reversed graph (All edges reversed)
reverse_graph = {vert: [] for vert in range(n)}

Copy link
Member

@l3str4nge l3str4nge Jun 17, 2020

Choose a reason for hiding this comment

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

reverse_graph --> reversed_graph . I think comment above is unnecessary. Variable name defines exactly what is it.

components_list = []
# reinitialize visited list
visited = n * [False]

Copy link
Member

Choose a reason for hiding this comment

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

Remove comments, rename variables better.


"""

test_graph_1 = {
Copy link
Member

Choose a reason for hiding this comment

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

@cclauss Do you have any idea how we can avoid declaring these variables here but keep current doctests?

Copy link
Member

Choose a reason for hiding this comment

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

I think we are OK here. Naming helps a lot so when I see test_graph_1, I immediately know that this data is for testing purposes. If I was going to move this algorithm to some larger program, I will need to do some level of refactoring. I might move all the doctests and the test data to a separate test_xyz.py file or I might remove them altogether. For our purposes in this repo, a single file that contains the algorithm, tests, and test data is good for learning purposes as long as we are diligent with our naming.

"""

visited[vert] = True

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

Comment on lines 79 to 81
n = len(graph)

visited = n * [False]
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
n = len(graph)
visited = n * [False]
visited = len(graph) * [False]

@TravisBuddy
Copy link

Hey @nikalosa,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: b99b17b0-b09b-11ea-b9a3-b9754cbad7e4

Co-authored-by: Christian Clauss <cclauss@me.com>
@cclauss cclauss merged commit fb3a228 into TheAlgorithms:master Jun 17, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Implement strongly connected components for graph algorithms

* fixup! Format Python code with psf/black push

* Delete trailing whitespace

* updating DIRECTORY.md

* Add doctests and typehints

* Remove unnecessary comments, change variable names

* fixup! Format Python code with psf/black push

* Change undefined variable's name

* Apply suggestions from code review

Co-authored-by: Christian Clauss <cclauss@me.com>

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.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.

4 participants