Aleph-w  1.9
General library for algorithms and data structures
hamiltonian.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef HAMILTONIAN_H
28 # define HAMILTONIAN_H
29 
30 # include <tpl_graph.H>
31 
32 namespace Aleph
33 {
34 
52 template <class GT,
53  class SN = Dft_Show_Node<GT>,
54  class SA = Dft_Show_Arc<GT> >
56 {
57  SN & sn;
58  SA & sa;
59 
60  bool test_graph(GT & g)
61  {
62  assert(not g.is_digraph());
63 
64  const size_t & n = g.get_num_nodes();
65 
66  for (Node_Iterator<GT, SN> i(g, sn); i.has_curr(); i.next_ne())
67  {
68  typename GT::Node * src = i.get_curr_ne();
69  const size_t & nsrc = g.get_num_arcs(src);
70 
71  i.next_ne();
72 
73  for (Node_Iterator<GT, SN> j(i); j.has_curr(); j.next_ne())
74  if (nsrc + g.get_num_arcs(j.get_curr_ne()) < n)
75  return false;
76  }
77 
78  return true;
79  }
80 
81  bool test_digraph(GT & g)
82  {
83  assert(g.is_digraph());
84 
85  g.reset_counter_nodes();
86 
87  // una primera pasada sobre arcos para contar grados de entrada
88  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
89  NODE_COUNTER(it.get_tgt_node_ne())++;
90 
91  const size_t & n = g.get_num_nodes();
92 
93  for (Node_Iterator<GT, SN> i(g, sn); i.has_curr(); i.next_ne())
94  {
95  typename GT::Node * src = i.get_curr_ne();
96 
97  for (Node_Iterator<GT, SN> j(g, sn); j.has_curr(); j.next_ne())
98  {
99  typename GT::Node * tgt = j.get_curr_ne();
100 
101  if (src == tgt)
102  continue;
103 
104  if (g.get_num_arcs(src) + NODE_COUNTER(tgt) >= n)
105  continue;
106 
107  // buscar si hay arco src-->tgt
108  bool terminate = true;
109  for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr();
110  it.next_ne())
111  if (src == it.get_tgt_node_ne())
112  {
113  terminate = false;
114  break;
115  }
116 
117  if (terminate)
118  return false;
119  }
120  }
121 
122  return true;
123  }
124 
125 public:
126 
127  Test_Hamiltonian_Sufficiency(SN && __sn = SN(), SA && __sa = SA())
128  : sn(__sn), sa(__sa)
129  {
130  // empty
131  }
132 
133  // retorna true si el grafo o digrafo satisface la prueba de
134  // suficiencia para ser hamiltoniano.
135  bool operator () (GT & g)
136  {
137  if (g.is_digraph())
138  return test_digraph(g);
139  else
140  return test_graph(g);
141  }
142 };
143 
144 
145 
146 } // end namespace Aleph
147 
148 # endif // HAMILTONIAN_H
Definition: hamiltonian.H:55
Definition: tpl_graph.H:1225
#define NODE_COUNTER(p)
Definition: aleph-graph.H:311
Definition: tpl_graph.H:1257
Definition: ah-comb.H:35
Definition: tpl_graph.H:1063
Definition: tpl_graph.H:1270
Definition: tpl_graph.H:1177

Leandro Rabindranath León