test/bfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 27 Sep 2008 13:45:50 +0200
changeset 283 66bb22401834
parent 228 b6732e0d38c5
child 286 da414906fe21
permissions -rw-r--r--
Merge
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@100
     5
 * Copyright (C) 2003-2008
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;
alpar@209
    57
alpar@100
    58
  Digraph G;
kpeter@171
    59
  Digraph::Node n;
kpeter@171
    60
  Digraph::Arc e;
alpar@100
    61
  int l;
alpar@100
    62
  bool b;
alpar@100
    63
  BType::DistMap d(G);
alpar@100
    64
  BType::PredMap p(G);
alpar@209
    65
alpar@100
    66
  BType bfs_test(G);
alpar@209
    67
alpar@100
    68
  bfs_test.run(n);
alpar@209
    69
alpar@100
    70
  l  = bfs_test.dist(n);
alpar@100
    71
  e  = bfs_test.predArc(n);
alpar@100
    72
  n  = bfs_test.predNode(n);
alpar@100
    73
  d  = bfs_test.distMap();
alpar@100
    74
  p  = bfs_test.predMap();
alpar@100
    75
  b  = bfs_test.reached(n);
alpar@100
    76
alpar@100
    77
  Path<Digraph> pp = bfs_test.path(n);
alpar@100
    78
}
alpar@100
    79
alpar@209
    80
void checkBfsFunctionCompile()
alpar@100
    81
{
alpar@100
    82
  typedef int VType;
alpar@100
    83
  typedef concepts::Digraph Digraph;
alpar@100
    84
  typedef Digraph::Arc Arc;
alpar@100
    85
  typedef Digraph::Node Node;
alpar@209
    86
alpar@100
    87
  Digraph g;
kpeter@278
    88
  bool b;
kpeter@278
    89
  bfs(g).run(Node());
kpeter@278
    90
  b=bfs(g).run(Node(),Node());
kpeter@278
    91
  bfs(g).run();
alpar@100
    92
  bfs(g)
kpeter@278
    93
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
    94
    .distMap(concepts::ReadWriteMap<Node,VType>())
alpar@100
    95
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
    96
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@100
    97
    .run(Node());
kpeter@278
    98
  b=bfs(g)
kpeter@278
    99
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   100
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   101
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   102
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   103
    .path(concepts::Path<Digraph>())
kpeter@278
   104
    .dist(VType())
kpeter@278
   105
    .run(Node(),Node());
kpeter@278
   106
  bfs(g)
kpeter@278
   107
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   108
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   109
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   110
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   111
    .run();
alpar@100
   112
}
alpar@100
   113
kpeter@171
   114
template <class Digraph>
kpeter@171
   115
void checkBfs() {
kpeter@171
   116
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100
   117
alpar@100
   118
  Digraph G;
alpar@100
   119
  Node s, t;
alpar@209
   120
deba@228
   121
  std::istringstream input(test_lgf);
deba@228
   122
  digraphReader(input, G).
deba@228
   123
    node("source", s).
deba@228
   124
    node("target", t).
deba@228
   125
    run();
alpar@209
   126
alpar@100
   127
  Bfs<Digraph> bfs_test(G);
alpar@100
   128
  bfs_test.run(s);
alpar@209
   129
kpeter@278
   130
  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
alpar@100
   131
alpar@100
   132
  Path<Digraph> p = bfs_test.path(t);
deba@228
   133
  check(p.length()==2,"path() found a wrong path.");
alpar@100
   134
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   135
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   136
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   137
alpar@100
   138
deba@228
   139
  for(ArcIt a(G); a!=INVALID; ++a) {
deba@228
   140
    Node u=G.source(a);
deba@228
   141
    Node v=G.target(a);
alpar@100
   142
    check( !bfs_test.reached(u) ||
deba@222
   143
           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
kpeter@278
   144
           "Wrong output. " << G.id(u) << "->" << G.id(v));
alpar@100
   145
  }
alpar@100
   146
deba@222
   147
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   148
    if (bfs_test.reached(v)) {
deba@228
   149
      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   150
      if (bfs_test.predArc(v)!=INVALID ) {
deba@228
   151
        Arc a=bfs_test.predArc(v);
deba@228
   152
        Node u=G.source(a);
deba@228
   153
        check(u==bfs_test.predNode(v),"Wrong tree.");
deba@228
   154
        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
deba@228
   155
              "Wrong distance. Difference: "
kpeter@278
   156
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
deba@228
   157
      }
alpar@100
   158
    }
alpar@100
   159
  }
kpeter@278
   160
kpeter@278
   161
  {
kpeter@278
   162
    NullMap<Node,Arc> myPredMap;
kpeter@278
   163
    bfs(G).predMap(myPredMap).run(s);
kpeter@278
   164
  }
alpar@100
   165
}
alpar@100
   166
kpeter@171
   167
int main()
kpeter@171
   168
{
kpeter@171
   169
  checkBfs<ListDigraph>();
kpeter@171
   170
  checkBfs<SmartDigraph>();
kpeter@171
   171
  return 0;
kpeter@171
   172
}