operations_research::ConnectedComponents< NodeIndex, ArcIndex > Class Template Reference

Detailed Description

template<typename NodeIndex, typename ArcIndex>
class operations_research::ConnectedComponents< NodeIndex, ArcIndex >

Template class implementing a Union-Find algorithm with path compression for maintaining the connected components of a graph.

See Cormen et al. 2nd Edition. MIT Press, 2001. ISBN 0-262-03293-7. Chapter 21: Data structures for Disjoint Sets, pp. 498-524. and Tarjan (1975). Efficiency of a Good But Not Linear Set Union Algorithm. Journal of the ACM 22(2):215-225 It is implemented as a template so that the size of NodeIndex can be chosen depending on the size of the graphs considered. The main interest is that arcs do not need to be kept. Thus the memory complexity is O(n) where n is the number of nodes in the graph. The complexity of this algorithm is O(n . alpha(n)) where alpha(n) is the inverse Ackermann function. alpha(n) <= log(log(log(..log(log(n))..) In practice alpha(n) <= 5. See Tarjan and van Leeuwen (1984). Worst-case analysis of set union algorithms. Journal of the ACM 31(2):245-281.

Usage example: ConnectedComponents<int, int> components; components.Init(num_nodes); for (int arc = 0; arc < num_arcs; ++arc) { components.AddArc(tail[arc], head[arc]); } int num_connected_components = components.GetNumberOfConnectedComponents(); if (num_connected_components == 1) { ///< Graph is completely connected. } ///< Group the nodes in the same connected component together. ///< group[class_number][i] contains the i-th node in group class_number. hash_map<int, std::vector<int> > group(num_connected_components); for (int node = 0; node < num_nodes; ++node) { group[components.GetClassRepresentative(node)].push_back(node); }

Keywords: graph, connected components.

Definition at line 67 of file connectivity.h.

Public Member Functions

 ConnectedComponents ()
 
void Init (NodeIndex num_nodes)
 Reserves memory for num_nodes and resets the data structures. More...
 
void AddArc (NodeIndex tail, NodeIndex head)
 Adds the information that NodeIndex tail and NodeIndex head are connected. More...
 
template<typename Graph >
void AddGraph (const Graph &graph)
 Adds a complete StarGraph to the object. More...
 
NodeIndex CompressPath (NodeIndex node)
 Compresses the path for node. More...
 
NodeIndex GetClassRepresentative (NodeIndex node)
 Returns the equivalence class representative for node. More...
 
NodeIndex GetNumberOfConnectedComponents ()
 Returns the number of connected components. More...
 
void MergeClasses (NodeIndex node1, NodeIndex node2)
 Merges the equivalence classes of node1 and node2. More...
 

Constructor & Destructor Documentation

◆ ConnectedComponents()

template<typename NodeIndex, typename ArcIndex>
operations_research::ConnectedComponents< NodeIndex, ArcIndex >::ConnectedComponents ( )
inline

Definition at line 69 of file connectivity.h.

Member Function Documentation

◆ AddArc()

template<typename NodeIndex, typename ArcIndex>
void operations_research::ConnectedComponents< NodeIndex, ArcIndex >::AddArc ( NodeIndex  tail,
NodeIndex  head 
)
inline

Adds the information that NodeIndex tail and NodeIndex head are connected.

Definition at line 83 of file connectivity.h.

◆ AddGraph()

template<typename NodeIndex, typename ArcIndex>
template<typename Graph >
void operations_research::ConnectedComponents< NodeIndex, ArcIndex >::AddGraph ( const Graph &  graph)
inline

Adds a complete StarGraph to the object.

Note that Depth-First Search is a better algorithm for finding connected components on graphs.

Todo:
(user): implement Depth-First Search-based connected components finder.

Definition at line 95 of file connectivity.h.

◆ CompressPath()

template<typename NodeIndex, typename ArcIndex>
NodeIndex operations_research::ConnectedComponents< NodeIndex, ArcIndex >::CompressPath ( NodeIndex  node)
inline

Compresses the path for node.

Definition at line 106 of file connectivity.h.

◆ GetClassRepresentative()

template<typename NodeIndex, typename ArcIndex>
NodeIndex operations_research::ConnectedComponents< NodeIndex, ArcIndex >::GetClassRepresentative ( NodeIndex  node)
inline

Returns the equivalence class representative for node.

Definition at line 123 of file connectivity.h.

◆ GetNumberOfConnectedComponents()

template<typename NodeIndex, typename ArcIndex>
NodeIndex operations_research::ConnectedComponents< NodeIndex, ArcIndex >::GetNumberOfConnectedComponents ( )
inline

Returns the number of connected components.

Allocates num_nodes_ bits for the computation.

Definition at line 129 of file connectivity.h.

◆ Init()

template<typename NodeIndex, typename ArcIndex>
void operations_research::ConnectedComponents< NodeIndex, ArcIndex >::Init ( NodeIndex  num_nodes)
inline

Reserves memory for num_nodes and resets the data structures.

Definition at line 72 of file connectivity.h.

◆ MergeClasses()

template<typename NodeIndex, typename ArcIndex>
void operations_research::ConnectedComponents< NodeIndex, ArcIndex >::MergeClasses ( NodeIndex  node1,
NodeIndex  node2 
)
inline

Merges the equivalence classes of node1 and node2.

It's faster (~10%) to swap the two values and have a single piece of code for merging the classes.

Definition at line 138 of file connectivity.h.


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