Skip to content

Commit 7f5a198

Browse files
committed
added computation of shortest paths
1 parent 0d30bfd commit 7f5a198

File tree

3 files changed

+552
-0
lines changed

3 files changed

+552
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
#pragma once
27+
28+
#include "graph_algorithm/netlist_graph.h"
29+
#include "hal_core/defines.h"
30+
#include "hal_core/utilities/result.h"
31+
32+
#include <igraph/igraph.h>
33+
#include <set>
34+
35+
namespace hal
36+
{
37+
class Gate;
38+
39+
namespace graph_algorithm
40+
{
41+
/**
42+
* Compute a shortest path from the specified `from_gate` to each of the given `to_gates` by traversing in the provided direction.
43+
* Returns one shortest path for each end gate, even if multiple shortest paths exist.
44+
* Each shortest path is given as a vector of vertices in the order of traversal.
45+
*
46+
* @param[in] graph - The netlist graph.
47+
* @param[in] from_gate - The start gate of the shortest path.
48+
* @param[in] to_gates - A vector of end gates of the shortest path.
49+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_gate`.
50+
* @returns The shortest paths in order of the `to_gates` on success, an error otherwise.
51+
*/
52+
Result<std::vector<std::vector<u32>>> get_shortest_paths(NetlistGraph* graph, Gate* from_gate, const std::vector<Gate*>& to_gates, NetlistGraph::Direction direction);
53+
54+
/**
55+
* Compute a shortest path from the specified `from_vertex` to each of the given `to_vertices` by traversing in the provided direction.
56+
* Returns one shortest path for each end vertex, even if multiple shortest paths exist.
57+
* Each shortest path is given as a vector of vertices in the order of traversal.
58+
*
59+
* @param[in] graph - The netlist graph.
60+
* @param[in] from_gate - The start vertex of the shortest path.
61+
* @param[in] to_gates - A vector of end vertices of the shortest path.
62+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_vertex`.
63+
* @returns The shortest paths in order of the `to_vertices` on success, an error otherwise.
64+
*/
65+
Result<std::vector<std::vector<u32>>> get_shortest_paths(NetlistGraph* graph, u32 from_vertex, const std::vector<u32>& to_vertices, NetlistGraph::Direction direction);
66+
67+
/**
68+
* Compute a shortest path from the specified `from_vertex` to each of the given `to_vertices` by traversing in the provided direction.
69+
* Returns one shortest path for each end vertex, even if multiple shortest paths exist.
70+
* Each shortest path is given as a vector of vertices in the order of traversal.
71+
*
72+
* @param[in] graph - The netlist graph.
73+
* @param[in] from_gate - The start vertex of the shortest path.
74+
* @param[in] to_gates - An igraph vector of end vertices of the shortest path.
75+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_vertex`.
76+
* @returns The shortest paths in order of the `to_vertices` on success, an error otherwise.
77+
*/
78+
Result<std::vector<std::vector<u32>>> get_shortest_paths_igraph(NetlistGraph* graph, u32 from_vertex, const igraph_vector_int_t* to_vertices, NetlistGraph::Direction direction);
79+
80+
/**
81+
* Compute a shortest path from the specified `from_gate` to each of the given `to_gates` by traversing in the provided direction.
82+
* Returns all shortest paths for each end gate.
83+
* Each shortest path is given as a vector of vertices in the order of traversal.
84+
*
85+
* @param[in] graph - The netlist graph.
86+
* @param[in] from_gate - The start gate of the shortest path.
87+
* @param[in] to_gates - A vector of end gates of the shortest path.
88+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_gate`.
89+
* @returns The shortest paths in order of the `to_gates` on success, an error otherwise.
90+
*/
91+
Result<std::vector<std::vector<u32>>> get_all_shortest_paths(NetlistGraph* graph, Gate* from_gate, const std::vector<Gate*>& to_gates, NetlistGraph::Direction direction);
92+
93+
/**
94+
* Compute a shortest path from the specified `from_vertex` to each of the given `to_vertices` by traversing in the provided direction.
95+
* Returns all shortest paths for each end gate.
96+
* Each shortest path is given as a vector of vertices in the order of traversal.
97+
*
98+
* @param[in] graph - The netlist graph.
99+
* @param[in] from_gate - The start vertex of the shortest path.
100+
* @param[in] to_gates - A vector of end vertices of the shortest path.
101+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_vertex`.
102+
* @returns The shortest paths in order of the `to_vertices` on success, an error otherwise.
103+
*/
104+
Result<std::vector<std::vector<u32>>> get_all_shortest_paths(NetlistGraph* graph, u32 from_vertex, const std::vector<u32>& to_vertices, NetlistGraph::Direction direction);
105+
106+
/**
107+
* Compute a shortest path from the specified `from_vertex` to each of the given `to_vertices` by traversing in the provided direction.
108+
* Returns all shortest paths for each end gate.
109+
* Each shortest path is given as a vector of vertices in the order of traversal.
110+
*
111+
* @param[in] graph - The netlist graph.
112+
* @param[in] from_gate - The start vertex of the shortest path.
113+
* @param[in] to_gates - An igraph vector of end vertices of the shortest path.
114+
* @param[in] direction - The direction in which to compute the shortest paths starting at the `from_vertex`.
115+
* @returns The shortest paths in order of the `to_vertices` on success, an error otherwise.
116+
*/
117+
Result<std::vector<std::vector<u32>>> get_all_shortest_paths_igraph(NetlistGraph* graph, u32 from_vertex, const igraph_vector_int_t* to_vertices, NetlistGraph::Direction direction);
118+
} // namespace graph_algorithm
119+
} // namespace hal

plugins/graph_algorithm/python/python_bindings.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "graph_algorithm/algorithms/components.h"
55
#include "graph_algorithm/algorithms/neighborhood.h"
6+
#include "graph_algorithm/algorithms/shortest_path.h"
67
#include "graph_algorithm/algorithms/subgraph.h"
78
#include "graph_algorithm/netlist_graph.h"
89
#include "graph_algorithm/plugin_graph_algorithm.h"
@@ -551,6 +552,134 @@ namespace hal
551552
:rtype: list[list[int]] or None
552553
)");
553554

555+
m.def(
556+
"get_shortest_paths",
557+
[](graph_algorithm::NetlistGraph* graph, Gate* from_gate, const std::vector<Gate*>& to_gates, graph_algorithm::NetlistGraph::Direction direction)
558+
-> std::optional<std::vector<std::vector<u32>>> {
559+
auto res = graph_algorithm::get_shortest_paths(graph, from_gate, to_gates, direction);
560+
if (res.is_ok())
561+
{
562+
return res.get();
563+
}
564+
else
565+
{
566+
log_error("python_context", "error encountered while computing shortest paths:\n{}", res.get_error().get());
567+
return std::nullopt;
568+
}
569+
},
570+
py::arg("graph"),
571+
py::arg("from_gate"),
572+
py::arg("to_gates"),
573+
py::arg("direction"),
574+
R"(
575+
Compute a shortest path from the specified ``from_gate`` to each of the given ``to_gates`` by traversing in the provided direction.
576+
Returns one shortest path for each end gate, even if multiple shortest paths exist.
577+
Each shortest path is given as a list of vertices in the order of traversal.
578+
579+
:param graph_algorithm.NetlistGraph graph: The netlist graph.
580+
:param hal_py.Gate from_gate: The start gate of the shortest path.
581+
:param list[hal_py.Gate] to_gates: A list of end gates of the shortest path.
582+
:param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_gate``.
583+
:returns: The shortest paths in order of the ``to_gates`` on success, an error otherwise.
584+
:rtype: list[list[int]] or None
585+
)");
586+
587+
m.def(
588+
"get_shortest_paths",
589+
[](graph_algorithm::NetlistGraph* graph, u32 from_vertice, const std::vector<u32>& to_vertices, graph_algorithm::NetlistGraph::Direction direction)
590+
-> std::optional<std::vector<std::vector<u32>>> {
591+
auto res = graph_algorithm::get_shortest_paths(graph, from_vertice, to_vertices, direction);
592+
if (res.is_ok())
593+
{
594+
return res.get();
595+
}
596+
else
597+
{
598+
log_error("python_context", "error encountered while computing shortest paths:\n{}", res.get_error().get());
599+
return std::nullopt;
600+
}
601+
},
602+
py::arg("graph"),
603+
py::arg("from_vertex"),
604+
py::arg("to_vertices"),
605+
py::arg("direction"),
606+
R"(
607+
Compute a shortest path from the specified ``from_vertex`` to each of the given ``to_vertices`` by traversing in the provided direction.
608+
Returns one shortest path for each end vertex, even if multiple shortest paths exist.
609+
Each shortest path is given as a list of vertices in the order of traversal.
610+
611+
:param graph_algorithm.NetlistGraph graph: The netlist graph.
612+
:param int from_vertex: The start vertex of the shortest path.
613+
:param list[int] to_vertices: A list of end vertices of the shortest path.
614+
:param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_vertex``.
615+
:returns: The shortest paths in order of the ``to_vertices`` on success, an error otherwise.
616+
:rtype: list[list[int]] or None
617+
)");
618+
619+
m.def(
620+
"get_all_shortest_paths",
621+
[](graph_algorithm::NetlistGraph* graph, Gate* from_gate, const std::vector<Gate*>& to_gates, graph_algorithm::NetlistGraph::Direction direction)
622+
-> std::optional<std::vector<std::vector<u32>>> {
623+
auto res = graph_algorithm::get_all_shortest_paths(graph, from_gate, to_gates, direction);
624+
if (res.is_ok())
625+
{
626+
return res.get();
627+
}
628+
else
629+
{
630+
log_error("python_context", "error encountered while computing all shortest paths:\n{}", res.get_error().get());
631+
return std::nullopt;
632+
}
633+
},
634+
py::arg("graph"),
635+
py::arg("from_gate"),
636+
py::arg("to_gates"),
637+
py::arg("direction"),
638+
R"(
639+
Compute a shortest path from the specified ``from_gate`` to each of the given ``to_gates`` by traversing in the provided direction.
640+
Returns all shortest paths for each end gate.
641+
Each shortest path is given as a list of vertices in the order of traversal.
642+
643+
:param graph_algorithm.NetlistGraph graph: The netlist graph.
644+
:param hal_py.Gate from_gate: The start gate of the shortest path.
645+
:param list[hal_py.Gate] to_gates: A list of end gates of the shortest path.
646+
:param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_gate``.
647+
:returns: The shortest paths in order of the ``to_gates`` on success, an error otherwise.
648+
:rtype: list[list[int]] or None
649+
)");
650+
651+
m.def(
652+
"get_all_shortest_paths",
653+
[](graph_algorithm::NetlistGraph* graph, u32 from_vertice, const std::vector<u32>& to_vertices, graph_algorithm::NetlistGraph::Direction direction)
654+
-> std::optional<std::vector<std::vector<u32>>> {
655+
auto res = graph_algorithm::get_all_shortest_paths(graph, from_vertice, to_vertices, direction);
656+
if (res.is_ok())
657+
{
658+
return res.get();
659+
}
660+
else
661+
{
662+
log_error("python_context", "error encountered while computing all shortest paths:\n{}", res.get_error().get());
663+
return std::nullopt;
664+
}
665+
},
666+
py::arg("graph"),
667+
py::arg("from_vertex"),
668+
py::arg("to_vertices"),
669+
py::arg("direction"),
670+
R"(
671+
Compute a shortest path from the specified ``from_vertex`` to each of the given ``to_vertices`` by traversing in the provided direction.
672+
Returns all shortest paths for each end gate.
673+
Each shortest path is given as a list of vertices in the order of traversal.
674+
675+
:param graph_algorithm.NetlistGraph graph: The netlist graph.
676+
:param int from_vertex: The start vertex of the shortest path.
677+
:param list[int] to_vertices: A list of end vertices of the shortest path.
678+
:param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_vertex``.
679+
:returns: The shortest paths in order of the ``to_vertices`` on success, an error otherwise.
680+
:rtype: list[list[int]] or None
681+
)");
682+
554683
m.def(
555684
"get_subgraph",
556685
[](graph_algorithm::NetlistGraph* graph, const std::vector<Gate*>& subgraph_gates) -> std::optional<std::unique_ptr<graph_algorithm::NetlistGraph>> {

0 commit comments

Comments
 (0)