test/bfs_test.cc
author athos
Wed, 30 Aug 2006 16:08:03 +0000
changeset 2185 e2bf51eab7f7
parent 2111 ea1fa1bc3f6d
child 2247 269a0dcee70b
permissions -rw-r--r--
Fixed the mip error (mipstatus was incorrect).
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@1956
     5
 * Copyright (C) 2003-2006
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>
klao@959
    23
#include<lemon/concept/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
{
deba@2111
    32
  typedef concept::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
alpar@1283
    62
  DirPath<Graph> pp(G);
alpar@1283
    63
  bfs_test.getPath(pp,n);
alpar@774
    64
}
alpar@774
    65
alpar@1220
    66
void check_Bfs_Function_Compile() 
alpar@1220
    67
{
alpar@1220
    68
  typedef int VType;
deba@2111
    69
  typedef concept::Graph Graph;
alpar@1220
    70
alpar@1220
    71
  typedef Graph::Edge Edge;
alpar@1220
    72
  typedef Graph::Node Node;
alpar@1220
    73
  typedef Graph::EdgeIt EdgeIt;
alpar@1220
    74
  typedef Graph::NodeIt NodeIt;
alpar@1220
    75
  typedef concept::ReadMap<Edge,VType> LengthMap;
alpar@1220
    76
   
alpar@2135
    77
  Graph g;
alpar@2135
    78
  bfs(g,Node()).run();
alpar@2135
    79
  bfs(g).source(Node()).run();
alpar@2135
    80
  bfs(g)
alpar@1220
    81
    .predMap(concept::WriteMap<Node,Edge>())
alpar@1220
    82
    .distMap(concept::WriteMap<Node,VType>())
alpar@1220
    83
    .reachedMap(concept::ReadWriteMap<Node,bool>())
alpar@1220
    84
    .processedMap(concept::WriteMap<Node,bool>())
alpar@1220
    85
    .run(Node());
alpar@1220
    86
  
alpar@1220
    87
}
alpar@1220
    88
alpar@774
    89
int main()
alpar@774
    90
{
alpar@774
    91
    
alpar@774
    92
  typedef SmartGraph Graph;
alpar@774
    93
alpar@774
    94
  typedef Graph::Edge Edge;
alpar@774
    95
  typedef Graph::Node Node;
alpar@774
    96
  typedef Graph::EdgeIt EdgeIt;
alpar@774
    97
  typedef Graph::NodeIt NodeIt;
alpar@774
    98
  typedef Graph::EdgeMap<int> LengthMap;
alpar@774
    99
alpar@774
   100
  Graph G;
alpar@774
   101
  Node s, t;
alpar@774
   102
  PetStruct<Graph> ps = addPetersen(G,PET_SIZE);
alpar@774
   103
   
alpar@774
   104
  s=ps.outer[2];
alpar@774
   105
  t=ps.inner[0];
alpar@774
   106
  
alpar@774
   107
  Bfs<Graph> bfs_test(G);
alpar@774
   108
  bfs_test.run(s);
alpar@774
   109
  
alpar@774
   110
  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
alpar@774
   111
alpar@1283
   112
  DirPath<Graph> p(G);
alpar@1283
   113
  check(bfs_test.getPath(p,t),"getPath() failed to set the path.");
alpar@1283
   114
  check(p.length()==3,"getPath() found a wrong path.");
alpar@1283
   115
  
alpar@774
   116
alpar@774
   117
  for(EdgeIt e(G); e==INVALID; ++e) {
alpar@986
   118
    Node u=G.source(e);
alpar@986
   119
    Node v=G.target(e);
alpar@774
   120
    check( !bfs_test.reached(u) ||
alpar@774
   121
	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
alpar@774
   122
	   "Wrong output.");
alpar@774
   123
  }
alpar@774
   124
alpar@780
   125
  for(NodeIt v(G); v==INVALID; ++v) {
alpar@780
   126
    check(bfs_test.reached(v),"Each node should be reached.");
deba@1763
   127
    if ( bfs_test.predEdge(v)!=INVALID ) {
deba@1763
   128
      Edge e=bfs_test.predEdge(v);
alpar@986
   129
      Node u=G.source(e);
alpar@780
   130
      check(u==bfs_test.predNode(v),"Wrong tree.");
alpar@774
   131
      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
alpar@780
   132
	    "Wrong distance. Difference: " 
alpar@774
   133
	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
alpar@774
   134
			- 1));
alpar@774
   135
    }
alpar@780
   136
  }
alpar@774
   137
}
alpar@780
   138