Skip to content

Commit 0d30bfd

Browse files
committed
added function to get all (used) vertices
1 parent 0324065 commit 0d30bfd

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

plugins/graph_algorithm/include/graph_algorithm/netlist_graph.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ namespace hal
174174
*/
175175
u32 get_num_edges() const;
176176

177+
/**
178+
* Get the vertices in the netlist graph.
179+
*
180+
* @param[in] only_connected - Set `true` to only return vertices connected to at least one edge, `false` otherwise. Defaults to `false`.
181+
* @returns A vector of vertices on success, an error otherwise.
182+
*/
183+
Result<std::vector<u32>> get_vertices(bool only_connected = false) const;
184+
177185
/**
178186
* Get the edges between vertices in the netlist graph.
179187
*

plugins/graph_algorithm/python/python_bindings.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,29 @@ namespace hal
292292
:rtype: int
293293
)");
294294

295+
py_netlist_graph.def(
296+
"get_vertices",
297+
[](const graph_algorithm::NetlistGraph& self, bool only_connected = false) -> std::optional<std::vector<u32>> {
298+
auto res = self.get_vertices(only_connected);
299+
if (res.is_ok())
300+
{
301+
return res.get();
302+
}
303+
else
304+
{
305+
log_error("python_context", "error encountered while getting vertices:\n{}", res.get_error().get());
306+
return std::nullopt;
307+
}
308+
},
309+
py::arg("only_connected") = false,
310+
R"(
311+
Get the vertices in the netlist graph.
312+
313+
:param bool only_connected: Set ``True`` to only return vertices connected to at least one edge, ``False`` otherwise. Defaults to ``False``.
314+
:returns: A list of vertices on success, ``None`` otherwise.
315+
:rtype: list[int] or None
316+
)");
317+
295318
py_netlist_graph.def(
296319
"get_edges",
297320
[](const graph_algorithm::NetlistGraph& self) -> std::optional<std::vector<std::pair<u32, u32>>> {

plugins/graph_algorithm/src/netlist_graph.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,58 @@ namespace hal
396396
return igraph_ecount(&m_graph);
397397
}
398398

399+
Result<std::vector<u32>> NetlistGraph::get_vertices(bool only_connected) const
400+
{
401+
u32 num_vertices = igraph_vcount(&m_graph);
402+
403+
if (!only_connected)
404+
{
405+
std::vector<u32> vertices(num_vertices);
406+
for (u32 i = 0; i < num_vertices; i++)
407+
{
408+
vertices[i] = i;
409+
}
410+
return OK(vertices);
411+
}
412+
else
413+
{
414+
std::vector<u32> vertices;
415+
416+
igraph_vector_int_t degrees;
417+
if (auto res = igraph_vector_int_init(&degrees, num_vertices); res != IGRAPH_SUCCESS)
418+
{
419+
return ERR(igraph_strerror(res));
420+
}
421+
422+
igraph_vs_t v_sel;
423+
if (auto res = igraph_vs_all(&v_sel); res != IGRAPH_SUCCESS)
424+
{
425+
igraph_vector_int_destroy(&degrees);
426+
return ERR(igraph_strerror(res));
427+
}
428+
429+
if (auto res = igraph_degree(&m_graph, &degrees, v_sel, IGRAPH_ALL, IGRAPH_LOOPS); res != IGRAPH_SUCCESS)
430+
{
431+
igraph_vs_destroy(&v_sel);
432+
igraph_vector_int_destroy(&degrees);
433+
return ERR(igraph_strerror(res));
434+
}
435+
436+
for (u32 i = 0; i < num_vertices; i++)
437+
{
438+
if (VECTOR(degrees)[i] != 0)
439+
{
440+
vertices.push_back(i);
441+
}
442+
}
443+
444+
igraph_vector_int_destroy(&degrees);
445+
igraph_vs_destroy(&v_sel);
446+
447+
return OK(vertices);
448+
}
449+
}
450+
399451
Result<std::vector<std::pair<u32, u32>>> NetlistGraph::get_edges() const
400452
{
401453
const u32 ecount = igraph_ecount(&m_graph);

0 commit comments

Comments
 (0)