# HG changeset patch # User alpar # Date 1080549061 0 # Node ID 45107782cbcae69ccbc8a24a614d0271c5f32ac1 # Parent 483ba4ffe90aecfb21f4db337e829fee37805f23 dijkstra.h and fib_heap.h has moved to include. The versions of bin_heap.hh shuld be merged and renamed to bin_heap.h diff -r 483ba4ffe90a -r 45107782cbca doc/Doxyfile --- a/doc/Doxyfile Mon Mar 29 08:22:39 2004 +0000 +++ b/doc/Doxyfile Mon Mar 29 08:31:01 2004 +0000 @@ -395,9 +395,9 @@ ../src/include/invalid.h \ ../src/include/smart_graph.h \ ../src/include/skeletons/maps.h \ - ../src/demo/alpar/dijkstra/dijkstra.h \ + ../src/include/dijkstra.h \ ../src/demo/alpar/dijkstra/bin_heap.hh \ - ../src/demo/alpar/dijkstra/fib_heap.h \ + ../src/include/fib_heap.h \ ../src/demo/athos/xy/xy.h \ maps.dox diff -r 483ba4ffe90a -r 45107782cbca src/include/dijkstra.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/include/dijkstra.h Mon Mar 29 08:31:01 2004 +0000 @@ -0,0 +1,236 @@ +// -*- C++ -*- + +/* + *template > + * + *Constructor: + * + *Dijkstra(Graph G, LengthMap length) + * + * + *Methods: + * + *void run(Node s) + * + *T dist(Node v) : After run(s) was run, it returns the distance from s to v. + * Returns T() if v is not reachable from s. + * + *Edge pred(Node v) : After run(s) was run, it returns the last + * edge of a shortest s-v path. It is INVALID for s and for + * the nodes not reachable from s. + * + *bool reached(Node v) : After run(s) was run, it is true iff v is + * reachable from s + * + */ + +#ifndef HUGO_DIJKSTRA_H +#define HUGO_DIJKSTRA_H + +///\file +///\brief Dijkstra algorithm. + +#include "fib_heap.h" +#include "bin_heap.hh" +#include "invalid.h" + +namespace hugo { + + //Alpar: Changed the order of the parameters + + ///%Dijkstra algorithm class. + + ///This class provides an efficient implementation of %Dijkstra algorithm. + ///The edge lengths are passed to the algorithm using a + ///\ref ReadMapSkeleton "readable map", + ///so it is easy to change it to any kind of length. + /// + ///The type of the length is determined by the \c ValueType of the length map. + /// + ///It is also possible to change the underlying priority heap. + /// + ///\param Graph The graph type the algorithm runs on. + ///\param LengthMap This read-only + ///EdgeMap + ///determines the + ///lengths of the edges. It is read once for each edge, so the map + ///may involve in relatively time consuming process to compute the edge + ///length if it is necessary. The default map type is + ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap" + ///\param Heap The heap type used by the %Dijkstra + ///algorithm. The default + ///is using \ref BinHeap "binary heap". + +#ifdef DOXYGEN + template +#else + template , + template class Heap = BinHeap > +// typename Heap=BinHeap > > +#endif + class Dijkstra{ + public: + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::Edge Edge; + typedef typename Graph::OutEdgeIt OutEdgeIt; + + typedef typename LengthMap::ValueType ValueType; + typedef typename Graph::NodeMap PredMap; + typedef typename Graph::NodeMap PredNodeMap; + typedef typename Graph::NodeMap DistMap; + + private: + const Graph& G; + const LengthMap& length; + PredMap predecessor; + //In place of reach: + PredNodeMap pred_node; + DistMap distance; + //I don't like this: + // //FIXME: + // typename Graph::NodeMap reach; + // //typename Graph::NodeMap reach; + + public : + + /* + The distance of the nodes is 0. + */ + Dijkstra(Graph& _G, LengthMap& _length) : + G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { } + + void run(Node s); + + ///The distance of a node from the source. + + ///Returns the distance of a node from the source. + ///\pre \ref run() must be called before using this function. + ///\warning If node \c v in unreachable from the source the return value + ///of this funcion is undefined. + ValueType dist(Node v) const { return distance[v]; } + ///Returns the edges of the shortest path tree. + + ///For a node \c v it returns the last edge of the shortest path + ///from the source to \c v or INVALID if \c v is unreachable + ///from the source. + ///\pre \ref run() must be called before using this function. + Edge pred(Node v) const { return predecessor[v]; } + ///Returns the nodes of the shortest paths. + + ///For a node \c v it returns the last but one node of the shortest path + ///from the source to \c v or INVALID if \c v is unreachable + ///from the source. + ///\pre \ref run() must be called before using this function. + Node predNode(Node v) const { return pred_node[v]; } + + ///Returns a reference to the NodeMap of distances. + + ///\pre \ref run() must be called before using this function. + /// + const DistMap &distMap() const { return distance;} + ///Returns a reference to the shortest path tree map. + + ///Returns a reference to the NodeMap of the edges of the + ///shortest path tree. + ///\pre \ref run() must be called before using this function. + const PredMap &predMap() const { return predecessor;} + ///Returns a reference to the map of nodes of shortest paths. + + ///Returns a reference to the NodeMap of the last but one nodes of the + ///shortest paths. + ///\pre \ref run() must be called before using this function. + const PredNodeMap &predNodeMap() const { return pred_node;} + + // bool reached(Node v) { return reach[v]; } + + ///Checks if a node is reachable from the source. + + ///Returns \c true if \c v is reachable from the source. + ///\warning the source node is reported to be unreached! + ///\todo Is this what we want? + ///\pre \ref run() must be called before using this function. + /// + bool reached(Node v) { return G.valid(predecessor[v]); } + + }; + + + // ********************************************************************** + // IMPLEMENTATIONS + // ********************************************************************** + + ///Runs %Dijkstra algorithm from node the source. + + ///This method runs the %Dijkstra algorithm from a source node \c s + ///in order to + ///compute the + ///shortest path to each node. The algorithm computes + ///- The shortest path tree. + ///- The distance of each node from the source. + template class Heap > + void Dijkstra::run(Node s) { + + NodeIt u; + for ( G.first(u) ; G.valid(u) ; G.next(u) ) { + predecessor.set(u,INVALID); + pred_node.set(u,INVALID); + // If a node is unreacheable, then why should be the dist=0? + // distance.set(u,0); + // reach.set(u,false); + } + + //We don't need it at all. + // //FIXME: + // typename Graph::NodeMap scanned(G,false); + // //typename Graph::NodeMap scanned(G,false); + typename Graph::NodeMap heap_map(G,-1); + + //Heap heap(heap_map); + Heap > heap(heap_map); + + heap.push(s,0); + // reach.set(s, true); + + while ( !heap.empty() ) { + + Node v=heap.top(); + ValueType oldvalue=heap[v]; + heap.pop(); + distance.set(v, oldvalue); + + for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) { + Node w=G.head(e); + + switch(heap.state(w)) { + case heap.PRE_HEAP: + // reach.set(w,true); + heap.push(w,oldvalue+length[e]); + predecessor.set(w,e); + pred_node.set(w,v); + break; + case heap.IN_HEAP: + if ( oldvalue+length[e] < heap[w] ) { + heap.decrease(w, oldvalue+length[e]); + predecessor.set(w,e); + pred_node.set(w,v); + } + break; + case heap.POST_HEAP: + break; + } + } + } + } + +} //END OF NAMESPACE HUGO + +#endif + + diff -r 483ba4ffe90a -r 45107782cbca src/include/fib_heap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/include/fib_heap.h Mon Mar 29 08:31:01 2004 +0000 @@ -0,0 +1,392 @@ +// -*- C++ -*- +/* + *template > + * + *constructors: + * + *FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare) + * + *Member functions: + * + *int size() : returns the number of elements in the heap + * + *bool empty() : true iff size()=0 + * + *void set(Item, Prio) : calls push(Item, Prio) if Item is not + * in the heap, and calls decrease/increase(Item, Prio) otherwise + * + *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item + * mustn't be in the heap. + * + *Item top() : returns the Item with least Prio. + * Must be called only if heap is nonempty. + * + *Prio prio() : returns the least Prio + * Must be called only if heap is nonempty. + * + *Prio get(Item) : returns Prio of Item + * Must be called only if Item is in heap. + * + *void pop() : deletes the Item with least Prio + * + *void erase(Item) : deletes Item from the heap if it was already there + * + *void decrease(Item, P) : decreases prio of Item to P. + * Item must be in the heap with prio at least P. + * + *void increase(Item, P) : sets prio of Item to P. + * + *state_enum state(Item) : returns PRE_HEAP if Item has not been in the + * heap until now, IN_HEAP if it is in the heap at the moment, and + * POST_HEAP otherwise. In the latter case it is possible that Item + * will get back to the heap again. + * + *In Fibonacci heaps, increase and erase are not efficient, in case of + *many calls to these operations, it is better to use bin_heap. + */ + +#ifndef FIB_HEAP_H +#define FIB_HEAP_H + +///\file +///\brief Fibonacci Heap implementation. + +#include +#include +#include + +namespace hugo { + + /// A Fibonacci Heap implementation. + template > + class FibHeap { + + typedef Prio PrioType; + + class store; + + std::vector container; + int minimum; + ItemIntMap &iimap; + Compare comp; + int num_items; + + ///\todo It is use nowhere + ///\todo It doesn't conform to the naming conventions. + public: + enum state_enum { + IN_HEAP = 0, + PRE_HEAP = -1, + POST_HEAP = -2 + }; + + public : + + FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {} + FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(), + iimap(_iimap), comp(_comp), num_items() {} + + + int size() const { + return num_items; + } + + + bool empty() const { return num_items==0; } + + + void set (Item const it, PrioType const value) { + int i=iimap[it]; + if ( i >= 0 && container[i].in ) { + if ( comp(value, container[i].prio) ) decrease(it, value); + if ( comp(container[i].prio, value) ) increase(it, value); + } else push(it, value); + } + + + void push (Item const it, PrioType const value) { + int i=iimap[it]; + if ( i < 0 ) { + int s=container.size(); + iimap.set( it, s ); + store st; + st.name=it; + container.push_back(st); + i=s; + } else { + container[i].parent=container[i].child=-1; + container[i].degree=0; + container[i].in=true; + container[i].marked=false; + } + + if ( num_items ) { + container[container[minimum].right_neighbor].left_neighbor=i; + container[i].right_neighbor=container[minimum].right_neighbor; + container[minimum].right_neighbor=i; + container[i].left_neighbor=minimum; + if ( comp( value, container[minimum].prio) ) minimum=i; + } else { + container[i].right_neighbor=container[i].left_neighbor=i; + minimum=i; + } + container[i].prio=value; + ++num_items; + } + + + Item top() const { + return container[minimum].name; + } + + + PrioType prio() const { + return container[minimum].prio; + } + + + + + PrioType& operator[](const Item& it) { + return container[iimap[it]].prio; + } + + const PrioType& operator[](const Item& it) const { + return container[iimap[it]].prio; + } + +// const PrioType get(const Item& it) const { +// return container[iimap[it]].prio; +// } + + void pop() { + /*The first case is that there are only one root.*/ + if ( container[minimum].left_neighbor==minimum ) { + container[minimum].in=false; + if ( container[minimum].degree!=0 ) { + makeroot(container[minimum].child); + minimum=container[minimum].child; + balance(); + } + } else { + int right=container[minimum].right_neighbor; + unlace(minimum); + container[minimum].in=false; + if ( container[minimum].degree > 0 ) { + int left=container[minimum].left_neighbor; + int child=container[minimum].child; + int last_child=container[child].left_neighbor; + + makeroot(child); + + container[left].right_neighbor=child; + container[child].left_neighbor=left; + container[right].left_neighbor=last_child; + container[last_child].right_neighbor=right; + } + minimum=right; + balance(); + } // the case where there are more roots + --num_items; + } + + + void erase (const Item& it) { + int i=iimap[it]; + + if ( i >= 0 && container[i].in ) { + if ( container[i].parent!=-1 ) { + int p=container[i].parent; + cut(i,p); + cascade(p); + } + minimum=i; //As if its prio would be -infinity + pop(); + } + } + + + void decrease (Item it, PrioType const value) { + int i=iimap[it]; + container[i].prio=value; + int p=container[i].parent; + + if ( p!=-1 && comp(value, container[p].prio) ) { + cut(i,p); + cascade(p); + } + if ( comp(value, container[minimum].prio) ) minimum=i; + } + + + void increase (Item it, PrioType const value) { + erase(it); + push(it, value); + } + + + state_enum state(const Item &it) const { + int i=iimap[it]; + if( i>=0 ) { + if ( container[i].in ) i=0; + else i=-2; + } + return state_enum(i); + } + + + private: + + void balance() { + + int maxdeg=int( floor( 2.08*log(double(container.size()))))+1; + + std::vector A(maxdeg,-1); + + /* + *Recall that now minimum does not point to the minimum prio element. + *We set minimum to this during balance(). + */ + int anchor=container[minimum].left_neighbor; + int next=minimum; + bool end=false; + + do { + int active=next; + if ( anchor==active ) end=true; + int d=container[active].degree; + next=container[active].right_neighbor; + + while (A[d]!=-1) { + if( comp(container[active].prio, container[A[d]].prio) ) { + fuse(active,A[d]); + } else { + fuse(A[d],active); + active=A[d]; + } + A[d]=-1; + ++d; + } + A[d]=active; + } while ( !end ); + + + while ( container[minimum].parent >=0 ) minimum=container[minimum].parent; + int s=minimum; + int m=minimum; + do { + if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; + s=container[s].right_neighbor; + } while ( s != m ); + } + + + void makeroot (int c) { + int s=c; + do { + container[s].parent=-1; + s=container[s].right_neighbor; + } while ( s != c ); + } + + + void cut (int a, int b) { + /* + *Replacing a from the children of b. + */ + --container[b].degree; + + if ( container[b].degree !=0 ) { + int child=container[b].child; + if ( child==a ) + container[b].child=container[child].right_neighbor; + unlace(a); + } + + + /*Lacing a to the roots.*/ + int right=container[minimum].right_neighbor; + container[minimum].right_neighbor=a; + container[a].left_neighbor=minimum; + container[a].right_neighbor=right; + container[right].left_neighbor=a; + + container[a].parent=-1; + container[a].marked=false; + } + + + void cascade (int a) + { + if ( container[a].parent!=-1 ) { + int p=container[a].parent; + + if ( container[a].marked==false ) container[a].marked=true; + else { + cut(a,p); + cascade(p); + } + } + } + + + void fuse (int a, int b) { + unlace(b); + + /*Lacing b under a.*/ + container[b].parent=a; + + if (container[a].degree==0) { + container[b].left_neighbor=b; + container[b].right_neighbor=b; + container[a].child=b; + } else { + int child=container[a].child; + int last_child=container[child].left_neighbor; + container[child].left_neighbor=b; + container[b].right_neighbor=child; + container[last_child].right_neighbor=b; + container[b].left_neighbor=last_child; + } + + ++container[a].degree; + + container[b].marked=false; + } + + + /* + *It is invoked only if a has siblings. + */ + void unlace (int a) { + int leftn=container[a].left_neighbor; + int rightn=container[a].right_neighbor; + container[leftn].right_neighbor=rightn; + container[rightn].left_neighbor=leftn; + } + + + class store { + friend class FibHeap; + + Item name; + int parent; + int left_neighbor; + int right_neighbor; + int child; + int degree; + bool marked; + bool in; + PrioType prio; + + store() : parent(-1), child(-1), degree(), marked(false), in(true) {} + }; + + }; + +} //namespace hugo +#endif diff -r 483ba4ffe90a -r 45107782cbca src/work/alpar/dijkstra/dijkstra.h --- a/src/work/alpar/dijkstra/dijkstra.h Mon Mar 29 08:22:39 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,236 +0,0 @@ -// -*- C++ -*- - -/* - *template > - * - *Constructor: - * - *Dijkstra(Graph G, LengthMap length) - * - * - *Methods: - * - *void run(Node s) - * - *T dist(Node v) : After run(s) was run, it returns the distance from s to v. - * Returns T() if v is not reachable from s. - * - *Edge pred(Node v) : After run(s) was run, it returns the last - * edge of a shortest s-v path. It is INVALID for s and for - * the nodes not reachable from s. - * - *bool reached(Node v) : After run(s) was run, it is true iff v is - * reachable from s - * - */ - -#ifndef HUGO_DIJKSTRA_H -#define HUGO_DIJKSTRA_H - -///\file -///\brief Dijkstra algorithm. - -#include -#include -#include - -namespace hugo { - - //Alpar: Changed the order of the parameters - - ///%Dijkstra algorithm class. - - ///This class provides an efficient implementation of %Dijkstra algorithm. - ///The edge lengths are passed to the algorithm using a - ///\ref ReadMapSkeleton "readable map", - ///so it is easy to change it to any kind of length. - /// - ///The type of the length is determined by the \c ValueType of the length map. - /// - ///It is also possible to change the underlying priority heap. - /// - ///\param Graph The graph type the algorithm runs on. - ///\param LengthMap This read-only - ///EdgeMap - ///determines the - ///lengths of the edges. It is read once for each edge, so the map - ///may involve in relatively time consuming process to compute the edge - ///length if it is necessary. The default map type is - ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap" - ///\param Heap The heap type used by the %Dijkstra - ///algorithm. The default - ///is using \ref BinHeap "binary heap". - -#ifdef DOXYGEN - template -#else - template , - template class Heap = BinHeap > -// typename Heap=BinHeap > > -#endif - class Dijkstra{ - public: - typedef typename Graph::Node Node; - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::Edge Edge; - typedef typename Graph::OutEdgeIt OutEdgeIt; - - typedef typename LengthMap::ValueType ValueType; - typedef typename Graph::NodeMap PredMap; - typedef typename Graph::NodeMap PredNodeMap; - typedef typename Graph::NodeMap DistMap; - - private: - const Graph& G; - const LengthMap& length; - PredMap predecessor; - //In place of reach: - PredNodeMap pred_node; - DistMap distance; - //I don't like this: - // //FIXME: - // typename Graph::NodeMap reach; - // //typename Graph::NodeMap reach; - - public : - - /* - The distance of the nodes is 0. - */ - Dijkstra(Graph& _G, LengthMap& _length) : - G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { } - - void run(Node s); - - ///The distance of a node from the source. - - ///Returns the distance of a node from the source. - ///\pre \ref run() must be called before using this function. - ///\warning If node \c v in unreachable from the source the return value - ///of this funcion is undefined. - ValueType dist(Node v) const { return distance[v]; } - ///Returns the edges of the shortest path tree. - - ///For a node \c v it returns the last edge of the shortest path - ///from the source to \c v or INVALID if \c v is unreachable - ///from the source. - ///\pre \ref run() must be called before using this function. - Edge pred(Node v) const { return predecessor[v]; } - ///Returns the nodes of the shortest paths. - - ///For a node \c v it returns the last but one node of the shortest path - ///from the source to \c v or INVALID if \c v is unreachable - ///from the source. - ///\pre \ref run() must be called before using this function. - Node predNode(Node v) const { return pred_node[v]; } - - ///Returns a reference to the NodeMap of distances. - - ///\pre \ref run() must be called before using this function. - /// - const DistMap &distMap() const { return distance;} - ///Returns a reference to the shortest path tree map. - - ///Returns a reference to the NodeMap of the edges of the - ///shortest path tree. - ///\pre \ref run() must be called before using this function. - const PredMap &predMap() const { return predecessor;} - ///Returns a reference to the map of nodes of shortest paths. - - ///Returns a reference to the NodeMap of the last but one nodes of the - ///shortest paths. - ///\pre \ref run() must be called before using this function. - const PredNodeMap &predNodeMap() const { return pred_node;} - - // bool reached(Node v) { return reach[v]; } - - ///Checks if a node is reachable from the source. - - ///Returns \c true if \c v is reachable from the source. - ///\warning the source node is reported to be unreached! - ///\todo Is this what we want? - ///\pre \ref run() must be called before using this function. - /// - bool reached(Node v) { return G.valid(predecessor[v]); } - - }; - - - // ********************************************************************** - // IMPLEMENTATIONS - // ********************************************************************** - - ///Runs %Dijkstra algorithm from node the source. - - ///This method runs the %Dijkstra algorithm from a source node \c s - ///in order to - ///compute the - ///shortest path to each node. The algorithm computes - ///- The shortest path tree. - ///- The distance of each node from the source. - template class Heap > - void Dijkstra::run(Node s) { - - NodeIt u; - for ( G.first(u) ; G.valid(u) ; G.next(u) ) { - predecessor.set(u,INVALID); - pred_node.set(u,INVALID); - // If a node is unreacheable, then why should be the dist=0? - // distance.set(u,0); - // reach.set(u,false); - } - - //We don't need it at all. - // //FIXME: - // typename Graph::NodeMap scanned(G,false); - // //typename Graph::NodeMap scanned(G,false); - typename Graph::NodeMap heap_map(G,-1); - - //Heap heap(heap_map); - Heap > heap(heap_map); - - heap.push(s,0); - // reach.set(s, true); - - while ( !heap.empty() ) { - - Node v=heap.top(); - ValueType oldvalue=heap[v]; - heap.pop(); - distance.set(v, oldvalue); - - for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) { - Node w=G.head(e); - - switch(heap.state(w)) { - case heap.PRE_HEAP: - // reach.set(w,true); - heap.push(w,oldvalue+length[e]); - predecessor.set(w,e); - pred_node.set(w,v); - break; - case heap.IN_HEAP: - if ( oldvalue+length[e] < heap[w] ) { - heap.decrease(w, oldvalue+length[e]); - predecessor.set(w,e); - pred_node.set(w,v); - } - break; - case heap.POST_HEAP: - break; - } - } - } - } - -} //END OF NAMESPACE HUGO - -#endif - - diff -r 483ba4ffe90a -r 45107782cbca src/work/alpar/dijkstra/fib_heap.h --- a/src/work/alpar/dijkstra/fib_heap.h Mon Mar 29 08:22:39 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,392 +0,0 @@ -// -*- C++ -*- -/* - *template > - * - *constructors: - * - *FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare) - * - *Member functions: - * - *int size() : returns the number of elements in the heap - * - *bool empty() : true iff size()=0 - * - *void set(Item, Prio) : calls push(Item, Prio) if Item is not - * in the heap, and calls decrease/increase(Item, Prio) otherwise - * - *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item - * mustn't be in the heap. - * - *Item top() : returns the Item with least Prio. - * Must be called only if heap is nonempty. - * - *Prio prio() : returns the least Prio - * Must be called only if heap is nonempty. - * - *Prio get(Item) : returns Prio of Item - * Must be called only if Item is in heap. - * - *void pop() : deletes the Item with least Prio - * - *void erase(Item) : deletes Item from the heap if it was already there - * - *void decrease(Item, P) : decreases prio of Item to P. - * Item must be in the heap with prio at least P. - * - *void increase(Item, P) : sets prio of Item to P. - * - *state_enum state(Item) : returns PRE_HEAP if Item has not been in the - * heap until now, IN_HEAP if it is in the heap at the moment, and - * POST_HEAP otherwise. In the latter case it is possible that Item - * will get back to the heap again. - * - *In Fibonacci heaps, increase and erase are not efficient, in case of - *many calls to these operations, it is better to use bin_heap. - */ - -#ifndef FIB_HEAP_H -#define FIB_HEAP_H - -///\file -///\brief Fibonacci Heap implementation. - -#include -#include -#include - -namespace hugo { - - /// A Fibonacci Heap implementation. - template > - class FibHeap { - - typedef Prio PrioType; - - class store; - - std::vector container; - int minimum; - ItemIntMap &iimap; - Compare comp; - int num_items; - - ///\todo It is use nowhere - ///\todo It doesn't conform to the naming conventions. - public: - enum state_enum { - IN_HEAP = 0, - PRE_HEAP = -1, - POST_HEAP = -2 - }; - - public : - - FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {} - FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(), - iimap(_iimap), comp(_comp), num_items() {} - - - int size() const { - return num_items; - } - - - bool empty() const { return num_items==0; } - - - void set (Item const it, PrioType const value) { - int i=iimap[it]; - if ( i >= 0 && container[i].in ) { - if ( comp(value, container[i].prio) ) decrease(it, value); - if ( comp(container[i].prio, value) ) increase(it, value); - } else push(it, value); - } - - - void push (Item const it, PrioType const value) { - int i=iimap[it]; - if ( i < 0 ) { - int s=container.size(); - iimap.set( it, s ); - store st; - st.name=it; - container.push_back(st); - i=s; - } else { - container[i].parent=container[i].child=-1; - container[i].degree=0; - container[i].in=true; - container[i].marked=false; - } - - if ( num_items ) { - container[container[minimum].right_neighbor].left_neighbor=i; - container[i].right_neighbor=container[minimum].right_neighbor; - container[minimum].right_neighbor=i; - container[i].left_neighbor=minimum; - if ( comp( value, container[minimum].prio) ) minimum=i; - } else { - container[i].right_neighbor=container[i].left_neighbor=i; - minimum=i; - } - container[i].prio=value; - ++num_items; - } - - - Item top() const { - return container[minimum].name; - } - - - PrioType prio() const { - return container[minimum].prio; - } - - - - - PrioType& operator[](const Item& it) { - return container[iimap[it]].prio; - } - - const PrioType& operator[](const Item& it) const { - return container[iimap[it]].prio; - } - -// const PrioType get(const Item& it) const { -// return container[iimap[it]].prio; -// } - - void pop() { - /*The first case is that there are only one root.*/ - if ( container[minimum].left_neighbor==minimum ) { - container[minimum].in=false; - if ( container[minimum].degree!=0 ) { - makeroot(container[minimum].child); - minimum=container[minimum].child; - balance(); - } - } else { - int right=container[minimum].right_neighbor; - unlace(minimum); - container[minimum].in=false; - if ( container[minimum].degree > 0 ) { - int left=container[minimum].left_neighbor; - int child=container[minimum].child; - int last_child=container[child].left_neighbor; - - makeroot(child); - - container[left].right_neighbor=child; - container[child].left_neighbor=left; - container[right].left_neighbor=last_child; - container[last_child].right_neighbor=right; - } - minimum=right; - balance(); - } // the case where there are more roots - --num_items; - } - - - void erase (const Item& it) { - int i=iimap[it]; - - if ( i >= 0 && container[i].in ) { - if ( container[i].parent!=-1 ) { - int p=container[i].parent; - cut(i,p); - cascade(p); - } - minimum=i; //As if its prio would be -infinity - pop(); - } - } - - - void decrease (Item it, PrioType const value) { - int i=iimap[it]; - container[i].prio=value; - int p=container[i].parent; - - if ( p!=-1 && comp(value, container[p].prio) ) { - cut(i,p); - cascade(p); - } - if ( comp(value, container[minimum].prio) ) minimum=i; - } - - - void increase (Item it, PrioType const value) { - erase(it); - push(it, value); - } - - - state_enum state(const Item &it) const { - int i=iimap[it]; - if( i>=0 ) { - if ( container[i].in ) i=0; - else i=-2; - } - return state_enum(i); - } - - - private: - - void balance() { - - int maxdeg=int( floor( 2.08*log(double(container.size()))))+1; - - std::vector A(maxdeg,-1); - - /* - *Recall that now minimum does not point to the minimum prio element. - *We set minimum to this during balance(). - */ - int anchor=container[minimum].left_neighbor; - int next=minimum; - bool end=false; - - do { - int active=next; - if ( anchor==active ) end=true; - int d=container[active].degree; - next=container[active].right_neighbor; - - while (A[d]!=-1) { - if( comp(container[active].prio, container[A[d]].prio) ) { - fuse(active,A[d]); - } else { - fuse(A[d],active); - active=A[d]; - } - A[d]=-1; - ++d; - } - A[d]=active; - } while ( !end ); - - - while ( container[minimum].parent >=0 ) minimum=container[minimum].parent; - int s=minimum; - int m=minimum; - do { - if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; - s=container[s].right_neighbor; - } while ( s != m ); - } - - - void makeroot (int c) { - int s=c; - do { - container[s].parent=-1; - s=container[s].right_neighbor; - } while ( s != c ); - } - - - void cut (int a, int b) { - /* - *Replacing a from the children of b. - */ - --container[b].degree; - - if ( container[b].degree !=0 ) { - int child=container[b].child; - if ( child==a ) - container[b].child=container[child].right_neighbor; - unlace(a); - } - - - /*Lacing a to the roots.*/ - int right=container[minimum].right_neighbor; - container[minimum].right_neighbor=a; - container[a].left_neighbor=minimum; - container[a].right_neighbor=right; - container[right].left_neighbor=a; - - container[a].parent=-1; - container[a].marked=false; - } - - - void cascade (int a) - { - if ( container[a].parent!=-1 ) { - int p=container[a].parent; - - if ( container[a].marked==false ) container[a].marked=true; - else { - cut(a,p); - cascade(p); - } - } - } - - - void fuse (int a, int b) { - unlace(b); - - /*Lacing b under a.*/ - container[b].parent=a; - - if (container[a].degree==0) { - container[b].left_neighbor=b; - container[b].right_neighbor=b; - container[a].child=b; - } else { - int child=container[a].child; - int last_child=container[child].left_neighbor; - container[child].left_neighbor=b; - container[b].right_neighbor=child; - container[last_child].right_neighbor=b; - container[b].left_neighbor=last_child; - } - - ++container[a].degree; - - container[b].marked=false; - } - - - /* - *It is invoked only if a has siblings. - */ - void unlace (int a) { - int leftn=container[a].left_neighbor; - int rightn=container[a].right_neighbor; - container[leftn].right_neighbor=rightn; - container[rightn].left_neighbor=leftn; - } - - - class store { - friend class FibHeap; - - Item name; - int parent; - int left_neighbor; - int right_neighbor; - int child; - int degree; - bool marked; - bool in; - PrioType prio; - - store() : parent(-1), child(-1), degree(), marked(false), in(true) {} - }; - - }; - -} //namespace hugo -#endif