COIN-OR::LEMON - Graph Library

Changeset 1098:e3b3667c6857 in lemon-0.x for src


Ignore:
Timestamp:
01/27/05 17:11:54 (15 years ago)
Author:
jacint
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1497
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/test/max_matching_test.cc

    r1092 r1098  
    1616
    1717#include <iostream>
     18#include <vector>
    1819#include <queue>
    1920#include <math.h>
     
    2425#include <lemon/list_graph.h>
    2526#include <lemon/max_matching.h>
    26 //temporarily
    27 #include <work/jacint/graph_gen.h>
    2827
    2928using namespace std;
     
    4039  typedef Graph::Node Node;
    4140   
    42   Graph G;
     41  Graph g;
     42  g.clear();
    4343
    44   random_init();
    45   randomGraph(G, random(5000), random(20000) ); 
     44  std::vector<Graph::Node> nodes;
     45  for (int i=0; i<13; ++i)
     46      nodes.push_back(g.addNode());
    4647
    47   MaxMatching<Graph> max_matching(G);
     48  g.addEdge(nodes[0], nodes[0]);
     49  g.addEdge(nodes[6], nodes[10]);
     50  g.addEdge(nodes[5], nodes[10]);
     51  g.addEdge(nodes[4], nodes[10]);
     52  g.addEdge(nodes[3], nodes[11]); 
     53  g.addEdge(nodes[1], nodes[6]);
     54  g.addEdge(nodes[4], nodes[7]); 
     55  g.addEdge(nodes[1], nodes[8]);
     56  g.addEdge(nodes[0], nodes[8]);
     57  g.addEdge(nodes[3], nodes[12]);
     58  g.addEdge(nodes[6], nodes[9]);
     59  g.addEdge(nodes[9], nodes[11]);
     60  g.addEdge(nodes[2], nodes[10]);
     61  g.addEdge(nodes[10], nodes[8]);
     62  g.addEdge(nodes[5], nodes[8]);
     63  g.addEdge(nodes[6], nodes[3]);
     64  g.addEdge(nodes[0], nodes[5]);
     65  g.addEdge(nodes[6], nodes[12]);
     66 
     67  MaxMatching<Graph> max_matching(g);
    4868  max_matching.runEdmonds(0);
    4969 
    5070  int s=0;
    51   Graph::NodeMap<Node> mate(G,INVALID);
     71  Graph::NodeMap<Node> mate(g,INVALID);
    5272  max_matching.writeNMapNode(mate);
    53   for(NodeIt v(G); v!=INVALID; ++v) {
     73  for(NodeIt v(g); v!=INVALID; ++v) {
    5474    if ( mate[v]!=INVALID ) ++s;
    5575  }
    5676  int size=(int)s/2;  //size will be used as the size of a maxmatching
    5777
    58   for(NodeIt v(G); v!=INVALID; ++v) {
     78  for(NodeIt v(g); v!=INVALID; ++v) {
    5979    max_matching.mate(v);
    6080  }
     
    6282  check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
    6383
    64   Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(G);
     84  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(g);
    6585  max_matching.writePos(pos0);
    6686 
     
    6989  s=0;
    7090  max_matching.writeNMapNode(mate);
    71   for(NodeIt v(G); v!=INVALID; ++v) {
     91  for(NodeIt v(g); v!=INVALID; ++v) {
    7292    if ( mate[v]!=INVALID ) ++s;
    7393  }
    7494  check ( (int)s/2 == size, "The size does not equal!" );
    7595
    76   Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(G);
     96  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(g);
    7797  max_matching.writePos(pos1);
    7898
     
    80100  s=0;
    81101  max_matching.writeNMapNode(mate);
    82   for(NodeIt v(G); v!=INVALID; ++v) {
     102  for(NodeIt v(g); v!=INVALID; ++v) {
    83103    if ( mate[v]!=INVALID ) ++s;
    84104  }
    85105  check ( (int)s/2 == size, "The size does not equal!" );
    86106 
    87   Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(G);
     107  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(g);
    88108  max_matching.writePos(pos2);
    89109
     
    92112  s=0;
    93113  max_matching.writeNMapNode(mate);
    94   for(NodeIt v(G); v!=INVALID; ++v) {
     114  for(NodeIt v(g); v!=INVALID; ++v) {
    95115    if ( mate[v]!=INVALID ) ++s;
    96116  }
    97117  check ( (int)s/2 == size, "The size does not equal!" );
    98118 
    99   Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(G);
     119  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(g);
    100120  max_matching.writePos(pos);
    101121   
    102122  bool ismatching=true;
    103   for(NodeIt v(G); v!=INVALID; ++v) {
     123  for(NodeIt v(g); v!=INVALID; ++v) {
    104124    if ( mate[v]!=INVALID ) {
    105125      Node u=mate[v];
     
    110130
    111131  bool coincide=true;
    112   for(NodeIt v(G); v!=INVALID; ++v) {
     132  for(NodeIt v(g); v!=INVALID; ++v) {
    113133   if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
    114134     coincide=false;
     
    118138
    119139  bool noedge=true;
    120   for(UndirEdgeIt e(G); e!=INVALID; ++e) {
    121    if ( (pos[G.target(e)]==max_matching.C && pos[G.source(e)]==max_matching.D) ||
    122          (pos[G.target(e)]==max_matching.D && pos[G.source(e)]==max_matching.C) )
     140  for(UndirEdgeIt e(g); e!=INVALID; ++e) {
     141   if ( (pos[g.target(e)]==max_matching.C && pos[g.source(e)]==max_matching.D) ||
     142         (pos[g.target(e)]==max_matching.D && pos[g.source(e)]==max_matching.C) )
    123143      noedge=false;
    124144  }
     
    126146 
    127147  bool oddcomp=true;
    128   Graph::NodeMap<bool> todo(G,true);
     148  Graph::NodeMap<bool> todo(g,true);
    129149  int num_comp=0;
    130   for(NodeIt v(G); v!=INVALID; ++v) {
     150  for(NodeIt v(g); v!=INVALID; ++v) {
    131151   if ( pos[v]==max_matching.D && todo[v] ) {
    132152      int comp_size=1;
     
    138158        Node w=Q.front();       
    139159        Q.pop();
    140         for(IncEdgeIt e(G,w); e!=INVALID; ++e) {
    141           Node u=G.target(e);
     160        for(IncEdgeIt e(g,w); e!=INVALID; ++e) {
     161          Node u=g.target(e);
    142162          if ( pos[u]==max_matching.D && todo[u] ) {
    143163            ++comp_size;
     
    150170    }
    151171  }
    152   check ( oddcomp, "A component of G[D] is not odd." );
     172  check ( oddcomp, "A component of g[D] is not odd." );
    153173
    154174  int barrier=0;
    155   for(NodeIt v(G); v!=INVALID; ++v) {
     175  for(NodeIt v(g); v!=INVALID; ++v) {
    156176    if ( pos[v]==max_matching.A ) ++barrier;
    157177  }
    158   int expected_size=(int)( countNodes(G)-num_comp+barrier)/2;
     178  int expected_size=(int)( countNodes(g)-num_comp+barrier)/2;
    159179  check ( size==expected_size, "The size of the matching is wrong." );
    160180 
    161181  return 0;
    162182}
    163 
    164 
    165 void random_init()
    166 {
    167   unsigned int seed = getpid();
    168   seed |= seed << 15;
    169   seed ^= time(0);
    170  
    171   srand(seed);
    172 }
    173 
    174 
    175 int random(int m)
    176 {
    177   return int( double(m) * rand() / (RAND_MAX + 1.0) );
    178 }
    179 
    180 
    181 /// Generates a random graph with n nodes and m edges.
    182 /// Before generating the random graph, \c g.clear() is called.
    183 template<typename Graph>
    184 void randomGraph(Graph& g, int n, int m) {
    185   g.clear();
    186   std::vector<typename Graph::Node> nodes;
    187   for (int i=0; i<n; ++i)
    188     nodes.push_back(g.addNode());
    189   for (int i=0; i<m; ++i)
    190     g.addEdge(nodes[random(n)], nodes[random(n)]);
    191 }
Note: See TracChangeset for help on using the changeset viewer.