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.
This change dramatically improves the performance of dfs on dense graphs. I've also added some more performance tests.
On the Dfs we can't mark as visited a node until we pop it from the stack (unlike Bfs). This means that we potentially add a node to the stack several times until the first time we pop it.
Before this PR:
For each time we pop a vertex v from the stack, we push its un-visited neighbors on the stack. For the first time we visit v this is fine, but on the following times, we will be pushing vertices that are already pushed on the stack. Those vertices, in turn, have also been pushed twice or more on the stack, so the same problem happens for them.
After this PR:
We don't push to the stack the neighbors of vertices that have already been visited.
Notes:
This is the reason why #55 improved performance over master dramatically on the complete graph dfs test: I've also introduced this improvement there without noticing.
As food for thought about the discussion on #55: notice that this improvement is implemented both in the
goalTest:andtoIndex:versions of the dfs, but I could only add a unit test on thegoalTest:version. ThetoIndex:version does not allow to inject anything that gets called over all the visited vertices. Both versions share 95% of code, but one of them cannot be tested on this behaviour. On the other hand, modifying the code to make it testable introduces performance degradation. Unsettling...