XNetwork 1.7.5; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
digraphs.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <cassert>
12#include <py2cpp/py2cpp.hpp>
13#include <type_traits>
14#include <utility>
15#include <vector>
16#include <xnetwork/classes/coreviews.hpp> // import AtlasView, AdjacencyView
18#include <xnetwork/classes/reportviews.hpp> // import NodeView, EdgeView, DegreeView
19
20// #if __cplusplus > 201703L
21// #include <cppcoro/generator.hpp>
22// #endif
23
24namespace xnetwork {
25
33 template <typename nodeview_t, typename adjlist_t = py::dict<Value_type<nodeview_t>, int>,
34 typename adjlist_outer_dict_factory = py::dict<Value_type<nodeview_t>, adjlist_t>>
35 class DiGraphS : public Graph<nodeview_t, adjlist_t, adjlist_outer_dict_factory> {
37
38 public:
39 using Node = typename _Base::Node; // luk
40 using edge_t = std::pair<Node, Node>;
41 // using graph_attr_dict_factory = typename _Base::graph_attr_dict_factory;
42 // using adjlist_outer_dict_factory =
43 // typename _Base::adjlist_outer_dict_factory;
44 using key_type = typename _Base::key_type;
45 using value_type = typename _Base::value_type;
46
47 public:
48 // adjlist_outer_dict_factory &_adj; // successor
49
52 explicit DiGraphS(const nodeview_t& Nodes) : _Base{Nodes} {}
53
56 explicit DiGraphS(uint32_t num_nodes) : _Base{num_nodes} {}
57
59 auto adj() const {
60 using T = decltype(this->_adj);
61 return AdjacencyView<T>(this->_adj);
62 }
63
66 auto succ() const {
67 using T = decltype(this->_adj);
68 return AdjacencyView<T>(this->_adj);
69 }
70
119 template <typename U = key_type> auto add_edge(const Node& node_u, const Node& node_v) ->
120 typename std::enable_if<std::is_same<U, value_type>::value>::type {
121 this->_adj[node_u].insert(node_v);
122 }
123
131 template <typename U = key_type> auto add_edge(const Node& node_u, const Node& node_v) ->
132 typename std::enable_if<!std::is_same<U, value_type>::value>::type {
133 using T = typename adjlist_t::mapped_type;
134 auto data = this->_adj[node_u].get(node_v, T{});
135 this->_adj[node_u][node_v] = data;
136 }
137
146 template <typename T> auto add_edge(const Node& node_u, const Node& node_v, const T& data) {
147 this->_adj[node_u][node_v] = data;
148 }
149
158 template <typename C1, typename C2> auto add_edges_from(const C1& edges, const C2& data) {
159 auto N = edges.size();
160 for (auto idx = 0U; idx != N; ++idx) {
161 const auto& e = edges[idx];
162 this->add_edge(e.first, e.second, data[idx]);
163 }
164 }
165
170 auto has_successor(const Node& node_u, const Node& node_v) const -> bool {
171 return this->_node.contains(node_u) && this->_adj.at(node_u).contains(node_v);
172 }
173
177 auto successors(const Node& node) -> auto& { return this->_adj[node]; }
178
182 auto successors(const Node& node) const -> const auto& { return this->_adj[node]; }
183
187 auto degree(const Node& node) const { return this->_adj[node].size(); }
188
192 auto number_of_edges() const -> size_t {
193 size_t n_edges = 0;
194 for (const auto& node : this->_node) {
195 n_edges += this->_adj.at(node).size();
196 }
197 return n_edges; // No division needed since it's a directed graph
198 }
199
201 auto clear() {
202 this->_adj.clear();
203 // this->_pred.clear()
204 // this->_node.clear();
205 this->graph.clear();
206 }
207
210 auto is_multigraph() const { return false; }
211
214 auto is_directed() const { return true; }
215 };
216
220 py::dict<uint32_t, int>, std::vector<py::dict<uint32_t, int>>>;
221
222 // template <typename nodeview_t,
223 // typename adjlist_t> DiGraphS(int )
224 // -> DiGraphS<decltype(py::range<int>(1)), py::set<int>>;
225
226} // namespace xnetwork
Read-only map of maps of maps (view into a dict-of-dict-of-dict structure)
Definition coreviews.hpp:109
AdjacencyView(Atlas &d)
Construct an AdjacencyView from an Atlas container.
Definition coreviews.hpp:115
auto at(const T &key) const -> const auto &
Access element at specified key (const version)
Definition coreviews.hpp:79
auto size() const -> size_t
Get the number of elements in the view.
Definition coreviews.hpp:49
Directed graph with arbitrary node types.
Definition digraphs.hpp:35
typename _Base::key_type key_type
Definition digraphs.hpp:44
auto adj() const
Get adjacency mapping (successors) of the directed graph (const version)
Definition digraphs.hpp:59
auto successors(const Node &node) const -> const auto &
Get iterator over successors of a node (const)
Definition digraphs.hpp:182
auto succ() const
Get the successors mapping (same as adj)
Definition digraphs.hpp:66
auto has_successor(const Node &node_u, const Node &node_v) const -> bool
Check if a node has a specific successor.
Definition digraphs.hpp:170
auto add_edge(const Node &node_u, const Node &node_v, const T &data)
Add an edge between two nodes with data.
Definition digraphs.hpp:146
auto is_directed() const
Check if the graph is directed.
Definition digraphs.hpp:214
typename _Base::value_type value_type
Definition digraphs.hpp:45
auto number_of_edges() const -> size_t
Get the number of edges in the directed graph.
Definition digraphs.hpp:192
auto add_edge(const Node &node_u, const Node &node_v) -> typename std::enable_if<!std::is_same< U, value_type >::value >::type
Add an edge between two nodes (for complex key type)
Definition digraphs.hpp:131
typename _Base::Node Node
Definition digraphs.hpp:39
auto add_edges_from(const C1 &edges, const C2 &data)
Add edges from a container with associated data.
Definition digraphs.hpp:158
DiGraphS(const nodeview_t &Nodes)
Construct a directed graph from a node container.
Definition digraphs.hpp:52
std::pair< Node, Node > edge_t
Definition digraphs.hpp:40
auto is_multigraph() const
Check if the graph is a multigraph.
Definition digraphs.hpp:210
DiGraphS(uint32_t num_nodes)
Construct a directed graph with a given number of integer nodes.
Definition digraphs.hpp:56
auto successors(const Node &node) -> auto &
Get iterator over successors of a node (non-const)
Definition digraphs.hpp:177
auto clear()
Remove all nodes and edges from the graph.
Definition digraphs.hpp:201
auto degree(const Node &node) const
Get the out-degree of a node in the directed graph.
Definition digraphs.hpp:187
auto add_edge(const Node &node_u, const Node &node_v) -> typename std::enable_if< std::is_same< U, value_type >::value >::type
Add a directed edge between two nodes (for simple key type, SFINAE)
Definition digraphs.hpp:119
Undirected graph with arbitrary node types.
Definition graph.hpp:43
typename adjlist_t::value_type value_type
Definition graph.hpp:56
auto edges() const -> std::vector< edge_t >
Return a vector of all edges as (u, v) pairs.
Definition graph.hpp:223
_nodeview_t nodeview_t
Definition graph.hpp:45
typename adjlist_t::key_type key_type
Definition graph.hpp:55
nodeview_t _node
Container holding all nodes in the graph.
Definition graph.hpp:63
typename nodeview_t::value_type Node
Definition graph.hpp:46
adjlist_outer_dict_factory _adj
Adjacency dictionary keyed by node.
Definition graph.hpp:66
Read-only view classes for graph data structures.
Undirected graph data structure for XNetwork.
Definition digraphs.hpp:24
View classes for nodes, edges, and degrees in XNetwork graphs.