Aleph-w 3.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 <gsl/gsl_rng.h>
53
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
73 extern gsl_rng *rand_gen;
74
75 template <class GT>
76 struct Init_P
77 {
78 long W, H;
79
81
87 Init_P(const long w, const long h)
88 : W(w), H(h)
89 {
90 ah_domain_error_if(W <= 0 or H <= 0)
91 << "Init_P(): width and height must be > 0";
92 }
93
94 void operator ()(GT &, typename GT::Node *p)
95 {
96 long x, y;
97 while (true)
98 {
101 std::pair<int, int> q(x, y);
102 if (puntos.search(q) != nullptr)
103 continue;
104
105 puntos.insert(q);
106 break;
107 }
108
109 My_P & my_p = p->get_info();
110 my_p.x = x;
111 my_p.y = y;
112 }
113 };
114
115 template <class GT>
116 struct Init_Arc
117 {
119
120 Init_Arc(const int max) : max_offset(max) {}
121
122 void operator ()(GT & g, typename GT::Arc *a)
123 {
124 typename GT::Node *src = g.get_src_node(a);
125 typename GT::Node *tgt = g.get_tgt_node(a);
126
127 const My_P & psrc = src->get_info();
128 const My_P & ptgt = tgt->get_info();
129
130 const double dist = std::hypot(static_cast<double>(psrc.x - ptgt.x),
131 static_cast<double>(psrc.y - ptgt.y));
132
133 const int offset = max_offset > 0 ? static_cast<int>(gsl_rng_uniform_int(rand_gen, max_offset)) : 0;
134
135 a->get_info() = dist + offset;
136 }
137 };
138
139 template <class GT>
140 struct Wnode
141 {
142 void operator ()(std::ostream & output, GT &, typename GT::Node *p)
143 {
144 output << p->get_info().x << " " << p->get_info().y << '\n';
145 }
146 };
147
148 template <class GT>
149 struct Rnode
150 {
151 void operator ()(std::istream & input, GT &, typename GT::Node *p)
152 {
153 input >> p->get_info().x;
154 input >> p->get_info().y;
155 }
156 };
157
158 template <class GT>
159 struct Warc
160 {
161 void operator ()(std::ostream & output, GT &, typename GT::Arc *a)
162 {
163 output << a->get_info() << '\n';
164 }
165 };
166
167 template <class GT>
168 struct Rarc
169 {
170 void operator ()(std::istream & input, GT &, typename GT::Arc *a)
171 {
172 input >> a->get_info();
173 }
174 };
175
176
177 template <AlephGraph GT>
178 inline
179 GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h,
180 unsigned int seed)
181 {
182 ah_domain_error_if(w <= 0 or h <= 0)
183 << "gen_random_euclidian_graph(): width and height must be > 0";
184
185 const auto max_unique = static_cast<size_t>(w) * static_cast<size_t>(h);
187 << "gen_random_euclidian_graph(): requested n exceeds available unique grid points";
188
190 ah_bad_alloc_if(rand_gen == nullptr);
192
194 const int max_offset = std::max(1,
195 static_cast<int>(std::ceil(std::hypot(static_cast<double>(w),
196 static_cast<double>(h)))));
197 Init_Arc<GT> initarc(max_offset);
198
200
202 rand_gen = nullptr;
203
204 return g;
205 }
206} // end namespace Aleph
207
208# endif // EUCLIDIAN_GRAPH_COMMON_H
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Definition ah-errors.H:434
C++20 concepts for the protocol shared by graph algorithms.
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) const
Find an element in the set.
Random undirected graph generator.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
__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:4121
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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.
Definition ah-arena.H:89
gsl_rng * rand_gen
Internal RNG handle used by the helper functors in this header.
GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h, unsigned int seed)
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