test/dfs_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 28 Feb 2013 18:05:56 +0100
changeset 1058 2f00ef323c2e
parent 966 c8fce9beb46a
parent 1008 d216e1c8b3fa
child 1086 97f1760dcd13
permissions -rw-r--r--
Rename DefFlowMap named parameter to SetFlowMap (#177)
in EdmondsKarp according to Preflow
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@100
     4
 *
alpar@877
     5
 * Copyright (C) 2003-2010
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
kpeter@171
    19
#include <lemon/concepts/digraph.h>
kpeter@171
    20
#include <lemon/smart_graph.h>
alpar@100
    21
#include <lemon/list_graph.h>
deba@228
    22
#include <lemon/lgf_reader.h>
alpar@100
    23
#include <lemon/dfs.h>
alpar@100
    24
#include <lemon/path.h>
kpeter@171
    25
kpeter@171
    26
#include "graph_test.h"
kpeter@171
    27
#include "test_tools.h"
alpar@100
    28
alpar@100
    29
using namespace lemon;
alpar@100
    30
deba@228
    31
char test_lgf[] =
deba@228
    32
  "@nodes\n"
deba@228
    33
  "label\n"
deba@228
    34
  "0\n"
deba@228
    35
  "1\n"
deba@228
    36
  "2\n"
deba@228
    37
  "3\n"
deba@228
    38
  "4\n"
deba@228
    39
  "5\n"
deba@228
    40
  "6\n"
deba@228
    41
  "@arcs\n"
deba@228
    42
  "     label\n"
deba@228
    43
  "0 1  0\n"
deba@228
    44
  "1 2  1\n"
deba@228
    45
  "2 3  2\n"
deba@228
    46
  "1 4  3\n"
deba@228
    47
  "4 2  4\n"
deba@228
    48
  "4 5  5\n"
deba@228
    49
  "5 0  6\n"
deba@228
    50
  "6 3  7\n"
deba@228
    51
  "@attributes\n"
deba@228
    52
  "source 0\n"
alpar@906
    53
  "target 5\n"
alpar@906
    54
  "source1 6\n"
alpar@906
    55
  "target1 3\n";
alpar@906
    56
deba@228
    57
alpar@209
    58
void checkDfsCompile()
alpar@100
    59
{
alpar@100
    60
  typedef concepts::Digraph Digraph;
alpar@100
    61
  typedef Dfs<Digraph> DType;
kpeter@286
    62
  typedef Digraph::Node Node;
kpeter@286
    63
  typedef Digraph::Arc Arc;
alpar@209
    64
alpar@100
    65
  Digraph G;
kpeter@286
    66
  Node s, t;
kpeter@286
    67
  Arc e;
kpeter@585
    68
  int l, i;
alpar@100
    69
  bool b;
alpar@1007
    70
  ignore_unused_variable_warning(l,i,b);
alpar@1007
    71
alpar@100
    72
  DType::DistMap d(G);
alpar@100
    73
  DType::PredMap p(G);
kpeter@286
    74
  Path<Digraph> pp;
kpeter@585
    75
  concepts::ReadMap<Arc,bool> am;
alpar@209
    76
kpeter@286
    77
  {
kpeter@286
    78
    DType dfs_test(G);
kpeter@585
    79
    const DType& const_dfs_test = dfs_test;
alpar@209
    80
kpeter@286
    81
    dfs_test.run(s);
kpeter@286
    82
    dfs_test.run(s,t);
kpeter@286
    83
    dfs_test.run();
alpar@209
    84
kpeter@585
    85
    dfs_test.init();
kpeter@585
    86
    dfs_test.addSource(s);
kpeter@585
    87
    e = dfs_test.processNextArc();
kpeter@585
    88
    e = const_dfs_test.nextArc();
kpeter@585
    89
    b = const_dfs_test.emptyQueue();
kpeter@585
    90
    i = const_dfs_test.queueSize();
alpar@877
    91
kpeter@585
    92
    dfs_test.start();
kpeter@585
    93
    dfs_test.start(t);
kpeter@585
    94
    dfs_test.start(am);
kpeter@585
    95
kpeter@585
    96
    l  = const_dfs_test.dist(t);
kpeter@585
    97
    e  = const_dfs_test.predArc(t);
kpeter@585
    98
    s  = const_dfs_test.predNode(t);
kpeter@585
    99
    b  = const_dfs_test.reached(t);
kpeter@585
   100
    d  = const_dfs_test.distMap();
kpeter@585
   101
    p  = const_dfs_test.predMap();
kpeter@585
   102
    pp = const_dfs_test.path(t);
kpeter@286
   103
  }
kpeter@286
   104
  {
kpeter@286
   105
    DType
kpeter@286
   106
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
kpeter@286
   107
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
kpeter@286
   108
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
kpeter@585
   109
      ::SetStandardProcessedMap
kpeter@286
   110
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
kpeter@286
   111
      ::Create dfs_test(G);
alpar@100
   112
kpeter@585
   113
    concepts::ReadWriteMap<Node,Arc> pred_map;
kpeter@585
   114
    concepts::ReadWriteMap<Node,int> dist_map;
kpeter@585
   115
    concepts::ReadWriteMap<Node,bool> reached_map;
kpeter@585
   116
    concepts::WriteMap<Node,bool> processed_map;
alpar@877
   117
kpeter@585
   118
    dfs_test
kpeter@585
   119
      .predMap(pred_map)
kpeter@585
   120
      .distMap(dist_map)
kpeter@585
   121
      .reachedMap(reached_map)
kpeter@585
   122
      .processedMap(processed_map);
kpeter@585
   123
kpeter@286
   124
    dfs_test.run(s);
kpeter@286
   125
    dfs_test.run(s,t);
kpeter@286
   126
    dfs_test.run();
kpeter@585
   127
    dfs_test.init();
kpeter@585
   128
kpeter@585
   129
    dfs_test.addSource(s);
kpeter@585
   130
    e = dfs_test.processNextArc();
kpeter@585
   131
    e = dfs_test.nextArc();
kpeter@585
   132
    b = dfs_test.emptyQueue();
kpeter@585
   133
    i = dfs_test.queueSize();
alpar@877
   134
kpeter@585
   135
    dfs_test.start();
kpeter@585
   136
    dfs_test.start(t);
kpeter@585
   137
    dfs_test.start(am);
kpeter@286
   138
kpeter@286
   139
    l  = dfs_test.dist(t);
kpeter@286
   140
    e  = dfs_test.predArc(t);
kpeter@286
   141
    s  = dfs_test.predNode(t);
kpeter@286
   142
    b  = dfs_test.reached(t);
kpeter@286
   143
    pp = dfs_test.path(t);
kpeter@286
   144
  }
alpar@100
   145
}
alpar@100
   146
alpar@209
   147
void checkDfsFunctionCompile()
alpar@100
   148
{
alpar@100
   149
  typedef int VType;
alpar@100
   150
  typedef concepts::Digraph Digraph;
alpar@100
   151
  typedef Digraph::Arc Arc;
alpar@100
   152
  typedef Digraph::Node Node;
alpar@209
   153
alpar@100
   154
  Digraph g;
kpeter@278
   155
  bool b;
alpar@1007
   156
  ignore_unused_variable_warning(b);
alpar@1007
   157
kpeter@278
   158
  dfs(g).run(Node());
kpeter@278
   159
  b=dfs(g).run(Node(),Node());
kpeter@278
   160
  dfs(g).run();
alpar@100
   161
  dfs(g)
kpeter@278
   162
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   163
    .distMap(concepts::ReadWriteMap<Node,VType>())
alpar@100
   164
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
   165
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@209
   166
    .run(Node());
kpeter@278
   167
  b=dfs(g)
kpeter@278
   168
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   169
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   170
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   171
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   172
    .path(concepts::Path<Digraph>())
kpeter@278
   173
    .dist(VType())
kpeter@278
   174
    .run(Node(),Node());
kpeter@278
   175
  dfs(g)
kpeter@278
   176
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   177
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   178
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   179
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   180
    .run();
alpar@100
   181
}
alpar@100
   182
kpeter@171
   183
template <class Digraph>
kpeter@171
   184
void checkDfs() {
kpeter@171
   185
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100
   186
alpar@100
   187
  Digraph G;
alpar@100
   188
  Node s, t;
alpar@906
   189
  Node s1, t1;
alpar@209
   190
deba@228
   191
  std::istringstream input(test_lgf);
kpeter@293
   192
  digraphReader(G, input).
deba@228
   193
    node("source", s).
deba@228
   194
    node("target", t).
alpar@906
   195
    node("source1", s1).
alpar@906
   196
    node("target1", t1).
deba@228
   197
    run();
alpar@209
   198
alpar@100
   199
  Dfs<Digraph> dfs_test(G);
alpar@209
   200
  dfs_test.run(s);
alpar@209
   201
alpar@100
   202
  Path<Digraph> p = dfs_test.path(t);
kpeter@171
   203
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
alpar@100
   204
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   205
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   206
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   207
alpar@100
   208
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   209
    if (dfs_test.reached(v)) {
deba@228
   210
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   211
      if (dfs_test.predArc(v)!=INVALID ) {
deba@228
   212
        Arc e=dfs_test.predArc(v);
deba@228
   213
        Node u=G.source(e);
deba@228
   214
        check(u==dfs_test.predNode(v),"Wrong tree.");
deba@228
   215
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
deba@228
   216
              "Wrong distance. (" << dfs_test.dist(u) << "->"
kpeter@278
   217
              << dfs_test.dist(v) << ")");
deba@228
   218
      }
alpar@100
   219
    }
alpar@100
   220
  }
kpeter@278
   221
kpeter@278
   222
  {
alpar@906
   223
  Dfs<Digraph> dfs(G);
alpar@906
   224
  check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6.");
alpar@906
   225
  }
alpar@906
   226
  
alpar@906
   227
  {
kpeter@278
   228
    NullMap<Node,Arc> myPredMap;
kpeter@278
   229
    dfs(G).predMap(myPredMap).run(s);
kpeter@278
   230
  }
alpar@100
   231
}
alpar@100
   232
kpeter@171
   233
int main()
kpeter@171
   234
{
kpeter@171
   235
  checkDfs<ListDigraph>();
kpeter@171
   236
  checkDfs<SmartDigraph>();
kpeter@171
   237
  return 0;
kpeter@171
   238
}