ConnectedComponentsFinder< T, CompareOrHashT > Class Template Reference

Detailed Description

template<typename T, typename CompareOrHashT = std::less<T>>
class ConnectedComponentsFinder< T, CompareOrHashT >

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
 
ConnectedComponentsFinderoperator= (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...
 

Constructor & Destructor Documentation

◆ ConnectedComponentsFinder() [1/2]

template<typename T , typename CompareOrHashT = std::less<T>>
ConnectedComponentsFinder< T, CompareOrHashT >::ConnectedComponentsFinder ( )
inline

Constructs a connected components finder.

Definition at line 192 of file connected_components.h.

◆ ConnectedComponentsFinder() [2/2]

template<typename T , typename CompareOrHashT = std::less<T>>
ConnectedComponentsFinder< T, CompareOrHashT >::ConnectedComponentsFinder ( const ConnectedComponentsFinder< T, CompareOrHashT > &  )
delete

Member Function Documentation

◆ AddEdge()

template<typename T , typename CompareOrHashT = std::less<T>>
void ConnectedComponentsFinder< T, CompareOrHashT >::AddEdge ( node1,
node2 
)
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.

◆ AddNode()

template<typename T , typename CompareOrHashT = std::less<T>>
void ConnectedComponentsFinder< T, CompareOrHashT >::AddNode ( node)
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.

◆ Connected()

template<typename T , typename CompareOrHashT = std::less<T>>
bool ConnectedComponentsFinder< T, CompareOrHashT >::Connected ( node1,
node2 
)
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.

◆ FindConnectedComponents() [1/2]

template<typename T , typename CompareOrHashT = std::less<T>>
std::vector<std::vector<T> > ConnectedComponentsFinder< T, CompareOrHashT >::FindConnectedComponents ( )
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:

  • 'c' belongs to the same component as a node 'a' added before 'b', or
  • the component for 'c' comes after the one for 'b'. There are two versions:
  • The first one returns the result, and stores each component in a vector. This is the preferred version.
  • The second one populates the result, and stores each component in a set.

Definition at line 232 of file connected_components.h.

◆ FindConnectedComponents() [2/2]

template<typename T , typename CompareOrHashT = std::less<T>>
void ConnectedComponentsFinder< T, CompareOrHashT >::FindConnectedComponents ( std::vector< typename internal::ConnectedComponentsTypeHelper< T, CompareOrHashT >::Set > *  components)
inline

Definition at line 240 of file connected_components.h.

◆ GetNumberOfComponents()

template<typename T , typename CompareOrHashT = std::less<T>>
int ConnectedComponentsFinder< T, CompareOrHashT >::GetNumberOfComponents ( ) const
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.

◆ GetNumberOfNodes()

template<typename T , typename CompareOrHashT = std::less<T>>
int ConnectedComponentsFinder< T, CompareOrHashT >::GetNumberOfNodes ( ) const
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.

◆ GetSize()

template<typename T , typename CompareOrHashT = std::less<T>>
int ConnectedComponentsFinder< T, CompareOrHashT >::GetSize ( node)
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.

◆ operator=()

template<typename T , typename CompareOrHashT = std::less<T>>
ConnectedComponentsFinder& ConnectedComponentsFinder< T, CompareOrHashT >::operator= ( const ConnectedComponentsFinder< T, CompareOrHashT > &  )
delete

The documentation for this class was generated from the following file: