Implement connected_subgraphs()
#35
Merged
+78
−0
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 PR introduces
connected_subgraphs()
which generates all the connected subgraphs of a given graph. I needed this for my applications, and I was unable to find anything similar even in Python's NetworkX.Apparently, there does not seem to be a trivial algorithm for that. Intuitive solution of checking each of 2^N vertex subsets for connectivity is quite expensive and inefficient for sparse graphs.
My implementation is based on a modified BFS starting at each vertex and forking off on every neighbour. Intuitively (= no proof) this should work on directed and multiedge graphs, as
neighbours()
is used in this modified BFS.Current implementation could be made more efficient by using something else than the difference of
Set::Object
objects to find the next expansion tier for the modified BFS. Maybe a matrix form. But for small graphs this does the trick.