30 # include <ahFunction.H> 32 # include <tpl_find_path.H> 33 # include <tpl_agraph.H> 38 # define DNassert(p) ((Node_Info*) NODE_COOKIE((p))) 41 # define TREENODE(p) (((Tree_Node_Info*)DNassert(p))->tree_node) 43 # define ACC(p) (DNassert(p)->dist) // Acceso a la distancia acumulada 44 # define HEAPNODE(p) (DNassert(p)->heap_node) 45 # define PARENT(p) (DNassert(p)->ret_node) 47 # define DAassert(p) ((Arc_Info*) ARC_COOKIE(p)) 48 # define ARC_DIST(p) (Distance () (p)) 49 # define TREEARC(p) (((Tree_Arc_Info*)DAassert(p))->tree_arc) 50 # define POT(p) (DAassert(p)->pot) 51 # define GRAPHNODE(p) (static_cast<typename GT::Node*>(NODE_COOKIE(p))) 104 typename Distance::Distance_Type pot;
108 struct Tree_Arc_Info :
public Arc_Info
110 typename GT::Arc * tree_arc;
111 typename Distance::Distance_Type pot;
115 struct Get_Potential_Arc :
public Distance
117 Get_Potential_Arc() noexcept { }
119 Get_Potential_Arc(Distance & d) noexcept : Distance(d) { }
121 typename Distance::Distance_Type
125 return arc_info->pot;
132 typename Distance::Distance_Type dist = 0;
133 void * heap_node =
nullptr;
134 void * ret_node =
nullptr;
138 struct Tree_Node_Info :
public Node_Info
140 typename GT::Node * tree_node =
nullptr;
141 typename Distance::Distance_Type dist;
142 void * heap_node =
nullptr;
146 struct Dijkstra_Heap_Info
149 ArcHeap<GT, Get_Potential_Arc, Dijkstra_Heap_Info>::Node Node;
151 Node *&
operator () (
typename GT::Node * p)
const noexcept
153 return (Node*&) HEAPNODE(p);
158 struct Initialize_Node
160 void operator () (
const GT & g,
typename GT::Node * p)
const noexcept
162 g.reset_bit(p, Aleph::Spanning_Tree);
170 void operator () (
const GT &,
typename GT::Node * p)
const noexcept
172 void * tmp = PARENT(p);
179 struct Initialize_Arc
181 void operator () (
const GT & g,
typename GT::Arc * a)
const noexcept
183 g.reset_bit(a, Aleph::Spanning_Tree);
192 void operator () (
const GT &,
typename GT::Arc * ga)
const noexcept
199 struct Initialize_Tree_Node
201 void operator () (
const GT & g,
typename GT::Node * p)
const noexcept
203 g.reset_bit(p, Aleph::Spanning_Tree);
209 struct Destroy_Tree_Node
211 void operator () (
const GT &,
typename GT::Node * p)
const noexcept
213 auto aux = (Tree_Node_Info *) DNassert(p);
214 auto tp = TREENODE(p);
228 struct Initialize_Tree_Arc
230 void operator () (
const GT & g,
typename GT::Arc * a)
const noexcept
232 g.reset_bit(a, Aleph::Spanning_Tree);
235 TREEARC(a) =
nullptr;
240 struct Destroy_Tree_Arc
242 void operator () (
const GT &,
typename GT::Arc * ga)
const noexcept
244 auto aux = (Tree_Arc_Info *) DAassert(ga);
245 typename GT::Arc * ta = TREEARC(ga);
256 typedef Dijkstra_Heap_Info Heap_Info;
258 typedef ArcHeap<GT, Get_Potential_Arc, Heap_Info> Heap;
261 Get_Potential_Arc get_pot;
263 bool painted =
false;
264 GT * ptr_g =
nullptr;
265 typename GT::Node * s =
nullptr;
279 noexcept(
std::is_nothrow_move_assignable<SA>::value)
280 : sa(__sa), get_pot(dist), heap(get_pot, Heap_Info()),
281 painted(false), ptr_g(
nullptr), s(
nullptr)
288 template <
class IN,
class IA>
289 void init(
const GT & g,
typename GT::Node * start)
293 ptr_g = &
const_cast<GT&
>(g);
301 template <
class DN,
class DA>
323 init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
327 NODE_BITS(start).set_bit(Aleph::Spanning_Tree,
true);
329 auto ret = TREENODE(start) = tree.insert_node(start->get_info());
332 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
334 auto arc = it.get_current_arc_ne();
335 POT(arc) = ARC_DIST(arc);
336 heap.put_arc(arc, it.get_tgt_node());
339 const auto & n = g.get_num_nodes();
341 while (tree.get_num_nodes() < n)
343 auto garc = heap.get_min_arc();
347 auto gsrc = g.get_src_node(garc);
348 auto gtgt = g.get_tgt_node(garc);
355 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree,
true);
358 std::swap(gsrc, gtgt);
360 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree,
true);
362 auto ttgt = tree.insert_node(gtgt->get_info());
363 TREENODE(gtgt) = ttgt;
364 auto tsrc = TREENODE(gsrc);
366 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
367 TREEARC(garc) = tarc;
369 ACC(gtgt) = ACC(gsrc) + ARC_DIST(garc);
370 const auto & acc = ACC(gtgt);
373 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
375 auto arc = it.get_current_arc_ne();
379 auto tgt = it.get_tgt_node();
383 POT(arc) = acc + ARC_DIST(arc);
384 heap.put_arc(arc, tgt);
388 uninit<Destroy_Tree_Node, Destroy_Tree_Arc> ();
408 typename GT::Node * start,
409 typename GT::Node * end,
412 init <Initialize_Tree_Node, Initialize_Tree_Arc> (g, start);
415 NODE_BITS(start).set_bit(Aleph::Spanning_Tree,
true);
417 TREENODE(start) = tree.insert_node(start->get_info());
420 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
422 auto arc = it.get_current_arc_ne();
423 POT(arc) = ARC_DIST(arc);
424 heap.put_arc(arc, it.get_tgt_node());
427 const auto & n = g.get_num_nodes();
429 while (tree.get_num_nodes() < n and not heap.is_empty())
431 auto garc = heap.get_min_arc();
435 auto gsrc = g.get_src_node(garc);
436 auto gtgt = g.get_tgt_node(garc);
443 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree,
true);
446 std::swap(gsrc, gtgt);
448 auto ttgt = tree.insert_node(gtgt->get_info());
449 TREENODE(gtgt) = ttgt;
450 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree,
true);
453 tree.insert_arc(TREENODE(gsrc), TREENODE(gtgt), garc->get_info());
454 TREEARC(garc) = tarc;
459 ACC(gtgt) = ACC(gsrc) + ARC_DIST(garc);
460 const auto & acc = ACC(gtgt);
463 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
465 auto arc = it.get_current_arc_ne();
469 auto tgt = it.get_tgt_node();
473 POT(arc) = acc + ARC_DIST(arc);
474 heap.put_arc(arc, tgt);
478 uninit <Destroy_Tree_Node, Destroy_Tree_Arc> ();
491 typename GT::Node * start,
492 typename GT::Node * end)
494 bool ret_val =
false;
495 init<Initialize_Node, Initialize_Arc> (g, start);
497 NODE_BITS(start).set_bit(Aleph::Spanning_Tree,
true);
500 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
502 auto arc = it.get_current_arc_ne();
503 POT(arc) = ARC_DIST(arc);
504 heap.put_arc(arc, it.get_tgt_node());
507 const auto & n = g.get_num_nodes();
510 while (tn < n and not heap.is_empty())
512 auto garc = heap.get_min_arc();
516 auto src = g.get_src_node(garc);
517 auto tgt = g.get_tgt_node(garc);
524 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree,
true);
529 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree,
true);
540 ACC(tgt) = ACC(src) + ARC_DIST(garc);
542 const auto & acc = ACC(tgt);
545 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
547 auto a = it.get_current_arc_ne();
551 auto t = it.get_tgt_node();
555 POT(a) = acc + ARC_DIST(a);
560 uninit<Destroy_Node, Destroy_Arc> ();
575 init<Initialize_Node, Initialize_Arc> (g, start);
577 NODE_BITS(start).set_bit(Aleph::Spanning_Tree,
true);
580 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
582 auto arc = it.get_current_arc_ne();
583 POT(arc) = ARC_DIST(arc);
584 heap.put_arc(arc, it.get_tgt_node());
587 const auto & n = g.get_num_nodes();
590 while (tn < n and not heap.is_empty())
592 auto garc = heap.get_min_arc();
596 auto src = g.get_src_node(garc);
597 auto tgt = g.get_tgt_node(garc);
604 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree,
true);
609 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree,
true);
614 ACC(tgt) = ACC(src) + ARC_DIST(garc);
615 const auto & acc = ACC(tgt);
618 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
620 auto a = it.get_current_arc_ne();
624 auto t = it.get_tgt_node();
628 POT(a) = acc + ARC_DIST(a);
633 uninit<Destroy_Node, Destroy_Arc> ();
659 typename Distance::Distance_Type
662 if (ptr_g ==
nullptr)
663 throw std::domain_error(
"Min path has not been computed");
666 throw std::domain_error(
"Graph has not previously painted");
668 return Aleph::get_min_path<GT, Distance>(s, end, path);
689 typename Distance::Distance_Type
691 typename GT::Node * start,
typename GT::Node * end,
698 return numeric_limits<typename Distance::Distance_Type>::max();
740 typename Distance::Distance_Type
744 throw std::domain_error(
"Graph has not previously painted");
755 typename GT::Node * s,
756 typename GT::Node * e,
765 typename Distance::Distance_Type dist;
767 Total() noexcept : dist(0) { }
797 typename Distance::Distance_Type
800 if (ptr_g ==
nullptr)
801 throw std::domain_error(
"Min path has not been computed");
803 auto ts = mapped_node<GT>(s);
804 auto te = mapped_node<GT>(end);
814 path.
append(mapped_node<GT>(it.get_current_node_ne()));
833 # endif // DIJKSTRA_H Definition: tpl_graph.H:3633
void clear_graph(GT &g) noexcept
Definition: tpl_graph.H:3463
bool has_curr() const noexcept
Return true if the iterator has current item.
Definition: dlink.H:658
#define NODE_COOKIE(p)
Definition: aleph-graph.H:333
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Definition: Dijkstra.H:660
Distance::Distance_Type copy_painted_min_paths_tree(const GT &g, GT &tree)
Definition: Dijkstra.H:741
Definition: tpl_graph.H:2493
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Definition: Dijkstra.H:407
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Definition: Dijkstra.H:573
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:327
GT::Node * compute_min_paths_tree(const GT &g, typename GT::Node *start, GT &tree)
Definition: Dijkstra.H:321
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm...
Definition: Dijkstra.H:86
#define ARC_COOKIE(p)
Definition: aleph-graph.H:366
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Definition: Dijkstra.H:690
void map_arcs(typename GTS::Arc *p, typename GTT::Arc *q) noexcept
Definition: tpl_graph.H:3445
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:360
Definition: tpl_graph.H:53
void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
Definition: tpl_graph.H:3419
Definition: tpl_find_path.H:69
Distance::Distance_Type get_min_path(const GT &tree, typename GT::Node *end, Path< GT > &path)
Definition: Dijkstra.H:798
Definition: tpl_graph.H:1063
void next_ne() noexcept
Definition: tpl_dynDlist.H:433
Definition: tpl_graph.H:3723
#define NODE_BITS(p)
Definition: aleph-graph.H:305
bool paint_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end)
Definition: Dijkstra.H:490
Definition: tpl_graph_utils.H:1753
void next()
Definition: tpl_dynDlist.H:441
void init(Node *start_node)
Definition: tpl_graph.H:2722
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Definition: Dijkstra.H:722
#define ARC_BITS(p)
Definition: aleph-graph.H:351
Clase totalizadora de distancias.
Definition: Dijkstra.H:763
Dijkstra_Min_Paths(Distance dist=Distance(), SA __sa=SA()) noexcept(std::is_nothrow_move_assignable< SA >::value)
Definition: Dijkstra.H:277
void empty()
Clean the path: all the nodes and arc are removed.
Definition: tpl_graph.H:2761
void append(Arc *arc)
Definition: tpl_graph.H:2808
Definition: tpl_graph.H:1177
Distance::Distance_Type dist
Accumutive distance from the first seen arc until the last seen.
Definition: tpl_graph.H:3726
Definition: tpl_graph.H:3135
Definition: tpl_graph.H:2540