HAL
components.cpp
Go to the documentation of this file.
2 
4 
5 #include <map>
6 
7 namespace hal
8 {
9  namespace graph_algorithm
10  {
12  {
13  if (graph == nullptr)
14  {
15  return ERR("graph is a nullptr");
16  }
17 
18  igraph_vector_int_t membership;
19  if (auto res = igraph_vector_int_init(&membership, 0); res != IGRAPH_SUCCESS)
20  {
21  return ERR(igraph_strerror(res));
22  }
23 
24  const igraph_t* i_graph = graph->get_graph();
25  if (auto res = igraph_connected_components(i_graph, &membership, nullptr, nullptr, strong ? IGRAPH_STRONG : IGRAPH_WEAK); res != IGRAPH_SUCCESS)
26  {
27  igraph_vector_int_destroy(&membership);
28  return ERR(igraph_strerror(res));
29  }
30 
31  u32 num_vertices = igraph_vcount(i_graph);
32  std::map<u32, std::set<u32>> components_raw;
33 
34  for (i32 i = 0; i < num_vertices; i++)
35  {
36  components_raw[VECTOR(membership)[i]].insert(i);
37  }
38 
39  std::vector<std::vector<u32>> components;
40  for (auto& [_, members] : components_raw)
41  {
42  if (members.size() < min_size)
43  {
44  continue;
45  }
46 
47  components.push_back(std::vector<u32>(members.begin(), members.end()));
48  }
49 
50  igraph_vector_int_destroy(&membership);
51 
52  return OK(components);
53  }
54  } // namespace graph_algorithm
55 } // namespace hal
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:57
igraph_t * get_graph() const
Get the graph object of the netlist graph.
This file contains functions related to graph components.
int32_t i32
Definition: defines.h:36
#define ERR(message)
Definition: result.h:53
#define OK(...)
Definition: result.h:49
Result< std::vector< std::vector< u32 > > > get_connected_components(const NetlistGraph *graph, bool strong, u32 min_size=0)
Compute the (strongly) connected components of the specified graph.
Definition: components.cpp:11
quint32 u32
This file contains the class that holds a netlist graph.