test/bfs_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 15 Jun 2008 22:03:33 +0200
changeset 170 91fb4372688f
child 171 02f4d5d9bfd7
permissions -rw-r--r--
Port dijkstra_test.cc from SVN -r3499
alpar@100
     1
/* -*- C++ -*-
alpar@100
     2
 *
alpar@100
     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
alpar@100
    19
#include "test_tools.h"
alpar@100
    20
//#include <lemon/smart_graph.h>
alpar@100
    21
#include <lemon/list_graph.h>
alpar@100
    22
#include <lemon/bfs.h>
alpar@100
    23
#include <lemon/path.h>
alpar@100
    24
#include<lemon/concepts/digraph.h>
alpar@100
    25
alpar@100
    26
using namespace lemon;
alpar@100
    27
alpar@100
    28
const int PET_SIZE =5;
alpar@100
    29
alpar@100
    30
alpar@100
    31
void check_Bfs_Compile() 
alpar@100
    32
{
alpar@100
    33
  typedef concepts::Digraph Digraph;
alpar@100
    34
alpar@100
    35
  typedef Digraph::Arc Arc;
alpar@100
    36
  typedef Digraph::Node Node;
alpar@100
    37
  typedef Digraph::ArcIt ArcIt;
alpar@100
    38
  typedef Digraph::NodeIt NodeIt;
alpar@100
    39
 
alpar@100
    40
  typedef Bfs<Digraph> BType;
alpar@100
    41
  
alpar@100
    42
  Digraph G;
alpar@100
    43
  Node n;
alpar@100
    44
  Arc e;
alpar@100
    45
  int l;
alpar@100
    46
  bool b;
alpar@100
    47
  BType::DistMap d(G);
alpar@100
    48
  BType::PredMap p(G);
alpar@100
    49
  //  BType::PredNodeMap pn(G);
alpar@100
    50
  
alpar@100
    51
  BType bfs_test(G);
alpar@100
    52
  
alpar@100
    53
  bfs_test.run(n);
alpar@100
    54
  
alpar@100
    55
  l  = bfs_test.dist(n);
alpar@100
    56
  e  = bfs_test.predArc(n);
alpar@100
    57
  n  = bfs_test.predNode(n);
alpar@100
    58
  d  = bfs_test.distMap();
alpar@100
    59
  p  = bfs_test.predMap();
alpar@100
    60
  //  pn = bfs_test.predNodeMap();
alpar@100
    61
  b  = bfs_test.reached(n);
alpar@100
    62
alpar@100
    63
  Path<Digraph> pp = bfs_test.path(n);
alpar@100
    64
}
alpar@100
    65
alpar@100
    66
void check_Bfs_Function_Compile() 
alpar@100
    67
{
alpar@100
    68
  typedef int VType;
alpar@100
    69
  typedef concepts::Digraph Digraph;
alpar@100
    70
alpar@100
    71
  typedef Digraph::Arc Arc;
alpar@100
    72
  typedef Digraph::Node Node;
alpar@100
    73
  typedef Digraph::ArcIt ArcIt;
alpar@100
    74
  typedef Digraph::NodeIt NodeIt;
alpar@100
    75
  typedef concepts::ReadMap<Arc,VType> LengthMap;
alpar@100
    76
   
alpar@100
    77
  Digraph g;
alpar@100
    78
  bfs(g,Node()).run();
alpar@100
    79
  bfs(g).source(Node()).run();
alpar@100
    80
  bfs(g)
alpar@100
    81
    .predMap(concepts::WriteMap<Node,Arc>())
alpar@100
    82
    .distMap(concepts::WriteMap<Node,VType>())
alpar@100
    83
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
alpar@100
    84
    .processedMap(concepts::WriteMap<Node,bool>())
alpar@100
    85
    .run(Node());
alpar@100
    86
  
alpar@100
    87
}
alpar@100
    88
alpar@100
    89
int main()
alpar@100
    90
{
alpar@100
    91
    
alpar@100
    92
  // typedef SmartDigraph Digraph;
alpar@100
    93
  typedef ListDigraph Digraph;
alpar@100
    94
alpar@100
    95
  typedef Digraph::Arc Arc;
alpar@100
    96
  typedef Digraph::Node Node;
alpar@100
    97
  typedef Digraph::ArcIt ArcIt;
alpar@100
    98
  typedef Digraph::NodeIt NodeIt;
alpar@100
    99
  typedef Digraph::ArcMap<int> LengthMap;
alpar@100
   100
alpar@100
   101
  Digraph G;
alpar@100
   102
  Node s, t;
alpar@100
   103
  PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
alpar@100
   104
   
alpar@100
   105
  s=ps.outer[2];
alpar@100
   106
  t=ps.inner[0];
alpar@100
   107
  
alpar@100
   108
  Bfs<Digraph> bfs_test(G);
alpar@100
   109
  bfs_test.run(s);
alpar@100
   110
  
alpar@100
   111
  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
alpar@100
   112
alpar@100
   113
  Path<Digraph> p = bfs_test.path(t);
alpar@100
   114
  check(p.length()==3,"getPath() found a wrong path.");
alpar@100
   115
  check(checkPath(G, p),"path() found a wrong path.");
alpar@100
   116
  check(pathSource(G, p) == s,"path() found a wrong path.");
alpar@100
   117
  check(pathTarget(G, p) == t,"path() found a wrong path.");
alpar@100
   118
  
alpar@100
   119
alpar@100
   120
  for(ArcIt e(G); e==INVALID; ++e) {
alpar@100
   121
    Node u=G.source(e);
alpar@100
   122
    Node v=G.target(e);
alpar@100
   123
    check( !bfs_test.reached(u) ||
alpar@100
   124
	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
alpar@100
   125
	   "Wrong output.");
alpar@100
   126
  }
alpar@100
   127
alpar@100
   128
  for(NodeIt v(G); v==INVALID; ++v) {
alpar@100
   129
    check(bfs_test.reached(v),"Each node should be reached.");
alpar@100
   130
    if ( bfs_test.predArc(v)!=INVALID ) {
alpar@100
   131
      Arc e=bfs_test.predArc(v);
alpar@100
   132
      Node u=G.source(e);
alpar@100
   133
      check(u==bfs_test.predNode(v),"Wrong tree.");
alpar@100
   134
      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
alpar@100
   135
	    "Wrong distance. Difference: " 
alpar@100
   136
	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
alpar@100
   137
			- 1));
alpar@100
   138
    }
alpar@100
   139
  }
alpar@100
   140
}
alpar@100
   141