Skip to content

Commit

Permalink
added function to get all (used) vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed May 8, 2024
1 parent 0324065 commit 0d30bfd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ namespace hal
*/
u32 get_num_edges() const;

/**
* Get the vertices in the netlist graph.
*
* @param[in] only_connected - Set `true` to only return vertices connected to at least one edge, `false` otherwise. Defaults to `false`.
* @returns A vector of vertices on success, an error otherwise.
*/
Result<std::vector<u32>> get_vertices(bool only_connected = false) const;

/**
* Get the edges between vertices in the netlist graph.
*
Expand Down
23 changes: 23 additions & 0 deletions plugins/graph_algorithm/python/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ namespace hal
:rtype: int
)");

py_netlist_graph.def(
"get_vertices",
[](const graph_algorithm::NetlistGraph& self, bool only_connected = false) -> std::optional<std::vector<u32>> {
auto res = self.get_vertices(only_connected);
if (res.is_ok())
{
return res.get();
}
else
{
log_error("python_context", "error encountered while getting vertices:\n{}", res.get_error().get());
return std::nullopt;
}
},
py::arg("only_connected") = false,
R"(
Get the vertices in the netlist graph.
:param bool only_connected: Set ``True`` to only return vertices connected to at least one edge, ``False`` otherwise. Defaults to ``False``.
:returns: A list of vertices on success, ``None`` otherwise.
:rtype: list[int] or None
)");

py_netlist_graph.def(
"get_edges",
[](const graph_algorithm::NetlistGraph& self) -> std::optional<std::vector<std::pair<u32, u32>>> {
Expand Down
52 changes: 52 additions & 0 deletions plugins/graph_algorithm/src/netlist_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,58 @@ namespace hal
return igraph_ecount(&m_graph);
}

Result<std::vector<u32>> NetlistGraph::get_vertices(bool only_connected) const
{
u32 num_vertices = igraph_vcount(&m_graph);

if (!only_connected)
{
std::vector<u32> vertices(num_vertices);
for (u32 i = 0; i < num_vertices; i++)
{
vertices[i] = i;
}
return OK(vertices);
}
else
{
std::vector<u32> vertices;

igraph_vector_int_t degrees;
if (auto res = igraph_vector_int_init(&degrees, num_vertices); res != IGRAPH_SUCCESS)
{
return ERR(igraph_strerror(res));
}

igraph_vs_t v_sel;
if (auto res = igraph_vs_all(&v_sel); res != IGRAPH_SUCCESS)
{
igraph_vector_int_destroy(&degrees);
return ERR(igraph_strerror(res));
}

if (auto res = igraph_degree(&m_graph, &degrees, v_sel, IGRAPH_ALL, IGRAPH_LOOPS); res != IGRAPH_SUCCESS)
{
igraph_vs_destroy(&v_sel);
igraph_vector_int_destroy(&degrees);
return ERR(igraph_strerror(res));
}

for (u32 i = 0; i < num_vertices; i++)
{
if (VECTOR(degrees)[i] != 0)
{
vertices.push_back(i);
}
}

igraph_vector_int_destroy(&degrees);
igraph_vs_destroy(&v_sel);

return OK(vertices);
}
}

Result<std::vector<std::pair<u32, u32>>> NetlistGraph::get_edges() const
{
const u32 ecount = igraph_ecount(&m_graph);
Expand Down

0 comments on commit 0d30bfd

Please sign in to comment.