Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
AStar.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
50#ifndef ASTAR_H
51#define ASTAR_H
52
53# include <cmath>
54# include <iostream>
55# include <ahFunction.H>
56# include <ah-errors.H>
57# include <archeap.H>
58# include <tpl_find_path.H>
59# include <tpl_agraph.H>
60# include <shortest_path_common.H>
61
62namespace Aleph
63{
64
75template <class GT, class Distance = Dft_Dist<GT>>
77{
79
80 Distance_Type operator()(typename GT::Node*, typename GT::Node*) const
81 {
82 return Distance_Type(0);
83 }
84};
85
149template <class GT,
150 class Distance = Dft_Dist<GT>,
152 template <typename, class> class Itor = Node_Arc_Iterator,
153 class SA = Dft_Show_Arc<GT>,
154 template <class, class, class> class HeapT = ArcHeap>
156 : public Shortest_Path_Base<GT, Distance, Itor, SA, HeapT>
157{
159
160 // Import types from base class
173
174 // Local cookie access macros
175#define ANassert(p) (static_cast<Node_Info*>(NODE_COOKIE(p)))
176#define ATREENODE(p) (static_cast<Tree_Node_Info*>(NODE_COOKIE(p))->tree_node)
177#define AACC(p) (ANassert(p)->dist)
178#define AHEAPNODE(p) (ANassert(p)->heap_node)
179#define APARENT(p) (ANassert(p)->ret_node)
180#define AAassert(p) (static_cast<Arc_Info*>(ARC_COOKIE(p)))
181#define AARC_DIST(p) (Distance()(p))
182#define ATREEARC(p) (static_cast<Tree_Arc_Info*>(ARC_COOKIE(p))->tree_arc)
183#define APOT(p) (AAassert(p)->pot)
184
185 // Import member variables from base
186 using Base::sa;
187 using Base::heap;
188 using Base::painted;
189 using Base::ptr_g;
190 using Base::s;
191
192 // A* specific: heuristic functor
194
195#ifndef NDEBUG
196 // Debug mode: track heuristic validation
198
200 typename GT::Node* goal,
201 const typename Distance::Distance_Type& actual_cost)
202 {
204 return;
205
206 auto h_estimate = heuristic(node, goal);
208 {
209 std::cerr << "WARNING: Inadmissible heuristic detected!\n";
210 std::cerr << " Heuristic estimate h(" << node << ") = " << h_estimate << "\n";
211 std::cerr << " Actual remaining cost = " << actual_cost << "\n";
212 std::cerr << " Heuristic overestimates by " << (h_estimate - actual_cost) << "\n";
213 std::cerr << " A* may return suboptimal paths.\n";
214 }
215 }
216#endif
217
218public:
227 SA __sa = SA())
228 : Base(dist, __sa), heuristic(__heuristic)
229 {
230 // empty
231 }
232
233 // Import public methods from base
235 using Base::is_painted;
237 using Base::get_graph;
238 using Base::get_distance;
239 using Base::get_min_path;
241
242 // =========================================================================
243 // Methods WITH Heuristic (A* specific)
244 // =========================================================================
245
260 typename GT::Node*
262 typename GT::Node* start,
263 typename GT::Node* end,
264 GT& tree)
265 {
266 ah_domain_error_if(start == nullptr) << "start node cannot be null";
267 ah_domain_error_if(end == nullptr) << "end node cannot be null";
268 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
269
271 clear_graph(tree);
272
273 // Handle trivial case: start == end
274 if (start == end)
275 {
276 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
277 AACC(start) = 0;
278 auto tree_node = tree.insert_node(start->get_info());
279 ATREENODE(start) = tree_node;
280 NODE_COOKIE(tree_node) = start;
282 return tree_node;
283 }
284
285 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
286 AACC(start) = 0;
287 ATREENODE(start) = tree.insert_node(start->get_info());
288 NODE_COOKIE(ATREENODE(start)) = start;
289
290 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
291 {
292 auto arc = it.get_current_arc_ne();
293 auto tgt = it.get_tgt_node();
294 auto g_cost = AARC_DIST(arc);
295 auto h_cost = heuristic(tgt, end);
296 APOT(arc) = this->checked_add(g_cost, h_cost);
297 heap.put_arc(arc, tgt);
298 }
299
300 typename GT::Node* result = nullptr;
301
302 while (not heap.is_empty())
303 {
304 auto garc = heap.get_min_arc();
306 continue;
307
308 auto gsrc = g.get_src_node(garc);
309 auto gtgt = g.get_tgt_node(garc);
310
313 continue;
314
315 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
316
318 std::swap(gsrc, gtgt);
319
320 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
321
322 auto ttgt = tree.insert_node(gtgt->get_info());
324 auto tsrc = ATREENODE(gsrc);
325
326 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
327 ATREEARC(garc) = tarc;
328
330
331 if (gtgt == end)
332 {
333 result = ttgt;
334 break;
335 }
336
337 const auto& g_cost = AACC(gtgt);
338
339 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
340 {
341 auto arc = it.get_current_arc_ne();
343 continue;
344
345 auto tgt = it.get_tgt_node();
347 continue;
348
349 auto new_g = this->checked_add(g_cost, AARC_DIST(arc));
350 auto h = heuristic(tgt, end);
351 APOT(arc) = this->checked_add(new_g, h);
352 heap.put_arc(arc, tgt);
353 }
354 }
355
357
358 return result;
359 }
360
373 bool paint_partial_path(const GT& g,
374 typename GT::Node* start,
375 typename GT::Node* end)
376 {
377 ah_domain_error_if(start == nullptr) << "start node cannot be null";
378 ah_domain_error_if(end == nullptr) << "end node cannot be null";
379 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
380
381 this->template init<Initialize_Node, Initialize_Arc>(g, start);
382
383 // Handle trivial case: start == end
384 if (start == end)
385 {
386 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
387 AACC(start) = 0;
388 this->template uninit<Destroy_Node, Destroy_Arc>();
389 painted = true;
390 return true;
391 }
392
393 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
394 AACC(start) = 0;
395
396 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
397 {
398 auto arc = it.get_current_arc_ne();
399 auto tgt = it.get_tgt_node();
400 auto g_cost = AARC_DIST(arc);
401 auto h_cost = heuristic(tgt, end);
402 APOT(arc) = this->checked_add(g_cost, h_cost);
403 heap.put_arc(arc, tgt);
404 }
405
406 bool found = false;
407
408 while (not heap.is_empty())
409 {
410 auto garc = heap.get_min_arc();
412 continue;
413
414 auto src = g.get_src_node(garc);
415 auto tgt = g.get_tgt_node(garc);
416
419 continue;
420
421 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
422
424 std::swap(src, tgt);
425
426 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
427 APARENT(tgt) = src;
428
429 if (tgt == end)
430 {
431 found = true;
432 break;
433 }
434
435 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
436 const auto& g_cost = AACC(tgt);
437
438 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
439 {
440 auto arc = it.get_current_arc_ne();
442 continue;
443
444 auto t = it.get_tgt_node();
446 continue;
447
448 auto new_g = this->checked_add(g_cost, AARC_DIST(arc));
449 auto h = heuristic(t, end);
450 APOT(arc) = this->checked_add(new_g, h);
451 heap.put_arc(arc, t);
452 }
453 }
454
455 this->template uninit<Destroy_Node, Destroy_Arc>();
456 painted = true;
457
458 return found;
459 }
460
474 find_path(const GT& g,
475 typename GT::Node* start,
476 typename GT::Node* end,
477 Path<GT>& path)
478 {
479 path.empty();
480 if (paint_partial_path(g, start, end))
481 return this->get_min_path(end, path);
482
483 return std::numeric_limits<typename Distance::Distance_Type>::max();
484 }
485
486 // =========================================================================
487 // Backward Compatibility Aliases
488 // =========================================================================
489
491 typename GT::Node*
492 compute_path(const GT& g, typename GT::Node* start,
493 typename GT::Node* end, GT& tree)
494 {
495 return compute_partial_path(g, start, end, tree);
496 }
497
499 bool paint_path(const GT& g, typename GT::Node* start, typename GT::Node* end)
500 {
501 return paint_partial_path(g, start, end);
502 }
503
504 // =========================================================================
505 // Methods WITHOUT Heuristic (Dijkstra-compatible)
506 // =========================================================================
507
524 typename GT::Node*
525 compute_min_paths_tree(const GT& g, typename GT::Node* start, GT& tree)
526 {
527 ah_domain_error_if(start == nullptr) << "start node cannot be null";
528 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
529
531
532 clear_graph(tree);
533
534 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
535 AACC(start) = 0;
536 auto ret = ATREENODE(start) = tree.insert_node(start->get_info());
537 NODE_COOKIE(ATREENODE(start)) = start;
538
539 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
540 {
541 auto arc = it.get_current_arc_ne();
542 APOT(arc) = AARC_DIST(arc); // No heuristic: pot = g only
543 heap.put_arc(arc, it.get_tgt_node());
544 }
545
546 const auto& n = g.get_num_nodes();
547
548 while (tree.get_num_nodes() < n and not heap.is_empty())
549 {
550 auto garc = heap.get_min_arc();
552 continue;
553
554 auto gsrc = g.get_src_node(garc);
555 auto gtgt = g.get_tgt_node(garc);
556
559 continue;
560
561 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
562
564 std::swap(gsrc, gtgt);
565
566 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
567
568 auto ttgt = tree.insert_node(gtgt->get_info());
570 auto tsrc = ATREENODE(gsrc);
571
572 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
573 ATREEARC(garc) = tarc;
574
576 const auto& acc = AACC(gtgt);
577
578 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
579 {
580 auto arc = it.get_current_arc_ne();
582 continue;
583
584 auto tgt = it.get_tgt_node();
586 continue;
587
588 APOT(arc) = this->checked_add(acc, AARC_DIST(arc));
589 heap.put_arc(arc, tgt);
590 }
591 }
592
594
595 return ret;
596 }
597
610 typename GT::Node* start,
611 typename GT::Node* end,
612 GT& tree)
613 {
614 ah_domain_error_if(start == nullptr) << "start node cannot be null";
615 ah_domain_error_if(end == nullptr) << "end node cannot be null";
616 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
617
619 clear_graph(tree);
620
621 // Handle trivial case: start == end
622 if (start == end)
623 {
624 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
625 AACC(start) = 0;
626 auto tree_node = tree.insert_node(start->get_info());
627 ATREENODE(start) = tree_node;
628 NODE_COOKIE(tree_node) = start;
630 return;
631 }
632
633 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
634 AACC(start) = 0;
635 ATREENODE(start) = tree.insert_node(start->get_info());
636 NODE_COOKIE(ATREENODE(start)) = start;
637
638 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
639 {
640 auto arc = it.get_current_arc_ne();
641 APOT(arc) = AARC_DIST(arc);
642 heap.put_arc(arc, it.get_tgt_node());
643 }
644
645 const auto& n = g.get_num_nodes();
646
647 while (tree.get_num_nodes() < n and not heap.is_empty())
648 {
649 auto garc = heap.get_min_arc();
651 continue;
652
653 auto gsrc = g.get_src_node(garc);
654 auto gtgt = g.get_tgt_node(garc);
655
658 continue;
659
660 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
661
663 std::swap(gsrc, gtgt);
664
665 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
666
667 auto ttgt = tree.insert_node(gtgt->get_info());
669
670 auto tarc = tree.insert_arc(ATREENODE(gsrc), ATREENODE(gtgt), garc->get_info());
671 ATREEARC(garc) = tarc;
672
674
675 if (gtgt == end)
676 break;
677 const auto& acc = AACC(gtgt);
678
679 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
680 {
681 auto arc = it.get_current_arc_ne();
683 continue;
684
685 auto tgt = it.get_tgt_node();
687 continue;
688
689 APOT(arc) = this->checked_add(acc, AARC_DIST(arc));
690 heap.put_arc(arc, tgt);
691 }
692 }
693
695 }
696
709 typename GT::Node* start,
710 typename GT::Node* end)
711 {
712 ah_domain_error_if(start == nullptr) << "start node cannot be null";
713 ah_domain_error_if(end == nullptr) << "end node cannot be null";
714 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
715
716 this->template init<Initialize_Node, Initialize_Arc>(g, start);
717
718 // Handle trivial case: start == end
719 if (start == end)
720 {
721 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
722 AACC(start) = 0;
723 this->template uninit<Destroy_Node, Destroy_Arc>();
724 painted = true;
725 return true;
726 }
727
728 bool ret_val = false;
729
730 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
731 AACC(start) = 0;
732
733 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
734 {
735 auto arc = it.get_current_arc_ne();
736 APOT(arc) = AARC_DIST(arc);
737 heap.put_arc(arc, it.get_tgt_node());
738 }
739
740 const auto& n = g.get_num_nodes();
741 size_t tn = 1;
742
743 while (tn < n and not heap.is_empty())
744 {
745 auto garc = heap.get_min_arc();
747 continue;
748
749 auto src = g.get_src_node(garc);
750 auto tgt = g.get_tgt_node(garc);
751
754 continue;
755
756 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
757
759 std::swap(src, tgt);
760
761 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
762 APARENT(tgt) = src;
763
764 ++tn;
765
766 if (tgt == end)
767 {
768 ret_val = true;
769 break;
770 }
771
772 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
773 const auto& acc = AACC(tgt);
774
775 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
776 {
777 auto a = it.get_current_arc_ne();
779 continue;
780
781 auto t = it.get_tgt_node();
783 continue;
784
785 APOT(a) = this->checked_add(acc, AARC_DIST(a));
786 heap.put_arc(a, t);
787 }
788 }
789
790 this->template uninit<Destroy_Node, Destroy_Arc>();
791 painted = true;
792
793 return ret_val;
794 }
795
806 void paint_min_paths_tree(const GT& g, typename GT::Node* start)
807 {
808 ah_domain_error_if(start == nullptr) << "start node cannot be null";
809 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
810
811 this->template init<Initialize_Node, Initialize_Arc>(g, start);
812
813 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
814 AACC(start) = 0;
815
816 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
817 {
818 auto arc = it.get_current_arc_ne();
819 APOT(arc) = AARC_DIST(arc);
820 heap.put_arc(arc, it.get_tgt_node());
821 }
822
823 const auto& n = g.get_num_nodes();
824 size_t tn = 1;
825
826 while (tn < n and not heap.is_empty())
827 {
828 auto garc = heap.get_min_arc();
830 continue;
831
832 auto src = g.get_src_node(garc);
833 auto tgt = g.get_tgt_node(garc);
834
837 continue;
838
839 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
840
842 std::swap(src, tgt);
843
844 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
845 APARENT(tgt) = src;
846
847 ++tn;
848
849 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
850 const auto& acc = AACC(tgt);
851
852 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
853 {
854 auto a = it.get_current_arc_ne();
856 continue;
857
858 auto t = it.get_tgt_node();
860 continue;
861
862 APOT(a) = this->checked_add(acc, AARC_DIST(a));
863 heap.put_arc(a, t);
864 }
865 }
866
867 this->template uninit<Destroy_Node, Destroy_Arc>();
868 painted = true;
869 }
870
884 typename GT::Node* start,
885 typename GT::Node* end,
887 {
888 min_path.empty();
889 if (paint_partial_min_paths_tree(g, start, end))
890 return this->get_min_path(end, min_path);
891
892 return std::numeric_limits<typename Distance::Distance_Type>::max();
893 }
894
895 // =========================================================================
896 // Operator Interfaces
897 // =========================================================================
898
907 void operator()(const GT& g, typename GT::Node* s, GT& tree)
908 {
909 compute_min_paths_tree(g, s, tree);
910 }
911
921 typename GT::Node* s,
922 typename GT::Node* e,
923 Path<GT>& path)
924 {
925 return find_path(g, s, e, path);
926 }
927
928#undef ANassert
929#undef APARENT
930#undef ATREENODE
931#undef AACC
932#undef AHEAPNODE
933#undef AAassert
934#undef AARC_DIST
935#undef ATREEARC
936#undef APOT
937};
938
949template <class GT, class Distance = Dft_Dist<GT>>
951{
953
954 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
955 {
956 auto& f = from->get_info();
957 auto& t = to->get_info();
958 auto dx = f.x - t.x;
959 auto dy = f.y - t.y;
960 return static_cast<Distance_Type>(std::sqrt(dx * dx + dy * dy));
961 }
962};
963
974template <class GT, class Distance = Dft_Dist<GT>>
976{
978
979 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
980 {
981 auto& f = from->get_info();
982 auto& t = to->get_info();
983 return static_cast<Distance_Type>(std::abs(f.x - t.x) + std::abs(f.y - t.y));
984 }
985};
986
987} // end namespace Aleph
988
989#endif // ASTAR_H
#define APARENT(p)
Definition AStar.H:179
#define APOT(p)
Definition AStar.H:183
#define AACC(p)
Definition AStar.H:177
#define ATREEARC(p)
Definition AStar.H:182
#define AARC_DIST(p)
Definition AStar.H:181
#define ATREENODE(p)
Definition AStar.H:176
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:522
Standard functor implementations and comparison objects.
Arc heap for graph algorithms.
long double h
Definition btreepic.C:154
A* algorithm for finding the shortest path between two nodes.
Definition AStar.H:157
Heuristic heuristic
Definition AStar.H:193
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes shortest path by painting the graph (without heuristic).
Definition AStar.H:883
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 AStar.H:525
GT::Node * compute_path(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Alias for compute_partial_path (backward compatibility).
Definition AStar.H:492
void check_heuristic_admissibility(typename GT::Node *node, typename GT::Node *goal, const typename Distance::Distance_Type &actual_cost)
Definition AStar.H:199
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the partial spanning tree from start to end (without a heuristic).
Definition AStar.H:609
AStar_Min_Path(Distance dist=Distance(), Heuristic __heuristic=Heuristic(), SA __sa=SA())
Constructor.
Definition AStar.H:225
Distance::Distance_Type operator()(const GT &g, typename GT::Node *s, typename GT::Node *e, Path< GT > &path)
Finds shortest path using A* heuristic.
Definition AStar.H:920
GT::Node * s
Start node.
GT::Node * compute_partial_path(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the shortest path from start to end using A*.
Definition AStar.H:261
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 (without heuristic).
Definition AStar.H:708
bool paint_partial_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints the shortest path from start to end on the graph using A*.
Definition AStar.H:373
bool paint_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Alias for paint_partial_path (backward compatibility).
Definition AStar.H:499
bool painted
Whether graph has been painted.
Distance::Distance_Type find_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &path)
Finds the shortest path from start to end using A*.
Definition AStar.H:474
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 (without heuristic).
Definition AStar.H:806
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Computes the spanning tree of all shortest paths.
Definition AStar.H:907
Default distance accessor for arc weights.
void empty() noexcept
empty the list
Definition htlist.H:1689
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Path on a graph.
Definition tpl_graph.H:2669
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:2819
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.
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.
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:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
#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) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3549
#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.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Common utilities and base class for shortest path algorithms.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Euclidean distance heuristic for A* in 2D grids.
Definition AStar.H:951
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:954
typename Distance::Distance_Type Distance_Type
Definition AStar.H:952
Manhattan distance heuristic for A* in grid graphs.
Definition AStar.H:976
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:979
typename Distance::Distance_Type Distance_Type
Definition AStar.H:977
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
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.
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Definition AStar.H:77
Distance_Type operator()(typename GT::Node *, typename GT::Node *) const
Definition AStar.H:80
typename Distance::Distance_Type Distance_Type
Definition AStar.H:78
Distance accessor.
Array-based graph implementation.
Path finding algorithms in graphs.