HAL
subgraph.cpp
Go to the documentation of this file.
2 
5 
6 namespace hal
7 {
8  namespace graph_algorithm
9  {
10  Result<std::unique_ptr<NetlistGraph>> get_subgraph(const NetlistGraph* graph, const std::vector<Gate*>& subgraph_gates)
11  {
12  if (graph == nullptr)
13  {
14  return ERR("graph is a nullptr");
15  }
16 
17  if (subgraph_gates.empty())
18  {
19  return ERR("no subgraph gates provided");
20  }
21 
22  igraph_vector_int_t i_gates;
23  if (auto res = graph->get_vertices_from_gates_igraph(subgraph_gates); res.is_ok())
24  {
25  i_gates = std::move(res.get());
26  }
27  else
28  {
29  return ERR(res.get_error());
30  }
31 
32  auto res = get_subgraph_igraph(graph, &i_gates);
33 
34  igraph_vector_int_destroy(&i_gates);
35 
36  if (res.is_error())
37  {
38  return ERR(res.get_error());
39  }
40 
41  return res;
42  }
43 
44  Result<std::unique_ptr<NetlistGraph>> get_subgraph(const NetlistGraph* graph, const std::set<Gate*>& subgraph_gates)
45  {
46  if (graph == nullptr)
47  {
48  return ERR("graph is a nullptr");
49  }
50 
51  if (subgraph_gates.empty())
52  {
53  return ERR("no subgraph gates provided");
54  }
55 
56  igraph_vector_int_t i_gates;
57  if (auto res = graph->get_vertices_from_gates_igraph(subgraph_gates); res.is_ok())
58  {
59  i_gates = std::move(res.get());
60  }
61  else
62  {
63  return ERR(res.get_error());
64  }
65 
66  auto res = get_subgraph_igraph(graph, &i_gates);
67 
68  igraph_vector_int_destroy(&i_gates);
69 
70  if (res.is_error())
71  {
72  return ERR(res.get_error());
73  }
74 
75  return res;
76  }
77 
78  Result<std::unique_ptr<NetlistGraph>> get_subgraph(const NetlistGraph* graph, const std::vector<u32>& subgraph_vertices)
79  {
80  if (graph == nullptr)
81  {
82  return ERR("graph is a nullptr");
83  }
84 
85  if (subgraph_vertices.empty())
86  {
87  return ERR("no subgraph vertices provided");
88  }
89 
90  igraph_vector_int_t i_gates;
91  if (auto res = igraph_vector_int_init(&i_gates, subgraph_vertices.size()); res != IGRAPH_SUCCESS)
92  {
93  return ERR(igraph_strerror(res));
94  }
95 
96  for (u32 i = 0; i < subgraph_vertices.size(); i++)
97  {
98  VECTOR(i_gates)[i] = subgraph_vertices.at(i);
99  }
100 
101  auto res = get_subgraph_igraph(graph, &i_gates);
102 
103  igraph_vector_int_destroy(&i_gates);
104 
105  if (res.is_error())
106  {
107  return ERR(res.get_error());
108  }
109 
110  return res;
111  }
112 
113  Result<std::unique_ptr<NetlistGraph>> get_subgraph(const NetlistGraph* graph, const std::set<u32>& subgraph_vertices)
114  {
115  if (graph == nullptr)
116  {
117  return ERR("graph is a nullptr");
118  }
119 
120  if (subgraph_vertices.empty())
121  {
122  return ERR("no subgraph vertices provided");
123  }
124 
125  igraph_vector_int_t i_gates;
126  if (auto res = igraph_vector_int_init(&i_gates, subgraph_vertices.size()); res != IGRAPH_SUCCESS)
127  {
128  return ERR(igraph_strerror(res));
129  }
130 
131  u32 i = 0;
132  for (auto it = subgraph_vertices.begin(); it != subgraph_vertices.end(); it++)
133  {
134  VECTOR(i_gates)[i] = *it;
135  i++;
136  }
137 
138  auto res = get_subgraph_igraph(graph, &i_gates);
139 
140  igraph_vector_int_destroy(&i_gates);
141 
142  if (res.is_error())
143  {
144  return ERR(res.get_error());
145  }
146 
147  return res;
148  }
149 
150  Result<std::unique_ptr<NetlistGraph>> get_subgraph_igraph(const NetlistGraph* graph, const igraph_vector_int_t* subgraph_vertices)
151  {
152  if (graph == nullptr)
153  {
154  return ERR("graph is a nullptr");
155  }
156 
157  igraph_vs_t v_sel = igraph_vss_vector(subgraph_vertices);
158  u32 subgraph_size = igraph_vector_int_size(subgraph_vertices);
159 
160  igraph_vector_int_t i_vertex_map;
161  if (auto res = igraph_vector_int_init(&i_vertex_map, subgraph_size); res != IGRAPH_SUCCESS)
162  {
163  igraph_vs_destroy(&v_sel);
164  return ERR(igraph_strerror(res));
165  }
166 
167  igraph_t i_subg;
168  if (const auto res = igraph_induced_subgraph_map(graph->get_graph(), &i_subg, v_sel, IGRAPH_SUBGRAPH_AUTO, nullptr, &i_vertex_map); res != IGRAPH_SUCCESS)
169  {
170  igraph_vs_destroy(&v_sel);
171  igraph_vector_int_destroy(&i_vertex_map);
172  return ERR(igraph_strerror(res));
173  }
174 
175  std::unordered_map<u32, Gate*> nodes_to_gates;
176  if (const auto res = graph->get_gates_from_vertices_igraph(&i_vertex_map); res.is_ok())
177  {
178  std::vector<Gate*> gates = res.get();
179  for (u32 i = 0; i < gates.size(); i++)
180  {
181  nodes_to_gates[i] = gates.at(i);
182  }
183  }
184  else
185  {
186  igraph_destroy(&i_subg);
187  igraph_vs_destroy(&v_sel);
188  igraph_vector_int_destroy(&i_vertex_map);
189  return ERR(res.get_error());
190  }
191 
192  auto subgraph = std::unique_ptr<NetlistGraph>(new NetlistGraph(graph->get_netlist(), std::move(i_subg), std::move(nodes_to_gates)));
193 
194  igraph_vs_destroy(&v_sel);
195  igraph_vector_int_destroy(&i_vertex_map);
196 
197  return OK(std::move(subgraph));
198  }
199  } // namespace graph_algorithm
200 } // namespace hal
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:57
Netlist * get_netlist() const
Get the netlist associated with the netlist graph.
Result< igraph_vector_int_t > get_vertices_from_gates_igraph(const std::vector< Gate * > &gates) const
Get the vertices corresponding to the specified gates.
Result< std::vector< Gate * > > get_gates_from_vertices_igraph(const igraph_vector_int_t *vertices) const
Get the gates corresponding to the specified vertices.
igraph_t * get_graph() const
Get the graph object of the netlist graph.
#define ERR(message)
Definition: result.h:53
#define OK(...)
Definition: result.h:49
Result< std::unique_ptr< NetlistGraph > > get_subgraph_igraph(const NetlistGraph *graph, const igraph_vector_int_t *subgraph_vertices)
Compute the subgraph induced by the specified vertices, including all edges between these vertices.
Definition: subgraph.cpp:150
Result< std::unique_ptr< NetlistGraph > > get_subgraph(const NetlistGraph *graph, const std::vector< Gate * > &subgraph_gates)
Compute the subgraph induced by the specified gates, including all edges between the corresponding ve...
Definition: subgraph.cpp:10
quint32 u32
This file contains the class that holds a netlist graph.
This file contains functions related to subgraphs.