Aleph-w  1.9
General library for algorithms and data structures
tpl_test_path.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 TEST_PATH_H
28 # define TEST_PATH_H
29 
30 # include <tpl_graph.H>
31 
32 namespace Aleph {
33 
34 
54  template <class GT, class SA = Dft_Show_Arc<GT> >
56 {
57  SA sa;
58  typename GT::Node * tgt = nullptr;
59 
60  bool test_path(typename GT::Node * curr)
61  {
62  if (curr == tgt)
63  return true; // se alcanzó a tgt
64 
65  if (IS_NODE_VISITED(curr, Find_Path)) // ¿se visitó curr_node?
66  return false; // sí, no explore
67 
68  NODE_BITS(curr).set_bit(Find_Path, true); // pintar curr_node
69 
70  // buscar recursivamente a través de arcos de curr
71  for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
72  {
73  typename GT::Arc * arc = i.get_current_arc_ne();
74  if (IS_ARC_VISITED(arc, Find_Path))
75  continue;
76 
77  ARC_BITS(arc).set_bit(Find_Path, true); // pintar arco
78  if (test_path(i.get_tgt_node()))
79  return true;
80  }
81 
82  // todos los arcos adyacentes de curr_node explorados sin
83  // encontrar a end_node ==> no existe camino por curr_node
84  return false;
85  }
86 
87  bool test_path(const GT & g, typename GT::Node * src, typename GT::Node * dest)
88  { // si el grafo es conexo ==> existe camino
89  if (not g.is_digraph() and g.get_num_arcs() >= g.get_num_nodes())
90  return true;
91 
92  g.reset_bit_nodes(Find_Path);
93  g.reset_bit_arcs(Find_Path);
94 
95  tgt = dest;
96 
97  // buscar recursivamente por arcos adyacentes a src
98  for (Node_Arc_Iterator<GT, SA> i(src, sa); i.has_curr(); i.next_ne())
99  {
100  typename GT::Arc * arc = i.get_current_arc_ne();
101  ARC_BITS(arc).set_bit(Find_Path, true); // marcar arco
102  if (test_path(i.get_tgt_node()))
103  return true;
104  }
105 
106  // todos los arcos de start_node han sido explorados sin
107  // encontrar camino hasta end_node ==> no existe camino
108  return false;
109  }
110 
111 public:
112 
113  Test_For_Path(SA __sa = SA()) : sa(__sa) { /* empty */ }
114 
122  bool operator () (const GT& g, typename GT::Node * start_node,
123  typename GT::Node * end_node)
124  {
125  return test_path(g, start_node, end_node);
126  }
127 };
128 
129 
130 
131 } // end namespace Aleph
132 
133 # endif // TEST_PAT_H
bool operator()(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Definition: tpl_test_path.H:122
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:327
Definition: tpl_test_path.H:55
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:360
Definition: ah-comb.H:35
#define NODE_BITS(p)
Definition: aleph-graph.H:305
#define ARC_BITS(p)
Definition: aleph-graph.H:351
Definition: tpl_graph.H:1177

Leandro Rabindranath León