Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_graph_helpers.h
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31# ifndef ALEPH_TEST_GRAPH_HELPERS_H
32# define ALEPH_TEST_GRAPH_HELPERS_H
33
34# include <cstddef>
35# include <initializer_list>
36# include <random>
37# include <utility>
38# include <vector>
39
40# include <tpl_array.H>
41
43{
44 template <class GT>
47 const size_t n,
48 const std::vector<std::pair<size_t, size_t>> & edges)
49 {
50 using Node = typename GT::Node;
51
53 for (size_t i = 0; i < n; ++i)
54 nodes(i) = g.insert_node(static_cast<int>(i));
55
56 for (const auto & [u, v] : edges)
57 {
58 if (u >= n or v >= n)
59 continue;
60 g.insert_arc(nodes(u), nodes(v), 1);
61 }
62
63 return nodes;
64 }
65
66 template <class GT>
69 GT & g,
70 const size_t n,
71 const std::initializer_list<std::pair<size_t, size_t>> & edges)
72 {
74 g, n, std::vector<std::pair<size_t, size_t>>(edges));
75 }
76
77 template <class GT>
80 const size_t n,
81 const Aleph::Array<std::pair<size_t, size_t>> & edges)
82 {
83 using Node = typename GT::Node;
84
86 for (size_t i = 0; i < n; ++i)
87 nodes(i) = g.insert_node(static_cast<int>(i));
88
89 for (auto it = edges.get_it(); it.has_curr(); it.next_ne())
90 {
91 const auto & e = it.get_curr();
92 const size_t u = e.first;
93 const size_t v = e.second;
94 if (u >= n or v >= n)
95 continue;
96 g.insert_arc(nodes(u), nodes(v), 1);
97 }
98
99 return nodes;
100 }
101
103 {
104 template <class Arc>
105 bool operator()(Arc * a) const noexcept
106 {
107 return a->get_info() > 0;
108 }
109 };
110
111 template <class EdgeContainer>
112 EdgeContainer make_random_tree_edges(const size_t n, std::mt19937 & rng)
113 {
114 EdgeContainer edges;
115 if constexpr (requires(EdgeContainer e, size_t cap) { e.reserve(cap); })
116 edges.reserve(n > 0 ? n - 1 : 0);
117
118 for (size_t v = 1; v < n; ++v)
119 {
120 std::uniform_int_distribution<size_t> pick(0, v - 1);
121 auto e = std::make_pair(v, pick(rng));
122 if constexpr (requires(EdgeContainer c, const std::pair<size_t, size_t> & p)
123 {
124 c.append(p);
125 })
126 edges.append(e);
127 else
128 edges.emplace_back(e);
129 }
130
131 return edges;
132 }
133}
134
135# endif
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
static mt19937 rng
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Aleph::Array< typename GT::Node * > build_graph_with_unit_arcs(GT &g, const size_t n, const std::vector< std::pair< size_t, size_t > > &edges)
EdgeContainer make_random_tree_edges(const size_t n, std::mt19937 &rng)
Dynamic array container with automatic resizing.