Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
K_Shortest_Paths.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
65# ifndef K_SHORTEST_PATHS_H
66# define K_SHORTEST_PATHS_H
67
68# include <ah-graph-concepts.H>
69
70# include <cmath>
71# include <cstddef>
72# include <limits>
73# include <type_traits>
74# include <utility>
75
76# include <Dijkstra.H>
77# include <ah-errors.H>
78# include <cookie_guard.H>
79# include <shortest_path_common.H>
80# include <htlist.H>
81# include <tpl_array.H>
82# include <tpl_dynListQueue.H>
83# include <tpl_dynMapTree.H>
84# include <tpl_dynSetTree.H>
85
86namespace Aleph
87{
94 template <AlephGraph GT, typename Cost_Type>
96 {
97 Cost_Type total_cost = Cost_Type{0};
99 };
100
101
102 namespace k_shortest_paths_detail
103 {
104 template <AlephGraph GT, typename Cost_Type>
111
112
113 template <AlephGraph GT, typename Cost_Type>
115 {
116 using Node = typename GT::Node;
117 using Arc = typename GT::Arc;
118
119 DynMapTree<Node *, Cost_Type> dist_to_target; // of the nodes that reach it within max()
122 DynSetTree<Node *> beyond_max; // the nodes that reach it only beyond max()
123 };
124
125
128 template <AlephGraph GT, ArcDistance<GT> Distance,
129 template <typename, class> class Itor, ArcFilter<GT> SA>
130 class Painting_Within_Max : public Dijkstra_Min_Paths<GT, Distance, Itor, SA>
131 {
132 public:
136 {
137 this->leave_out_beyond_max = true;
138 }
139
142 [[nodiscard]] bool went_beyond_max() const noexcept { return this->beyond_max; }
143 };
144
145
149 template <template <typename, class> class Itor, AlephGraph GT, ArcFilter<GT> SA,
150 class Op>
151 void for_each_left_out(typename GT::Node * start, SA sa, Op op)
152 {
153 using Node = typename GT::Node;
156 seen.insert(start);
157 queue.put(start);
158 while (not queue.is_empty())
159 {
160 Node * p = queue.get();
162 op(p);
163 for (Itor<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
164 if (Node * q = it.get_tgt_node(); seen.insert(q) != nullptr)
165 queue.put(q);
166 }
167 }
168
169
170 template <AlephGraph GT, ArcFilter<GT> SA>
172 {
173 using Node = typename GT::Node;
174 using Arc = typename GT::Arc;
175
176 const GT *g = nullptr;
180
181 bool operator()(Arc *arc) const
182 {
183 if (not base_sa(arc))
184 return false;
185
186 if (forbidden_arcs != nullptr and forbidden_arcs->contains(arc))
187 return false;
188
189 if (forbidden_nodes != nullptr)
190 {
191 Node *src = g->get_src_node(arc);
192 Node *tgt = g->get_tgt_node(arc);
194 return false;
195 }
196
197 return true;
198 }
199 };
200
201
202
203
204
205 template <AlephGraph GT, ArcDistance<GT> Distance>
207 {
208 using Arc = typename GT::Arc;
210
219 // Context for reverse-graph Dijkstra in build_suffix_index_digraph().
220 // This is intentionally thread-local, but not re-entrant on the same
221 // thread: nested calls can overwrite map_ptr/base_ptr. The
222 // Reset_Reverse_Distance guard below restores this state.
223 inline static thread_local const DynMapTree<Arc *, Arc *> * map_ptr = nullptr;
224 inline static thread_local const Distance * base_ptr = nullptr;
225
226 Distance_Type operator()(Arc * rev_arc) const
227 {
228 ah_runtime_error_if(map_ptr == nullptr)
229 << "Reverse_Arc_Distance: reverse arc map is null";
230 ah_runtime_error_if(base_ptr == nullptr)
231 << "Reverse_Arc_Distance: base distance functor is null";
232
233 auto * pair = map_ptr->search(rev_arc);
234 auto * orig_arc = pair == nullptr ? nullptr : pair->second;
235 ah_runtime_error_if(orig_arc == nullptr)
236 << "Reverse_Arc_Distance: missing reverse-to-original arc map";
237
238 return (*base_ptr)(orig_arc);
239 }
240 };
241
242
243 template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA>
244 requires std::is_arithmetic_v<typename Distance::Distance_Type>
246 Distance distance,
247 SA sa)
248 {
249 using Arc = typename GT::Arc;
250 using Cost_Type = typename Distance::Distance_Type;
251
252 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
253 {
254 Arc *arc = it.get_curr_ne();
255 const Cost_Type w = distance(arc);
256
257 if constexpr (std::is_floating_point_v<Cost_Type>)
258 ah_domain_error_if(not std::isfinite(w))
259 << "K shortest paths require finite arc weights";
260
261 ah_domain_error_if(w < Cost_Type{0})
262 << "K shortest paths require non-negative arc weights";
263 }
264 }
265
266
267 template <AlephGraph GT, typename Cost_Type>
273
274
275 template <AlephGraph GT, typename Cost_Type>
278 {
280 snapshot.nodes.reserve(path.nodes.size());
281 snapshot.arcs.reserve(path.arcs.size());
282
283 for (auto it = path.nodes.get_it(); it.has_curr(); it.next_ne())
284 snapshot.nodes.append(it.get_curr_ne());
285
286 for (auto it = path.arcs.get_it(); it.has_curr(); it.next_ne())
287 snapshot.arcs.append(it.get_curr_ne());
288
289 return snapshot;
290 }
291
292
293 template <AlephGraph GT, typename Cost_Type>
294 Path_State<GT, Cost_Type>
295 path_to_state(const Path<GT> & path, const Cost_Type total_cost)
296 {
298 state.total_cost = total_cost;
299
300 for (typename Path<GT>::Iterator it(path); it.has_current_node(); it.next_ne())
301 {
302 state.nodes.append(it.get_current_node_ne());
303 if (it.has_current_arc())
304 state.arcs.append(it.get_current_arc_ne());
305 }
306
307 return state;
308 }
309
310
311 template <AlephGraph GT, typename Cost_Type>
313 {
314 Path<GT> path(g);
315 if (state.nodes.is_empty())
316 return path;
317
318 path.set_graph(g, state.nodes.get_first());
319
320 if (g.is_digraph())
321 {
322 for (auto it = state.arcs.get_it(); it.has_curr(); it.next_ne())
323 path.append_directed(it.get_curr_ne());
324 }
325 else
326 {
327 for (auto it = state.arcs.get_it(); it.has_curr(); it.next_ne())
328 path.append(it.get_curr_ne());
329 }
330
331 return path;
332 }
333
334
335 template <AlephGraph GT, typename Cost_Type>
338 {
339 if (a.nodes.size() != b.nodes.size())
340 return false;
341 if (a.arcs.size() != b.arcs.size())
342 return false;
343
344 auto an = a.nodes.get_it();
345 auto bn = b.nodes.get_it();
346 while (an.has_curr())
347 {
348 if (an.get_curr_ne() != bn.get_curr_ne())
349 return false;
350 an.next_ne();
351 bn.next_ne();
352 }
353
354 auto aa = a.arcs.get_it();
355 auto ba = b.arcs.get_it();
356 while (aa.has_curr())
357 {
358 if (aa.get_curr_ne() != ba.get_curr_ne())
359 return false;
360 aa.next_ne();
361 ba.next_ne();
362 }
363
364 return true;
365 }
366
367
368 template <AlephGraph GT, typename Cost_Type>
371 const size_t spur_index)
372 {
373 if (a.nodes.size() <= spur_index or b.nodes.size() <= spur_index)
374 return false;
375 if (a.arcs.size() <= spur_index or b.arcs.size() <= spur_index)
376 return false;
377
378 for (size_t i = 0; i <= spur_index; ++i)
379 if (a.nodes[i] != b.nodes[i])
380 return false;
381
382 for (size_t i = 0; i < spur_index; ++i)
383 if (a.arcs[i] != b.arcs[i])
384 return false;
385
386 return true;
387 }
388
389
390 template <AlephGraph GT, typename Cost_Type>
392 const Path_State<GT, Cost_Type> & candidate)
393 {
394 for (const auto & p: container)
395 if (same_path_sequence(p, candidate))
396 return true;
397 return false;
398 }
399
400
401 template <AlephGraph GT, typename Cost_Type>
402 typename GT::Node *
404 typename GT::Node * node)
405 {
406 auto * p = index.tree_next.search(node);
407 return p == nullptr ? nullptr : p->second;
408 }
409
410
411 template <AlephGraph GT, typename Cost_Type>
412 typename GT::Arc *
414 typename GT::Node * node)
415 {
416 auto * p = index.tree_arc.search(node);
417 return p == nullptr ? nullptr : p->second;
418 }
419
420
421 template <template <class, class> class Itor,
423 Suffix_Index<GT, typename Distance::Distance_Type>
425 typename GT::Node * target,
426 Distance distance,
427 SA sa)
428 {
429 using Node = typename GT::Node;
430 using Arc = typename GT::Arc;
431 using Cost_Type = typename Distance::Distance_Type;
432
434
435 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
436 {
437 Node * node = it.get_curr_ne();
438 index.tree_next.insert(node, nullptr);
439 index.tree_arc.insert(node, nullptr);
440 }
441
442 auto & mg = const_cast<GT &>(g);
443 Cookie_Saver<GT> cookie_saver(mg, true, true);
444 mg.reset_nodes();
445 mg.reset_arcs();
446
447 Painting_Within_Max<GT, Distance, Itor, SA> reverse_dijkstra(distance, sa);
448 reverse_dijkstra.paint_min_paths_tree(g, target);
449 if (reverse_dijkstra.went_beyond_max())
450 for_each_left_out<Itor, GT, SA>(target, sa, [&index] (Node * p)
451 {
452 index.beyond_max.insert(p);
453 });
454
455 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
456 {
457 Node * node = it.get_curr_ne();
459 continue;
460
461 index.dist_to_target.insert(node, reverse_dijkstra.get_distance(node));
462
463 Node * parent = static_cast<Node *>(NODE_COOKIE(node));
464 index.tree_next.find(node) = parent;
465
466 if (parent == nullptr)
467 continue;
468
469 Arc * selected = nullptr;
470 for (Node_Arc_Iterator<GT, SA> ait(node, sa); ait.has_curr(); ait.next_ne())
471 {
472 Arc * arc = ait.get_current_arc_ne();
473 if (ait.get_tgt_node() == parent and IS_ARC_VISITED(arc, Aleph::Spanning_Tree))
474 {
475 selected = arc;
476 break;
477 }
478 }
479
480 if (selected == nullptr)
481 {
482 const Cost_Type dist_node = index.dist_to_target.find(node);
483 const Cost_Type dist_parent = index.dist_to_target.find(parent);
484 bool has_best = false;
485 Cost_Type best_diff{};
486 for (Node_Arc_Iterator<GT, SA> ait(node, sa); ait.has_curr(); ait.next_ne())
487 {
488 Arc * arc = ait.get_current_arc_ne();
489 if (ait.get_tgt_node() != parent)
490 continue;
491
492 Cost_Type cand{};
493 if (not shortest_path_detail::add_if_fits(distance(arc), dist_parent, cand))
494 continue; // not the arc of the tree, whose sum is dist_node
495 const Cost_Type diff =
496 cand >= dist_node ? cand - dist_node : dist_node - cand;
497 if (not has_best or diff < best_diff)
498 {
499 best_diff = diff;
500 selected = arc;
501 has_best = true;
502 }
503 }
504 }
505
506 index.tree_arc.find(node) = selected;
507 }
508
509 return index;
510 }
511
512
513 template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA>
514 Suffix_Index<GT, typename Distance::Distance_Type>
516 typename GT::Node * target,
517 Distance distance,
518 SA sa)
519 {
520 using Node = typename GT::Node;
521 using Arc = typename GT::Arc;
522 using Cost_Type = typename Distance::Distance_Type;
523
525
526 GT reverse_graph;
527 DynMapTree<Node *, Node *> orig_to_rev;
528 DynMapTree<Node *, Node *> rev_to_orig;
529 DynMapTree<Arc *, Arc *> rev_arc_to_orig;
530
531 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
532 {
533 Node * node = it.get_curr_ne();
534 index.tree_next.insert(node, nullptr);
535 index.tree_arc.insert(node, nullptr);
536
537 Node * rev_node = reverse_graph.insert_node(node->get_info());
538 orig_to_rev.insert(node, rev_node);
539 rev_to_orig.insert(rev_node, node);
540 }
541
542 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
543 {
544 Arc * arc = it.get_curr_ne();
545 Node * src = g.get_src_node(arc);
546 Node * tgt = g.get_tgt_node(arc);
547
548 Arc * rev_arc = reverse_graph.insert_arc(
549 orig_to_rev.find(tgt),
550 orig_to_rev.find(src),
551 arc->get_info());
552 rev_arc_to_orig.insert(rev_arc, arc);
553 }
554
555 using Reverse_Distance = Reverse_Arc_Distance<GT, Distance>;
556 using Rev_Show_Arc = Dft_Show_Arc<GT>;
557 Reverse_Distance::map_ptr = &rev_arc_to_orig;
558 Reverse_Distance::base_ptr = &distance;
559 // Restores thread-local reverse-distance context on scope exit.
560 // Nested/re-entrant use on the same thread is not supported.
561 struct Reset_Reverse_Distance
562 {
563 ~Reset_Reverse_Distance()
564 {
565 Reverse_Distance::map_ptr = nullptr;
566 Reverse_Distance::base_ptr = nullptr;
567 }
568 } reset_reverse_distance;
569
570 Cookie_Saver<GT> reverse_cookie_saver(reverse_graph, true, true);
571 reverse_graph.reset_nodes();
572 reverse_graph.reset_arcs();
573
575 reverse_dijkstra{Reverse_Distance(), Rev_Show_Arc()};
576
577 Node * rev_target = orig_to_rev.find(target);
578 reverse_dijkstra.paint_min_paths_tree(reverse_graph, rev_target);
579 if (reverse_dijkstra.went_beyond_max())
580 for_each_left_out<Node_Arc_Iterator, GT, Rev_Show_Arc>(
581 rev_target, Rev_Show_Arc(), [&index, &rev_to_orig] (Node * p)
582 {
583 index.beyond_max.insert(rev_to_orig.find(p));
584 });
585
586 // Every distance first: the arc toward the next node is chosen with
587 // the distance of that node, which may come later
588 for (Node_Iterator<GT> rit(reverse_graph); rit.has_curr(); rit.next_ne())
589 if (Node * rev_node = rit.get_curr_ne();
591 index.dist_to_target.insert(rev_to_orig.find(rev_node),
592 reverse_dijkstra.get_distance(rev_node));
593
594 for (Node_Iterator<GT> rit(reverse_graph); rit.has_curr(); rit.next_ne())
595 {
596 Node * rev_node = rit.get_curr_ne();
597 Node * orig_node = rev_to_orig.find(rev_node);
598
599 if (not IS_NODE_VISITED(rev_node, Aleph::Spanning_Tree))
600 continue;
601
602 Node * rev_parent = static_cast<Node *>(NODE_COOKIE(rev_node));
603 if (rev_parent == nullptr)
604 continue;
605
606 Node * orig_parent = rev_to_orig.find(rev_parent);
607 index.tree_next.find(orig_node) = orig_parent;
608
609 Arc * selected = nullptr;
610 const Cost_Type dist_u = index.dist_to_target.find(orig_node);
611 const Cost_Type dist_v = index.dist_to_target.find(orig_parent);
612 bool has_best = false;
613 Cost_Type best_diff{};
614 for (Node_Arc_Iterator<GT, SA> ait(orig_node, sa); ait.has_curr(); ait.next_ne())
615 {
616 Arc * arc = ait.get_current_arc_ne();
617 if (ait.get_tgt_node() != orig_parent)
618 continue;
619
620 if (selected == nullptr)
621 selected = arc; // fallback: first arc to parent
622
623 if (Cost_Type cand{};
624 shortest_path_detail::add_if_fits(distance(arc), dist_v, cand))
625 {
626 const Cost_Type diff =
627 cand >= dist_u ? cand - dist_u : dist_u - cand;
628 if (not has_best or diff < best_diff)
629 {
630 best_diff = diff;
631 selected = arc;
632 has_best = true;
633 }
634 }
635 }
636
637 index.tree_arc.find(orig_node) = selected;
638 }
639
640 return index;
641 }
642
643
644 template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA>
645 Suffix_Index<GT, typename Distance::Distance_Type>
647 typename GT::Node * target,
648 Distance distance,
649 SA sa)
650 {
651 if (g.is_digraph())
652 return build_suffix_index_digraph<GT, Distance, SA>(
653 g, target, distance, sa);
654
655 return build_suffix_index_with_itor<Node_Arc_Iterator, GT, Distance, SA>(
656 g, target, distance, sa);
657 }
658
659
660 template <AlephGraph GT, typename Cost_Type>
661 bool build_suffix_state(const GT & g,
662 const Suffix_Index<GT, Cost_Type> & index,
663 typename GT::Node * start,
664 typename GT::Node * target,
665 Path_State<GT, Cost_Type> & out_suffix)
666 {
667 out_suffix = Path_State<GT, Cost_Type>();
668 out_suffix.nodes.append(start);
669 auto * dist = index.dist_to_target.search(start);
670 if (dist == nullptr) // start does not reach target, or only beyond max()
671 return false;
672 out_suffix.total_cost = dist->second;
673
674 typename GT::Node * curr = start;
675 size_t guard = 0;
676 const size_t max_steps = g.get_num_nodes() + 1;
677
678 while (curr != target)
679 {
680 if (++guard > max_steps)
681 return false;
682
683 auto * next = get_tree_next(index, curr);
684 auto * arc = get_tree_arc(index, curr);
685 if (next == nullptr or arc == nullptr)
686 return false;
687
688 out_suffix.arcs.append(arc);
689 out_suffix.nodes.append(next);
690 curr = next;
691 }
692
693 return true;
694 }
695
696
697 // The shortest path from source to target, without the forbidden
698 // nodes and arcs, in out. False if there is none, or if its cost does
699 // not fit, which sets beyond.
700 template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA>
702 typename GT::Node *source,
703 typename GT::Node *target,
704 Distance distance,
705 SA sa,
706 const DynSetTree<typename GT::Node *> *forbidden_nodes,
707 const DynSetTree<typename GT::Arc *> *forbidden_arcs,
709 bool & beyond)
710 {
711 using Cost_Type = typename Distance::Distance_Type;
712 using Filter = Filtered_Show_Arc<GT, SA>;
713
714 if (forbidden_nodes != nullptr and
715 (forbidden_nodes->contains(source) or forbidden_nodes->contains(target)))
716 return false;
717
718 if (source == target)
719 {
721 out.total_cost = Cost_Type{0};
722 out.nodes.append(source);
723 return true;
724 }
725
726 Cookie_Saver<GT> cookie_saver(const_cast<GT &>(g), true, true);
727 const_cast<GT &>(g).reset_nodes();
728 const_cast<GT &>(g).reset_arcs();
729
730 Filter filter{&g, sa, forbidden_nodes, forbidden_arcs};
732
733 Path<GT> shortest(g);
734 Cost_Type dist{};
735 try
736 {
737 dist = dijkstra(g, source, target, shortest);
738 }
739 // target is reached only beyond max(); an overflow_error of distance
740 // or of sa passes on
742 {
743 beyond = true;
744 return false;
745 }
746 if (shortest.is_empty()) // max() is a cost too
747 return false;
748
749 out = path_to_state<GT, Cost_Type>(shortest, dist);
750 return true;
751 }
752
753
754 // The cost of arcs in total, if it fits
755 template <AlephGraph GT, ArcDistance<GT> Distance>
757 Distance distance,
758 typename Distance::Distance_Type & total)
759 {
760 total = typename Distance::Distance_Type{0};
761 for (auto it = arcs.get_it(); it.has_curr(); it.next_ne())
762 if (not shortest_path_detail::add_if_fits(total, distance(it.get_curr_ne()), total))
763 return false;
764 return true;
765 }
766
767
768 template <AlephGraph GT, typename Cost_Type>
769 Path_State<GT, Cost_Type>
771 const size_t spur_index,
772 const Path_State<GT, Cost_Type> & spur_state)
773 {
775
776 for (size_t i = 0; i <= spur_index; ++i)
777 candidate.nodes.append(base_path.nodes[i]);
778
779 for (size_t i = 0; i < spur_index; ++i)
780 candidate.arcs.append(base_path.arcs[i]);
781
782 bool first_spur_node = true;
783 for (auto it = spur_state.nodes.get_it(); it.has_curr(); it.next_ne())
784 {
785 if (first_spur_node)
786 {
787 first_spur_node = false;
788 continue;
789 }
790
791 candidate.nodes.append(it.get_curr_ne());
792 }
793
794 for (auto it = spur_state.arcs.get_it(); it.has_curr(); it.next_ne())
795 candidate.arcs.append(it.get_curr_ne());
796
797 return candidate;
798 }
799
800
801 template <AlephGraph GT, typename Cost_Type>
802 Path_State<GT, Cost_Type>
804 const size_t spur_index,
805 typename GT::Arc * deviation_arc,
806 typename GT::Node * deviation_next_node,
807 const Path_State<GT, Cost_Type> & suffix_state)
808 {
810
811 for (size_t i = 0; i <= spur_index; ++i)
812 candidate.nodes.append(base_path.nodes[i]);
813
814 for (size_t i = 0; i < spur_index; ++i)
815 candidate.arcs.append(base_path.arcs[i]);
816
817 candidate.arcs.append(deviation_arc);
818 candidate.nodes.append(deviation_next_node);
819
820 bool first_suffix_node = true;
821 for (auto it = suffix_state.nodes.get_it(); it.has_curr(); it.next_ne())
822 {
823 if (first_suffix_node)
824 {
825 first_suffix_node = false;
826 continue;
827 }
828
829 candidate.nodes.append(it.get_curr_ne());
830 }
831
832 for (auto it = suffix_state.arcs.get_it(); it.has_curr(); it.next_ne())
833 candidate.arcs.append(it.get_curr_ne());
834
835 return candidate;
836 }
837
838
839 // Adds to candidates the deviations of base_path that are not
840 // there or in accepted. One whose cost does not fit is left out, and
841 // sets beyond.
842 template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA, typename Cost_Type>
844 const GT & g,
845 const Path_State<GT, Cost_Type> & base_path,
846 typename GT::Node * target,
847 const Suffix_Index<GT, Cost_Type> & suffix_index,
848 Distance distance,
849 SA sa,
850 const Array<Path_State<GT, Cost_Type>> & accepted,
851 Array<Path_State<GT, Cost_Type>> & candidates,
852 bool & beyond)
853 {
854 using Node = typename GT::Node;
855 using Arc = typename GT::Arc;
856
857 if (base_path.arcs.is_empty())
858 return;
859
860 const Path_Snapshot<GT, Cost_Type> base_snapshot =
861 make_path_snapshot<GT, Cost_Type>(base_path);
862
863 Cost_Type prefix_cost = Cost_Type{0};
864 const size_t arc_count = base_snapshot.arcs.size();
865
866 // At every node of the path, its last one too: a walk may go on from
867 // the target and come back to it
868 for (size_t spur_index = 0; spur_index <= arc_count; ++spur_index)
869 {
870 Node * spur_node = base_snapshot.nodes[spur_index];
871 Arc * base_arc = spur_index < arc_count ? base_snapshot.arcs[spur_index] : nullptr;
872
873 for (Node_Arc_Iterator<GT, SA> it(spur_node, sa); it.has_curr(); it.next_ne())
874 {
875 Arc * deviation_arc = it.get_current_arc_ne();
876 if (deviation_arc == base_arc)
877 continue;
878
879 Node * deviation_next_node = it.get_tgt_node();
880 auto * dist_of_next = suffix_index.dist_to_target.search(deviation_next_node);
881 if (dist_of_next == nullptr) // it does not reach target, or only beyond max()
882 {
883 if (not suffix_index.beyond_max.is_empty()
884 and suffix_index.beyond_max.contains(deviation_next_node))
885 beyond = true;
886 continue;
887 }
888 const Cost_Type suffix_dist = dist_of_next->second;
889
890 Path_State<GT, Cost_Type> suffix_state;
891 if (not build_suffix_state(g,
892 suffix_index,
893 deviation_next_node,
894 target,
895 suffix_state))
896 continue;
897
898 Cost_Type through{}, total{};
900 prefix_cost, distance(deviation_arc), through)
901 or not shortest_path_detail::add_if_fits(through, suffix_dist, total))
902 {
903 beyond = true;
904 continue;
905 }
906
907 Path_State<GT, Cost_Type> candidate =
908 compose_general_candidate<GT, Cost_Type>(
909 base_snapshot,
910 spur_index,
911 deviation_arc,
912 deviation_next_node,
913 suffix_state);
914
915 candidate.total_cost = total;
916
917 if (not contains_path(accepted, candidate)
918 and not contains_path(candidates, candidate))
919 candidates.append(candidate);
920 }
921
922 if (base_arc != nullptr)
924 prefix_cost,
925 distance(base_arc));
926 }
927 }
928 } // namespace k_shortest_paths_detail
929
930
968 template <AlephGraph GT,
969 ArcDistance<GT> Distance = Dft_Dist<GT>,
970 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
971 requires std::is_arithmetic_v<typename Distance::Distance_Type>
974 typename GT::Node *source,
975 typename GT::Node *target,
976 const size_t k,
977 Distance distance = Distance(),
978 SA sa = SA())
979 {
980 using Node = typename GT::Node;
981 using Arc = typename GT::Arc;
982 using Cost_Type = typename Distance::Distance_Type;
985
986 DynList<Item> result;
987 if (k == 0)
988 return result;
989
990 ah_domain_error_if(source == nullptr) << "yen_k_shortest_paths(): source is null";
991 ah_domain_error_if(target == nullptr) << "yen_k_shortest_paths(): target is null";
992
994
995 if (source == target)
996 {
997 Item item;
998 item.total_cost = Cost_Type{0};
999 item.path = Path<GT>(g, source);
1000 result.append(item);
1001 return result;
1002 }
1003
1004 Array<State> accepted;
1005 Array<State> candidates;
1006 accepted.reserve(k);
1007 candidates.reserve(k * 2 + 1);
1008
1009 // A path whose cost does not fit was left out; the spur paths are
1010 // searched only while more paths are asked
1011 bool beyond = false;
1012 State first;
1013 if (not k_shortest_paths_detail::shortest_path_filtered<GT, Distance, SA>(
1014 g, source, target, distance, sa, nullptr, nullptr, first, beyond))
1015 {
1016 ah_overflow_error_if(beyond)
1017 << "yen_k_shortest_paths(): the cost of the path 1 does not fit in its type";
1018 return result;
1019 }
1020
1021 accepted.append(first);
1022
1023 for (size_t kth = 1; kth < k; ++kth)
1024 {
1025 const State & previous = accepted.get_last();
1026 const auto previous_snapshot =
1027 k_shortest_paths_detail::make_path_snapshot<GT, Cost_Type>(previous);
1028 if (previous_snapshot.nodes.size() < 2)
1029 break;
1030
1032 accepted_snapshots;
1033 accepted_snapshots.reserve(accepted.size());
1034 for (const State & p: accepted)
1035 accepted_snapshots.append(
1036 k_shortest_paths_detail::make_path_snapshot<GT, Cost_Type>(p));
1037
1038 for (size_t spur_index = 0; spur_index + 1 < previous_snapshot.nodes.size();
1039 ++spur_index)
1040 {
1041 Node *spur_node = previous_snapshot.nodes[spur_index];
1042
1043 DynSetTree<Node *> forbidden_nodes;
1044 DynSetTree<Arc *> forbidden_arcs;
1045
1046 for (size_t i = 0; i < spur_index; ++i)
1047 forbidden_nodes.insert(previous_snapshot.nodes[i]);
1048
1049 for (size_t p_idx = 0; p_idx < accepted_snapshots.size(); ++p_idx)
1050 {
1051 const auto & p_snapshot = accepted_snapshots[p_idx];
1053 p_snapshot, previous_snapshot, spur_index))
1054 forbidden_arcs.insert(p_snapshot.arcs[spur_index]);
1055 }
1056
1057 State spur_state;
1058 if (not k_shortest_paths_detail::shortest_path_filtered<GT, Distance, SA>(
1059 g,
1060 spur_node,
1061 target,
1062 distance,
1063 sa,
1064 &forbidden_nodes,
1065 &forbidden_arcs,
1066 spur_state,
1067 beyond))
1068 continue;
1069
1070 State candidate =
1072 previous_snapshot, spur_index, spur_state);
1073 if (not k_shortest_paths_detail::cost_of_arcs_if_fits<GT, Distance>(
1074 candidate.arcs, distance, candidate.total_cost))
1075 {
1076 beyond = true;
1077 continue;
1078 }
1079
1080 if (not k_shortest_paths_detail::contains_path(accepted, candidate)
1081 and not k_shortest_paths_detail::contains_path(candidates, candidate))
1082 candidates.append(candidate);
1083 }
1084
1085 if (candidates.is_empty())
1086 break;
1087
1088 size_t best_idx = 0;
1089 for (size_t i = 1; i < candidates.size(); ++i)
1090 if (candidates[i].total_cost < candidates[best_idx].total_cost)
1091 best_idx = i;
1092
1093 State best = candidates[best_idx];
1094 if (best_idx + 1 < candidates.size())
1095 candidates[best_idx] = candidates.get_last();
1096 const auto removed_candidate = candidates.remove_last();
1097 (void) removed_candidate;
1098
1099 accepted.append(best);
1100 }
1101
1102 // The next path that was asked exists, and its cost does not fit
1103 ah_overflow_error_if(accepted.size() < k and beyond)
1104 << "yen_k_shortest_paths(): the cost of the path " << accepted.size() + 1
1105 << " does not fit in its type";
1106
1107 for (const State & state: accepted)
1108 {
1109 Item item;
1110 item.total_cost = state.total_cost;
1111 item.path = k_shortest_paths_detail::state_to_path(g, state);
1112 result.append(item);
1113 }
1114
1115 return result;
1116 }
1117
1118
1167 template <AlephGraph GT,
1168 ArcDistance<GT> Distance = Dft_Dist<GT>,
1169 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1170 requires std::is_arithmetic_v<typename Distance::Distance_Type>
1173 typename GT::Node *source,
1174 typename GT::Node *target,
1175 const size_t k,
1176 Distance distance = Distance(),
1177 SA sa = SA())
1178 {
1179 using Cost_Type = typename Distance::Distance_Type;
1182
1183 DynList<Item> result;
1184 if (k == 0)
1185 return result;
1186
1187 ah_domain_error_if(source == nullptr) << "eppstein_k_shortest_paths(): source is null";
1188 ah_domain_error_if(target == nullptr) << "eppstein_k_shortest_paths(): target is null";
1189
1191
1192 if (source == target)
1193 {
1194 Item item;
1195 item.total_cost = Cost_Type{0};
1196 item.path = Path<GT>(g, source);
1197 result.append(item);
1198 return result;
1199 }
1200
1201 Array<State> accepted;
1202 Array<State> candidates;
1203 accepted.reserve(k);
1204 candidates.reserve(k * 3 + 1);
1205
1206 const auto suffix_index =
1207 k_shortest_paths_detail::build_suffix_index<GT, Distance, SA>(
1208 g, target, distance, sa);
1209
1210 State first;
1211 if (not k_shortest_paths_detail::build_suffix_state<GT, Cost_Type>(
1212 g, suffix_index, source, target, first))
1213 {
1214 // source reaches target only beyond max(): no walk fits
1215 ah_overflow_error_if(suffix_index.beyond_max.contains(source))
1216 << "eppstein_k_shortest_paths(): the cost of the walk 1 does not fit in its type";
1217 return result;
1218 }
1219
1220 // A walk whose cost does not fit was left out; the deviations are
1221 // made only while more walks are asked, so that a walk that was not
1222 // asked does not throw
1223 bool beyond = false;
1224 accepted.append(first);
1225 if (accepted.size() < k)
1226 k_shortest_paths_detail::generate_general_deviation_candidates<GT, Distance, SA, Cost_Type>(
1227 g, first, target, suffix_index, distance, sa, accepted, candidates, beyond);
1228
1229 while (accepted.size() < k and not candidates.is_empty())
1230 {
1231 size_t best_idx = 0;
1232 for (size_t i = 1; i < candidates.size(); ++i)
1233 if (candidates[i].total_cost < candidates[best_idx].total_cost)
1234 best_idx = i;
1235
1236 State best = candidates[best_idx];
1237 if (best_idx + 1 < candidates.size())
1238 candidates[best_idx] = candidates.get_last();
1239 const auto removed_candidate = candidates.remove_last();
1240 (void) removed_candidate;
1241
1242 if (k_shortest_paths_detail::contains_path(accepted, best))
1243 continue;
1244
1245 accepted.append(best);
1246 if (accepted.size() < k)
1247 k_shortest_paths_detail::generate_general_deviation_candidates<GT, Distance, SA, Cost_Type>(
1248 g, best, target, suffix_index, distance, sa, accepted, candidates, beyond);
1249 }
1250
1251 // The next walk that was asked exists, and its cost does not fit
1252 ah_overflow_error_if(accepted.size() < k and beyond)
1253 << "eppstein_k_shortest_paths(): the cost of the walk " << accepted.size() + 1
1254 << " does not fit in its type";
1255
1256 for (const State & state: accepted)
1257 {
1258 Item item;
1259 item.total_cost = state.total_cost;
1260 item.path = k_shortest_paths_detail::state_to_path(g, state);
1261 result.append(item);
1262 }
1263
1264 return result;
1265 }
1266
1267
1281 template <AlephGraph GT,
1282 ArcDistance<GT> Distance = Dft_Dist<GT>,
1283 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1285 {
1288
1289 public:
1291 SA sa = SA())
1292 : distance_(std::move(distance)),
1293 sa_(std::move(sa))
1294 {
1295 // empty
1296 }
1297
1326 operator()(const GT & g,
1327 typename GT::Node *source,
1328 typename GT::Node *target,
1329 const size_t k) const
1330 requires std::is_arithmetic_v<typename Distance::Distance_Type>
1331 {
1332 return yen_k_shortest_paths<GT, Distance, SA>(
1333 g, source, target, k, distance_, sa_);
1334 }
1335 };
1336
1337
1342 template <AlephGraph GT,
1343 ArcDistance<GT> Distance = Dft_Dist<GT>,
1344 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1346 {
1349
1350 public:
1352 SA sa = SA())
1353 : distance_(std::move(distance)),
1354 sa_(std::move(sa))
1355 {
1356 // empty
1357 }
1358
1380 operator()(const GT & g,
1381 typename GT::Node *source,
1382 typename GT::Node *target,
1383 const size_t k) const
1384 requires std::is_arithmetic_v<typename Distance::Distance_Type>
1385 {
1386 return eppstein_k_shortest_paths<GT, Distance, SA>(
1387 g, source, target, k, distance_, sa_);
1388 }
1389 };
1390} // namespace Aleph
1391
1392# endif // K_SHORTEST_PATHS_H
Dijkstra's shortest path algorithm.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
C++20 concepts for the protocol shared by graph algorithms.
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
long double w
Definition btreepic.C:153
size_t size_t int32_t * out
Definition ca-c-api.h:120
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
T & get_last()
Return a modifiable reference to the last element.
Definition tpl_array.H:483
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
RAII guard that saves and restores graph cookies.
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 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 get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
bool is_empty() const
returns true if the set is empty
Functor wrapper for Eppstein-style k-shortest paths API.
DynList< K_Shortest_Path_Item< GT, typename Distance::Distance_Type > > operator()(const GT &g, typename GT::Node *source, typename GT::Node *target, const size_t k) const
Compute k shortest general (loopy) paths.
Eppstein_K_Shortest_Paths(Distance distance=Distance(), SA sa=SA())
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
constexpr bool is_empty() const noexcept
Definition htlist.H:434
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
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:4512
Path on a graph.
Definition tpl_graph.H:3832
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:3977
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
void append_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:4124
bool leave_out_beyond_max
Whether a painting leaves out without throwing the nodes whose distance does not fit.
bool beyond_max
A sum of the painting went beyond max()
Functor wrapper for Yen's k-shortest loopless paths algorithm.
DynList< K_Shortest_Path_Item< GT, typename Distance::Distance_Type > > operator()(const GT &g, typename GT::Node *source, typename GT::Node *target, const size_t k) const
Compute k shortest loopless paths.
Yen_K_Shortest_Paths(Distance distance=Distance(), SA sa=SA())
A painting of Dijkstra that leaves out the nodes whose distances do not fit, as nodes that the start ...
bool went_beyond_max() const noexcept
Whether a sum of the last painting went beyond max(), so that it may have left out some node.
Painting_Within_Max(Distance distance, SA sa)
A painting with the distance and the filter sa.
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1213
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
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:1206
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
A graph usable by the graph algorithms.
An arc distance (Distance) for graph GT.
An arc filter (SA) for graph GT.
RAII guards for graph node/arc cookies.
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
DynList< K_Shortest_Path_Item< GT, typename Distance::Distance_Type > > yen_k_shortest_paths(const GT &g, typename GT::Node *source, typename GT::Node *target, const size_t k, Distance distance=Distance(), SA sa=SA())
Compute k shortest loopless paths using Yen's algorithm.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
DynList< K_Shortest_Path_Item< GT, typename Distance::Distance_Type > > eppstein_k_shortest_paths(const GT &g, typename GT::Node *source, typename GT::Node *target, const size_t k, Distance distance=Distance(), SA sa=SA())
Compute k shortest general (loopy) paths.
#define NODE_COOKIE(p)
Return the node cookie
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
@ Spanning_Tree
Definition aleph-graph.H:79
Singly linked list implementations with head-tail access.
Knapsack_Item< int, int > Item
Path< GT > state_to_path(const GT &g, const Path_State< GT, Cost_Type > &state)
GT::Node * get_tree_next(const Suffix_Index< GT, Cost_Type > &index, typename GT::Node *node)
Suffix_Index< GT, typename Distance::Distance_Type > build_suffix_index(const GT &g, typename GT::Node *target, Distance distance, SA sa)
Path_State< GT, Cost_Type > compose_general_candidate(const Path_Snapshot< GT, Cost_Type > &base_path, const size_t spur_index, typename GT::Arc *deviation_arc, typename GT::Node *deviation_next_node, const Path_State< GT, Cost_Type > &suffix_state)
bool contains_path(const Array< Path_State< GT, Cost_Type > > &container, const Path_State< GT, Cost_Type > &candidate)
bool shortest_path_filtered(const GT &g, typename GT::Node *source, typename GT::Node *target, Distance distance, SA sa, const DynSetTree< typename GT::Node * > *forbidden_nodes, const DynSetTree< typename GT::Arc * > *forbidden_arcs, Path_State< GT, typename Distance::Distance_Type > &out, bool &beyond)
bool same_path_sequence(const Path_State< GT, Cost_Type > &a, const Path_State< GT, Cost_Type > &b)
Path_State< GT, Cost_Type > compose_candidate(const Path_Snapshot< GT, Cost_Type > &base_path, const size_t spur_index, const Path_State< GT, Cost_Type > &spur_state)
void generate_general_deviation_candidates(const GT &g, const Path_State< GT, Cost_Type > &base_path, typename GT::Node *target, const Suffix_Index< GT, Cost_Type > &suffix_index, Distance distance, SA sa, const Array< Path_State< GT, Cost_Type > > &accepted, Array< Path_State< GT, Cost_Type > > &candidates, bool &beyond)
bool build_suffix_state(const GT &g, const Suffix_Index< GT, Cost_Type > &index, typename GT::Node *start, typename GT::Node *target, Path_State< GT, Cost_Type > &out_suffix)
bool cost_of_arcs_if_fits(const DynList< typename GT::Arc * > &arcs, Distance distance, typename Distance::Distance_Type &total)
Suffix_Index< GT, typename Distance::Distance_Type > build_suffix_index_with_itor(const GT &g, typename GT::Node *target, Distance distance, SA sa)
Path_State< GT, Cost_Type > path_to_state(const Path< GT > &path, const Cost_Type total_cost)
Suffix_Index< GT, typename Distance::Distance_Type > build_suffix_index_digraph(const GT &g, typename GT::Node *target, Distance distance, SA sa)
void validate_non_negative_weights(const GT &g, Distance distance, SA sa)
Path_Snapshot< GT, Cost_Type > make_path_snapshot(const Path_State< GT, Cost_Type > &path)
void for_each_left_out(typename GT::Node *start, SA sa, Op op)
Calls op on each node that start reaches through the arcs that sa shows, but that a painting from sta...
bool same_prefix_nodes(const Path_Snapshot< GT, Cost_Type > &a, const Path_Snapshot< GT, Cost_Type > &b, const size_t spur_index)
GT::Arc * get_tree_arc(const Suffix_Index< GT, Cost_Type > &index, typename GT::Node *node)
bool add_if_fits(const T &a, const T &b, T &out)
Adds two distance values when the sum fits.
T checked_add(const T &a, const T &b)
Safely add two distance values with overflow checking.
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
STL namespace.
Common utilities and base class for shortest path algorithms.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
A shortest-path item returned by k-shortest algorithms.
Cost_Type total_cost
Total path cost.
Path< GT > path
Path from source to target.
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
static thread_local const Distance * base_ptr
static thread_local const DynMapTree< Arc *, Arc * > * map_ptr
Context for reverse-graph Dijkstra in build_suffix_index_digraph().
DynMapTree< Node *, Cost_Type > dist_to_target
The std::overflow_error of a painting that leaves out a node whose distance does not fit.
Distance accessor.
Aleph::Array< Display_Node > nodes
Root at index zero.
static int * k
Dynamic array container with automatic resizing.
Dynamic queue implementation based on linked lists.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.