Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
euclidian-graph-common.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
43# ifndef EUCLIDIAN_GRAPH_COMMON_H
44# define EUCLIDIAN_GRAPH_COMMON_H
45
46# include <ah-graph-concepts.H>
47
48# include <cmath>
49# include <cstddef>
50# include <limits>
51
52# include <ah-random.H>
53# include <ah_init_guard.H>
54# include <tpl_sgraph.H>
55# include <io_graph.H>
56# include <random_graph.H>
57
58namespace Aleph
59{
61 struct My_P
62 {
63 long x, y;
64 };
65
75
76 template <class GT>
77 struct Init_P
78 {
79 long W, H;
80
82
88 Init_P(const long w, const long h)
89 : W(w), H(h)
90 {
91 ah_domain_error_if(W <= 0 or H <= 0)
92 << "Init_P(): width and height must be > 0";
93 }
94
95 void operator ()(GT &, typename GT::Node *p)
96 {
97 long x, y;
98 while (true)
99 {
100 x = rand_gen->uniform_int(W);
102 std::pair<int, int> q(x, y);
103 if (puntos.search(q) != nullptr)
104 continue;
105
106 puntos.insert(q);
107 break;
108 }
109
110 My_P & my_p = p->get_info();
111 my_p.x = x;
112 my_p.y = y;
113 }
114 };
115
116 template <class GT>
117 struct Init_Arc
118 {
120
121 Init_Arc(const int max) : max_offset(max) {}
122
123 void operator ()(GT & g, typename GT::Arc *a)
124 {
125 typename GT::Node *src = g.get_src_node(a);
126 typename GT::Node *tgt = g.get_tgt_node(a);
127
128 const My_P & psrc = src->get_info();
129 const My_P & ptgt = tgt->get_info();
130
131 const double dist = std::hypot(static_cast<double>(psrc.x - ptgt.x),
132 static_cast<double>(psrc.y - ptgt.y));
133
134 const int offset = max_offset > 0 ? static_cast<int>(rand_gen->uniform_int(max_offset)) : 0;
135
136 a->get_info() = dist + offset;
137 }
138 };
139
140 template <class GT>
141 struct Wnode
142 {
143 void operator ()(std::ostream & output, GT &, typename GT::Node *p)
144 {
145 output << p->get_info().x << " " << p->get_info().y << '\n';
146 }
147 };
148
149 template <class GT>
150 struct Rnode
151 {
152 void operator ()(std::istream & input, GT &, typename GT::Node *p)
153 {
154 input >> p->get_info().x;
155 input >> p->get_info().y;
156 }
157 };
158
159 template <class GT>
160 struct Warc
161 {
162 void operator ()(std::ostream & output, GT &, typename GT::Arc *a)
163 {
164 output << a->get_info() << '\n';
165 }
166 };
167
168 template <class GT>
169 struct Rarc
170 {
171 void operator ()(std::istream & input, GT &, typename GT::Arc *a)
172 {
173 input >> a->get_info();
174 }
175 };
176
177
178 template <AlephGraph GT>
179 inline
180 GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h,
181 unsigned int seed)
182 {
183 ah_domain_error_if(w <= 0 or h <= 0)
184 << "gen_random_euclidian_graph(): width and height must be > 0";
185
186 const auto max_unique = static_cast<size_t>(w) * static_cast<size_t>(h);
187 ah_domain_error_if(n > max_unique)
188 << "gen_random_euclidian_graph(): requested n exceeds available unique grid points";
189
191 rand_gen = &rng;
192 Init_Guard clear_rand_gen([] { rand_gen = nullptr; });
193
194 Init_P<GT> initp(w, h);
195 const int max_offset = std::max(1,
196 static_cast<int>(std::ceil(std::hypot(static_cast<double>(w),
197 static_cast<double>(h)))));
198 Init_Arc<GT> initarc(max_offset);
199
200 return Random_Graph<GT, Init_P<GT>, Init_Arc<GT>>(seed, initp, initarc)(n, m);
201 }
202} // end namespace Aleph
203
204# endif // EUCLIDIAN_GRAPH_COMMON_H
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
RAII guard for exception-safe resource cleanup.
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Key * search(const Key &key)
Find an element in the set.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
RAII guard for graph algorithm initialization.
Random undirected graph generator.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
static mt19937 rng
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Graph serialization and deserialization utilities.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h, unsigned int seed)
Gsl_Compatible_Rng * rand_gen
Internal RNG handle used by the helper functors in this header.
Random graph generation utilities.
void operator()(GT &g, typename GT::Arc *a)
Init_P(const long w, const long h)
Create a node initializer that assigns unique random positions.
void operator()(GT &, typename GT::Node *p)
DynSetAvlTree< std::pair< int, int > > puntos
Simple integer point used as node payload by the helper functors in this header.
void operator()(std::istream &input, GT &, typename GT::Arc *a)
void operator()(std::istream &input, GT &, typename GT::Node *p)
void operator()(std::ostream &output, GT &, typename GT::Arc *a)
void operator()(std::ostream &output, GT &, typename GT::Node *p)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
Simple graph implementation with adjacency lists.
ofstream output
Definition writeHeap.C:215