test/bfs_test.cc
author kpeter
Mon, 25 Feb 2008 12:35:06 +0000
changeset 2579 691ce54544c5
parent 2391 14a343be7a5a
permissions -rw-r--r--
Bug fixes in min cost flow files.
Use enum type instead of static constants in NetworkSimplex to avoid
linker errors.
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@774
    19
#include "test_tools.h"
alpar@921
    20
#include <lemon/smart_graph.h>
alpar@921
    21
#include <lemon/bfs.h>
alpar@1283
    22
#include <lemon/path.h>
alpar@2260
    23
#include<lemon/concepts/graph.h>
alpar@774
    24
alpar@921
    25
using namespace lemon;
alpar@774
    26
alpar@774
    27
const int PET_SIZE =5;
alpar@774
    28
alpar@774
    29
alpar@793
    30
void check_Bfs_Compile() 
alpar@774
    31
{
alpar@2260
    32
  typedef concepts::Graph Graph;
alpar@774
    33
alpar@774
    34
  typedef Graph::Edge Edge;
alpar@774
    35
  typedef Graph::Node Node;
alpar@774
    36
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    37
  typedef Graph::NodeIt NodeIt;
alpar@774
    38
 
alpar@774
    39
  typedef Bfs<Graph> BType;
alpar@774
    40
  
alpar@774
    41
  Graph G;
alpar@774
    42
  Node n;
alpar@774
    43
  Edge e;
alpar@793
    44
  int l;
alpar@774
    45
  bool b;
alpar@774
    46
  BType::DistMap d(G);
alpar@774
    47
  BType::PredMap p(G);
alpar@1218
    48
  //  BType::PredNodeMap pn(G);
alpar@774
    49
  
alpar@774
    50
  BType bfs_test(G);
alpar@774
    51
  
alpar@774
    52
  bfs_test.run(n);
alpar@774
    53
  
alpar@774
    54
  l  = bfs_test.dist(n);
deba@1763
    55
  e  = bfs_test.predEdge(n);
alpar@774
    56
  n  = bfs_test.predNode(n);
alpar@774
    57
  d  = bfs_test.distMap();
alpar@774
    58
  p  = bfs_test.predMap();
alpar@1218
    59
  //  pn = bfs_test.predNodeMap();
alpar@774
    60
  b  = bfs_test.reached(n);
alpar@774
    61
deba@2335
    62
  Path<Graph> pp = bfs_test.path(n);
alpar@774
    63
}
alpar@774
    64
alpar@1220
    65
void check_Bfs_Function_Compile() 
alpar@1220
    66
{
alpar@1220
    67
  typedef int VType;
alpar@2260
    68
  typedef concepts::Graph Graph;
alpar@1220
    69
alpar@1220
    70
  typedef Graph::Edge Edge;
alpar@1220
    71
  typedef Graph::Node Node;
alpar@1220
    72
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    73
  typedef Graph::NodeIt NodeIt;
alpar@2260
    74
  typedef concepts::ReadMap<Edge,VType> LengthMap;
alpar@1220
    75
   
alpar@2135
    76
  Graph g;
alpar@2135
    77
  bfs(g,Node()).run();
alpar@2135
    78
  bfs(g).source(Node()).run();
alpar@2135
    79
  bfs(g)
alpar@2260
    80
    .predMap(concepts::WriteMap<Node,Edge>())
alpar@2260
    81
    .distMap(concepts::WriteMap<Node,VType>())
alpar@2260
    82
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@2260
    83
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@1220
    84
    .run(Node());
alpar@1220
    85
  
alpar@1220
    86
}
alpar@1220
    87
alpar@774
    88
int main()
alpar@774
    89
{
alpar@774
    90
    
alpar@774
    91
  typedef SmartGraph Graph;
alpar@774
    92
alpar@774
    93
  typedef Graph::Edge Edge;
alpar@774
    94
  typedef Graph::Node Node;
alpar@774
    95
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    96
  typedef Graph::NodeIt NodeIt;
alpar@774
    97
  typedef Graph::EdgeMap<int> LengthMap;
alpar@774
    98
alpar@774
    99
  Graph G;
alpar@774
   100
  Node s, t;
alpar@774
   101
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@774
   102
   
alpar@774
   103
  s=ps.outer[2];
alpar@774
   104
  t=ps.inner[0];
alpar@774
   105
  
alpar@774
   106
  Bfs<Graph> bfs_test(G);
alpar@774
   107
  bfs_test.run(s);
alpar@774
   108
  
alpar@774
   109
  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
alpar@774
   110
deba@2335
   111
  Path<Graph> p = bfs_test.path(t);
alpar@1283
   112
  check(p.length()==3,"getPath() found a wrong path.");
deba@2335
   113
  check(checkPath(G, p),"path() found a wrong path.");
deba@2335
   114
  check(pathSource(G, p) == s,"path() found a wrong path.");
deba@2335
   115
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@1283
   116
  
alpar@774
   117
alpar@774
   118
  for(EdgeIt e(G); e==INVALID; ++e) {
alpar@986
   119
    Node u=G.source(e);
alpar@986
   120
    Node v=G.target(e);
alpar@774
   121
    check( !bfs_test.reached(u) ||
alpar@774
   122
	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
alpar@774
   123
	   "Wrong output.");
alpar@774
   124
  }
alpar@774
   125
alpar@780
   126
  for(NodeIt v(G); v==INVALID; ++v) {
alpar@780
   127
    check(bfs_test.reached(v),"Each node should be reached.");
deba@1763
   128
    if ( bfs_test.predEdge(v)!=INVALID ) {
deba@1763
   129
      Edge e=bfs_test.predEdge(v);
alpar@986
   130
      Node u=G.source(e);
alpar@780
   131
      check(u==bfs_test.predNode(v),"Wrong tree.");
alpar@774
   132
      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
alpar@780
   133
	    "Wrong distance. Difference: " 
alpar@774
   134
	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
alpar@774
   135
			- 1));
alpar@774
   136
    }
alpar@780
   137
  }
alpar@774
   138
}
alpar@780
   139