lemon/karp.h
changeset 767 11c946fa8d13
parent 765 3b544a9c92db
child 768 0a42883c8221
     1.1 --- a/lemon/karp.h	Tue Aug 11 21:53:39 2009 +0200
     1.2 +++ b/lemon/karp.h	Tue Aug 11 22:52:35 2009 +0200
     1.3 @@ -148,11 +148,10 @@
     1.4      // Data sturcture for path data
     1.5      struct PathData
     1.6      {
     1.7 -      bool found;
     1.8        LargeValue dist;
     1.9        Arc pred;
    1.10 -      PathData(bool f = false, LargeValue d = 0, Arc p = INVALID) :
    1.11 -        found(f), dist(d), pred(p) {}
    1.12 +      PathData(LargeValue d, Arc p = INVALID) :
    1.13 +        dist(d), pred(p) {}
    1.14      };
    1.15  
    1.16      typedef typename Digraph::template NodeMap<std::vector<PathData> >
    1.17 @@ -186,6 +185,9 @@
    1.18      std::vector<Node> _process;
    1.19  
    1.20      Tolerance _tolerance;
    1.21 +    
    1.22 +    // Infinite constant
    1.23 +    const LargeValue INF;
    1.24  
    1.25    public:
    1.26  
    1.27 @@ -241,7 +243,10 @@
    1.28            const LengthMap &length ) :
    1.29        _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
    1.30        _cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
    1.31 -      _cycle_path(NULL), _local_path(false), _data(digraph)
    1.32 +      _cycle_path(NULL), _local_path(false), _data(digraph),
    1.33 +      INF(std::numeric_limits<LargeValue>::has_infinity ?
    1.34 +          std::numeric_limits<LargeValue>::infinity() :
    1.35 +          std::numeric_limits<LargeValue>::max())
    1.36      {}
    1.37  
    1.38      /// Destructor.
    1.39 @@ -458,7 +463,7 @@
    1.40          return false;
    1.41        }      
    1.42        for (int i = 0; i < n; ++i) {
    1.43 -        _data[(*_nodes)[i]].resize(n + 1);
    1.44 +        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
    1.45        }
    1.46        return true;
    1.47      }
    1.48 @@ -468,7 +473,7 @@
    1.49      // node to node v containing exactly k arcs.
    1.50      void processRounds() {
    1.51        Node start = (*_nodes)[0];
    1.52 -      _data[start][0] = PathData(true, 0);
    1.53 +      _data[start][0] = PathData(0);
    1.54        _process.clear();
    1.55        _process.push_back(start);
    1.56  
    1.57 @@ -493,12 +498,9 @@
    1.58            e = _out_arcs[u][j];
    1.59            v = _gr.target(e);
    1.60            d = _data[u][k-1].dist + _length[e];
    1.61 -          if (!_data[v][k].found) {
    1.62 -            next.push_back(v);
    1.63 -            _data[v][k] = PathData(true, _data[u][k-1].dist + _length[e], e);
    1.64 -          }
    1.65 -          else if (_tolerance.less(d, _data[v][k].dist)) {
    1.66 -            _data[v][k] = PathData(true, d, e);
    1.67 +          if (_tolerance.less(d, _data[v][k].dist)) {
    1.68 +            if (_data[v][k].dist == INF) next.push_back(v);
    1.69 +            _data[v][k] = PathData(d, e);
    1.70            }
    1.71          }
    1.72        }
    1.73 @@ -516,8 +518,8 @@
    1.74            e = _out_arcs[u][j];
    1.75            v = _gr.target(e);
    1.76            d = _data[u][k-1].dist + _length[e];
    1.77 -          if (!_data[v][k].found || _tolerance.less(d, _data[v][k].dist)) {
    1.78 -            _data[v][k] = PathData(true, d, e);
    1.79 +          if (_tolerance.less(d, _data[v][k].dist)) {
    1.80 +            _data[v][k] = PathData(d, e);
    1.81            }
    1.82          }
    1.83        }
    1.84 @@ -528,12 +530,12 @@
    1.85        int n = _nodes->size();
    1.86        for (int i = 0; i < n; ++i) {
    1.87          Node u = (*_nodes)[i];
    1.88 -        if (!_data[u][n].found) continue;
    1.89 +        if (_data[u][n].dist == INF) continue;
    1.90          LargeValue length, max_length = 0;
    1.91          int size, max_size = 1;
    1.92          bool found_curr = false;
    1.93          for (int k = 0; k < n; ++k) {
    1.94 -          if (!_data[u][k].found) continue;
    1.95 +          if (_data[u][k].dist == INF) continue;
    1.96            length = _data[u][n].dist - _data[u][k].dist;
    1.97            size = n - k;
    1.98            if (!found_curr || length * max_size > max_length * size) {