Skip to content
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

Fix bfs iterator for multiple source nodes #382

Merged
merged 16 commits into from
Jun 26, 2024
Merged

Conversation

Tortar
Copy link
Contributor

@Tortar Tortar commented Jun 4, 2024

I noticed that the previous implementation of multi source bfs was wrong, because it didn't start with the first level nodes (also my fault :( ), this should be correct instead, and also faster than the one in #381. I see a 1.7x improvement over the previous version on a erdos_renyi(1000000, 0.00001) starting from a random node.

There is still a problem though, I think multi-source dfs suffers from a similar problem. But I unfortunately don't have time to fix it at the moment.

Copy link

codecov bot commented Jun 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.31%. Comparing base (43f9f18) to head (5d1275c).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #382      +/-   ##
==========================================
- Coverage   97.31%   97.31%   -0.01%     
==========================================
  Files         120      120              
  Lines        6954     6953       -1     
==========================================
- Hits         6767     6766       -1     
  Misses        187      187              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Tortar
Copy link
Contributor Author

Tortar commented Jun 18, 2024

Gentle bump

@Tortar
Copy link
Contributor Author

Tortar commented Jun 19, 2024

Hi @gdalle @simonschoelly sorry for the ping but I think this one would be good for a patch version release, if you have some time to spend reviewing it

@@ -35,7 +35,7 @@
end
end
nodes_visited = collect(BFSIterator(g2, [1, 6]))
@test nodes_visited == [1, 2, 3, 6, 5, 7, 4]
@test nodes_visited == [1, 6, 2, 3, 5, 7, 4]
Copy link
Member

Choose a reason for hiding this comment

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

If these tests are sensitive to the ordering at each level, which is an implementation detail, can you rework them to make them independent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit unsure if the ordering on each level could be considered an implementation detail, if this is it we could speed-up the running time by 2x by ordering the level nodes (for cache locality reasons I presume), but this is actually wrong because you want to follow strictly how bfs works, which is to look for each of the neighbors of a certain node in the previous level and just then go to the next node of the previous level

Copy link
Member

Choose a reason for hiding this comment

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

If you consider two graph structures where the neighbors of each vertex make up the same (mathematical) set but are stored in different orders, the BFS algorithm will return different things for node_visited. And they will both be correct. So our tests should be agnostic to that

Copy link
Contributor Author

@Tortar Tortar Jun 21, 2024

Choose a reason for hiding this comment

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

yes I mean I think you are right that we should be agnostic to that, just to clarify what I mean:

         0 
      /      \
    1         2
   /  \     /  \
  3    4    5    6

given a graph like this if we start at node 0 it is okay to have e.g. 0, 1, 2, 3, 4, 5, 6 or 0, 2, 1, 5, 6, 3, 4 but not 0, 1, 2, 5, 6, 3, 4 or 0, 1, 2, 5, 3, 4, 6.

But I think this is what you are actually saying in your last comment, so we need to have tests which are okay with all acceptable versions.

Copy link
Member

Choose a reason for hiding this comment

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

Exactly. It's a bit of a pain so I'm not making it strictly necessary for the PR to be merged, but essentially in your example we would want to check that the returned vector has the form [.|..|....] where the first subset is {0}, the second is {1, 2} and the third is {3, 4, 5, 6} but in any order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your definition iterates sligthly differently than what I had in mind...but it's totally okay I wanted just to understand which one was preferable and indeed parallelizing the algorithm effectively would require to drop mine. So let's go with yours, I think that we can also get a 2x speed-up by sorting with that :-)

Copy link
Member

Choose a reason for hiding this comment

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

I'm slightly biased by a recent bachelor project I supervised on... parallel BFS ;) check out the repo of my interns https://github.com/KassFlute/ParallelGraphs.jl for a multithreaded and even BLAS-ified version of BFS that is much faster than the one here! ping @KassFlute and @AntoineBut

Copy link
Member

Choose a reason for hiding this comment

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

Gentle ping @Tortar if you want to adjust the tests so that I can merge while it's still fresh in our minds

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be okay now 👍

Copy link
Member

Choose a reason for hiding this comment

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

thanks!

src/iterators/bfs.jl Outdated Show resolved Hide resolved
src/iterators/bfs.jl Outdated Show resolved Hide resolved
src/iterators/bfs.jl Show resolved Hide resolved
src/iterators/bfs.jl Show resolved Hide resolved
Tortar and others added 5 commits June 19, 2024 23:15
Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
@gdalle gdalle merged commit c5ea323 into JuliaGraphs:master Jun 26, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants