-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Conversation
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
There was a problem hiding this 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!🤩
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. |
I don't see the difference you mentioned $ 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 |
@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.
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
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 |
If possible can you show us with some practical examples? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks okay
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
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
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
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
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:
Checklist:
Fixes: #{$ISSUE_NO}
.