COIN-OR::LEMON - Graph Library

Ticket #431: 23a4fa3a7e62.patch

File 23a4fa3a7e62.patch, 3.9 KB (added by Alpar Juttner, 12 years ago)
  • lemon/circulation.h

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1323499932 -3600
    # Node ID 23a4fa3a7e62e25da2995751932d62fc7fb8884e
    # Parent  9a716871028e3ccf95f14b603956f1f7ff03892a
    Remember the lastly evaluated arcs in Circulation (#431)
    
    diff --git a/lemon/circulation.h b/lemon/circulation.h
    a b  
    236236    typedef typename Digraph::template NodeMap<Value> ExcessMap;
    237237    ExcessMap* _excess;
    238238
     239    typename Digraph::template NodeMap<Arc> _next_arc;
     240 
    239241    Tolerance _tol;
    240242    int _el;
    241243
     
    344346                const UpperMap &upper, const SupplyMap &supply)
    345347      : _g(graph), _lo(&lower), _up(&upper), _supply(&supply),
    346348        _flow(NULL), _local_flow(false), _level(NULL), _local_level(false),
    347         _excess(NULL) {}
     349        _excess(NULL), _next_arc(graph) {}
    348350
    349351    /// Destructor.
    350352    ~Circulation() {
     
    574576      Node act;
    575577      Node bact=INVALID;
    576578      Node last_activated=INVALID;
     579
     580      for(NodeIt n(_g);n!=INVALID;++n)
     581        if((_next_arc[n]=OutArcIt(_g,n))==INVALID)
     582          _next_arc[n]=InArcIt(_g,n);
     583     
    577584      while((act=_level->highestActive())!=INVALID) {
    578585        int actlevel=(*_level)[act];
    579         int mlevel=_node_num;
    580586        Value exc=(*_excess)[act];
    581587
    582         for(OutArcIt e(_g,act);e!=INVALID; ++e) {
     588        Arc next_a = _next_arc[act];
     589        if(next_a == INVALID) goto next_l;
     590        if (_g.source(next_a)!=act)
     591          goto next_in;
     592        for(OutArcIt e(_g,next_a);e!=INVALID; ++e) {
    583593          Node v = _g.target(e);
    584594          Value fc=(*_up)[e]-(*_flow)[e];
    585595          if(!_tol.positive(fc)) continue;
     
    591601                _level->activate(v);
    592602              (*_excess)[act] = 0;
    593603              _level->deactivate(act);
     604              _next_arc[act]=e;
    594605              goto next_l;
    595606            }
    596607            else {
     
    601612              exc-=fc;
    602613            }
    603614          }
    604           else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
    605615        }
    606         for(InArcIt e(_g,act);e!=INVALID; ++e) {
     616        next_a = InArcIt(_g,act);
     617      next_in:
     618        for(InArcIt e(_g,next_a);e!=INVALID; ++e) {
    607619          Node v = _g.source(e);
    608620          Value fc=(*_flow)[e]-(*_lo)[e];
    609621          if(!_tol.positive(fc)) continue;
     
    615627                _level->activate(v);
    616628              (*_excess)[act] = 0;
    617629              _level->deactivate(act);
     630              _next_arc[act]=e;
    618631              goto next_l;
    619632            }
    620633            else {
     
    625638              exc-=fc;
    626639            }
    627640          }
    628           else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
    629641        }
    630642
     643        if((_next_arc[act]=OutArcIt(_g,act))==INVALID)
     644          _next_arc[act]=InArcIt(_g,act);
     645
    631646        (*_excess)[act] = exc;
    632647        if(!_tol.positive(exc)) _level->deactivate(act);
    633         else if(mlevel==_node_num) {
    634           _level->liftHighestActiveToTop();
    635           _el = _node_num;
    636           return false;
    637         }
    638648        else {
    639           _level->liftHighestActive(mlevel+1);
    640           if(_level->onLevel(actlevel)==0) {
    641             _el = actlevel;
     649          int mlevel=_node_num;
     650          Node rn;
     651          for(OutArcIt e(_g,act);e!=INVALID; ++e)
     652            if(_tol.positive((*_up)[e]-(*_flow)[e]) &&
     653               (*_level)[rn = _g.runningNode(e)]<mlevel)
     654              mlevel=(*_level)[rn];
     655          for(InArcIt e(_g,act);e!=INVALID; ++e)
     656            if(_tol.positive((*_flow)[e]-(*_lo)[e]) &&
     657               (*_level)[rn = _g.runningNode(e)]<mlevel)
     658              mlevel=(*_level)[rn];
     659
     660          if(mlevel==_node_num) {
     661            _level->liftHighestActiveToTop();
     662            _el = _node_num;
    642663            return false;
    643664          }
     665          else {
     666            _level->liftHighestActive(mlevel+1);
     667            if(_level->onLevel(actlevel)==0) {
     668              _el = actlevel;
     669              return false;
     670            }
     671          }
    644672        }
    645673      next_l:
    646674        ;