[Lemon-commits] [lemon_svn] alpar: r318 - hugo/trunk/src/work/alpar/dijkstra

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:38:42 CET 2006


Author: alpar
Date: Sat Mar 20 22:38:16 2004
New Revision: 318

Added:
   hugo/trunk/src/work/alpar/dijkstra/
   hugo/trunk/src/work/alpar/dijkstra/bin_heap.hh
   hugo/trunk/src/work/alpar/dijkstra/dijkstra.cc
   hugo/trunk/src/work/alpar/dijkstra/dijkstra.h
   hugo/trunk/src/work/alpar/dijkstra/fib_heap.h
   hugo/trunk/src/work/alpar/dijkstra/makefile

Log:
Aprosagok...


Added: hugo/trunk/src/work/alpar/dijkstra/bin_heap.hh
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/alpar/dijkstra/bin_heap.hh	Sat Mar 20 22:38:16 2004
@@ -0,0 +1,235 @@
+/* FIXME: Copyright ... 
+ *
+ * This implementation is heavily based on STL's heap functions and
+ * the similar class by Alpar Juttner in IKTA...
+ */
+
+/******
+ *
+ * BinHeap<KeyType, ValueType, KeyIntMap, [ValueCompare]>
+ *
+ * Ez az osztaly kulcs-ertek parok tarolasara alkalmas binaris kupacot
+ * valosit meg.
+ * A kupacban legfolul mindig az a par talalhato, amiben az _ertek_ a
+ * legkisebb. (Gondolj a Dijkstra pont-tavolsag kupacara; igazabol ahhoz
+ * lett keszitve...)
+ *
+ * Megjegyzes: egy kicsit gyanus nekem, hogy a kupacos temakorben nem
+ * azt hivjak kulcsnak, amit most en annak nevezek. :) En olyan 
+ * property_map -os ertelemben hasznalom.
+ *
+ * A hasznalatahoz szukseg van egy irhato/olvashato property_map-re, ami
+ * a kulcsokhoz egy int-et tud tarolni (ezzel tudom megkeresni az illeto
+ * elemet a kupacban a csokkentes es hasonlo muveletekhez).
+ * A map-re csak referenciat tarol, ugy hogy a kupac elete folyan a map-nek
+ * is elnie kell. (???)
+ *
+ * Ketfele modon hasznalhato:
+ * Lusta mod:
+ * put(Key, Value) metodussal pakolunk a kupacba,
+ * aztan o majd eldonti, hogy ez az elem mar benne van-e es ha igen, akkor
+ * csokkentettunk-e rajta, vagy noveltunk.
+ * Ehhez nagyon fontos, hogy az atadott property map inicializalva legyen
+ * minden szobajovo kulcs ertekre, -1 -es ertekkel!
+ * Es ilyen esetben a kulcsokrol lekerdezheto az allapotuk a state metodussal:
+ * (nem jart meg a kupacban PRE_HEAP=-1, epp a kupacban van IN_HEAP=0,
+ *  mar kikerult a kupacbol POST_HEAP=-2).
+ * Szoval ebben a modban a kupac nagyjabol hasznalhato property_map-kent, csak
+ * meg meg tudja mondani a "legkisebb" erteku elemet. De csak nagyjabol,
+ * hiszen a kupacbol kikerult elemeknek elvesz az ertekuk...
+ *
+ * Kozvetlen mod:
+ * push(Key, Value) metodussal belerakunk a kupacba (ha az illeto kulcs mar
+ * benn volt, akkor gaz).
+ * increase/decrease(Key k, Value new_value) metodusokkal lehet
+ * novelni/csokkenteni az illeto kulcshoz tartozo erteket. (Ha nem volt meg
+ * benne a kupacban az illeto kulcs, vagy nem abba az iranyba valtoztattad
+ * az erteket, amerre mondtad -- gaz).
+ *
+ * Termeszetesen a fenti ket modot ertelemszeruen lehet keverni.
+ * Ja es mindig nagyon gaz, ha belepiszkalsz a map-be, amit a kupac
+ * hasznal. :-))
+ *
+ *
+ * Bocs, most faradt vagyok, majd egyszer leforditom. (Misi)
+ *
+ */
+
+
+#ifndef BIN_HEAP_HH
+#define BIN_HEAP_HH
+
+#include <vector>
+#include <utility>
+#include <functional>
+
+namespace hugo {
+
+  template <typename Key, typename Val, typename KeyIntMap,
+	    typename Compare = std::less<Val> >
+  class BinHeap {
+
+  public:
+    typedef Key	             KeyType;
+    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
+    typedef Val              ValueType;
+    typedef std::pair<KeyType,ValueType>     PairType;
+    typedef KeyIntMap        KeyIntMapType;
+    typedef Compare          ValueCompare;
+
+    /**
+     * Each Key element have a state associated to it. It may be "in heap",
+     * "pre heap" or "post heap". The later two are indifferent from the
+     * heap's point of view, but may be useful to the user.
+     *
+     * The KeyIntMap _should_ be initialized in such way, that it maps
+     * PRE_HEAP (-1) to any element to be put in the heap...
+     */
+    ///\todo it is used nowhere
+    ///
+    enum state_enum {
+      IN_HEAP = 0,
+      PRE_HEAP = -1,
+      POST_HEAP = -2
+    };
+
+  private:
+    std::vector<PairType> data;
+    Compare comp;
+    // FIXME: jo ez igy???
+    KeyIntMap &kim;
+
+  public:
+    BinHeap(KeyIntMap &_kim) : kim(_kim) {}
+    BinHeap(KeyIntMap &_kim, const Compare &_comp) : comp(_comp), kim(_kim) {}
+
+
+    int size() const { return data.size(); }
+    bool empty() const { return data.empty(); }
+
+  private:
+    static int parent(int i) { return (i-1)/2; }
+    static int second_child(int i) { return 2*i+2; }
+    bool less(const PairType &p1, const PairType &p2) {
+      return comp(p1.second, p2.second);
+    }
+
+    int bubble_up(int hole, PairType p);
+    int bubble_down(int hole, PairType p, int length);
+
+    void move(const PairType &p, int i) {
+      data[i] = p;
+      kim.set(p.first, i);
+    }
+
+    void rmidx(int h) {
+      int n = data.size()-1;
+      if( h>=0 && h<=n ) {
+	kim.set(data[h].first, POST_HEAP);
+	if ( h<n ) {
+	  bubble_down(h, data[n], n);
+	}
+	data.pop_back();
+      }
+    }
+
+  public:
+    void push(const PairType &p) {
+      int n = data.size();
+      data.resize(n+1);
+      bubble_up(n, p);
+    }
+    void push(const Key &k, const Val &v) { push(PairType(k,v)); }
+
+    Key top() const {
+      // FIXME: test size>0 ?
+      return data[0].first;
+    }
+    Val topValue() const {
+      // FIXME: test size>0 ?
+      return data[0].second;
+    }
+
+    void pop() {
+      rmidx(0);
+    }
+
+    void erase(const Key &k) {
+      rmidx(kim[k]);
+    }
+
+    Val operator[](const Key &k) const {
+      int idx = kim[k];
+      return data[idx].second;
+    }
+    
+    void put(const Key &k, const Val &v) {
+      int idx = kim[k];
+      if( idx < 0 ) {
+	push(k,v);
+      }
+      else if( comp(v, data[idx].second) ) {
+	bubble_up(idx, PairType(k,v));
+      }
+      else {
+	bubble_down(idx, PairType(k,v), data.size());
+      }
+    }
+
+    void decrease(const Key &k, const Val &v) {
+      int idx = kim[k];
+      bubble_up(idx, PairType(k,v));
+    }
+    void increase(const Key &k, const Val &v) {
+      int idx = kim[k];
+      bubble_down(idx, PairType(k,v), data.size());
+    }
+
+    state_enum state(const Key &k) const {
+      int s = kim[k];
+      if( s>=0 )
+	s=0;
+      return state_enum(s);
+    }
+
+  }; // class BinHeap
+
+  
+  template <typename K, typename V, typename M, typename C>
+  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
+    int par = parent(hole);
+    while( hole>0 && less(p,data[par]) ) {
+      move(data[par],hole);
+      hole = par;
+      par = parent(hole);
+    }
+    move(p, hole);
+    return hole;
+  }
+
+  template <typename K, typename V, typename M, typename C>
+  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
+    int child = second_child(hole);
+    while(child < length) {
+      if( less(data[child-1], data[child]) ) {
+	--child;
+      }
+      if( !less(data[child], p) )
+	goto ok;
+      move(data[child], hole);
+      hole = child;
+      child = second_child(hole);
+    }
+    child--;
+    if( child<length && less(data[child], p) ) {
+      move(data[child], hole);
+      hole=child;
+    }
+  ok:
+    move(p, hole);
+    return hole;
+  }
+
+} // namespace hugo
+
+#endif // BIN_HEAP_HH

Added: hugo/trunk/src/work/alpar/dijkstra/dijkstra.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/alpar/dijkstra/dijkstra.cc	Sat Mar 20 22:38:16 2004
@@ -0,0 +1,114 @@
+#include <iostream>
+#include <fstream>
+
+#include <smart_graph.h>
+#include <list_graph.h>
+#include <dimacs.h>
+#include <dijkstra.h>
+#include <time_measure.h>
+
+#include <bin_heap.hh>
+#include <fib_heap.h>
+
+using namespace hugo;
+
+int main(int, char **) {
+  typedef SmartGraph::Node Node;
+  typedef SmartGraph::NodeIt NodeIt;
+  typedef SmartGraph::InEdgeIt InEdgeIt; 
+
+  SmartGraph G;
+  Node s, t;
+  SmartGraph::EdgeMap<int> cap(G);
+  Timer tim;
+  std::cout << "DIMACS load ..." << std::endl;
+  readDimacsMaxFlow(std::cin, G, s, t, cap);
+  std::cout << "               " << tim <<std::endl;
+
+  std::cout << "dijkstra demo ..." << std::endl;
+  
+  //double pre_time=currTime();
+  tim.reset();
+  Dijkstra <SmartGraph,
+    SmartGraph::EdgeMap<int>,
+    FibHeap<SmartGraph::Node, int, SmartGraph::NodeMap<int> >
+    > dijkstra_test(G, cap); 
+  
+  dijkstra_test.run(s);
+  //double post_time=currTime();
+  
+  std::cout << "running time with fib_heap: " 
+    // << post_time-pre_time << " sec"
+	    << tim
+	    << std::endl; 
+ 
+  //pre_time=currTime();
+  tim.reset();
+  Dijkstra < SmartGraph,
+    SmartGraph::EdgeMap<int>,
+    BinHeap<SmartGraph::Node, int, SmartGraph::NodeMap<int> > > 
+    dijkstra_test2(G, cap);
+  
+  dijkstra_test2.run(s);
+  //post_time=currTime();
+  
+  std::cout << "running time with bin_heap: " 
+    //    << post_time-pre_time << " sec"
+	    << tim
+	    << std::endl; 
+  
+
+  int hiba_fib=0;
+  int hiba_bin=0;
+  NodeIt u;
+  for ( G.first(u) ; G.valid(u); G.next(u) ) {
+    InEdgeIt e;
+    for ( G.first(e,u); G.valid(e); G.next(e) ) {
+      Node v=G.tail(e);
+      if ( dijkstra_test.dist(u) - dijkstra_test.dist(v) > cap[e] )
+	{
+	  std::cout<<"Hibas el a fibonaccis Dijkstraban: " 
+		   << dijkstra_test.dist(u) - dijkstra_test.dist(v) - 
+	    cap[e]<<std::endl;
+	  ++hiba_fib;
+	}
+      if ( dijkstra_test2.dist(u) - dijkstra_test2.dist(v) > cap[e] )
+	{
+	  std::cout<<"Hibas el a binarisos Dijkstraban: " 
+		   << dijkstra_test2.dist(u) - dijkstra_test2.dist(v) - 
+	    cap[e]<<std::endl;
+	  ++hiba_bin;
+	}
+      if ( e==dijkstra_test.pred(u) && 
+	   dijkstra_test.dist(u) - dijkstra_test.dist(v) != cap[e] )
+	{
+	  std::cout<<"Hibas fael a fibonaccis Dijkstraban: "<<
+	    dijkstra_test.dist(u) - dijkstra_test.dist(v)- cap[e]<<std::endl;
+	  ++hiba_fib;
+	}
+      if ( e==dijkstra_test2.pred(u) && 
+	   dijkstra_test2.dist(u) - dijkstra_test2.dist(v) != cap[e] )
+	{
+	  std::cout<<"Hibas fael a binarisos Dijkstraban: "<<
+	    dijkstra_test2.dist(u) - dijkstra_test2.dist(v)- cap[e]<<std::endl;
+	  ++hiba_bin;
+	}
+    }
+ 
+    if ( dijkstra_test.dist(u) != dijkstra_test2.dist(u) ) 
+      std::cout << "Nem egyezik meg a tavolsag!"<<std::endl;
+
+
+ }
+
+  std::cout << "Hibas elek szama a fibonaccis Dijkstraban: " 
+	    << hiba_fib << " a " << G.edgeNum() <<"-bol."<< std::endl;
+  
+  std::cout << "Hibas elek szama a binarisos Dijkstraban: " 
+	    << hiba_bin << " a " << G.edgeNum() <<"-bol."<< std::endl;
+  
+
+
+
+  return 0;
+}

Added: hugo/trunk/src/work/alpar/dijkstra/dijkstra.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/alpar/dijkstra/dijkstra.h	Sat Mar 20 22:38:16 2004
@@ -0,0 +1,150 @@
+// -*- C++ -*-
+/* 
+ *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> >
+ *
+ *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
+
+#include <fib_heap.h>
+#include <invalid.h>
+
+namespace hugo {
+  
+  //Alpar: Changed the order of the parameters
+  template <typename Graph,
+	    typename LengthMap=typename Graph::EdgeMap<int>,
+	    typename Heap=FibHeap<typename Graph::Node,
+				  typename LengthMap::ValueType, 
+				  typename Graph::NodeMap<int> > >
+  class Dijkstra{
+  public:
+    typedef typename LengthMap::ValueType ValueType;
+
+  private:
+    typedef typename Graph::Node Node;
+    typedef typename Graph::NodeIt NodeIt;
+    typedef typename Graph::Edge Edge;
+    typedef typename Graph::OutEdgeIt OutEdgeIt;
+    
+    const Graph& G;
+    const LengthMap& length;
+    typedef typename Graph::NodeMap<Edge> PredMap;
+    PredMap predecessor;
+    //In place of reach:
+    typedef typename Graph::NodeMap<Node> PredNodeMap;
+    PredNodeMap pred_node;
+    typedef typename Graph::NodeMap<ValueType> DistMap;
+    DistMap distance;
+    //I don't like this:
+    //     //FIXME:
+    //     typename Graph::NodeMap<bool> reach;
+    //     //typename Graph::NodeMap<int> 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);
+    
+    ValueType dist(Node v) const { return distance[v]; }
+    Edge pred(Node v) const { return predecessor[v]; }
+    Node predNode(Node v) const { return pred_node[v]; }
+    
+    const DistMap &distMap() const { return distance;}
+    const PredMap &predMap() const { return predecessor;}
+    const PredNodeMap &predNodeMap() const { return pred_node;}
+
+    //    bool reached(Node v) { return reach[v]; }
+    ///\warning \c s is not reached!
+    ///
+    bool reached(Node v) { return G.valid(predecessor[v]); }
+    
+  };
+  
+
+  // IMPLEMENTATIONS
+
+  template <typename Graph, typename LengthMap, typename Heap >
+  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
+    
+    NodeIt u;
+    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
+      predecessor.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<bool> scanned(G,false);
+    //     //typename Graph::NodeMap<int> scanned(G,false);
+    typename Graph::NodeMap<int> heap_map(G,-1);
+    
+    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
+
+

Added: hugo/trunk/src/work/alpar/dijkstra/fib_heap.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/alpar/dijkstra/fib_heap.h	Sat Mar 20 22:38:16 2004
@@ -0,0 +1,389 @@
+// -*- C++ -*-
+/*
+ *template <typename Item, 
+ *          typename Prio, 
+ *          typename ItemIntMap, 
+ *          typename Compare = std::less<Prio> >
+ * 
+ *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
+
+#include <vector>
+#include <functional>
+#include <math.h>
+
+namespace hugo {
+  
+  template <typename Item, typename Prio, typename ItemIntMap, 
+    typename Compare = std::less<Prio> >
+ 
+  class FibHeap {
+  
+    typedef Prio PrioType;
+    
+    class store;
+    
+    std::vector<store> container;
+    int minimum;
+    ItemIntMap &iimap;
+    Compare comp;
+    int num_items;
+
+    ///\todo It is use nowhere
+    ///\todo It doesn't conforms 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<int> 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 

Added: hugo/trunk/src/work/alpar/dijkstra/makefile
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/alpar/dijkstra/makefile	Sat Mar 20 22:38:16 2004
@@ -0,0 +1,19 @@
+CXX3 := $(shell type -p g++-3.3 || type -p g++-3.2 || type -p g++-3.0 || type -p g++-3 || echo g++)
+CXX2 = g++-2.95
+CXXFLAGS = -W -Wall -ansi -pedantic
+LEDAROOT ?= /ledasrc/LEDA-4.1
+
+BINARIES = dijkstra prim preflow
+
+all: $(BINARIES)
+
+makefile: .depend
+sinclude .depend
+
+dijkstra: 
+	$(CXX3) $(CXXFLAGS) -O3 -I. -I../../jacint -I../.. -I../../marci -I../../alpar  -o dijkstra dijkstra.cc
+
+clean:
+	$(RM) *.o $(BINARIES) .depend
+
+.PHONY: all clean dep depend



More information about the Lemon-commits mailing list