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.
I've tried
petgraph
for a little comparison of my C++ code and I noticed that the connected_components algorithm returns only the number of those components without having a way to access the items of such components.Another problem is that the
labels
variable has gaps between the elements (eg. in my test it contained[0, 0, 0, 3, 3, 3]
) so just returning it as is isn't feasible.Since it seemed a fairly easy task, I decided to implement it myself.
This new function returns the connected components of the graph similar to boost's connected_components: docs, usage.
The common code has been split in a new private function connected_components_inner.
I'm not really happy about using
HashMap
here, hopefully performance isn't too bad in real use cases...Maybe it can be improved in the future, if needed?
From the tests I've done, I also assumed that the
labels
variable returns aVec
mapping each node to its assigned components such that each position oflabels
maps to the node's index (ie.labels[i] <-> nodes[i]
).Let me know if the quality of the PR is subpar or just not needed.
Thank you!
TODO
labels
' indices referring tonodes
(ie.labels[i] <-> nodes[i]
)?EXAMPLE USAGE