Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_test_path.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
39# ifndef TEST_PATH_H
40# define TEST_PATH_H
41
42# include <tpl_graph.H>
43
44namespace Aleph {
45
46
66 template <class GT, class SA = Dft_Show_Arc<GT> >
68{
70 typename GT::Node * tgt = nullptr;
71
72 bool test_path(typename GT::Node * curr)
73 {
74 if (curr == tgt)
75 return true; // se alcanzó a tgt
76
77 if (IS_NODE_VISITED(curr, Find_Path)) // ¿se visitó curr_node?
78 return false; // sí, no explore
79
80 NODE_BITS(curr).set_bit(Find_Path, true); // pintar curr_node
81
82 // buscar recursivamente a través de arcos de curr
83 for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
84 {
85 typename GT::Arc * arc = i.get_current_arc_ne();
86 if (IS_ARC_VISITED(arc, Find_Path))
87 continue;
88
89 ARC_BITS(arc).set_bit(Find_Path, true); // pintar arco
90 if (test_path(i.get_tgt_node()))
91 return true;
92 }
93
94 // todos los arcos adyacentes de curr_node explorados sin
95 // encontrar a end_node ==> no existe camino por curr_node
96 return false;
97 }
98
99 bool test_path(const GT & g, typename GT::Node * src, typename GT::Node * dest)
100 { // si el grafo es conexo ==> existe camino
101 if (not g.is_digraph() and g.get_num_arcs() >= g.get_num_nodes())
102 return true;
103
106
107 tgt = dest;
108
109 // buscar recursivamente por arcos adyacentes a src
110 for (Node_Arc_Iterator<GT, SA> i(src, sa); i.has_curr(); i.next_ne())
111 {
112 typename GT::Arc * arc = i.get_current_arc_ne();
113 ARC_BITS(arc).set_bit(Find_Path, true); // marcar arco
114 if (test_path(i.get_tgt_node()))
115 return true;
116 }
117
118 // todos los arcos de start_node han sido explorados sin
119 // encontrar camino hasta end_node ==> no existe camino
120 return false;
121 }
122
123public:
124
125 Test_For_Path(SA __sa = SA()) : sa(__sa) { /* empty */ }
126
134 bool operator () (const GT& g, typename GT::Node * start_node,
135 typename GT::Node * end_node)
136 {
137 return test_path(g, start_node, end_node);
138 }
139};
140
141
142
143} // end namespace Aleph
144
145# endif // TEST_PAT_H
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
verfica si existe un camino entre dos nodos.
bool operator()(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Invoca a la prueba de existencia de camino entre dos nodos.
bool test_path(typename GT::Node *curr)
Test_For_Path(SA __sa=SA())
bool test_path(const GT &g, typename GT::Node *src, typename GT::Node *dest)
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1040
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:657
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1046
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define ARC_BITS(p)
Return the control bits of arc p.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
@ Find_Path
Definition aleph-graph.H:76
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
Generic graph and digraph implementations.