test/dfs_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 05 Oct 2011 12:02:56 +0200
changeset 953 08cbd48f51f0
parent 293 47fbc814aa31
child 959 17e36e175725
child 907 1937b6455b7d
permissions -rw-r--r--
Add lemon/lemon.pc.cmake to the release tarball (#428)
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/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;
alpar@100
    68
  int l;
alpar@100
    69
  bool b;
alpar@100
    70
  DType::DistMap d(G);
alpar@100
    71
  DType::PredMap p(G);
kpeter@286
    72
  Path<Digraph> pp;
alpar@209
    73
kpeter@286
    74
  {
kpeter@286
    75
    DType dfs_test(G);
alpar@209
    76
kpeter@286
    77
    dfs_test.run(s);
kpeter@286
    78
    dfs_test.run(s,t);
kpeter@286
    79
    dfs_test.run();
alpar@209
    80
kpeter@286
    81
    l  = dfs_test.dist(t);
kpeter@286
    82
    e  = dfs_test.predArc(t);
kpeter@286
    83
    s  = dfs_test.predNode(t);
kpeter@286
    84
    b  = dfs_test.reached(t);
kpeter@286
    85
    d  = dfs_test.distMap();
kpeter@286
    86
    p  = dfs_test.predMap();
kpeter@286
    87
    pp = dfs_test.path(t);
kpeter@286
    88
  }
kpeter@286
    89
  {
kpeter@286
    90
    DType
kpeter@286
    91
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
kpeter@286
    92
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
kpeter@286
    93
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
kpeter@286
    94
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
kpeter@286
    95
      ::SetStandardProcessedMap
kpeter@286
    96
      ::Create dfs_test(G);
alpar@100
    97
kpeter@286
    98
    dfs_test.run(s);
kpeter@286
    99
    dfs_test.run(s,t);
kpeter@286
   100
    dfs_test.run();
kpeter@286
   101
kpeter@286
   102
    l  = dfs_test.dist(t);
kpeter@286
   103
    e  = dfs_test.predArc(t);
kpeter@286
   104
    s  = dfs_test.predNode(t);
kpeter@286
   105
    b  = dfs_test.reached(t);
kpeter@286
   106
    pp = dfs_test.path(t);
kpeter@286
   107
  }
alpar@100
   108
}
alpar@100
   109
alpar@209
   110
void checkDfsFunctionCompile()
alpar@100
   111
{
alpar@100
   112
  typedef int VType;
alpar@100
   113
  typedef concepts::Digraph Digraph;
alpar@100
   114
  typedef Digraph::Arc Arc;
alpar@100
   115
  typedef Digraph::Node Node;
alpar@209
   116
alpar@100
   117
  Digraph g;
kpeter@278
   118
  bool b;
kpeter@278
   119
  dfs(g).run(Node());
kpeter@278
   120
  b=dfs(g).run(Node(),Node());
kpeter@278
   121
  dfs(g).run();
alpar@100
   122
  dfs(g)
kpeter@278
   123
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   124
    .distMap(concepts::ReadWriteMap<Node,VType>())
alpar@100
   125
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
   126
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@209
   127
    .run(Node());
kpeter@278
   128
  b=dfs(g)
kpeter@278
   129
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   130
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   131
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   132
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   133
    .path(concepts::Path<Digraph>())
kpeter@278
   134
    .dist(VType())
kpeter@278
   135
    .run(Node(),Node());
kpeter@278
   136
  dfs(g)
kpeter@278
   137
    .predMap(concepts::ReadWriteMap<Node,Arc>())
kpeter@278
   138
    .distMap(concepts::ReadWriteMap<Node,VType>())
kpeter@278
   139
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
kpeter@278
   140
    .processedMap(concepts::WriteMap<Node,bool>())
kpeter@278
   141
    .run();
alpar@100
   142
}
alpar@100
   143
kpeter@171
   144
template <class Digraph>
kpeter@171
   145
void checkDfs() {
kpeter@171
   146
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@100
   147
alpar@100
   148
  Digraph G;
alpar@100
   149
  Node s, t;
alpar@906
   150
  Node s1, t1;
alpar@209
   151
deba@228
   152
  std::istringstream input(test_lgf);
kpeter@293
   153
  digraphReader(G, input).
deba@228
   154
    node("source", s).
deba@228
   155
    node("target", t).
alpar@906
   156
    node("source1", s1).
alpar@906
   157
    node("target1", t1).
deba@228
   158
    run();
alpar@209
   159
alpar@100
   160
  Dfs<Digraph> dfs_test(G);
alpar@209
   161
  dfs_test.run(s);
alpar@209
   162
alpar@100
   163
  Path<Digraph> p = dfs_test.path(t);
kpeter@171
   164
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
alpar@100
   165
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   166
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   167
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@209
   168
alpar@100
   169
  for(NodeIt v(G); v!=INVALID; ++v) {
deba@228
   170
    if (dfs_test.reached(v)) {
deba@228
   171
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
deba@228
   172
      if (dfs_test.predArc(v)!=INVALID ) {
deba@228
   173
        Arc e=dfs_test.predArc(v);
deba@228
   174
        Node u=G.source(e);
deba@228
   175
        check(u==dfs_test.predNode(v),"Wrong tree.");
deba@228
   176
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
deba@228
   177
              "Wrong distance. (" << dfs_test.dist(u) << "->"
kpeter@278
   178
              << dfs_test.dist(v) << ")");
deba@228
   179
      }
alpar@100
   180
    }
alpar@100
   181
  }
kpeter@278
   182
kpeter@278
   183
  {
alpar@906
   184
  Dfs<Digraph> dfs(G);
alpar@906
   185
  check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6.");
alpar@906
   186
  }
alpar@906
   187
  
alpar@906
   188
  {
kpeter@278
   189
    NullMap<Node,Arc> myPredMap;
kpeter@278
   190
    dfs(G).predMap(myPredMap).run(s);
kpeter@278
   191
  }
alpar@100
   192
}
alpar@100
   193
kpeter@171
   194
int main()
kpeter@171
   195
{
kpeter@171
   196
  checkDfs<ListDigraph>();
kpeter@171
   197
  checkDfs<SmartDigraph>();
kpeter@171
   198
  return 0;
kpeter@171
   199
}