Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
euclidian-graph-common.H
1 # ifndef EUCLIDIAN_GRAPH_COMMON_H
2 # define EUCLIDIAN_GRAPH_COMMON_H
3 
4 # include <tpl_sgraph.H>
5 # include <io_graph.H>
6 # include <random_graph.H>
7 
8 namespace Aleph {
9 
10 struct My_P
11 {
12  int x, y;
13 };
14 
15 extern gsl_rng * rand_gen ;
16 
17  template <class GT>
18 struct Init_P
19 {
20  int W, H;
21 
23 
24  Init_P(unsigned long w, unsigned long h)
25  : W(w), H(h)
26  {
27  // empty
28  }
29 
30  void operator () (GT &, typename GT::Node * p)
31  {
32  unsigned long x, y;
33  while (true)
34  {
35  x = gsl_rng_uniform_int(rand_gen, H);
36  y = gsl_rng_uniform_int(rand_gen, W);
37  std::pair<int,int> q(x, y);
38  if (puntos.search(q) != NULL)
39  continue;
40 
41  puntos.insert(q);
42  break;
43  }
44 
45  My_P & my_p = p->get_info();
46  my_p.x = x;
47  my_p.y = y;
48  }
49 };
50 
51  template <class GT>
52 struct Init_Arc
53 {
54  int max_offset;
55 
56  Init_Arc(int max) : max_offset(max) { }
57 
58  void operator () (GT & g, typename GT::Arc * a)
59  {
60  typename GT::Node * src = g.get_src_node(a);
61  typename GT::Node * tgt = g.get_tgt_node(a);
62 
63  My_P & psrc = src->get_info();
64  My_P & ptgt = tgt->get_info();
65 
66  float dist = hypot(psrc.x - ptgt.x, psrc.y - ptgt.y);
67 
68  int offset = gsl_rng_uniform_int(rand_gen, max_offset);
69 
70  a->get_info() = dist + offset;
71  }
72 };
73 
74  template <class GT>
75 struct Wnode
76 {
77  void operator () (ostream & output, GT &, typename GT::Node * p)
78  {
79  output << p->get_info().x << " " << p->get_info().y << endl;
80  }
81 };
82 
83  template <class GT>
84 struct Rnode
85 {
86  void operator () (istream & input, GT &, typename GT::Node * p)
87  {
88  input >> p->get_info().x;
89  input >> p->get_info().y;
90  }
91 };
92 
93  template <class GT>
94 struct Warc
95 {
96  void operator () (ostream & output, GT &, typename GT::Arc * a)
97  {
98  output << a->get_info() << endl;
99  }
100 };
101 
102  template <class GT>
103 struct Rarc
104 {
105  void operator () (istream & input, GT &, typename GT::Arc * a)
106  {
107  input >> a->get_info();
108  }
109 };
110 
111 
112  template <class GT> inline
113 GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h,
114  unsigned int seed)
115 {
116  rand_gen = gsl_rng_alloc (gsl_rng_mt19937);
117  gsl_rng_set(rand_gen, seed % gsl_rng_max(rand_gen));
118 
119  Init_P<GT> initp(w, h);
120  Init_Arc<GT> initarc(hypot(w, h));
121 
123  (seed, initp, initarc) (n, m);
124 
125  gsl_rng_free(rand_gen);
126  rand_gen = NULL;
127 
128  return std::move(g);
129 }
130 
131 
132 } // end namespace Aleph
133 
134 # endif // EUCLIDIAN_GRAPH_COMMON_H
Definition: euclidian-graph-common.H:18
Definition: euclidian-graph-common.H:94
Definition: tpl_dynSetTree.H:1126
Definition: euclidian-graph-common.H:103
Definition: euclidian-graph-common.H:84
Definition: euclidian-graph-common.H:75
Definition: random_graph.H:321
Definition: euclidian-graph-common.H:52
Definition: euclidian-graph-common.H:10

Leandro Rabindranath León