Aleph-w 3.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 (typename Aleph::Array<std::pair<size_t, size_t>>::Iterator it(edges);
90 it.has_curr(); it.next_ne())
91 {
92 const auto & e = it.get_curr();
93 const size_t u = e.first;
94 const size_t v = e.second;
95 if (u >= n or v >= n)
96 continue;
97 g.insert_arc(nodes(u), nodes(v), 1);
98 }
99
100 return nodes;
101 }
102
104 {
105 template <class Arc>
106 bool operator()(Arc * a) const noexcept
107 {
108 return a->get_info() > 0;
109 }
110 };
111
112 template <class EdgeContainer>
113 EdgeContainer make_random_tree_edges(const size_t n, std::mt19937 & rng)
114 {
115 EdgeContainer edges;
116 if constexpr (requires(EdgeContainer e, size_t cap) { e.reserve(cap); })
117 edges.reserve(n > 0 ? n - 1 : 0);
118
119 for (size_t v = 1; v < n; ++v)
120 {
121 std::uniform_int_distribution<size_t> pick(0, v - 1);
122 auto e = std::make_pair(v, pick(rng));
123 if constexpr (requires(EdgeContainer c, const std::pair<size_t, size_t> & p)
124 {
125 c.append(p);
126 })
127 edges.append(e);
128 else
129 edges.emplace_back(e);
130 }
131
132 return edges;
133 }
134}
135
136# endif
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:194
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
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)
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Dynamic array container with automatic resizing.