test/bfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 11 Aug 2013 21:56:21 +0200
changeset 1098 cf6084618b1b
parent 1084 8b2d4e5d96e4
permissions -rw-r--r--
Merge headmerge to the default branch
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@1092
     5
 * Copyright (C) 2003-2013
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/bfs.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
  "@arcs\n"
deba@228
    41
  "     label\n"
deba@228
    42
  "0 1  0\n"
deba@228
    43
  "1 2  1\n"
deba@228
    44
  "2 3  2\n"
deba@228
    45
  "3 4  3\n"
deba@228
    46
  "0 3  4\n"
deba@228
    47
  "0 3  5\n"
deba@228
    48
  "5 2  6\n"
deba@228
    49
  "@attributes\n"
deba@228
    50
  "source 0\n"
deba@228
    51
  "target 4\n";
deba@228
    52
alpar@209
    53
void checkBfsCompile()
alpar@100
    54
{
alpar@100
    55
  typedef concepts::Digraph Digraph;
alpar@100
    56
  typedef Bfs<Digraph> BType;
kpeter@286
    57
  typedef Digraph::Node Node;
kpeter@286
    58
  typedef Digraph::Arc Arc;
alpar@209
    59
alpar@100
    60
  Digraph G;
kpeter@585
    61
  Node s, t, n;
kpeter@286
    62
  Arc e;
kpeter@585
    63
  int l, i;
alpar@1083
    64
  ::lemon::ignore_unused_variable_warning(l,i);
alpar@100
    65
  bool b;
alpar@100
    66
  BType::DistMap d(G);
alpar@100
    67
  BType::PredMap p(G);
kpeter@286
    68
  Path<Digraph> pp;
kpeter@585
    69
  concepts::ReadMap<Node,bool> nm;
alpar@209
    70
kpeter@286
    71
  {
kpeter@286
    72
    BType bfs_test(G);
kpeter@585
    73
    const BType& const_bfs_test = bfs_test;
alpar@209
    74
kpeter@286
    75
    bfs_test.run(s);
kpeter@286
    76
    bfs_test.run(s,t);
kpeter@286
    77
    bfs_test.run();
alpar@209
    78
kpeter@585
    79
    bfs_test.init();
kpeter@585
    80
    bfs_test.addSource(s);
kpeter@585
    81
    n = bfs_test.processNextNode();
kpeter@585
    82
    n = bfs_test.processNextNode(t, b);
kpeter@585
    83
    n = bfs_test.processNextNode(nm, n);
kpeter@585
    84
    n = const_bfs_test.nextNode();
kpeter@585
    85
    b = const_bfs_test.emptyQueue();
kpeter@585
    86
    i = const_bfs_test.queueSize();
alpar@877
    87
kpeter@585
    88
    bfs_test.start();
kpeter@585
    89
    bfs_test.start(t);
kpeter@585
    90
    bfs_test.start(nm);
kpeter@585
    91
kpeter@585
    92
    l  = const_bfs_test.dist(t);
kpeter@585
    93
    e  = const_bfs_test.predArc(t);
kpeter@585
    94
    s  = const_bfs_test.predNode(t);
kpeter@585
    95
    b  = const_bfs_test.reached(t);
kpeter@585
    96
    d  = const_bfs_test.distMap();
kpeter@585
    97
    p  = const_bfs_test.predMap();
kpeter@585
    98
    pp = const_bfs_test.path(t);
kpeter@286
    99
  }
kpeter@286
   100
  {
kpeter@286
   101
    BType
kpeter@286
   102
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
kpeter@286
   103
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
kpeter@286
   104
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
kpeter@585
   105
      ::SetStandardProcessedMap
kpeter@286
   106
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
kpeter@286
   107
      ::Create bfs_test(G);
alpar@877
   108
kpeter@585
   109
    concepts::ReadWriteMap<Node,Arc> pred_map;
kpeter@585
   110
    concepts::ReadWriteMap<Node,int> dist_map;
kpeter@585
   111
    concepts::ReadWriteMap<Node,bool> reached_map;
kpeter@585
   112
    concepts::WriteMap<Node,bool> processed_map;
alpar@877
   113
kpeter@585
   114
    bfs_test
kpeter@585
   115
      .predMap(pred_map)
kpeter@585
   116
      .distMap(dist_map)
kpeter@585
   117
      .reachedMap(reached_map)
kpeter@585
   118
      .processedMap(processed_map);
alpar@100
   119
kpeter@286
   120
    bfs_test.run(s);
kpeter@286
   121
    bfs_test.run(s,t);
kpeter@286
   122
    bfs_test.run();
alpar@877
   123
kpeter@585
   124
    bfs_test.init();
kpeter@585
   125
    bfs_test.addSource(s);
kpeter@585
   126
    n = bfs_test.processNextNode();
kpeter@585
   127
    n = bfs_test.processNextNode(t, b);
kpeter@585
   128
    n = bfs_test.processNextNode(nm, n);
kpeter@585
   129
    n = bfs_test.nextNode();
kpeter@585
   130
    b = bfs_test.emptyQueue();
kpeter@585
   131
    i = bfs_test.queueSize();
alpar@877
   132
kpeter@585
   133
    bfs_test.start();
kpeter@585
   134
    bfs_test.start(t);
kpeter@585
   135
    bfs_test.start(nm);
kpeter@286
   136
kpeter@286
   137
    l  = bfs_test.dist(t);
kpeter@286
   138
    e  = bfs_test.predArc(t);
kpeter@286
   139
    s  = bfs_test.predNode(t);
kpeter@286
   140
    b  = bfs_test.reached(t);
kpeter@286
   141
    pp = bfs_test.path(t);
kpeter@286
   142
  }
alpar@100
   143
}
alpar@100
   144
alpar@209
   145
void checkBfsFunctionCompile()
alpar@100
   146
{
alpar@100
   147
  typedef int VType;
alpar@100
   148
  typedef concepts::Digraph Digraph;
alpar@100
   149
  typedef Digraph::Arc Arc;
alpar@100
   150
  typedef Digraph::Node Node;
alpar@209
   151
alpar@100
   152
  Digraph g;
kpeter@278
   153
  bool b;
alpar@1083
   154
  ::lemon::ignore_unused_variable_warning(b);
alpar@1007
   155
kpeter@278
   156
  bfs(g).run(Node());
kpeter@278
   157
  b=bfs(g).run(Node(),Node());
kpeter@278
   158
  bfs(g).run();
alpar@100
   159
  bfs(g)
kpeter@278
   160
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   161
    .distMap(concepts::ReadWriteMap<Node,VType>())
alpar@100
   162
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
   163
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@100
   164
    .run(Node());
kpeter@278
   165
  b=bfs(g)
kpeter@278
   166
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   167
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   168
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   169
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   170
    .path(concepts::Path<Digraph>())
kpeter@278
   171
    .dist(VType())
kpeter@278
   172
    .run(Node(),Node());
kpeter@278
   173
  bfs(g)
kpeter@278
   174
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   175
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   176
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   177
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   178
    .run();
alpar@100
   179
}
alpar@100
   180
kpeter@171
   181
template <class Digraph>
kpeter@171
   182
void checkBfs() {
kpeter@171
   183
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100
   184
alpar@100
   185
  Digraph G;
alpar@100
   186
  Node s, t;
alpar@209
   187
deba@228
   188
  std::istringstream input(test_lgf);
kpeter@293
   189
  digraphReader(G, input).
deba@228
   190
    node("source", s).
deba@228
   191
    node("target", t).
deba@228
   192
    run();
alpar@209
   193
alpar@100
   194
  Bfs<Digraph> bfs_test(G);
alpar@100
   195
  bfs_test.run(s);
alpar@209
   196
kpeter@278
   197
  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
alpar@100
   198
alpar@100
   199
  Path<Digraph> p = bfs_test.path(t);
deba@228
   200
  check(p.length()==2,"path() found a wrong path.");
alpar@100
   201
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   202
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   203
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   204
alpar@100
   205
deba@228
   206
  for(ArcIt a(G); a!=INVALID; ++a) {
deba@228
   207
    Node u=G.source(a);
deba@228
   208
    Node v=G.target(a);
alpar@100
   209
    check( !bfs_test.reached(u) ||
deba@222
   210
           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
kpeter@278
   211
           "Wrong output. " << G.id(u) << "->" << G.id(v));
alpar@100
   212
  }
alpar@100
   213
deba@222
   214
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   215
    if (bfs_test.reached(v)) {
deba@228
   216
      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   217
      if (bfs_test.predArc(v)!=INVALID ) {
deba@228
   218
        Arc a=bfs_test.predArc(v);
deba@228
   219
        Node u=G.source(a);
deba@228
   220
        check(u==bfs_test.predNode(v),"Wrong tree.");
deba@228
   221
        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
deba@228
   222
              "Wrong distance. Difference: "
kpeter@278
   223
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
deba@228
   224
      }
alpar@100
   225
    }
alpar@100
   226
  }
kpeter@278
   227
kpeter@278
   228
  {
kpeter@278
   229
    NullMap<Node,Arc> myPredMap;
kpeter@278
   230
    bfs(G).predMap(myPredMap).run(s);
kpeter@278
   231
  }
alpar@100
   232
}
alpar@100
   233
kpeter@171
   234
int main()
kpeter@171
   235
{
kpeter@171
   236
  checkBfs<ListDigraph>();
kpeter@171
   237
  checkBfs<SmartDigraph>();
kpeter@171
   238
  return 0;
kpeter@171
   239
}