test/max_cardinality_search_test.cc
changeset 1020 70bee017b584
child 1088 7f6eeffe3cd1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/max_cardinality_search_test.cc	Mon Nov 15 22:23:35 2010 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2010
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +
    1.24 +#include "test_tools.h"
    1.25 +#include <lemon/smart_graph.h>
    1.26 +#include <lemon/max_cardinality_search.h>
    1.27 +#include <lemon/concepts/digraph.h>
    1.28 +#include <lemon/concepts/maps.h>
    1.29 +#include <lemon/concepts/heap.h>
    1.30 +#include <lemon/lgf_reader.h>
    1.31 +
    1.32 +using namespace lemon;
    1.33 +using namespace std;
    1.34 +
    1.35 +char test_lgf[] =
    1.36 +  "@nodes\n"
    1.37 +  "label\n"
    1.38 +  "0\n"
    1.39 +  "1\n"
    1.40 +  "2\n"
    1.41 +  "3\n"
    1.42 +  "@arcs\n"
    1.43 +  "    label capacity\n"
    1.44 +  "0 1 0     2\n"
    1.45 +  "1 0 1     2\n"
    1.46 +  "2 1 2     1\n"
    1.47 +  "2 3 3     3\n"
    1.48 +  "3 2 4     3\n"
    1.49 +  "3 1 5     5\n"
    1.50 +  "@attributes\n"
    1.51 +  "s 0\n"
    1.52 +  "x 1\n"
    1.53 +  "y 2\n"
    1.54 +  "z 3\n";
    1.55 +
    1.56 +void checkMaxCardSearchCompile() {
    1.57 +
    1.58 +  typedef concepts::Digraph Digraph;
    1.59 +  typedef int Value;
    1.60 +  typedef Digraph::Node Node;
    1.61 +  typedef Digraph::Arc Arc;
    1.62 +  typedef concepts::ReadMap<Arc,Value> CapMap;
    1.63 +  typedef concepts::ReadWriteMap<Node,Value> CardMap;
    1.64 +  typedef concepts::ReadWriteMap<Node,bool> ProcMap;
    1.65 +  typedef Digraph::NodeMap<int> HeapCrossRef;
    1.66 +
    1.67 +  Digraph g;
    1.68 +  Node n,s;
    1.69 +  CapMap cap;
    1.70 +  CardMap card;
    1.71 +  ProcMap proc;
    1.72 +  HeapCrossRef crossref(g);
    1.73 +  
    1.74 +  typedef MaxCardinalitySearch<Digraph,CapMap>
    1.75 +    ::SetCapacityMap<CapMap>
    1.76 +    ::SetCardinalityMap<CardMap>
    1.77 +    ::SetProcessedMap<ProcMap>
    1.78 +    ::SetStandardHeap<BinHeap<Value,HeapCrossRef> >
    1.79 +    ::Create MaxCardType;
    1.80 +
    1.81 +  MaxCardType maxcard(g,cap);
    1.82 +  const MaxCardType& const_maxcard = maxcard;
    1.83 +
    1.84 +  const MaxCardType::Heap& heap_const = const_maxcard.heap();
    1.85 +  MaxCardType::Heap& heap = const_cast<MaxCardType::Heap&>(heap_const);
    1.86 +  maxcard.heap(heap,crossref);
    1.87 +  
    1.88 +  maxcard.capacityMap(cap).cardinalityMap(card).processedMap(proc);
    1.89 +
    1.90 +  maxcard.init();
    1.91 +  maxcard.addSource(s);
    1.92 +  n = maxcard.nextNode();
    1.93 +   maxcard.processNextNode();
    1.94 +   maxcard.start();
    1.95 +   maxcard.run(s);
    1.96 +   maxcard.run();
    1.97 + }
    1.98 +
    1.99 + void checkWithIntMap( std::istringstream& input)
   1.100 + {
   1.101 +   typedef SmartDigraph Digraph;
   1.102 +   typedef Digraph::Node Node;
   1.103 +   typedef Digraph::ArcMap<int> CapMap;
   1.104 +
   1.105 +   Digraph g;
   1.106 +   Node s,x,y,z,a;
   1.107 +   CapMap cap(g);
   1.108 +
   1.109 +   DigraphReader<Digraph>(g,input).
   1.110 +     arcMap("capacity", cap).
   1.111 +     node("s",s).
   1.112 +     node("x",x).
   1.113 +     node("y",y).
   1.114 +     node("z",z).
   1.115 +     run();
   1.116 +
   1.117 +   MaxCardinalitySearch<Digraph,CapMap> maxcard(g,cap);
   1.118 +
   1.119 +   maxcard.init();
   1.120 +   maxcard.addSource(s);
   1.121 +   maxcard.start(x);
   1.122 +
   1.123 +   check(maxcard.processed(s) and !maxcard.processed(x) and
   1.124 +         !maxcard.processed(y), "Wrong processed()!");
   1.125 +
   1.126 +   a=maxcard.nextNode();
   1.127 +   check(maxcard.processNextNode()==a,
   1.128 +         "Wrong nextNode() or processNextNode() return value!");
   1.129 +
   1.130 +   check(maxcard.processed(a), "Wrong processNextNode()!");
   1.131 +
   1.132 +   maxcard.start();
   1.133 +   check(maxcard.cardinality(x)==2 and maxcard.cardinality(y)>=4,
   1.134 +         "Wrong cardinalities!");
   1.135 + }
   1.136 +
   1.137 + void checkWithConst1Map(std::istringstream &input) {
   1.138 +   typedef SmartDigraph Digraph;
   1.139 +   typedef Digraph::Node Node;
   1.140 +
   1.141 +   Digraph g;
   1.142 +   Node s,x,y,z;
   1.143 +
   1.144 +  DigraphReader<Digraph>(g,input).
   1.145 +    node("s",s).
   1.146 +    node("x",x).
   1.147 +    node("y",y).
   1.148 +    node("z",z).
   1.149 +    run();
   1.150 +
   1.151 +  MaxCardinalitySearch<Digraph> maxcard(g);
   1.152 +  maxcard.run(s);
   1.153 +  check(maxcard.cardinality(x)==1 &&
   1.154 +        maxcard.cardinality(y)+maxcard.cardinality(z)==3,
   1.155 +        "Wrong cardinalities!");
   1.156 +}
   1.157 +
   1.158 +int main() {
   1.159 +
   1.160 +  std::istringstream input1(test_lgf);
   1.161 +  checkWithIntMap(input1);
   1.162 +
   1.163 +  std::istringstream input2(test_lgf);
   1.164 +  checkWithConst1Map(input2);
   1.165 +}