Usage: ConnectedComponentsFinder<MyNodeType> cc; cc.AddNode(node1); cc.AddNode(node2); cc.AddEdge(node1, node2); ...
repeating, adding nodes and edges as needed. Adding an edge will automatically also add the two nodes at its ends, if they haven't already been added. std::vector<std::set<MyNodeType> > components; cc.FindConnectedComponents(&components); Each entry in components now contains all the nodes in a single connected component.
Usage with flat_hash_set: using ConnectedComponentType = flat_hash_set<MyNodeType>; ConnectedComponentsFinder<ConnectedComponentType::key_type, ConnectedComponentType::hasher> cc; ... std::vector<ConnectedComponentType> components; cc.FindConnectedComponents(&components);
If you want to, you can continue adding nodes and edges after calling FindConnectedComponents, then call it again later.
If your node type isn't STL-friendly, then you can use pointers to it instead: ConnectedComponentsFinder<MySTLUnfriendlyNodeType*> cc; cc.AddNode(&node1); ... and so on... Of course, in this usage, the connected components finder retains these pointers through its lifetime (though it doesn't dereference them).
Definition at line 189 of file connected_components.h.
Public Member Functions | |
| ConnectedComponentsFinder () | |
| Constructs a connected components finder. More... | |
| ConnectedComponentsFinder (const ConnectedComponentsFinder &)=delete | |
| ConnectedComponentsFinder & | operator= (const ConnectedComponentsFinder &)=delete |
| void | AddNode (T node) |
| Adds a node in the graph. More... | |
| void | AddEdge (T node1, T node2) |
| Adds an edge in the graph. More... | |
| bool | Connected (T node1, T node2) |
| Returns true iff both nodes are in the same connected component. More... | |
| int | GetSize (T node) |
| Finds the connected component containing a node, and returns the total number of nodes in that component. More... | |
| std::vector< std::vector< T > > | FindConnectedComponents () |
| Finds all the connected components and assigns them to components. More... | |
| void | FindConnectedComponents (std::vector< typename internal::ConnectedComponentsTypeHelper< T, CompareOrHashT >::Set > *components) |
| int | GetNumberOfComponents () const |
| Returns the current number of connected components. More... | |
| int | GetNumberOfNodes () const |
| Returns the current number of added distinct nodes. More... | |
|
inline |
Constructs a connected components finder.
Definition at line 192 of file connected_components.h.
|
delete |
|
inline |
Adds an edge in the graph.
Also adds both endpoint nodes as necessary. It is not an error to add the same edge twice. Self-edges are OK too.
Definition at line 204 of file connected_components.h.
|
inline |
Adds a node in the graph.
It is OK to add the same node more than once; additions after the first have no effect.
Definition at line 200 of file connected_components.h.
|
inline |
Returns true iff both nodes are in the same connected component.
Returns false if either node has not been already added with AddNode.
Definition at line 211 of file connected_components.h.
|
inline |
Finds all the connected components and assigns them to components.
Components are ordered in the same way nodes were added, i.e. if node 'b' was added before node 'c', then either:
Definition at line 232 of file connected_components.h.
|
inline |
Definition at line 240 of file connected_components.h.
|
inline |
Returns the current number of connected components.
This number can change as the new nodes or edges are added.
Definition at line 253 of file connected_components.h.
|
inline |
Returns the current number of added distinct nodes.
This includes nodes added explicitly via the calls to AddNode() method and implicitly via the calls to AddEdge() method. Nodes that were added several times only count once.
Definition at line 261 of file connected_components.h.
|
inline |
Finds the connected component containing a node, and returns the total number of nodes in that component.
Returns zero iff the node has not been already added with AddNode.
Definition at line 219 of file connected_components.h.
|
delete |