13#include "detail/tree_traits.hpp"
14#include "detail/visit.hpp"
16#include <boost/range/adaptor/transformed.hpp>
17#include <boost/range/iterator_range.hpp>
19#include <boost/graph/adjacency_list.hpp>
20#include <boost/graph/graphviz.hpp>
21#include <boost/graph/isomorphism.hpp>
22#include <boost/graph/topological_sort.hpp>
24#include <boost/graph/connected_components.hpp>
25#include <boost/graph/random.hpp>
26#include <boost/graph/random_spanning_tree.hpp>
28#include "detail/adl_resolution.hpp"
29#include "detail/utils.hpp"
34#include <range/v3/all.hpp>
38using no_property = boost::no_property;
44template <
class VertexProperty,
class EdgeProperty,
class CRTP>
class network_common
92 using edge_parallel_category =
typename base::edge_parallel_category;
111 std::vector<vertex_descriptor> order;
112 topological_sort(this->_graph, back_inserter(order));
121 return boost::degree(u, _graph);
135 return boost::out_degree(v, _graph);
143 auto range = boost::in_edges(u, _graph);
144 return std::distance(range.first, range.second) > 0;
152 auto range = boost::in_edges(u, _graph);
153 assert(std::distance(range.first, range.second) == 1);
154 return source(*range.first);
162 auto range = boost::out_edges(u, _graph);
163 assert(std::distance(range.first, range.second) != 1);
164 return std::distance(range.first, range.second) >= 2;
172 return boost::make_iterator_range(
out_edges(u, _graph)) |
173 boost::adaptors::transformed([
this](
auto it) {
return target(it); });
182 auto p = boost::edge(u, v, _graph);
183 return p.second ? std::make_optional(p.first) : std::nullopt;
191 return boost::source(e, _graph);
199 return boost::target(e, _graph);
210 return boost::add_vertex(_graph);
217 return boost::clear_vertex(u, _graph);
225 return boost::remove_vertex(u, _graph);
234 return boost::add_edge(u, v, _graph).first;
242 return boost::remove_edge(u, v, _graph);
249 return boost::remove_edge(e, _graph);
262 return boost::make_iterator_range(boost::in_edges(u, _graph));
270 return boost::make_iterator_range(boost::out_edges(u, _graph));
277 return boost::make_iterator_range(boost::vertices(_graph));
292 struct wrapper : boost::default_dfs_visitor
295 DFSVisitor &_visitor;
297 wrapper(
network_common &graph, DFSVisitor &v) : _graph(graph), _visitor(v)
302 _visitor(boost::visit::pre, v);
306 _visitor(boost::visit::in, _graph.
target(e));
310 _visitor(boost::visit::post, v);
312 } bgl_visitor(*
this, vis);
314 std::vector<boost::default_color_type> colors(boost::num_vertices(_graph));
315 boost::iterator_property_map color_map(colors.begin(), boost::get(boost::vertex_index, _graph));
326 struct wrapper : boost::default_dfs_visitor
329 DFSVisitor &_visitor;
331 wrapper(
const network_common &graph, DFSVisitor &v) : _graph(graph), _visitor(v)
336 _visitor(boost::visit::pre, v);
340 _visitor(boost::visit::in, _graph.
target(e));
344 _visitor(boost::visit::post, v);
346 } bgl_visitor(*
this, vis);
348 std::vector<boost::default_color_type> colors(boost::num_vertices(_graph));
349 boost::iterator_property_map color_map(colors.begin(), boost::get(boost::vertex_index, _graph));
361 using namespace boost;
362 return boost::write_graphviz(out, *
this, boost::make_label_writer(boost::get(vertex_bundle, *
this)),
363 boost::make_label_writer(boost::get(edge_bundle, *
this)));
372 return base::null_vertex();
379template <
class VertexProperty,
class EdgeProperty>
class network
427 return boost::add_vertex(_graph);
433 assert(children.size() > 1);
434 for (
auto const &c : children)
439 std::vector<edge_descriptor> edges(children.size());
440 std::transform(children.cbegin(), children.cend(), edges.begin(),
441 [parent,
this](
auto c) { return boost::add_edge(parent, c, this->_graph).first; });
466template <
class VertexProperty>
467 requires(!std::is_same_v<VertexProperty, no_property>)
469 : public detail::network_common<VertexProperty, no_property,
network<VertexProperty, no_property>>
502 return boost::add_vertex(p, this->_graph);
508 assert(children.size() > 1);
509 for (
auto const &c : children)
514 std::vector<edge_descriptor> edges(children.size());
515 std::transform(children.cbegin(), children.cend(), edges.begin(),
516 [parent,
this](
auto c) { return boost::add_edge(parent, c, this->_graph).first; });
528 return this->_graph[v];
534 return this->_graph[v];
558template <
class EdgeProperty>
559 requires(!std::is_same_v<EdgeProperty, no_property>)
561 : public detail::network_common<no_property, EdgeProperty,
network<no_property, EdgeProperty>>
592 vertex_descriptor add_vertex()
594 return boost::add_vertex(this->_graph);
598 std::vector<edge_descriptor> add_edges(vertex_descriptor parent,
599 const std::vector<std::pair<vertex_descriptor, EdgeProperty>> &children)
601 assert(children.size() > 1);
602 for (
auto const &c : children)
607 std::vector<edge_descriptor> edges(children.size());
608 std::transform(children.cbegin(), children.cend(), edges.begin(), [parent,
this](
const auto &c) {
609 return boost::add_edge(parent, c.first, c.second, this->_graph).first;
622 return this->_graph[edge];
628 return this->_graph[edge];
653template <
class VertexProperty,
class EdgeProperty>
654 requires(!std::is_same_v<VertexProperty, no_property> && !std::is_same_v<EdgeProperty, no_property>)
656 : public detail::network_common<VertexProperty, EdgeProperty,
network<no_property, EdgeProperty>>
690 return boost::add_vertex(p, this->_graph);
695 std::vector<std::tuple<vertex_descriptor, EdgeProperty>> children)
697 assert(children.size() > 1);
698 for (
auto const &[c, p] : children)
703 std::vector<edge_descriptor> edges(children.size());
704 std::transform(children.cbegin(), children.cend(), edges.begin(), [parent,
this](
const auto &c) {
705 return boost::add_edge(parent, std::get<0>(c), std::get<1>(c), this->_graph).first;
718 return this->_graph[v];
724 return this->_graph[v];
730 return this->_graph[edge];
736 return this->_graph[edge];
Definition network.hpp:45
void remove_vertex(vertex_descriptor u)
Remove vertex u from the graph, also removing all edges to and from vertex .
Definition network.hpp:222
auto out_edges(vertex_descriptor u) const
Provides a range to iterate over the out-going edges of vertex .
Definition network.hpp:268
bool has_successors(vertex_descriptor u) const
Evaluates if vertex has successors.
Definition network.hpp:160
void remove_edge(edge_descriptor e)
Remove the edge from the graph.
Definition network.hpp:247
auto vertices() const
Provides a range to iterate over the vertices of the graph.
Definition network.hpp:275
tree_traits::model< tree_traits::out_edge_list_type, tree_traits::vertex_list_type, tree_traits::directed_type, VertexProperty, EdgeProperty > base
The type of graph hold by the network class.
Definition network.hpp:50
auto successors(vertex_descriptor u) const
The successors of vertex .
Definition network.hpp:170
bool is_isomorphic(network_common const &other) const
Detects if there is a 1-to-1 mapping of the vertices in one graph to the vertices of another graph su...
Definition network.hpp:101
vertex_descriptor predecessor(vertex_descriptor u) const
The predecessor of vertex .
Definition network.hpp:150
typename base::degree_size_type degree_size_type
Degree size type.
Definition network.hpp:78
void to_graphviz(std::ostream &out) const
Print the graph to the graphviz format.
Definition network.hpp:359
static constexpr vertex_descriptor null_vertex()
Null vertex identifier.
Definition network.hpp:370
degree_size_type degree(vertex_descriptor u) const
Returns the number of in-edges plus out-edges.
Definition network.hpp:119
network_common()
Default constructor.
Definition network.hpp:56
void remove_edge(vertex_descriptor u, vertex_descriptor v)
Remove the edge from the graph.
Definition network.hpp:240
edge_descriptor add_edge(vertex_descriptor u, vertex_descriptor v)
Inserts the edge into the graph if it does not exist, and returns an edge descriptor pointing to the...
Definition network.hpp:232
bool has_predecessor(vertex_descriptor u) const
Evaluates if vertex has a predecessor.
Definition network.hpp:141
vertex_descriptor source(edge_descriptor e) const
Returns the source vertex of a directed edge.
Definition network.hpp:189
auto in_edges(vertex_descriptor u) const
Provides a range to iterate over the in-going edges of vertex .
Definition network.hpp:260
void depth_first_search(vertex_descriptor start, DFSVisitor &vis)
Performs a read-and-write depth-first traversal of the vertices starting at vertex .
Definition network.hpp:289
degree_size_type in_degree(vertex_descriptor u) const
Returns the number of in-edges of vertex .
Definition network.hpp:127
typename base::edge_descriptor edge_descriptor
Edge descriptor.
Definition network.hpp:69
void depth_first_search(vertex_descriptor start, DFSVisitor &vis) const
Performs a read-only depth-first traversal of the vertices starting at vertex .
Definition network.hpp:323
vertex_descriptor target(edge_descriptor e) const
Returns the target vertex of a directed edge.
Definition network.hpp:197
typename base::traversal_category traversal_category
The ways in which the vertices in the graph can be traversed.
Definition network.hpp:81
typename base::out_edge_iterator out_edge_iterator
Iterate through the out-edges.
Definition network.hpp:90
degree_size_type out_degree(vertex_descriptor v) const
Returns the number of out-edges of vertex .
Definition network.hpp:133
vertex_descriptor find_root_from(vertex_descriptor u) const
Finds the root of the network graph starting from a vertex .
Definition network.hpp:109
typename base::in_edge_iterator in_edge_iterator
Iterate through the in-edges.
Definition network.hpp:87
std::optional< edge_descriptor > edge(vertex_descriptor u, vertex_descriptor v) const
Returns the edge between two vertices of the graph if the edge exists.
Definition network.hpp:180
void clear_vertex(vertex_descriptor u)
Remove all edges to and from vertex from the graph.
Definition network.hpp:215
typename base::vertex_descriptor vertex_descriptor
Vertex descriptor ("node ID" in other words).
Definition network.hpp:66
VertexProperty vertex_property
Vertex information.
Definition network.hpp:72
typename base::directed_category directed_category
The graph is bidirected.
Definition network.hpp:84
network_common(size_t n)
Constructs a network with vertices.
Definition network.hpp:61
EdgeProperty edge_property
Edge information.
Definition network.hpp:75
typename base::vertex_descriptor vertex_descriptor
Vertex descriptor.
Definition network.hpp:666
EdgeProperty & operator[](const edge_descriptor &edge)
Read and write access to the edge property.
Definition network.hpp:734
network()
Default constructor. Initializes a graph with 0 vertices.
Definition network.hpp:673
const EdgeProperty & operator[](const edge_descriptor &edge) const
Read only access to the edge property.
Definition network.hpp:728
network(size_t n)
Construct graph with vertices.
Definition network.hpp:678
const VertexProperty & operator[](vertex_descriptor v) const
Read only access to the vertex property.
Definition network.hpp:716
typename base::edge_descriptor edge_descriptor
Edge descriptor.
Definition network.hpp:663
vertex_descriptor add_vertex(const VertexProperty &p)
Add a vertex and its properties to the graph.
Definition network.hpp:688
std::vector< edge_descriptor > add_edges(vertex_descriptor parent, std::vector< std::tuple< vertex_descriptor, EdgeProperty > > children)
Add edges from the parent vertex (source) to the children (target)
Definition network.hpp:694
VertexProperty & operator[](vertex_descriptor v)
Read and write access to the vertex property.
Definition network.hpp:722
typename base::vertex_descriptor vertex_descriptor
Vertex descriptor.
Definition network.hpp:479
network(size_t n)
Construct graph with vertices.
Definition network.hpp:490
typename base::edge_descriptor edge_descriptor
Edge descriptor.
Definition network.hpp:476
VertexProperty & operator[](vertex_descriptor v)
Read and write access to the vertex property.
Definition network.hpp:532
const VertexProperty & operator[](vertex_descriptor v) const
Read only access to the vertex property.
Definition network.hpp:526
network()
Default constructor. Initializes a graph with 0 vertices.
Definition network.hpp:485
auto add_vertex(const VertexProperty &p)
Add a vertex and its properties to the graph.
Definition network.hpp:500
std::vector< edge_descriptor > add_edges(vertex_descriptor parent, std::vector< vertex_descriptor > children)
Add edges from the parent vertex (source) to the children (target)
Definition network.hpp:506
EdgeProperty & operator[](const edge_descriptor &edge)
Read and write access to the edge property.
Definition network.hpp:626
typename base::edge_descriptor edge_descriptor
Edge descriptor.
Definition network.hpp:568
network(size_t n)
Construct graph with vertices.
Definition network.hpp:582
network()
Default constructor. Initializses a graph with 0 vertices.
Definition network.hpp:577
typename base::vertex_descriptor vertex_descriptor
Vertex descriptor.
Definition network.hpp:571
const EdgeProperty & operator[](const edge_descriptor &edge) const
Read only access to the edge property.
Definition network.hpp:620
network(size_t n)
Construct graph with vertices.
Definition network.hpp:416
vertex_descriptor add_vertex()
Add a vertex to the graph.
Definition network.hpp:425
std::vector< edge_descriptor > add_edges(vertex_descriptor parent, std::vector< vertex_descriptor > children)
Add edges from the parent vertex (source) to the children (target)
Definition network.hpp:431
network()
Default constructor. Initializes a graph with 0 vertices.
Definition network.hpp:411
Definition network.hpp:380
Definition cardinal_k_ary_tree.hpp:46
bool isomorphism(binary_tree< false, Vertex0 > const &g, binary_tree< false, Vertex1 > const &h)
Detects if there is a 1-to-1 mapping of the vertices in one graph to the vertices of another graph su...
Definition cardinal_k_ary_tree.hpp:616
void depth_first_search(binary_tree< false, Vertex > &g, vertex_descriptor_t< binary_tree< false, Vertex > > s, DFSTreeVisitor &vis)
performs a depth-first traversal of the vertices in a directed graph
Definition cardinal_k_ary_tree.hpp:573
Simulation of coalescent trees.
Definition coalescence.hpp:26
boost::vecS vertex_list_type
We don't allow for inserting vertices except at the end and we don't remove vertices....
Definition tree_traits.hpp:31
boost::bidirectionalS directed_type
Coalescent trees are directed acyclic graphs but we need bidirectionality for in-edges access.
Definition tree_traits.hpp:36
boost::setS out_edge_list_type
We want to enforce avoiding multi-graphs (edges with same end nodes)
Definition tree_traits.hpp:27
boost::adjacency_list< Types... > model
Trees are sparse graph in nature, adjacency_matrix would not be justified here.
Definition tree_traits.hpp:24