Repository navigation
Improved performance of topological_sort and topological_sort_recursive. - #763
Merged
Merged
Conversation
topological_sort - Replaced list.insert with list.append - Replaced dict with set topological_sort_recursive - Replaced list.insert with list.append - Replaced a linear search with a constant time search.
Member
|
Great, thanks! Good idea to use append() rather than insert(). If this passes our tests we can merge it ASAP. |
Member
|
All tests pass for me. I'll go ahead and merge it. Probably, there are other algorithms where similar improvements could be achieved, especially with respect to the "in" operation. Also, I will add a Wiki page which shows how to run tests on a pull request. |
chebee7i
added a commit
that referenced
this pull request
Sep 9, 2012
Improved performance of topological_sort and topological_sort_recursive.
Member
|
Actually, it doesn't look like the wiki is enabled for the NetworkX repository. Where should I put this information? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current implementations of topological_sort and topological_sort_recursive are slow because they do not take into consideration how list is implemented in CPython (it is implemented as an array).
Instead of inserting at the beginning of a list, it is preferable to insert at the end (append) and then reverse the list once at the end of the algorithm.
Moreover, "in" on a list takes O(N) as opposed to O(1) on a set.
On a randomly generated dag with 199496 vertices and 600000 arcs, topological_sort took ~15 seconds and topological_sort_recursive took > 15 minutes. Now they take less than a second. See https://github.com/larose/nx-topological-sort-benchmark