-
-
Notifications
You must be signed in to change notification settings - Fork 46.2k
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
Update breadth_first_search_2.py #7765
Conversation
Let's keep both the existing function and the new function in the same file and let's be sure that they are both tested with the same tests and test data so visitors can compare them side-by-side. Let's also add a timeit benchmark so visitors can understand the performance differences between the two implementations. This repo has lots of timeit benchmarks... https://github.com/TheAlgorithms/Python/search?q=timeit |
for more information, see https://pre-commit.ci
I still need to modify the existing function, because it returns traversed elements in the unordered collection. Both functions use exactly the same approach, just different data structure that implements a similar API. The not thread-safe deque beats thread-safe Queue in a single-threaded environment.
|
I do not understand this. Why should the new function force the old function to change? I would think that traversing elements in an unordered collection was the original problem. The benchmark looks awesome. Please put the benchmark results in the comments so that the reader has a sense of with is faster and by how much. |
BFS is an algorithm to traverse the tree in a specific order (gif from wiki). So 2 items are important here that all nodes are visited and the order in which they are visited. The problem with the existing implementation is that it returns all nodes in the collection that does not maintain order and remove duplicates (set). And the test was checking only that all nodes have been visited at least once. I consider this to be a very strange return type for the BFS and I am willing to address it. Another problem is that implementation of the queue data structure that is used in the first function is not really efficient for such tasks. It's like hammering nails with a microscope. I'd prefer o keep only one implementation with a more suitable data structure. |
Maintaining the breadth-first order is vital but the wiki definition says nothing about eliminating duplicate values. Having only a single implementation is not an option. |
Yes, because these algorithm does not suppose to have any duplication during traversal. Using set as a return value does not allow testing this.
Do not worry about it, there are at least 5 files related to it in the repository. These two function does not bring any diversity to the implementation, they are pretty much the same. I could keep both implementations, but using Queue here is a bad example. |
Describe your change:
set
tolist
. Order of traversal is important for BFS, set is not good data structure to represent it.Checklist:
Fixes: #{$ISSUE_NO}
.