COIN-OR::LEMON - Graph Library

Ticket #68: 68-6-2d82f9560e1a.patch

File 68-6-2d82f9560e1a.patch, 4.7 KB (added by Peter Kovacs, 15 years ago)
  • lemon/static_graph.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1251235706 -7200
    # Node ID 2d82f9560e1a2578c9d768c7962a0aa018ce6dae
    # Parent  6959186d3612eee5b995e5f2073ed9409fd7c5ce
    Add a new build() function to StaticDigraph (#68)
    
    This function builds the digraph from an arc list that
    contains pairs of integer indices from the range [0..n-1].
    It is useful in the cases when you would like to build a
    StaticDigraph from scratch, i.e. you do not want to build
    another digraph that can be copied using the other build()
    function.
    
    diff --git a/lemon/static_graph.h b/lemon/static_graph.h
    a b  
    194194      }
    195195      node_first_out[node_num] = arc_num;
    196196    }
     197   
     198    template <typename ArcList>
     199    void build(int n, const ArcList& arcs) {
     200      built = true;
     201
     202      node_num = n;
     203      arc_num = arcs.size();
     204
     205      node_first_out = new int[node_num + 1];
     206      node_first_in = new int[node_num];
     207
     208      arc_source = new int[arc_num];
     209      arc_target = new int[arc_num];
     210      arc_next_in = new int[arc_num];
     211     
     212      for (int i = 0; i != node_num; ++i) {
     213        node_first_in[i] = -1;
     214      }     
     215     
     216      int arc_index = 0;
     217      typename ArcList::const_iterator it = arcs.begin();
     218      for (int i = 0; i != node_num; ++i) {
     219        node_first_out[i] = arc_index;
     220        for ( ; it != arcs.end() && (*it).first == i; ++it) {
     221          int j = (*it).second;
     222          LEMON_ASSERT(j >= 0 && j < node_num,
     223            "Wrong arc list for StaticDigraph::build()");
     224          arc_source[arc_index] = i;
     225          arc_target[arc_index] = j;
     226          arc_next_in[arc_index] = node_first_in[j];
     227          node_first_in[j] = arc_index;
     228          ++arc_index;
     229        }
     230      }
     231      LEMON_ASSERT(it == arcs.end(),
     232        "Wrong arc list for StaticDigraph::build()");
     233      node_first_out[node_num] = arc_num;
     234    }
    197235
    198236  protected:
    199237
     
    324362      Parent::build(digraph, nodeRef, arcRef);
    325363    }
    326364 
     365    /// \brief Build the digraph from an arc list.
     366    ///
     367    /// This function builds the digraph from the given arc list.
     368    /// It can be called more than once, but in such case, the whole
     369    /// structure and all maps will be cleared and rebuilt.
     370    ///
     371    /// \param n The number of nodes.
     372    /// \param arcs The arc list. It must be an STL compatible container
     373    /// with <tt>std::pair<int,int></tt> as its \c value_type.
     374    /// So each arc must be specified by a pair of integer indices
     375    /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
     376    /// non-decreasing order with respect to their first values.</i>
     377    /// If the k-th pair in the list is <tt>(i,j)</tt>, then
     378    /// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>.
     379    ///
     380    /// For example, a simple digraph can be constructed like this.
     381    /// \code
     382    ///   std::vector<std::pair<int,int> > arcs;
     383    ///   arcs.push_back(std::make_pair(0,1));
     384    ///   arcs.push_back(std::make_pair(1,3));
     385    ///   arcs.push_back(std::make_pair(1,2));
     386    ///   arcs.push_back(std::make_pair(3,0));
     387    ///   StaticDigraph gr;
     388    ///   gr.build(4,arcs);
     389    /// \endcode
     390    template <typename ArcList>
     391    void build(int n, const ArcList& arcs) {
     392      if (built) Parent::clear();
     393      StaticDigraphBase::build(n, arcs);
     394      notifier(Node()).build();
     395      notifier(Arc()).build();
     396    }
     397
    327398    /// \brief Clear the digraph.
    328399    ///
    329400    /// This function erases all nodes and arcs from the digraph.
  • test/digraph_test.cc

    diff --git a/test/digraph_test.cc b/test/digraph_test.cc
    a b  
    441441
    442442  checkGraphConArcList(G, 4);
    443443
     444  std::vector<std::pair<int,int> > arcs;
     445  arcs.push_back(std::make_pair(0,1));
     446  arcs.push_back(std::make_pair(0,2));
     447  arcs.push_back(std::make_pair(1,3));
     448  arcs.push_back(std::make_pair(1,2));
     449  arcs.push_back(std::make_pair(3,0));
     450  arcs.push_back(std::make_pair(3,3));
     451  arcs.push_back(std::make_pair(4,2));
     452  arcs.push_back(std::make_pair(4,3));
     453  arcs.push_back(std::make_pair(4,1));
     454 
     455  G.build(6, arcs);
     456 
     457  checkGraphNodeList(G, 6);
     458  checkGraphArcList(G, 9);
     459 
     460  checkGraphOutArcList(G, G.node(0), 2);
     461  checkGraphOutArcList(G, G.node(1), 2);
     462  checkGraphOutArcList(G, G.node(2), 0);
     463  checkGraphOutArcList(G, G.node(3), 2);
     464  checkGraphOutArcList(G, G.node(4), 3);
     465  checkGraphOutArcList(G, G.node(5), 0);
     466
     467  checkGraphInArcList(G, G.node(0), 1);
     468  checkGraphInArcList(G, G.node(1), 2);
     469  checkGraphInArcList(G, G.node(2), 3);
     470  checkGraphInArcList(G, G.node(3), 3);
     471  checkGraphInArcList(G, G.node(4), 0);
     472  checkGraphInArcList(G, G.node(5), 0);
     473
     474  checkGraphConArcList(G, 9);
     475
    444476  checkNodeIds(G);
    445477  checkArcIds(G);
    446478  checkGraphNodeMap(G);