Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <connected_components.h>
Public Types | |
using | Set |
Public Member Functions | |
ConnectedComponentsFinder () | |
Constructs a connected components finder. | |
ConnectedComponentsFinder (const ConnectedComponentsFinder &)=delete | |
ConnectedComponentsFinder & | operator= (const ConnectedComponentsFinder &)=delete |
void | AddNode (T node) |
bool | AddEdge (T node1, T node2) |
bool | Connected (T node1, T node2) |
int | GetSize (T node) |
std::vector< std::vector< T > > | FindConnectedComponents () |
void | FindConnectedComponents (std::vector< Set > *components) |
int | GetNumberOfComponents () const |
int | GetNumberOfNodes () const |
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. vector<set<MyNodeType> > components; cc.FindConnectedComponents(&components); Each entry in components now contains all the nodes in a single connected component.
Protocol buffers can be used as the node type. Equality and hash functions for protocol buffers can be found in ortools/base/message_hasher.h.
Usage with flat_hash_set: using ConnectedComponentType = flat_hash_set<MyNodeType>; ConnectedComponentsFinder<ConnectedComponentType::key_type, ConnectedComponentType::hasher, ConnectedComponentType::key_equal> cc; ... 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 229 of file connected_components.h.
using ConnectedComponentsFinder< T, CompareOrHashT, Eq >::Set |
Definition at line 231 of file connected_components.h.
|
inline |
Constructs a connected components finder.
Definition at line 236 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. Returns true if the two nodes are newly connected, and false if they were already connected.
Definition at line 250 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 244 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 257 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 278 of file connected_components.h.
|
inline |
Definition at line 286 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 297 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 305 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 265 of file connected_components.h.
|
delete |