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 <ah-graph-concepts.H>
43
44# include <tpl_graph.H>
45
46namespace Aleph {
47
48
68 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT> >
70{
71 SA sa;
72 typename GT::Node * tgt = nullptr;
73
74 bool test_path(typename GT::Node * curr)
75 {
76 if (curr == tgt)
77 return true; // tgt was reached
78
79 if (IS_NODE_VISITED(curr, Find_Path)) // was curr_node visited?
80 return false; // yes, do not explore
81
82 NODE_BITS(curr).set_bit(Find_Path, true); // paint curr_node
83
84 // recursively search through curr's arcs
85 for (Node_Arc_Iterator<GT, SA> i(curr, sa); i.has_curr(); i.next_ne())
86 {
87 typename GT::Arc * arc = i.get_current_arc_ne();
88 if (IS_ARC_VISITED(arc, Find_Path))
89 continue;
90
91 ARC_BITS(arc).set_bit(Find_Path, true); // paint arc
92 if (test_path(i.get_tgt_node()))
93 return true;
94 }
95
96 // all of curr_node's adjacent arcs were explored without
97 // finding end_node ==> there is no path through curr_node
98 return false;
99 }
100
101 bool test_path(const GT & g, typename GT::Node * src, typename GT::Node * dest)
102 { // if the graph is connected ==> a path exists
103 if (not g.is_digraph() and g.get_num_arcs() >= g.get_num_nodes())
104 return true;
105
108
109 tgt = dest;
110
111 // recursively search through src's adjacent arcs
112 for (Node_Arc_Iterator<GT, SA> i(src, sa); i.has_curr(); i.next_ne())
113 {
114 typename GT::Arc * arc = i.get_current_arc_ne();
115 ARC_BITS(arc).set_bit(Find_Path, true); // mark arc
116 if (test_path(i.get_tgt_node()))
117 return true;
118 }
119
120 // all of start_node's arcs have been explored without
121 // finding a path to end_node ==> no path exists
122 return false;
123 }
124
125public:
126
127 Test_For_Path(SA __sa = SA()) : sa(__sa) { /* empty */ }
128
136 bool operator () (const GT& g, typename GT::Node * start_node,
137 typename GT::Node * end_node)
138 {
139 return test_path(g, start_node, end_node);
140 }
141};
142
143
144
145} // end namespace Aleph
146
147# endif // TEST_PAT_H
C++20 concepts for the protocol shared by graph algorithms.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Checks whether a path exists between two nodes.
bool operator()(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Invokes the test for a path's existence between two nodes.
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:1088
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:699
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1094
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
#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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#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
and
Check uniqueness with explicit hash + equality functors.
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
Generic graph and digraph implementations.