Aleph-w  1.9
General library for algorithms and data structures
tpl_test_acyclique.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 TPL_TEST_ACYCLIQUE_H
28 # define TPL_TEST_ACYCLIQUE_H
29 
30 namespace Aleph {
31 
32 
33 
53  template <class GT, class SA = Dft_Show_Arc<GT>>
55 {
56  SA & sa;
57 
58  bool is_acyclique(typename GT::Node * curr)
59  {
60  if (IS_NODE_VISITED(curr, Test_Cycle))
61  return false;
62 
63  NODE_BITS(curr).set_bit(Test_Cycle, true); // marcar nodo
64 
65  for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
66  {
67  typename GT::Arc * arc = i.get_current_arc_ne();
68  if (IS_ARC_VISITED(arc, Test_Cycle))
69  continue;
70 
71  ARC_BITS(arc).set_bit(Test_Cycle, true);
72 
73  if (not is_acyclique(i.get_tgt_node()))
74  return false;
75  }
76 
77  // todos los arcos recorridos sin encontrar ciclo ==>
78  // el grafo es acíclico pasando por curr_node
79  return true;
80  }
81 
82  bool is_acyclique(GT & g, size_t num_arcs)
83  {
84  if (g.is_digraph())
85  throw std::domain_error("is_graph_acyclique() does not work for digraps");
86 
87  if (num_arcs >= g.get_num_nodes())
88  return false;
89 
90  g.reset_bit_arcs(Test_Cycle);
91  g.reset_bit_nodes(Test_Cycle);
92 
93  for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
94  {
95  typename GT::Node * curr = it.get_current_node_ne();
96  if (IS_NODE_VISITED(curr, Test_Cycle))
97  continue;
98 
99  if (not is_acyclique(curr))
100  return false;
101  }
102 
103  return true;
104 }
105 
106 public:
107 
108  Is_Graph_Acyclique(SA && __sa = SA()) : sa(__sa) { /* empty */ }
109 
110  Is_Graph_Acyclique(SA & __sa) : sa(__sa) { /* empty */ }
111 
131  bool operator () (GT & g, size_t num_arcs)
132  {
133  return is_acyclique(g, num_arcs);
134  }
135 
151  bool operator () (GT & g)
152  {
153  return is_acyclique(g, g.get_num_arcs());
154  }
155 };
156 
176  template <class GT, class SA = Dft_Show_Arc<GT>>
178 {
179  SA & sa;
180 
181 public:
182 
183  Has_Cycle(SA && __sa = SA()) : sa(__sa) { /* empty */ }
184 
185  Has_Cycle(SA & __sa) : sa(__sa) { /* empty */ }
186 
202  bool operator () (GT & g) const
203  {
204  return not Is_Graph_Acyclique <GT, SA> (sa) (g);
205  }
206 
222  bool operator () (GT & g, size_t num_arcs) const
223  {
224  return not Is_Graph_Acyclique <GT, SA> (sa) (g, num_arcs);
225  }
226 };
227 
228 
229 
230 
231 } // end namespace Aleph
232 
233 # endif // TPL_TEST_ACYCLIQUE_H
Definition: tpl_test_acyclique.H:177
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:327
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:360
bool operator()(GT &g, size_t num_arcs)
Definition: tpl_test_acyclique.H:131
Definition: ah-comb.H:35
#define NODE_BITS(p)
Definition: aleph-graph.H:305
Definition: tpl_test_acyclique.H:54
#define ARC_BITS(p)
Definition: aleph-graph.H:351
Definition: tpl_graph.H:1177

Leandro Rabindranath León