COIN-OR::LEMON - Graph Library

Ticket #377: smaller_static_graph.patch

File smaller_static_graph.patch, 3.2 KB (added by Gabriel Gouvine, 8 years ago)

Patch for a slightly smaller StaticDigraph?: 2n + 3m instead of 2n + 4m

  • lemon/static_graph.h

    # HG changeset patch
    # User Gabriel Gouvine <gabriel.gouvine.GIT@gmx.com>
    # Date 1488481030 -3600
    #      Thu Mar 02 19:57:10 2017 +0100
    # Node ID b127b5408042aebd5f559a888d807fa4e596d61e
    # Parent  fd04b9f2058dc6979eb27fe01835b9e5c38e9034
    Remove arc_next_out from StaticDigraphBase
    
    * Was only used in DigraphExtender::OutArcIt, overwritten in StaticDigraph
    * Space complexity is now 2n + 3m (from 2n + 4m)
    * nextOut(Arc &) redefined for compatibility
    
    diff -r fd04b9f2058d -r b127b5408042 lemon/static_graph.h
    a b  
    3636      : built(false), node_num(0), arc_num(0),
    3737        node_first_out(NULL), node_first_in(NULL),
    3838        arc_source(NULL), arc_target(NULL),
    39         arc_next_in(NULL), arc_next_out(NULL) {}
     39        arc_next_in(NULL) {}
    4040
    4141    ~StaticDigraphBase() {
    4242      if (built) {
     
    4444        delete[] node_first_in;
    4545        delete[] arc_source;
    4646        delete[] arc_target;
    47         delete[] arc_next_out;
    4847        delete[] arc_next_in;
    4948      }
    5049    }
     
    8887      e.id = node_first_out[n.id] != node_first_out[n.id + 1] ?
    8988        node_first_out[n.id] : -1;
    9089    }
    91     void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
     90    void nextOut(Arc& e) const {
     91      Node n = source(e);
     92      Arc last;
     93      fastLastOut(last, n);
     94      ++e.id;
     95      if (e == last) e.id = -1;
     96    }
    9297
    9398    void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
    9499    void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
     
    135140        delete[] node_first_in;
    136141        delete[] arc_source;
    137142        delete[] arc_target;
    138         delete[] arc_next_out;
    139143        delete[] arc_next_in;
    140144      }
    141145      built = false;
     
    158162
    159163      arc_source = new int[arc_num];
    160164      arc_target = new int[arc_num];
    161       arc_next_out = new int[arc_num];
    162165      arc_next_in = new int[arc_num];
    163166
    164167      int node_index = 0;
     
    188191            arc_target[arc_index] = target;
    189192            arc_next_in[arc_index] = node_first_in[target];
    190193            node_first_in[target] = arc_index;
    191             arc_next_out[arc_index] = arc_index + 1;
    192194            ++arc_index;
    193195          }
    194           arc_next_out[arc_index - 1] = -1;
    195196        } else {
    196197          node_first_out[source] = arc_index;
    197198        }
     
    211212
    212213      arc_source = new int[arc_num];
    213214      arc_target = new int[arc_num];
    214       arc_next_out = new int[arc_num];
    215215      arc_next_in = new int[arc_num];
    216216
    217217      for (int i = 0; i != node_num; ++i) {
     
    229229          arc_target[arc_index] = j;
    230230          arc_next_in[arc_index] = node_first_in[j];
    231231          node_first_in[j] = arc_index;
    232           arc_next_out[arc_index] = arc_index + 1;
    233232          ++arc_index;
    234233        }
    235         if (arc_index > node_first_out[i])
    236           arc_next_out[arc_index - 1] = -1;
    237234      }
    238235      LEMON_ASSERT(first == last,
    239236        "Wrong arc list for StaticDigraph::build()");
     
    262259    int *arc_source;
    263260    int *arc_target;
    264261    int *arc_next_in;
    265     int *arc_next_out;
    266262  };
    267263
    268264  typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;