Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Dijkstra.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
44#ifndef DIJKSTRA_H
45#define DIJKSTRA_H
46
47# include <ah-graph-concepts.H>
48
49#include <ahFunction.H>
50#include <ah-errors.H>
51#include <archeap.H>
52#include <tpl_find_path.H>
53#include <tpl_agraph.H>
55
56namespace Aleph
57{
58
110template <AlephGraph GT,
111 ArcDistance<GT> Distance = Dft_Dist<GT>,
112 template <typename, class> class Itor = Node_Arc_Iterator,
113 ArcFilter<GT> SA = Dft_Show_Arc<GT>,
114 template <class, class, class> class HeapT = ArcHeap>
116 : public Shortest_Path_Base<GT, Distance, Itor, SA, HeapT>
117{
119
120 // Import types from base class
121 using typename Base::Node_Info;
122 using typename Base::Tree_Node_Info;
123 using typename Base::Arc_Info;
124 using typename Base::Tree_Arc_Info;
125 using typename Base::Initialize_Node;
126 using typename Base::Initialize_Arc;
127 using typename Base::Initialize_Tree_Node;
128 using typename Base::Initialize_Tree_Arc;
129 using typename Base::Destroy_Node;
130 using typename Base::Destroy_Arc;
131 using typename Base::Destroy_Tree_Node;
132 using typename Base::Destroy_Tree_Arc;
133
134 // Local cookie access macros
135#define DNassert(p) (static_cast<Node_Info*>(NODE_COOKIE(p)))
136#define TREENODE(p) (static_cast<Tree_Node_Info*>(NODE_COOKIE(p))->tree_node)
137#define ACC(p) (DNassert(p)->dist)
138#define HEAPNODE(p) (DNassert(p)->heap_node)
139#define DAassert(p) (static_cast<Arc_Info*>(ARC_COOKIE(p)))
140#define ARC_DIST(p) (this->distance(p))
141#define TREEARC(p) (static_cast<Tree_Arc_Info*>(ARC_COOKIE(p))->tree_arc)
142#define POT(p) (DAassert(p)->pot)
143
144 // Import member variables from base
145 using Base::sa;
146 using Base::heap;
147 using Base::painted;
148 using Base::ptr_g;
149 using Base::s;
150
151public:
157 Dijkstra_Min_Paths(Distance dist = Distance(), SA __sa = SA())
158 : Base(dist, __sa)
159 {
160 // empty
161 }
162
163 // Import public methods from base
165 using Base::is_painted;
167 using Base::get_graph;
168 using Base::get_distance;
169 using Base::get_min_path;
171
184 typename GT::Node*
185 compute_min_paths_tree(const GT& g, typename GT::Node* start, GT& tree)
186 {
187 ah_domain_error_if(start == nullptr) << "start node cannot be null";
188 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
189
190 this->template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
191 auto guard =
192 this->template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
193
194 clear_graph(tree);
195
196 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
197 ACC(start) = 0;
198 auto ret = TREENODE(start) = tree.insert_node(start->get_info());
199 NODE_COOKIE(TREENODE(start)) = start;
200
201 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
202 {
203 auto arc = it.get_current_arc_ne();
204 POT(arc) = ARC_DIST(arc);
205 heap.put_arc(arc, it.get_tgt_node());
206 }
207
208 const auto& n = g.get_num_nodes();
209
210 while (tree.get_num_nodes() < n and not heap.is_empty())
211 {
212 auto garc = heap.get_min_arc();
214 continue;
215
216 auto gsrc = g.get_src_node(garc);
217 auto gtgt = g.get_tgt_node(garc);
218
221 continue;
222
223 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
224
226 std::swap(gsrc, gtgt);
227
228 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
229
230 auto ttgt = tree.insert_node(gtgt->get_info());
231 TREENODE(gtgt) = ttgt;
232 auto tsrc = TREENODE(gsrc);
233
234 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
235 TREEARC(garc) = tarc;
236
237 ACC(gtgt) = this->checked_add(ACC(gsrc), ARC_DIST(garc));
238 const auto& acc = ACC(gtgt);
239
240 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
241 {
242 auto arc = it.get_current_arc_ne();
244 continue;
245
246 auto tgt = it.get_tgt_node();
248 continue;
249
250 if (this->add_if_fits(acc, ARC_DIST(arc), POT(arc)))
251 heap.put_arc(arc, tgt);
252 }
253 }
254
255 if (this->beyond_max)
256 this->check_beyond_max(g, start);
257
258 return ret;
259 }
260
273 typename GT::Node* start,
274 typename GT::Node* end,
275 GT& tree)
276 {
277 ah_domain_error_if(start == nullptr) << "start node cannot be null";
278 ah_domain_error_if(end == nullptr) << "end node cannot be null";
279 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
280
281 this->template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
282 auto guard =
283 this->template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
284 clear_graph(tree);
285
286 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
287 ACC(start) = 0;
288 TREENODE(start) = tree.insert_node(start->get_info());
289 NODE_COOKIE(TREENODE(start)) = start;
290
291 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
292 {
293 auto arc = it.get_current_arc_ne();
294 POT(arc) = ARC_DIST(arc);
295 heap.put_arc(arc, it.get_tgt_node());
296 }
297
298 const auto& n = g.get_num_nodes();
299
300 while (tree.get_num_nodes() < n and not heap.is_empty())
301 {
302 auto garc = heap.get_min_arc();
304 continue;
305
306 auto gsrc = g.get_src_node(garc);
307 auto gtgt = g.get_tgt_node(garc);
308
311 continue;
312
313 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
314
316 std::swap(gsrc, gtgt);
317
318 auto ttgt = tree.insert_node(gtgt->get_info());
319 TREENODE(gtgt) = ttgt;
320 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
321
322 auto tarc = tree.insert_arc(TREENODE(gsrc), TREENODE(gtgt), garc->get_info());
323 TREEARC(garc) = tarc;
324
325 if (gtgt == end)
326 break;
327
328 ACC(gtgt) = this->checked_add(ACC(gsrc), ARC_DIST(garc));
329 const auto& acc = ACC(gtgt);
330
331 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
332 {
333 auto arc = it.get_current_arc_ne();
335 continue;
336
337 auto tgt = it.get_tgt_node();
339 continue;
340
341 if (this->add_if_fits(acc, ARC_DIST(arc), POT(arc)))
342 heap.put_arc(arc, tgt);
343 }
344 }
345
347 this->check_beyond_max(g, start, end);
348 }
349
362 typename GT::Node* start,
363 typename GT::Node* end)
364 {
365 ah_domain_error_if(start == nullptr) << "start node cannot be null";
366 ah_domain_error_if(end == nullptr) << "end node cannot be null";
367 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
368
369 bool ret_val = false;
370 this->template init<Initialize_Node, Initialize_Arc>(g, start);
371 auto guard = this->template uninit_on_exit<Destroy_Node, Destroy_Arc>();
372
373 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
374 ACC(start) = 0;
375
376 if (start == end) // the path of no arc
377 {
378 painted = true;
379 return true;
380 }
381
382 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
383 {
384 auto arc = it.get_current_arc_ne();
385 POT(arc) = ARC_DIST(arc);
386 heap.put_arc(arc, it.get_tgt_node());
387 }
388
389 const auto& n = g.get_num_nodes();
390 size_t tn = 1;
391
392 while (tn < n and not heap.is_empty())
393 {
394 auto garc = heap.get_min_arc();
396 continue;
397
398 auto src = g.get_src_node(garc);
399 auto tgt = g.get_tgt_node(garc);
400
403 continue;
404
405 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
406
408 std::swap(src, tgt);
409
410 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
411 DNassert(tgt)->ret_node = src;
412
413 ++tn;
414
415 if (tgt == end)
416 {
417 ret_val = true;
418 break;
419 }
420
421 ACC(tgt) = this->checked_add(ACC(src), ARC_DIST(garc));
422 const auto& acc = ACC(tgt);
423
424 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
425 {
426 auto a = it.get_current_arc_ne();
428 continue;
429
430 auto t = it.get_tgt_node();
432 continue;
433
434 if (this->add_if_fits(acc, ARC_DIST(a), POT(a)))
435 heap.put_arc(a, t);
436 }
437 }
438
439 if (this->beyond_max and not ret_val)
440 this->check_beyond_max(g, start, end);
441
442 painted = true;
443
444 return ret_val;
445 }
446
455 void paint_min_paths_tree(const GT& g, typename GT::Node* start)
456 {
457 ah_domain_error_if(start == nullptr) << "start node cannot be null";
458 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
459
460 this->template init<Initialize_Node, Initialize_Arc>(g, start);
461 auto guard = this->template uninit_on_exit<Destroy_Node, Destroy_Arc>();
462
463 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
464 ACC(start) = 0;
465
466 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
467 {
468 auto arc = it.get_current_arc_ne();
469 POT(arc) = ARC_DIST(arc);
470 heap.put_arc(arc, it.get_tgt_node());
471 }
472
473 const auto& n = g.get_num_nodes();
474 size_t tn = 1;
475
476 while (tn < n and not heap.is_empty())
477 {
478 auto garc = heap.get_min_arc();
480 continue;
481
482 auto src = g.get_src_node(garc);
483 auto tgt = g.get_tgt_node(garc);
484
487 continue;
488
489 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
490
492 std::swap(src, tgt);
493
494 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
495 DNassert(tgt)->ret_node = src;
496
497 ++tn;
498
499 ACC(tgt) = this->checked_add(ACC(src), ARC_DIST(garc));
500 const auto& acc = ACC(tgt);
501
502 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
503 {
504 auto a = it.get_current_arc_ne();
506 continue;
507
508 auto t = it.get_tgt_node();
510 continue;
511
512 if (this->add_if_fits(acc, ARC_DIST(a), POT(a)))
513 heap.put_arc(a, t);
514 }
515 }
516
517 if (this->beyond_max)
518 this->check_beyond_max(g, start);
519
520 painted = true;
521 }
522
537 typename GT::Node* start, typename GT::Node* end,
538 Path<GT>& min_path)
539 {
540 min_path.empty();
541 if (paint_partial_min_paths_tree(g, start, end))
542 return this->get_min_path(end, min_path);
543
544 return std::numeric_limits<typename Distance::Distance_Type>::max();
545 }
546
555 void operator()(const GT& g, typename GT::Node* s, GT& tree)
556 {
557 compute_min_paths_tree(g, s, tree);
558 }
559
562 typename GT::Node* s,
563 typename GT::Node* e,
564 Path<GT>& path)
565 {
566 return find_min_path(g, s, e, path);
567 }
568
569#undef DNassert
570#undef TREENODE
571#undef ACC
572#undef HEAPNODE
573#undef DAassert
574#undef ARC_DIST
575#undef TREEARC
576#undef POT
577};
578
579} // end namespace Aleph
580
581#endif // DIJKSTRA_H
#define TREENODE(p)
Definition Dijkstra.H:136
#define POT(p)
Definition Dijkstra.H:142
#define ACC(p)
Definition Dijkstra.H:137
#define DNassert(p)
Definition Dijkstra.H:135
#define ARC_DIST(p)
Definition Dijkstra.H:140
#define TREEARC(p)
Definition Dijkstra.H:141
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
Standard functor implementations and comparison objects.
Arc heap for graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Default distance accessor for arc weights.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:117
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Computes the spanning tree of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:555
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Paints on graph g the spanning tree of all shortest paths starting from start.
Definition Dijkstra.H:455
Distance::Distance_Type operator()(const GT &g, typename GT::Node *s, typename GT::Node *e, Path< GT > &path)
See find_min_path().
Definition Dijkstra.H:561
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
Definition Dijkstra.H:536
GT::Node * compute_min_paths_tree(const GT &g, typename GT::Node *start, GT &tree)
Computes the spanning tree of all shortest paths from the start node.
Definition Dijkstra.H:185
bool painted
Whether graph has been painted.
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the partial spanning tree of all shortest paths from start that contains the path from start...
Definition Dijkstra.H:272
bool paint_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints on graph g the partial shortest paths tree starting from start and stopping when the end node ...
Definition Dijkstra.H:361
Dijkstra_Min_Paths(Distance dist=Distance(), SA __sa=SA())
Constructor.
Definition Dijkstra.H:157
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Path on a graph.
Definition tpl_graph.H:3832
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:3999
Base class providing common infrastructure for shortest path algorithms.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void check_beyond_max(const GT &g, typename GT::Node *start, typename GT::Node *end=nullptr) const
After a painting in which a sum went beyond max(), throws if a node that start reaches was left out,...
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
bool is_painted() const noexcept
Check if the graph has been painted.
bool has_computation() const noexcept
Check if a computation has been performed.
Distance::Distance_Type checked_add(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b) const
Checked addition to prevent integer overflow.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
bool add_if_fits(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b, typename Distance::Distance_Type &out)
Adds a tentative distance that may go beyond max().
bool beyond_max
A sum of the painting went beyond max()
GT * ptr_g
Pointer to the graph.
bool painted
Whether graph has been painted.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
#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 NODE_COOKIE(p)
Return the node cookie
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
#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.
@ Spanning_Tree
Definition aleph-graph.H:79
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Common utilities and base class for shortest path algorithms.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Information stored in arc cookies for painting version.
Arc cleanup for painting version.
Node cleanup for painting version.
Arc cleanup and mapping for tree-building version.
Node cleanup and mapping for tree-building version.
Arc initialization for painting version.
Node initialization for painting version.
Arc initialization for tree-building version.
Node initialization for tree-building version.
Information stored in node cookies for painting version.
Extended arc info with tree arc mapping.
Extended node info with tree node mapping.
Distance accessor.
Array-based graph implementation.
Path finding algorithms in graphs.