Skip to content

Changed how the Visited nodes are tracked #3811

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 1 commit into from
Nov 21, 2020

Conversation

akashgkrishnan
Copy link
Contributor

@akashgkrishnan akashgkrishnan commented Oct 28, 2020

Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows

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

Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited  from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

Thank you for your pull request!🤩

@poyea poyea added the hacktoberfest-accepted Accepted to be counted towards Hacktoberfest label Oct 29, 2020
@dhruvmanila
Copy link
Member

dhruvmanila commented Nov 8, 2020

Can you show us some data regarding the change? Data might include the difference in time in running the same algorithm on the same input with the same configuration if any.

Also, I don't know why you removed the newlines.

@dhruvmanila
Copy link
Member

I don't see the difference you mentioned O(n) -> O(1):

$ time python graphs/bfs_shortest_path.py  # with set
['G', 'C', 'A', 'B', 'D']
4

real    0m0.094s
user    0m0.044s
sys     0m0.036s


$ time python graphs/bfs_shortest_path.py  # with list
['G', 'C', 'A', 'B', 'D']
4

real    0m0.097s
user    0m0.046s
sys     0m0.036s

@akashgkrishnan
Copy link
Contributor Author

@dhruvmanila running on smaller inputs will yield similar results. the speed will be significantly faster if the graph has a 10^5 nodes

As you know the lookup time in an array is O(N) if we are using the in keyword.

4 in [1,2,3,4] 

will be O(N). getting items based on an index is O(1) other than that it is O(N) if we are using the in keyword

sets

4 in {1,2,3,4} 

will be O(1) as sets use hash function and store the items for fast lookups the only caveat being that all items need to be unique but here in this implementation that would be the same case even if we use the list ds

https://wiki.python.org/moin/TimeComplexity
the same is specified here.
you can check the time complexity of the in for both array and sets here

@dhruvmanila
Copy link
Member

If possible can you show us with some practical examples?
Also, please restore all the newlines.

Copy link

@theroyakash theroyakash left a comment

Choose a reason for hiding this comment

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

looks okay

@dhruvmanila dhruvmanila merged commit fa364df into TheAlgorithms:master Nov 21, 2020
@akashgkrishnan akashgkrishnan deleted the patch-4 branch November 21, 2020 11:30
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited  from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited  from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited  from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited  from O(N) to O(1)
as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accepted to be counted towards Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants