38#ifndef UTIL_GRAPH_CONNECTED_COMPONENTS_H_
39#define UTIL_GRAPH_CONNECTED_COMPONENTS_H_
48#include "absl/container/flat_hash_map.h"
49#include "absl/container/flat_hash_set.h"
50#include "absl/hash/hash.h"
51#include "absl/meta/type_traits.h"
58template <
class UndirectedGraph,
class NodeType>
60 const UndirectedGraph& graph);
77template <
class UndirectedGraph>
79 const UndirectedGraph& graph) {
103 bool AddEdge(
int node1,
int node2);
130 std::vector<int> parent_;
133 std::vector<int> component_size_;
135 std::vector<int> rank_;
137 int num_components_ = 0;
139 std::vector<int> roots_;
142 int num_nodes_at_last_get_roots_call_ = 0;
148template <
typename T,
typename CompareOrHashT,
typename Eq>
152 template <
typename U,
typename V,
typename E =
void>
154 using Set = std::set<T, CompareOrHashT>;
155 using Map = std::map<T, int, CompareOrHashT>;
164 template <
typename U,
typename V>
167 absl::enable_if_t<std::is_integral<decltype(std::declval<const U&>()(
168 std::declval<const T&>()))>::value &&
169 std::is_same_v<V, void>>> {
170 using Set = absl::flat_hash_set<T, CompareOrHashT>;
171 using Map = absl::flat_hash_map<T, int, CompareOrHashT>;
175 template <
typename U,
typename V>
178 absl::enable_if_t<std::is_integral<decltype(std::declval<const U&>()(
179 std::declval<const T&>()))>::value &&
180 !std::is_same_v<V, void>>> {
181 using Set = absl::flat_hash_set<T, CompareOrHashT, Eq>;
182 using Map = absl::flat_hash_map<T, int, CompareOrHashT, Eq>;
227template <
typename T,
typename CompareOrHashT = std::less<T>,
244 void AddNode(T node) { LookupOrInsertNode<true>(node); }
251 return delegate_.
AddEdge(LookupOrInsertNode<false>(node1),
252 LookupOrInsertNode<false>(node2));
281 for (
const auto& elem_id : index_) {
282 components[component_ids[elem_id.second]].push_back(elem_id.first);
290 for (
const auto& elem_id : index_) {
291 components->at(component_ids[elem_id.second]).insert(elem_id.first);
310 template <
bool update_delegate>
311 int LookupOrInsertNode(T node) {
312 const auto result = index_.emplace(node, index_.size());
313 const int node_id = result.first->second;
314 if (update_delegate && result.second) {
330template <
class UndirectedGraph,
typename NodeType>
332 const UndirectedGraph& graph) {
335 std::vector<NodeType> component_of_node(num_nodes, num_nodes);
336 std::vector<NodeType> bfs_queue;
337 NodeType num_components = 0;
338 for (NodeType src = 0; src < num_nodes; ++src) {
339 if (component_of_node[src] != num_nodes)
continue;
340 bfs_queue.push_back(src);
341 component_of_node[src] = num_components;
342 for (
size_t num_visited = 0; num_visited < bfs_queue.size();
344 const NodeType node = bfs_queue[num_visited];
345 for (
const NodeType neighbor : graph[node]) {
346 if (component_of_node[neighbor] != num_nodes)
continue;
347 component_of_node[neighbor] = num_components;
348 bfs_queue.push_back(neighbor);
354 return component_of_node;
ConnectedComponentsFinder(const ConnectedComponentsFinder &)=delete
int GetNumberOfComponents() const
typename internal::ConnectedComponentsTypeHelper< T, CompareOrHashT, Eq >::Set Set
int GetNumberOfNodes() const
bool Connected(T node1, T node2)
void FindConnectedComponents(std::vector< Set > *components)
std::vector< std::vector< T > > FindConnectedComponents()
ConnectedComponentsFinder()
Constructs a connected components finder.
bool AddEdge(T node1, T node2)
ConnectedComponentsFinder & operator=(const ConnectedComponentsFinder &)=delete
A connected components finder that only works on dense ints.
bool Connected(int node1, int node2)
bool AddEdge(int node1, int node2)
int GetNumberOfNodes() const
DenseConnectedComponentsFinder()
void SetNumberOfNodes(int num_nodes)
int GetNumberOfComponents() const
DenseConnectedComponentsFinder(const DenseConnectedComponentsFinder &)=default
We support copy and move construction.
std::vector< int > GetComponentIds()
Returns the same as GetConnectedComponents().
DenseConnectedComponentsFinder & operator=(DenseConnectedComponentsFinder &&)=default
DenseConnectedComponentsFinder & operator=(const DenseConnectedComponentsFinder &)=default
const std::vector< int > & GetComponentRoots()
DenseConnectedComponentsFinder(DenseConnectedComponentsFinder &&)=default
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
A collections of i/o utilities for the Graph classes in ./graph.h.
std::vector< int > GetConnectedComponents(int num_nodes, const UndirectedGraph &graph)
std::vector< NodeType > GetConnectedComponentsTpl(NodeType num_nodes, const UndirectedGraph &graph)
absl::flat_hash_set< T, CompareOrHashT > Set
absl::flat_hash_map< T, int, CompareOrHashT > Map
absl::flat_hash_map< T, int, CompareOrHashT, Eq > Map
absl::flat_hash_set< T, CompareOrHashT, Eq > Set
std::set< T, CompareOrHashT > Set
std::map< T, int, CompareOrHashT > Map
typename SelectContainer< CompareOrHashT, Eq >::Set Set
typename SelectContainer< CompareOrHashT, Eq >::Map Map