aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
pathFinding_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
42#pragma once
43
45
46namespace gum::graph {
47
48 namespace detail {
49 inline std::vector< NodeId >
51 std::vector< NodeId > v;
52 NodeId cur = n1;
53 while (cur != n2) {
54 v.push_back(cur);
55 if (!mark.exists(cur))
56 GUM_ERROR(NotFound, "reconstructPath: no BFS predecessor for node " << cur)
57 cur = mark[cur];
58 }
59 v.push_back(n2);
60 return v;
61 }
62 } // namespace detail
63
64 template < GUM_DiGraphable G >
65 std::optional< std::vector< NodeId > > directedPath(const G& g, NodeId n1, NodeId n2) {
66 List< NodeId > fifo;
67 fifo.pushBack(n2);
69 mark.insert(n2, n2);
70
71 while (!fifo.empty()) {
72 const NodeId current = fifo.front();
73 fifo.popFront();
74 for (const auto n: g.parents(current)) {
75 if (mark.exists(n)) continue;
76 mark.insert(n, current);
77 if (n == n1) return detail::reconstructPath(mark, n1, n2);
78 fifo.pushBack(n);
79 }
80 }
81
82 return std::nullopt;
83 }
84
85 template < GUM_DiGraphable G >
86 std::optional< std::vector< NodeId > > directedUnorientedPath(const G& g, NodeId n1, NodeId n2) {
87 List< NodeId > fifo;
88 fifo.pushBack(n2);
90 mark.insert(n2, n2);
91
92 auto tryVisit = [&](NodeId n, NodeId from) -> bool {
93 if (mark.exists(n)) return false;
94 mark.insert(n, from);
95 if (n == n1) return true;
96 fifo.pushBack(n);
97 return false;
98 };
99
100 while (!fifo.empty()) {
101 const NodeId current = fifo.front();
102 fifo.popFront();
103 for (const auto n: g.parents(current))
104 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
105 for (const auto n: g.children(current))
106 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
107 }
108
109 return std::nullopt;
110 }
111
112 template < GUM_DiGraphable G >
113 bool hasDirectedPath(const G& g, NodeId from, NodeId to) {
114 if (!g.existsNode(from)) return false;
115
116 List< NodeId > fifo;
117 fifo.pushBack(from);
118 NodeSet marked{from};
119
120 while (!fifo.empty()) {
121 const NodeId current = fifo.front();
122 fifo.popFront();
123 for (const auto chi: g.children(current)) {
124 if (chi == to) return true;
125 if (!marked.contains(chi)) {
126 fifo.pushBack(chi);
127 marked.insert(chi);
128 }
129 }
130 }
131
132 return false;
133 }
134
135 template < GUM_UndiGraphable G >
136 std::optional< std::vector< NodeId > > undirectedPath(const G& g, NodeId n1, NodeId n2) {
137 List< NodeId > fifo;
138 fifo.pushBack(n2);
140 mark.insert(n2, n2);
141
142 while (!fifo.empty()) {
143 const NodeId current = fifo.front();
144 fifo.popFront();
145 for (const auto n: g.neighbours(current)) {
146 if (mark.exists(n)) continue;
147 mark.insert(n, current);
148 if (n == n1) return detail::reconstructPath(mark, n1, n2);
149 fifo.pushBack(n);
150 }
151 }
152
153 return std::nullopt;
154 }
155
156 template < GUM_UndiGraphable G >
157 bool hasUndirectedPath(const G& g, NodeId n1, NodeId n2) {
158 NodeSet visited;
159 NodeSet frontier{n1};
160
161 while (!frontier.empty()) {
162 const NodeId current = *frontier.begin();
163 if (current == n2) return true;
164 frontier.erase(current);
165 visited.insert(current);
166 for (const auto next: g.neighbours(current))
167 if (!visited.contains(next)) frontier.insert(next);
168 }
169
170 return false;
171 }
172
173 template < GUM_UndiGraphable G >
174 bool hasUndirectedPath(const G& g, NodeId n1, NodeId n2, const NodeSet& except) {
175 if (except.contains(n2)) return false;
176
177 NodeSet visited;
178 NodeSet frontier{n1};
179
180 while (!frontier.empty()) {
181 const NodeId current = *frontier.begin();
182 if (current == n2) return true;
183 frontier.erase(current);
184 visited.insert(current);
185 for (const auto next: g.neighbours(current))
186 if (!visited.contains(next) && !except.contains(next)) frontier.insert(next);
187 }
188
189 return false;
190 }
191
192 template < GUM_UndiGraphable G >
193 bool hasUndirectedPath(const G& g, const NodeSet& n1, const NodeSet& n2, const NodeSet& except) {
194 NodeSet visited;
195 NodeSet frontier(n1);
196
197 while (!frontier.empty()) {
198 const NodeId current = *frontier.begin();
199 if (n2.contains(current)) return true;
200 frontier.erase(current);
201 visited.insert(current);
202 for (const auto next: g.neighbours(current))
203 if (!visited.contains(next) && !except.contains(next)) frontier.insert(next);
204 }
205
206 return false;
207 }
208
209 template < GUM_MixedGraphable G >
210 std::optional< std::vector< NodeId > > mixedOrientedPath(const G& g, NodeId n1, NodeId n2) {
211 List< NodeId > fifo;
212 fifo.pushBack(n2);
214 mark.insert(n2, n2);
215
216 auto tryVisit = [&](NodeId n, NodeId from) -> bool {
217 if (mark.exists(n)) return false;
218 mark.insert(n, from);
219 if (n == n1) return true;
220 fifo.pushBack(n);
221 return false;
222 };
223
224 while (!fifo.empty()) {
225 const NodeId current = fifo.front();
226 fifo.popFront();
227 for (const auto n: g.neighbours(current))
228 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
229 for (const auto n: g.parents(current))
230 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
231 }
232
233 return std::nullopt;
234 }
235
236 template < GUM_MixedGraphable G >
237 bool hasMixedOrientedPath(const G& g, NodeId n1, NodeId n2) {
238 return mixedOrientedPath(g, n1, n2).has_value();
239 }
240
241 template < GUM_MixedGraphable G >
242 std::optional< std::vector< NodeId > > mixedUnorientedPath(const G& g, NodeId n1, NodeId n2) {
243 List< NodeId > fifo;
244 fifo.pushBack(n2);
246 mark.insert(n2, n2);
247
248 auto tryVisit = [&](NodeId n, NodeId from) -> bool {
249 if (mark.exists(n)) return false;
250 mark.insert(n, from);
251 if (n == n1) return true;
252 fifo.pushBack(n);
253 return false;
254 };
255
256 while (!fifo.empty()) {
257 const NodeId current = fifo.front();
258 fifo.popFront();
259 for (const auto n: g.neighbours(current))
260 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
261 for (const auto n: g.parents(current))
262 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
263 for (const auto n: g.children(current))
264 if (tryVisit(n, current)) return detail::reconstructPath(mark, n1, n2);
265 }
266
267 return std::nullopt;
268 }
269
270} // namespace gum::graph
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Generic doubly linked lists.
Definition list.h:378
Val & front() const
Returns a reference to first element of a list, if any.
Definition list_tpl.h:1694
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1481
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition list_tpl.h:1822
void popFront()
Removes the first element of a List, if any.
Definition list_tpl.h:1816
Exception : the element we looked for cannot be found.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:409
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
std::vector< NodeId > reconstructPath(const NodeProperty< NodeId > &mark, NodeId n1, NodeId n2)
Reconstruct the BFS path [n1 … n2] from the predecessor mark table.
std::optional< std::vector< NodeId > > undirectedPath(const G &g, NodeId n1, NodeId n2)
Shortest undirected path from n1 to n2 (BFS).
bool hasMixedOrientedPath(const G &g, NodeId n1, NodeId n2)
Returns true if a mixed-oriented path from n1 to n2 exists.
std::optional< std::vector< NodeId > > directedUnorientedPath(const G &g, NodeId n1, NodeId n2)
Shortest path from n1 to n2 ignoring arc orientation (BFS).
std::optional< std::vector< NodeId > > directedPath(const G &g, NodeId n1, NodeId n2)
Shortest directed path from n1 to n2 (BFS, arc direction).
std::optional< std::vector< NodeId > > mixedUnorientedPath(const G &g, NodeId n1, NodeId n2)
Shortest path ignoring all orientations in a mixed graph.
bool hasDirectedPath(const G &g, NodeId from, NodeId to)
Returns true if there is a directed path from from to to.
std::optional< std::vector< NodeId > > mixedOrientedPath(const G &g, NodeId n1, NodeId n2)
Shortest mixed-oriented path from n1 to n2.
bool hasUndirectedPath(const G &g, NodeId n1, NodeId n2)
Returns true if an undirected path exists between n1 and n2.
Generic BFS-based path-finding algorithms for aGrUM graphs.