-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added subgraph computation algorithm
- Loading branch information
Showing
3 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
plugins/graph_algorithm/include/graph_algorithm/algorithms/subgraph.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved. | ||
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved. | ||
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved. | ||
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include "hal_core/defines.h" | ||
#include "hal_core/utilities/result.h" | ||
|
||
#include <igraph/igraph.h> | ||
#include <set> | ||
|
||
namespace hal | ||
{ | ||
class Gate; | ||
|
||
namespace graph_algorithm | ||
{ | ||
class NetlistGraph; | ||
|
||
/** | ||
* Compute the subgraph induced by the specified gates, including all edges between the corresponding vertices. | ||
* | ||
* @param[in] graph - The netlist graph. | ||
* @param[in] subgraph_gates - A vector of gates that make up the subgraph. | ||
* @returns The subgraph as a new netlist graph on success, an error otherwise. | ||
*/ | ||
Result<std::unique_ptr<NetlistGraph>> get_subgraph(NetlistGraph* graph, const std::vector<Gate*>& subgraph_gates); | ||
|
||
/** | ||
* Compute the subgraph induced by the specified vertices, including all edges between these vertices. | ||
* | ||
* @param[in] graph - The netlist graph. | ||
* @param[in] subgraph_vertices - A vector of vertices that make up the subgraph. | ||
* @returns The subgraph as a new netlist graph on success, an error otherwise. | ||
*/ | ||
Result<std::unique_ptr<NetlistGraph>> get_subgraph(NetlistGraph* graph, const std::vector<u32>& subgraph_vertices); | ||
|
||
/** | ||
* Compute the subgraph induced by the specified vertices, including all edges between these vertices. | ||
* | ||
* @param[in] graph - The netlist graph. | ||
* @param[in] subgraph_vertices - An igraph vector of vertices that make up the subgraph. | ||
* @returns The subgraph as a new netlist graph on success, an error otherwise. | ||
*/ | ||
Result<std::unique_ptr<NetlistGraph>> get_subgraph_igraph(NetlistGraph* graph, const igraph_vector_int_t* subgraph_vertices); | ||
} // namespace graph_algorithm | ||
} // namespace hal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "graph_algorithm/algorithms/subgraph.h" | ||
|
||
#include "graph_algorithm/netlist_graph.h" | ||
#include "hal_core/netlist/gate.h" | ||
|
||
namespace hal | ||
{ | ||
namespace graph_algorithm | ||
{ | ||
Result<std::unique_ptr<NetlistGraph>> get_subgraph(NetlistGraph* graph, const std::vector<Gate*>& subgraph_gates) | ||
{ | ||
if (graph == nullptr) | ||
{ | ||
return ERR("graph is a nullptr"); | ||
} | ||
|
||
if (subgraph_gates.empty()) | ||
{ | ||
return ERR("no subgraph gates provided"); | ||
} | ||
|
||
igraph_vector_int_t i_gates; | ||
if (auto res = graph->get_vertices_from_gates_igraph(subgraph_gates); res.is_ok()) | ||
{ | ||
i_gates = std::move(res.get()); | ||
} | ||
else | ||
{ | ||
return ERR(res.get_error()); | ||
} | ||
|
||
auto res = get_subgraph_igraph(graph, &i_gates); | ||
|
||
igraph_vector_int_destroy(&i_gates); | ||
|
||
if (res.is_error()) | ||
{ | ||
return ERR(res.get_error()); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
Result<std::unique_ptr<NetlistGraph>> get_subgraph(NetlistGraph* graph, const std::vector<u32>& subgraph_vertices) | ||
{ | ||
if (graph == nullptr) | ||
{ | ||
return ERR("graph is a nullptr"); | ||
} | ||
|
||
if (subgraph_vertices.empty()) | ||
{ | ||
return ERR("no subgraph vertices provided"); | ||
} | ||
|
||
igraph_vector_int_t i_gates; | ||
if (auto res = igraph_vector_int_init(&i_gates, subgraph_vertices.size()); res != IGRAPH_SUCCESS) | ||
{ | ||
return ERR(igraph_strerror(res)); | ||
} | ||
|
||
for (u32 i = 0; i < subgraph_vertices.size(); i++) | ||
{ | ||
VECTOR(i_gates)[i] = subgraph_vertices.at(i); | ||
} | ||
|
||
auto res = get_subgraph_igraph(graph, &i_gates); | ||
|
||
igraph_vector_int_destroy(&i_gates); | ||
|
||
if (res.is_error()) | ||
{ | ||
return ERR(res.get_error()); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
Result<std::unique_ptr<NetlistGraph>> get_subgraph_igraph(NetlistGraph* graph, const igraph_vector_int_t* subgraph_vertices) | ||
{ | ||
if (graph == nullptr) | ||
{ | ||
return ERR("graph is a nullptr"); | ||
} | ||
|
||
igraph_vs_t v_sel; | ||
if (auto res = igraph_vs_vector(&v_sel, subgraph_vertices); res != IGRAPH_SUCCESS) | ||
{ | ||
return ERR(igraph_strerror(res)); | ||
} | ||
|
||
u32 subgraph_size = igraph_vector_int_size(subgraph_vertices); | ||
|
||
igraph_vector_int_t i_vertex_map; | ||
if (auto res = igraph_vector_int_init(&i_vertex_map, subgraph_size); res != IGRAPH_SUCCESS) | ||
{ | ||
igraph_vs_destroy(&v_sel); | ||
return ERR(igraph_strerror(res)); | ||
} | ||
|
||
igraph_t i_subg; | ||
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) | ||
{ | ||
igraph_vs_destroy(&v_sel); | ||
igraph_vector_int_destroy(&i_vertex_map); | ||
return ERR(igraph_strerror(res)); | ||
} | ||
|
||
std::unordered_map<u32, Gate*> nodes_to_gates; | ||
if (const auto res = graph->get_gates_from_vertices_igraph(&i_vertex_map); res.is_ok()) | ||
{ | ||
std::vector<Gate*> gates = res.get(); | ||
for (u32 i = 0; i < gates.size(); i++) | ||
{ | ||
nodes_to_gates[i] = gates.at(i); | ||
} | ||
} | ||
else | ||
{ | ||
igraph_destroy(&i_subg); | ||
igraph_vs_destroy(&v_sel); | ||
igraph_vector_int_destroy(&i_vertex_map); | ||
return ERR(res.get_error()); | ||
} | ||
|
||
auto subgraph = std::unique_ptr<NetlistGraph>(new NetlistGraph(graph->get_netlist(), std::move(i_subg), std::move(nodes_to_gates))); | ||
|
||
igraph_vs_destroy(&v_sel); | ||
igraph_vector_int_destroy(&i_vertex_map); | ||
|
||
return OK(std::move(subgraph)); | ||
} | ||
} // namespace graph_algorithm | ||
} // namespace hal |