Graph Class
Directed acyclic graph of hybrid nodes with named ports. More...
Declaration
class simaai::neat::graph::Graph { ... }
Included Headers
#include <Graph.h>
Public Member Typedefs Index
| using | NodePtr = std::shared_ptr< Node > |
Public Constructors Index
| Graph ()=default | |
Public Member Functions Index
| NodeId | add (NodePtr node) |
| const NodePtr & | node (NodeId id) const |
| std::size_t | node_count () const noexcept |
| bool | empty () const noexcept |
| PortId | intern_port (std::string_view name) |
| const std::string & | port_name (PortId id) const |
| std::size_t | port_count () const noexcept |
| const std::vector< std::string > & | port_names () const noexcept |
| void | connect (NodeId from, NodeId to, const std::string &from_port="out", const std::string &to_port="in") |
| const std::vector< Edge > & | edges () const noexcept |
| const Edge & | edge (std::size_t idx) const |
| const std::vector< std::size_t > & | out_edges (NodeId id) const |
| const std::vector< std::size_t > & | in_edges (NodeId id) const |
| std::size_t | out_degree (NodeId id) const |
| std::size_t | in_degree (NodeId id) const |
| std::vector< NodeId > | topo_order () const |
| bool | is_dag () const |
Private Member Functions Index
| void | validate_id_ (NodeId id, const char *where) const |
| bool | has_edge_ (NodeId from, NodeId to, PortId from_port, PortId to_port) const |
Private Member Attributes Index
| std::vector< NodePtr > | nodes_ |
| std::vector< Edge > | edges_ |
| std::vector< std::vector< std::size_t > > | out_edges_ |
| std::vector< std::vector< std::size_t > > | in_edges_ |
| std::vector< std::string > | port_names_ |
| std::unordered_map< std::string, PortId > | port_ids_ |
Description
Directed acyclic graph of hybrid nodes with named ports.
Definition at line 26 of file Graph.h.
Public Member Functions
add()
| inline |
connect()
| inline |
Definition at line 87 of file Graph.h.
88 const std::string& to_port = "in") {
89 validate_id_(from, "graph::Graph::connect(from)");
90 validate_id_(to, "graph::Graph::connect(to)");
91 if (from == to) {
92 throw std::invalid_argument("graph::Graph::connect: self-loop is not allowed");
93 }
94 const PortId f = intern_port(from_port);
95 const PortId t = intern_port(to_port);
96
97 if (has_edge_(from, to, f, t))
98 return;
99
100 edges_.push_back({from, f, to, t});
101 const std::size_t idx = edges_.size() - 1;
102 out_edges_[from].push_back(idx);
103 in_edges_[to].push_back(idx);
104 }
edge()
| inline |
edges()
| inline noexcept |
empty()
| inline noexcept |
in_degree()
| inline |
in_edges()
| inline |
intern_port()
| inline |
Definition at line 57 of file Graph.h.
57 PortId intern_port(std::string_view name) {
58 const std::string key(name);
59 if (key.empty()) {
60 throw std::invalid_argument("graph::Graph::intern_port: empty port name");
61 }
62 auto it = port_ids_.find(key);
63 if (it != port_ids_.end())
64 return it->second;
66 port_names_.push_back(key);
67 port_ids_.emplace(port_names_.back(), id);
68 return id;
69 }
is_dag()
| inline |
Definition at line 175 of file Graph.h.
176 try {
177 (void)topo_order();
178 return true;
179 } catch (...) {
180 return false;
181 }
182 }
node()
| inline |
node_count()
| inline noexcept |
out_degree()
| inline |
Definition at line 127 of file Graph.h.
127 std::size_t out_degree(NodeId id) const {
128 validate_id_(id, "graph::Graph::out_degree");
129 return out_edges_[id].size();
130 }
out_edges()
| inline |
port_count()
| inline noexcept |
port_name()
| inline |
port_names()
| inline noexcept |
topo_order()
| inline |
Definition at line 138 of file Graph.h.
138 std::vector<NodeId> topo_order() const {
139 const std::size_t n = nodes_.size();
140 std::vector<std::size_t> indeg(n, 0);
141 for (NodeId i = 0; i < n; ++i)
142 indeg[i] = in_edges_[i].size();
143
144 std::queue<NodeId> q;
145 for (NodeId i = 0; i < n; ++i) {
146 if (indeg[i] == 0)
147 q.push(i);
148 }
149
150 std::vector<NodeId> order;
151 order.reserve(n);
152
153 while (!q.empty()) {
154 NodeId u = q.front();
155 q.pop();
156 order.push_back(u);
157
158 for (const std::size_t eidx : out_edges_[u]) {
159 const Edge& e = edges_[eidx];
160 if (indeg[e.to] == 0) {
161 continue;
162 }
163 indeg[e.to]--;
164 if (indeg[e.to] == 0)
165 q.push(e.to);
166 }
167 }
168
169 if (order.size() != n) {
170 throw std::runtime_error("graph::Graph::topo_order: cycle detected (graph is not a DAG)");
171 }
172 return order;
173 }
Private Member Functions
has_edge_()
| inline |
validate_id_()
Private Member Attributes
edges_
|
in_edges_
|
nodes_
|
out_edges_
|
port_ids_
|
port_names_
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.1.