Skip to content

Commit f6a7ab1

Browse files
author
Simon Klix
committed
added harmonic and betweenness centrality features and split up gate features into single and bulk features
1 parent 4d0c2f4 commit f6a7ab1

File tree

19 files changed

+1729
-588
lines changed

19 files changed

+1729
-588
lines changed

include/hal_core/netlist/decorators/netlist_abstraction_decorator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace hal
5858
* @param[in] exit_endpoint_filter - Filter condition to stop traversal on a fan-in/out endpoint.
5959
* @param[in] entry_endpoint_filter - Filter condition to stop traversal on a successor/predecessor endpoint.
6060
*/
61-
static Result<std::shared_ptr<NetlistAbstraction>> create(const Netlist* netlist,
61+
static Result<std::unique_ptr<NetlistAbstraction>> create(const Netlist* netlist,
6262
const std::vector<Gate*>& gates,
6363
const bool include_all_netlist_gates = false,
6464
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4+
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5+
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6+
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
26+
/**
27+
* @file centrality.h
28+
* @brief This file contains functions related to centrality metrics in graphs.
29+
*/
30+
31+
#pragma once
32+
33+
#include "graph_algorithm/netlist_graph.h"
34+
#include "hal_core/defines.h"
35+
#include "hal_core/utilities/result.h"
36+
37+
#include <igraph/igraph.h>
38+
#include <set>
39+
40+
namespace hal
41+
{
42+
class Gate;
43+
44+
namespace graph_algorithm
45+
{
46+
/**
47+
* @brief Compute the harmonic centrality for a set of gates within a given graph.
48+
*
49+
* Harmonic centrality is a measure of centrality in a graph that is computed based on the inverse distances to all other vertices in the graph.
50+
* In this version, the input vertices are provided as `Gate*` pointers. The specified `direction` parameter
51+
* determines the edge direction to be considered when computing shortest paths.
52+
*
53+
* @param[in] graph - The netlist graph for which the harmonic centrality is to be computed.
54+
* @param[in] gates - The gates representing vertices in the graph for which centrality should be computed.
55+
* @param[in] direction - The direction of edges to consider (forward, backward, or both).
56+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
57+
* @returns OK() and a vector of centrality values (in the same order as the input gates) on success, an error otherwise.
58+
*/
59+
Result<std::vector<double>> get_harmonic_centrality(const NetlistGraph* graph, const std::vector<Gate*>& gates, const NetlistGraph::Direction direction, const i32 cutoff = -1);
60+
61+
/**
62+
* @brief Compute the harmonic centrality for a set of vertices within a given graph.
63+
*
64+
* Harmonic centrality is a measure of centrality in a graph that is computed based on the inverse distances to all other vertices in the graph.
65+
* In this version, the input vertices are provided as their vertex IDs. The specified `direction` parameter
66+
* determines the edge direction to be considered when computing shortest paths.
67+
*
68+
* @param[in] graph - The netlist graph for which the harmonic centrality is to be computed.
69+
* @param[in] vertices - The vertex IDs for which centrality should be computed.
70+
* @param[in] direction - The direction of edges to consider (forward, backward, or both).
71+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
72+
* @returns OK() and a vector of centrality values (in the same order as the input vertices) on success, an error otherwise.
73+
*/
74+
Result<std::vector<double>> get_harmonic_centrality(const NetlistGraph* graph, const std::vector<u32>& vertices, const NetlistGraph::Direction direction, const i32 cutoff = -1);
75+
76+
/**
77+
* @brief Compute the harmonic centrality for a set of vertices within a given graph.
78+
*
79+
* Harmonic centrality is a measure of centrality in a graph that is computed based on the inverse distances to all other vertices in the graph.
80+
* In this version, the input vertices are provided as an `igraph_vector_int_t`, which is typically used by the underlying igraph library.
81+
* The specified `direction` parameter determines the edge direction to be considered when computing shortest paths.
82+
*
83+
* @param[in] graph - The netlist graph for which the harmonic centrality is to be computed.
84+
* @param[in] vertices - A pointer to an `igraph_vector_int_t` containing the vertex IDs for which centrality should be computed.
85+
* @param[in] direction - The direction of edges to consider (forward, backward, or both).
86+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
87+
* @returns OK() and a vector of centrality values (in the same order as the input vertices) on success, an error otherwise.
88+
*/
89+
Result<std::vector<double>> get_harmonic_centrality(const NetlistGraph* graph, const igraph_vector_int_t* vertices, const NetlistGraph::Direction direction, const i32 cutoff = -1);
90+
91+
/**
92+
* @brief Compute the betweenness centrality for a set of gates within a given graph.
93+
*
94+
* Betweenness centrality is a measure of centrality in a graph that is based on the number of shortest paths passing through a vertex.
95+
* In this version, the input vertices are provided as `Gate*` pointers. The `directed` parameter determines whether edges
96+
* are considered as directed or undirected when computing shortest paths.
97+
*
98+
* @param[in] graph - The netlist graph for which the betweenness centrality is to be computed.
99+
* @param[in] gates - The gates representing vertices in the graph for which centrality should be computed.
100+
* @param[in] directed - If `true`, consider the graph directed; if `false`, consider it undirected.
101+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
102+
* @returns OK() and a vector of centrality values (in the same order as the input gates) on success, an error otherwise.
103+
*/
104+
Result<std::vector<double>> get_betweenness_centrality(const NetlistGraph* graph, const std::vector<Gate*>& gates, const bool directed, const i32 cutoff = -1);
105+
106+
/**
107+
* @brief Compute the betweenness centrality for a set of vertices within a given graph.
108+
*
109+
* Betweenness centrality is a measure of centrality in a graph that is based on the number of shortest paths passing through a vertex.
110+
* In this version, the input vertices are provided as their vertex IDs. The `directed` parameter determines whether edges
111+
* are considered as directed or undirected.
112+
*
113+
* @param[in] graph - The netlist graph for which the betweenness centrality is to be computed.
114+
* @param[in] vertices - The vertex IDs for which centrality should be computed.
115+
* @param[in] directed - If `true`, consider the graph directed; if `false`, consider it undirected.
116+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
117+
* @returns OK() and a vector of centrality values (in the same order as the input vertices) on success, an error otherwise.
118+
*/
119+
Result<std::vector<double>> get_betweenness_centrality(const NetlistGraph* graph, const std::vector<u32>& vertices, const bool directed, const i32 cutoff = -1);
120+
121+
/**
122+
* @brief Compute the betweenness centrality for a set of vertices within a given graph.
123+
*
124+
* Betweenness centrality is a measure of centrality in a graph that is based on the number of shortest paths passing through a vertex.
125+
* In this version, the input vertices are provided as an `igraph_vector_int_t`, which is typically used by the underlying igraph library.
126+
* The `directed` parameter determines whether edges are considered as directed or undirected.
127+
*
128+
* @param[in] graph - The netlist graph for which the betweenness centrality is to be computed.
129+
* @param[in] vertices - A pointer to an `igraph_vector_int_t` containing the vertex IDs for which centrality should be computed.
130+
* @param[in] directed - If `true`, consider the graph directed; if `false`, consider it undirected.
131+
* @param[in] cutoff - The maximum path length to consider during computation. Defaults to `-1` (no cutoff).
132+
* @returns OK() and a vector of centrality values (in the same order as the input vertices) on success, an error otherwise.
133+
*/
134+
Result<std::vector<double>> get_betweenness_centrality(const NetlistGraph* graph, const igraph_vector_int_t* vertices, const bool directed, const i32 cutoff = -1);
135+
136+
} // namespace graph_algorithm
137+
} // namespace hal

plugins/graph_algorithm/python/python_bindings.cpp

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
#include "hal_core/python_bindings/python_bindings.h"
3-
2+
#include "graph_algorithm/algorithms/centrality.h"
43
#include "graph_algorithm/algorithms/components.h"
54
#include "graph_algorithm/algorithms/neighborhood.h"
65
#include "graph_algorithm/algorithms/shortest_path.h"
76
#include "graph_algorithm/algorithms/subgraph.h"
87
#include "graph_algorithm/netlist_graph.h"
98
#include "graph_algorithm/plugin_graph_algorithm.h"
9+
#include "hal_core/python_bindings/python_bindings.h"
1010

1111
#pragma GCC diagnostic push
1212
#pragma GCC diagnostic ignored "-Wshadow"
@@ -899,6 +899,182 @@ namespace hal
899899
:rtype: graph_algorithm.NetlistGraph or None
900900
)");
901901

902+
m.def(
903+
"get_harmonic_centrality",
904+
[](const graph_algorithm::NetlistGraph* graph, const std::vector<Gate*>& gates, graph_algorithm::NetlistGraph::Direction direction, int cutoff = -1) -> std::optional<std::vector<double>> {
905+
auto res = graph_algorithm::get_harmonic_centrality(graph, gates, direction, cutoff);
906+
if (res.is_ok())
907+
{
908+
return res.get();
909+
}
910+
else
911+
{
912+
log_error("python_context", "Error computing harmonic centrality: {}", res.get_error().get());
913+
return std::nullopt;
914+
}
915+
},
916+
py::arg("graph"),
917+
py::arg("gates"),
918+
py::arg("direction"),
919+
py::arg("cutoff") = -1,
920+
R"(
921+
Compute the harmonic centrality for a set of gates within a given graph.
922+
923+
:param hal_py.NetlistGraph graph: The netlist graph.
924+
:param list[hal_py.Gate] gates: The gates for which centrality is computed.
925+
:param hal_py.NetlistGraph.Direction direction: The edge direction to consider.
926+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
927+
:returns: A list of centrality values on success, None otherwise.
928+
:rtype: list[float] or None
929+
)");
930+
931+
m.def(
932+
"get_harmonic_centrality",
933+
[](const graph_algorithm::NetlistGraph* graph, const std::vector<uint32_t>& vertices, graph_algorithm::NetlistGraph::Direction direction, int cutoff = -1)
934+
-> std::optional<std::vector<double>> {
935+
auto res = graph_algorithm::get_harmonic_centrality(graph, vertices, direction, cutoff);
936+
if (res.is_ok())
937+
{
938+
return res.get();
939+
}
940+
else
941+
{
942+
log_error("python_context", "Error computing harmonic centrality: {}", res.get_error().get());
943+
return std::nullopt;
944+
}
945+
},
946+
py::arg("graph"),
947+
py::arg("vertices"),
948+
py::arg("direction"),
949+
py::arg("cutoff") = -1,
950+
R"(
951+
Compute the harmonic centrality for a set of vertices within a given graph.
952+
953+
:param hal_py.NetlistGraph graph: The netlist graph.
954+
:param list[int] vertices: The vertex IDs for which centrality is computed.
955+
:param hal_py.NetlistGraph.Direction direction: The edge direction to consider.
956+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
957+
:returns: A list of centrality values on success, None otherwise.
958+
:rtype: list[float] or None
959+
)");
960+
961+
m.def(
962+
"get_harmonic_centrality",
963+
[](const graph_algorithm::NetlistGraph* graph, const igraph_vector_int_t* vertices, graph_algorithm::NetlistGraph::Direction direction, int cutoff = -1)
964+
-> std::optional<std::vector<double>> {
965+
auto res = graph_algorithm::get_harmonic_centrality(graph, vertices, direction, cutoff);
966+
if (res.is_ok())
967+
{
968+
return res.get();
969+
}
970+
else
971+
{
972+
log_error("python_context", "Error computing harmonic centrality: {}", res.get_error().get());
973+
return std::nullopt;
974+
}
975+
},
976+
py::arg("graph"),
977+
py::arg("vertices"),
978+
py::arg("direction"),
979+
py::arg("cutoff") = -1,
980+
R"(
981+
Compute the harmonic centrality for a set of vertices within a given graph using an igraph vector.
982+
983+
:param hal_py.NetlistGraph graph: The netlist graph.
984+
:param hal_py.igraph_vector_int_t vertices: The igraph vector of vertex IDs for which centrality is computed.
985+
:param hal_py.NetlistGraph.Direction direction: The edge direction to consider.
986+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
987+
:returns: A list of centrality values on success, None otherwise.
988+
:rtype: list[float] or None
989+
)");
990+
991+
m.def(
992+
"get_betweenness_centrality",
993+
[](const graph_algorithm::NetlistGraph* graph, const std::vector<Gate*>& gates, bool directed, int cutoff = -1) -> std::optional<std::vector<double>> {
994+
auto res = graph_algorithm::get_betweenness_centrality(graph, gates, directed, cutoff);
995+
if (res.is_ok())
996+
{
997+
return res.get();
998+
}
999+
else
1000+
{
1001+
log_error("python_context", "Error computing betweenness centrality: {}", res.get_error().get());
1002+
return std::nullopt;
1003+
}
1004+
},
1005+
py::arg("graph"),
1006+
py::arg("gates"),
1007+
py::arg("directed"),
1008+
py::arg("cutoff") = -1,
1009+
R"(
1010+
Compute the betweenness centrality for a set of gates within a given graph.
1011+
1012+
:param hal_py.NetlistGraph graph: The netlist graph.
1013+
:param list[hal_py.Gate] gates: The gates for which centrality is computed.
1014+
:param bool directed: Whether the graph is directed.
1015+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
1016+
:returns: A list of centrality values on success, None otherwise.
1017+
:rtype: list[float] or None
1018+
)");
1019+
1020+
m.def(
1021+
"get_betweenness_centrality",
1022+
[](const graph_algorithm::NetlistGraph* graph, const std::vector<uint32_t>& vertices, bool directed, int cutoff = -1) -> std::optional<std::vector<double>> {
1023+
auto res = graph_algorithm::get_betweenness_centrality(graph, vertices, directed, cutoff);
1024+
if (res.is_ok())
1025+
{
1026+
return res.get();
1027+
}
1028+
else
1029+
{
1030+
log_error("python_context", "Error computing betweenness centrality: {}", res.get_error().get());
1031+
return std::nullopt;
1032+
}
1033+
},
1034+
py::arg("graph"),
1035+
py::arg("vertices"),
1036+
py::arg("directed"),
1037+
py::arg("cutoff") = -1,
1038+
R"(
1039+
Compute the betweenness centrality for a set of vertices within a given graph.
1040+
1041+
:param hal_py.NetlistGraph graph: The netlist graph.
1042+
:param list[int] vertices: The vertex IDs for which centrality is computed.
1043+
:param bool directed: Whether the graph is directed.
1044+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
1045+
:returns: A list of centrality values on success, None otherwise.
1046+
:rtype: list[float] or None
1047+
)");
1048+
1049+
m.def(
1050+
"get_betweenness_centrality",
1051+
[](const graph_algorithm::NetlistGraph* graph, const igraph_vector_int_t* vertices, bool directed, int cutoff = -1) -> std::optional<std::vector<double>> {
1052+
auto res = graph_algorithm::get_betweenness_centrality(graph, vertices, directed, cutoff);
1053+
if (res.is_ok())
1054+
{
1055+
return res.get();
1056+
}
1057+
else
1058+
{
1059+
log_error("python_context", "Error computing betweenness centrality: {}", res.get_error().get());
1060+
return std::nullopt;
1061+
}
1062+
},
1063+
py::arg("graph"),
1064+
py::arg("vertices"),
1065+
py::arg("directed"),
1066+
py::arg("cutoff") = -1,
1067+
R"(
1068+
Compute the betweenness centrality for a set of vertices within a given graph using an igraph vector.
1069+
1070+
:param hal_py.NetlistGraph graph: The netlist graph.
1071+
:param hal_py.igraph_vector_int_t vertices: The igraph vector of vertex IDs for which centrality is computed.
1072+
:param bool directed: Whether the graph is directed.
1073+
:param int cutoff: Maximum path length to consider. Defaults to -1 (no cutoff).
1074+
:returns: A list of centrality values on success, None otherwise.
1075+
:rtype: list[float] or None
1076+
)");
1077+
9021078
#ifndef PYBIND11_MODULE
9031079
return m.ptr();
9041080
#endif // PYBIND11_MODULE

0 commit comments

Comments
 (0)