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 <ah-graph-concepts.H>
54
55# include <cmath>
56# include <iostream>
57# include <ahFunction.H>
58# include <ah-errors.H>
59# include <archeap.H>
60# include <tpl_find_path.H>
61# include <tpl_agraph.H>
62# include <shortest_path_common.H>
63
64namespace Aleph
65{
66
77template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
79{
81
82 Distance_Type operator()(typename GT::Node*, typename GT::Node*) const
83 {
84 return Distance_Type(0);
85 }
86};
87
151template <AlephGraph GT,
154 template <typename, class> class Itor = Node_Arc_Iterator,
156 template <class, class, class> class HeapT = ArcHeap>
158 : public Shortest_Path_Base<GT, Distance, Itor, SA, HeapT>
159{
161
162 // Import types from base class
175
176 // Local cookie access macros
177#define ANassert(p) (static_cast<Node_Info*>(NODE_COOKIE(p)))
178#define ATREENODE(p) (static_cast<Tree_Node_Info*>(NODE_COOKIE(p))->tree_node)
179#define AACC(p) (ANassert(p)->dist)
180#define AHEAPNODE(p) (ANassert(p)->heap_node)
181#define APARENT(p) (ANassert(p)->ret_node)
182#define AAassert(p) (static_cast<Arc_Info*>(ARC_COOKIE(p)))
183#define AARC_DIST(p) (Distance()(p))
184#define ATREEARC(p) (static_cast<Tree_Arc_Info*>(ARC_COOKIE(p))->tree_arc)
185#define APOT(p) (AAassert(p)->pot)
186
187 // Import member variables from base
188 using Base::sa;
189 using Base::heap;
190 using Base::painted;
191 using Base::ptr_g;
192 using Base::s;
193
194 // A* specific: heuristic functor
196
197#ifndef NDEBUG
198 // Debug mode: track heuristic validation
200
202 typename GT::Node* goal,
203 const typename Distance::Distance_Type& actual_cost)
204 {
206 return;
207
208 auto h_estimate = heuristic(node, goal);
210 {
211 std::cerr << "WARNING: Inadmissible heuristic detected!\n";
212 std::cerr << " Heuristic estimate h(" << node << ") = " << h_estimate << "\n";
213 std::cerr << " Actual remaining cost = " << actual_cost << "\n";
214 std::cerr << " Heuristic overestimates by " << (h_estimate - actual_cost) << "\n";
215 std::cerr << " A* may return suboptimal paths.\n";
216 }
217 }
218#endif
219
220public:
229 SA __sa = SA())
230 : Base(dist, __sa), heuristic(__heuristic)
231 {
232 // empty
233 }
234
235 // Import public methods from base
237 using Base::is_painted;
239 using Base::get_graph;
240 using Base::get_distance;
241 using Base::get_min_path;
243
244 // =========================================================================
245 // Methods WITH Heuristic (A* specific)
246 // =========================================================================
247
262 typename GT::Node*
264 typename GT::Node* start,
265 typename GT::Node* end,
266 GT& tree)
267 {
268 ah_domain_error_if(start == nullptr) << "start node cannot be null";
269 ah_domain_error_if(end == nullptr) << "end node cannot be null";
270 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
271
273 clear_graph(tree);
274
275 // Handle trivial case: start == end
276 if (start == end)
277 {
278 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
279 AACC(start) = 0;
280 auto tree_node = tree.insert_node(start->get_info());
281 ATREENODE(start) = tree_node;
282 NODE_COOKIE(tree_node) = start;
284 return tree_node;
285 }
286
287 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
288 AACC(start) = 0;
289 ATREENODE(start) = tree.insert_node(start->get_info());
290 NODE_COOKIE(ATREENODE(start)) = start;
291
292 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
293 {
294 auto arc = it.get_current_arc_ne();
295 auto tgt = it.get_tgt_node();
296 auto g_cost = AARC_DIST(arc);
297 auto h_cost = heuristic(tgt, end);
298 APOT(arc) = this->checked_add(g_cost, h_cost);
299 heap.put_arc(arc, tgt);
300 }
301
302 typename GT::Node* result = nullptr;
303
304 while (not heap.is_empty())
305 {
306 auto garc = heap.get_min_arc();
308 continue;
309
310 auto gsrc = g.get_src_node(garc);
311 auto gtgt = g.get_tgt_node(garc);
312
315 continue;
316
317 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
318
320 std::swap(gsrc, gtgt);
321
322 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
323
324 auto ttgt = tree.insert_node(gtgt->get_info());
326 auto tsrc = ATREENODE(gsrc);
327
328 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
329 ATREEARC(garc) = tarc;
330
332
333 if (gtgt == end)
334 {
335 result = ttgt;
336 break;
337 }
338
339 const auto& g_cost = AACC(gtgt);
340
341 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
342 {
343 auto arc = it.get_current_arc_ne();
345 continue;
346
347 auto tgt = it.get_tgt_node();
349 continue;
350
351 auto new_g = this->checked_add(g_cost, AARC_DIST(arc));
352 auto h = heuristic(tgt, end);
353 APOT(arc) = this->checked_add(new_g, h);
354 heap.put_arc(arc, tgt);
355 }
356 }
357
359
360 return result;
361 }
362
375 bool paint_partial_path(const GT& g,
376 typename GT::Node* start,
377 typename GT::Node* end)
378 {
379 ah_domain_error_if(start == nullptr) << "start node cannot be null";
380 ah_domain_error_if(end == nullptr) << "end node cannot be null";
381 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
382
383 this->template init<Initialize_Node, Initialize_Arc>(g, start);
384
385 // Handle trivial case: start == end
386 if (start == end)
387 {
388 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
389 AACC(start) = 0;
390 this->template uninit<Destroy_Node, Destroy_Arc>();
391 painted = true;
392 return true;
393 }
394
395 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
396 AACC(start) = 0;
397
398 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
399 {
400 auto arc = it.get_current_arc_ne();
401 auto tgt = it.get_tgt_node();
402 auto g_cost = AARC_DIST(arc);
403 auto h_cost = heuristic(tgt, end);
404 APOT(arc) = this->checked_add(g_cost, h_cost);
405 heap.put_arc(arc, tgt);
406 }
407
408 bool found = false;
409
410 while (not heap.is_empty())
411 {
412 auto garc = heap.get_min_arc();
414 continue;
415
416 auto src = g.get_src_node(garc);
417 auto tgt = g.get_tgt_node(garc);
418
421 continue;
422
423 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
424
426 std::swap(src, tgt);
427
428 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
429 APARENT(tgt) = src;
430
431 if (tgt == end)
432 {
433 found = true;
434 break;
435 }
436
437 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
438 const auto& g_cost = AACC(tgt);
439
440 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
441 {
442 auto arc = it.get_current_arc_ne();
444 continue;
445
446 auto t = it.get_tgt_node();
448 continue;
449
450 auto new_g = this->checked_add(g_cost, AARC_DIST(arc));
451 auto h = heuristic(t, end);
452 APOT(arc) = this->checked_add(new_g, h);
453 heap.put_arc(arc, t);
454 }
455 }
456
457 this->template uninit<Destroy_Node, Destroy_Arc>();
458 painted = true;
459
460 return found;
461 }
462
476 find_path(const GT& g,
477 typename GT::Node* start,
478 typename GT::Node* end,
479 Path<GT>& path)
480 {
481 path.empty();
482 if (paint_partial_path(g, start, end))
483 return this->get_min_path(end, path);
484
485 return std::numeric_limits<typename Distance::Distance_Type>::max();
486 }
487
488 // =========================================================================
489 // Backward Compatibility Aliases
490 // =========================================================================
491
493 typename GT::Node*
494 compute_path(const GT& g, typename GT::Node* start,
495 typename GT::Node* end, GT& tree)
496 {
497 return compute_partial_path(g, start, end, tree);
498 }
499
501 bool paint_path(const GT& g, typename GT::Node* start, typename GT::Node* end)
502 {
503 return paint_partial_path(g, start, end);
504 }
505
506 // =========================================================================
507 // Methods WITHOUT Heuristic (Dijkstra-compatible)
508 // =========================================================================
509
526 typename GT::Node*
527 compute_min_paths_tree(const GT& g, typename GT::Node* start, GT& tree)
528 {
529 ah_domain_error_if(start == nullptr) << "start node cannot be null";
530 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
531
533
534 clear_graph(tree);
535
536 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
537 AACC(start) = 0;
538 auto ret = ATREENODE(start) = tree.insert_node(start->get_info());
539 NODE_COOKIE(ATREENODE(start)) = start;
540
541 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
542 {
543 auto arc = it.get_current_arc_ne();
544 APOT(arc) = AARC_DIST(arc); // No heuristic: pot = g only
545 heap.put_arc(arc, it.get_tgt_node());
546 }
547
548 const auto& n = g.get_num_nodes();
549
550 while (tree.get_num_nodes() < n and not heap.is_empty())
551 {
552 auto garc = heap.get_min_arc();
554 continue;
555
556 auto gsrc = g.get_src_node(garc);
557 auto gtgt = g.get_tgt_node(garc);
558
561 continue;
562
563 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
564
566 std::swap(gsrc, gtgt);
567
568 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
569
570 auto ttgt = tree.insert_node(gtgt->get_info());
572 auto tsrc = ATREENODE(gsrc);
573
574 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
575 ATREEARC(garc) = tarc;
576
578 const auto& acc = AACC(gtgt);
579
580 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
581 {
582 auto arc = it.get_current_arc_ne();
584 continue;
585
586 auto tgt = it.get_tgt_node();
588 continue;
589
590 APOT(arc) = this->checked_add(acc, AARC_DIST(arc));
591 heap.put_arc(arc, tgt);
592 }
593 }
594
596
597 return ret;
598 }
599
612 typename GT::Node* start,
613 typename GT::Node* end,
614 GT& tree)
615 {
616 ah_domain_error_if(start == nullptr) << "start node cannot be null";
617 ah_domain_error_if(end == nullptr) << "end node cannot be null";
618 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
619
621 clear_graph(tree);
622
623 // Handle trivial case: start == end
624 if (start == end)
625 {
626 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
627 AACC(start) = 0;
628 auto tree_node = tree.insert_node(start->get_info());
629 ATREENODE(start) = tree_node;
630 NODE_COOKIE(tree_node) = start;
632 return;
633 }
634
635 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
636 AACC(start) = 0;
637 ATREENODE(start) = tree.insert_node(start->get_info());
638 NODE_COOKIE(ATREENODE(start)) = start;
639
640 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
641 {
642 auto arc = it.get_current_arc_ne();
643 APOT(arc) = AARC_DIST(arc);
644 heap.put_arc(arc, it.get_tgt_node());
645 }
646
647 const auto& n = g.get_num_nodes();
648
649 while (tree.get_num_nodes() < n and not heap.is_empty())
650 {
651 auto garc = heap.get_min_arc();
653 continue;
654
655 auto gsrc = g.get_src_node(garc);
656 auto gtgt = g.get_tgt_node(garc);
657
660 continue;
661
662 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
663
665 std::swap(gsrc, gtgt);
666
667 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
668
669 auto ttgt = tree.insert_node(gtgt->get_info());
671
672 auto tarc = tree.insert_arc(ATREENODE(gsrc), ATREENODE(gtgt), garc->get_info());
673 ATREEARC(garc) = tarc;
674
676
677 if (gtgt == end)
678 break;
679 const auto& acc = AACC(gtgt);
680
681 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
682 {
683 auto arc = it.get_current_arc_ne();
685 continue;
686
687 auto tgt = it.get_tgt_node();
689 continue;
690
691 APOT(arc) = this->checked_add(acc, AARC_DIST(arc));
692 heap.put_arc(arc, tgt);
693 }
694 }
695
697 }
698
711 typename GT::Node* start,
712 typename GT::Node* end)
713 {
714 ah_domain_error_if(start == nullptr) << "start node cannot be null";
715 ah_domain_error_if(end == nullptr) << "end node cannot be null";
716 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
717
718 this->template init<Initialize_Node, Initialize_Arc>(g, start);
719
720 // Handle trivial case: start == end
721 if (start == end)
722 {
723 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
724 AACC(start) = 0;
725 this->template uninit<Destroy_Node, Destroy_Arc>();
726 painted = true;
727 return true;
728 }
729
730 bool ret_val = false;
731
732 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
733 AACC(start) = 0;
734
735 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
736 {
737 auto arc = it.get_current_arc_ne();
738 APOT(arc) = AARC_DIST(arc);
739 heap.put_arc(arc, it.get_tgt_node());
740 }
741
742 const auto& n = g.get_num_nodes();
743 size_t tn = 1;
744
745 while (tn < n and not heap.is_empty())
746 {
747 auto garc = heap.get_min_arc();
749 continue;
750
751 auto src = g.get_src_node(garc);
752 auto tgt = g.get_tgt_node(garc);
753
756 continue;
757
758 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
759
761 std::swap(src, tgt);
762
763 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
764 APARENT(tgt) = src;
765
766 ++tn;
767
768 if (tgt == end)
769 {
770 ret_val = true;
771 break;
772 }
773
774 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
775 const auto& acc = AACC(tgt);
776
777 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
778 {
779 auto a = it.get_current_arc_ne();
781 continue;
782
783 auto t = it.get_tgt_node();
785 continue;
786
787 APOT(a) = this->checked_add(acc, AARC_DIST(a));
788 heap.put_arc(a, t);
789 }
790 }
791
792 this->template uninit<Destroy_Node, Destroy_Arc>();
793 painted = true;
794
795 return ret_val;
796 }
797
808 void paint_min_paths_tree(const GT& g, typename GT::Node* start)
809 {
810 ah_domain_error_if(start == nullptr) << "start node cannot be null";
811 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
812
813 this->template init<Initialize_Node, Initialize_Arc>(g, start);
814
815 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
816 AACC(start) = 0;
817
818 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
819 {
820 auto arc = it.get_current_arc_ne();
821 APOT(arc) = AARC_DIST(arc);
822 heap.put_arc(arc, it.get_tgt_node());
823 }
824
825 const auto& n = g.get_num_nodes();
826 size_t tn = 1;
827
828 while (tn < n and not heap.is_empty())
829 {
830 auto garc = heap.get_min_arc();
832 continue;
833
834 auto src = g.get_src_node(garc);
835 auto tgt = g.get_tgt_node(garc);
836
839 continue;
840
841 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
842
844 std::swap(src, tgt);
845
846 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
847 APARENT(tgt) = src;
848
849 ++tn;
850
851 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
852 const auto& acc = AACC(tgt);
853
854 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
855 {
856 auto a = it.get_current_arc_ne();
858 continue;
859
860 auto t = it.get_tgt_node();
862 continue;
863
864 APOT(a) = this->checked_add(acc, AARC_DIST(a));
865 heap.put_arc(a, t);
866 }
867 }
868
869 this->template uninit<Destroy_Node, Destroy_Arc>();
870 painted = true;
871 }
872
886 typename GT::Node* start,
887 typename GT::Node* end,
889 {
890 min_path.empty();
891 if (paint_partial_min_paths_tree(g, start, end))
892 return this->get_min_path(end, min_path);
893
894 return std::numeric_limits<typename Distance::Distance_Type>::max();
895 }
896
897 // =========================================================================
898 // Operator Interfaces
899 // =========================================================================
900
909 void operator()(const GT& g, typename GT::Node* s, GT& tree)
910 {
911 compute_min_paths_tree(g, s, tree);
912 }
913
923 typename GT::Node* s,
924 typename GT::Node* e,
925 Path<GT>& path)
926 {
927 return find_path(g, s, e, path);
928 }
929
930#undef ANassert
931#undef APARENT
932#undef ATREENODE
933#undef AACC
934#undef AHEAPNODE
935#undef AAassert
936#undef AARC_DIST
937#undef ATREEARC
938#undef APOT
939};
940
951template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
953{
955
956 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
957 {
958 auto& f = from->get_info();
959 auto& t = to->get_info();
960 auto dx = f.x - t.x;
961 auto dy = f.y - t.y;
962 return static_cast<Distance_Type>(std::sqrt(dx * dx + dy * dy));
963 }
964};
965
976template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
978{
980
981 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
982 {
983 auto& f = from->get_info();
984 auto& t = to->get_info();
985 return static_cast<Distance_Type>(std::abs(f.x - t.x) + std::abs(f.y - t.y));
986 }
987};
988
989} // end namespace Aleph
990
991#endif // ASTAR_H
#define APARENT(p)
Definition AStar.H:181
#define APOT(p)
Definition AStar.H:185
#define AACC(p)
Definition AStar.H:179
#define ATREEARC(p)
Definition AStar.H:184
#define AARC_DIST(p)
Definition AStar.H:183
#define ATREENODE(p)
Definition AStar.H:178
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:527
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
long double h
Definition btreepic.C:154
A* algorithm for finding the shortest path between two nodes.
Definition AStar.H:159
Heuristic heuristic
Definition AStar.H:195
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:885
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:527
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:494
void check_heuristic_admissibility(typename GT::Node *node, typename GT::Node *goal, const typename Distance::Distance_Type &actual_cost)
Definition AStar.H:201
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:611
AStar_Min_Path(Distance dist=Distance(), Heuristic __heuristic=Heuristic(), SA __sa=SA())
Constructor.
Definition AStar.H:227
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:922
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:263
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:710
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:375
bool paint_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Alias for paint_partial_path (backward compatibility).
Definition AStar.H:501
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:476
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:808
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Computes the spanning tree of all shortest paths.
Definition AStar.H:909
Default distance accessor for arc weights.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
Path on a graph.
Definition tpl_graph.H:2772
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:2922
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:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
#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:3659
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.
@ Spanning_Tree
Definition aleph-graph.H:79
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:1001
Euclidean distance heuristic for A* in 2D grids.
Definition AStar.H:953
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:956
typename Distance::Distance_Type Distance_Type
Definition AStar.H:954
Manhattan distance heuristic for A* in grid graphs.
Definition AStar.H:978
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:981
typename Distance::Distance_Type Distance_Type
Definition AStar.H:979
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
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:79
Distance_Type operator()(typename GT::Node *, typename GT::Node *) const
Definition AStar.H:82
typename Distance::Distance_Type Distance_Type
Definition AStar.H:80
Distance accessor.
Array-based graph implementation.
Path finding algorithms in graphs.