30 # include <tpl_agraph.H> 31 # include <tpl_graph_utils.H> 36 using namespace Aleph;
41 typename GT::Node * tree_node =
nullptr;
42 void * heap_node =
nullptr;
44 Prim_Info() : tree_node(nullptr), heap_node(nullptr) { }
47 # define PRIMINFO(p) static_cast<Prim_Info<GT>*>(NODE_COOKIE(p)) 48 # define TREENODE(p) (PRIMINFO(p)->tree_node) 49 # define HEAPNODE(p) (PRIMINFO(p)->heap_node) 51 template <
class GT,
class Distance>
54 typedef typename ArcHeap<GT, Distance, Prim_Heap_Info>::Node Node;
56 Node *& operator () (
typename GT::Node * p)
58 return (Node*&) HEAPNODE(p);
62 template <
class GT,
class Distance>
63 struct Simple_Prim_Heap
65 typedef typename ArcHeap<GT, Distance, Simple_Prim_Heap>::Node Node;
67 Node *& operator () (
typename GT::Node * p)
78 Init_Prim_Info(GT & __tree) : tree(__tree) { }
80 void operator () (
const GT & g,
typename GT::Node * p)
82 g.reset_bit(p, Aleph::Spanning_Tree);
84 TREENODE(p) = tree.insert_node(p->get_info());
89 struct Uninit_Prim_Info
91 void operator () (
const GT &,
typename GT::Node * p)
93 Prim_Info<GT> * aux = PRIMINFO(p);
147 typedef Prim_Heap_Info<GT, Distance> Acc_Heap;
149 typedef Simple_Prim_Heap<GT, Distance> Acc_Simple_Heap;
151 typedef ArcHeap<GT, Distance, Acc_Heap> Heap;
153 typedef ArcHeap<GT, Distance, Acc_Simple_Heap> Simple_Heap;
167 : dist(__dist), sa(__sa)
174 void paint_min_spanning_tree(
const GT & g,
typename GT::Node * first)
177 throw std::domain_error(
"g is a digraph");
182 NODE_BITS(first).set_bit(Aleph::Spanning_Tree,
true);
184 Simple_Heap heap(dist, Acc_Simple_Heap());
187 typename GT::Arc * arc = it.get_current_arc_ne();
188 heap.put_arc(arc, it.get_tgt_node_ne());
191 const size_t V1 = g.get_num_nodes() - 1;
194 while (count < V1 and not heap.is_empty())
196 typename GT::Arc * min_arc = heap.get_min_arc();
200 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree,
true);
202 typename GT::Node * src = g.get_src_node(min_arc);
203 typename GT::Node * tgt = g.get_tgt_node(min_arc);
208 typename GT::Node * tgt_node =
211 NODE_BITS(tgt_node).set_bit(Aleph::Spanning_Tree,
true);
217 typename GT::Arc * arc = it.get_current_arc_ne();
221 typename GT::Node * tgt = it.get_tgt_node_ne();
225 heap.put_arc(arc, tgt);
229 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree,
true);
233 void min_spanning_tree(
const GT & g,
typename GT::Node * first, GT & tree)
236 throw std::domain_error(
"g is a digraph");
240 Init_Prim_Info <GT> init(tree);
244 NODE_BITS(first).set_bit(Aleph::Spanning_Tree,
true);
246 Heap heap(dist, Acc_Heap()) ;
250 typename GT::Arc * arc = it.get_current_arc_ne();
251 heap.put_arc(arc, it.get_tgt_node_ne());
254 const size_t V1 = g.get_num_nodes() - 1;
256 while (tree.get_num_arcs() < V1 and not heap.is_empty())
258 typename GT::Arc * min_arc = heap.get_min_arc();
262 ARC_BITS(min_arc).set_bit(Aleph::Spanning_Tree,
true);
264 typename GT::Node * src = g.get_src_node(min_arc);
265 typename GT::Node * tgt = g.get_tgt_node(min_arc);
270 typename GT::Node * tgt_node =
273 NODE_BITS(tgt_node).set_bit(Aleph::Spanning_Tree,
true);
279 typename GT::Arc * arc = it.get_current_arc_ne();
283 typename GT::Node * tgt = it.get_tgt_node_ne();
287 heap.put_arc(arc, tgt);
291 typename GT::Arc * tree_arc =
292 tree.insert_arc(TREENODE(src), TREENODE(tgt), min_arc->get_info());
313 void operator () (
const GT & g, GT & tree)
315 min_spanning_tree(g, g.get_first_node(), tree);
331 void operator () (
const GT & g,
typename GT::Node * start, GT & tree)
333 min_spanning_tree(g, start, tree);
337 void operator () (
const GT & g)
339 paint_min_spanning_tree(g, g.get_first_node());
342 void operator () (
const GT & g,
typename GT::Node * start)
344 paint_min_spanning_tree(g, start);
void clear_graph(GT &g) noexcept
Definition: tpl_graph.H:3463
#define NODE_COOKIE(p)
Definition: aleph-graph.H:333
Definition: tpl_graph.H:2493
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:327
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
void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
Definition: tpl_graph.H:3419
Prim_Min_Spanning_Tree(Distance __dist=Distance(), SA __sa=SA())
Definition: Prim.H:166
Definition: tpl_graph.H:1063
#define NODE_BITS(p)
Definition: aleph-graph.H:305
Definition: tpl_graph_utils.H:1753
#define ARC_BITS(p)
Definition: aleph-graph.H:351
Definition: tpl_graph.H:1177