test/bfs_test.cc
changeset 100 4f754b4cf82b
child 171 02f4d5d9bfd7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/bfs_test.cc	Thu Feb 07 21:37:07 2008 +0000
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include "test_tools.h"
    1.23 +//#include <lemon/smart_graph.h>
    1.24 +#include <lemon/list_graph.h>
    1.25 +#include <lemon/bfs.h>
    1.26 +#include <lemon/path.h>
    1.27 +#include<lemon/concepts/digraph.h>
    1.28 +
    1.29 +using namespace lemon;
    1.30 +
    1.31 +const int PET_SIZE =5;
    1.32 +
    1.33 +
    1.34 +void check_Bfs_Compile() 
    1.35 +{
    1.36 +  typedef concepts::Digraph Digraph;
    1.37 +
    1.38 +  typedef Digraph::Arc Arc;
    1.39 +  typedef Digraph::Node Node;
    1.40 +  typedef Digraph::ArcIt ArcIt;
    1.41 +  typedef Digraph::NodeIt NodeIt;
    1.42 + 
    1.43 +  typedef Bfs<Digraph> BType;
    1.44 +  
    1.45 +  Digraph G;
    1.46 +  Node n;
    1.47 +  Arc e;
    1.48 +  int l;
    1.49 +  bool b;
    1.50 +  BType::DistMap d(G);
    1.51 +  BType::PredMap p(G);
    1.52 +  //  BType::PredNodeMap pn(G);
    1.53 +  
    1.54 +  BType bfs_test(G);
    1.55 +  
    1.56 +  bfs_test.run(n);
    1.57 +  
    1.58 +  l  = bfs_test.dist(n);
    1.59 +  e  = bfs_test.predArc(n);
    1.60 +  n  = bfs_test.predNode(n);
    1.61 +  d  = bfs_test.distMap();
    1.62 +  p  = bfs_test.predMap();
    1.63 +  //  pn = bfs_test.predNodeMap();
    1.64 +  b  = bfs_test.reached(n);
    1.65 +
    1.66 +  Path<Digraph> pp = bfs_test.path(n);
    1.67 +}
    1.68 +
    1.69 +void check_Bfs_Function_Compile() 
    1.70 +{
    1.71 +  typedef int VType;
    1.72 +  typedef concepts::Digraph Digraph;
    1.73 +
    1.74 +  typedef Digraph::Arc Arc;
    1.75 +  typedef Digraph::Node Node;
    1.76 +  typedef Digraph::ArcIt ArcIt;
    1.77 +  typedef Digraph::NodeIt NodeIt;
    1.78 +  typedef concepts::ReadMap<Arc,VType> LengthMap;
    1.79 +   
    1.80 +  Digraph g;
    1.81 +  bfs(g,Node()).run();
    1.82 +  bfs(g).source(Node()).run();
    1.83 +  bfs(g)
    1.84 +    .predMap(concepts::WriteMap<Node,Arc>())
    1.85 +    .distMap(concepts::WriteMap<Node,VType>())
    1.86 +    .reachedMap(concepts::ReadWriteMap<Node,bool>())
    1.87 +    .processedMap(concepts::WriteMap<Node,bool>())
    1.88 +    .run(Node());
    1.89 +  
    1.90 +}
    1.91 +
    1.92 +int main()
    1.93 +{
    1.94 +    
    1.95 +  // typedef SmartDigraph Digraph;
    1.96 +  typedef ListDigraph Digraph;
    1.97 +
    1.98 +  typedef Digraph::Arc Arc;
    1.99 +  typedef Digraph::Node Node;
   1.100 +  typedef Digraph::ArcIt ArcIt;
   1.101 +  typedef Digraph::NodeIt NodeIt;
   1.102 +  typedef Digraph::ArcMap<int> LengthMap;
   1.103 +
   1.104 +  Digraph G;
   1.105 +  Node s, t;
   1.106 +  PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
   1.107 +   
   1.108 +  s=ps.outer[2];
   1.109 +  t=ps.inner[0];
   1.110 +  
   1.111 +  Bfs<Digraph> bfs_test(G);
   1.112 +  bfs_test.run(s);
   1.113 +  
   1.114 +  check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
   1.115 +
   1.116 +  Path<Digraph> p = bfs_test.path(t);
   1.117 +  check(p.length()==3,"getPath() found a wrong path.");
   1.118 +  check(checkPath(G, p),"path() found a wrong path.");
   1.119 +  check(pathSource(G, p) == s,"path() found a wrong path.");
   1.120 +  check(pathTarget(G, p) == t,"path() found a wrong path.");
   1.121 +  
   1.122 +
   1.123 +  for(ArcIt e(G); e==INVALID; ++e) {
   1.124 +    Node u=G.source(e);
   1.125 +    Node v=G.target(e);
   1.126 +    check( !bfs_test.reached(u) ||
   1.127 +	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
   1.128 +	   "Wrong output.");
   1.129 +  }
   1.130 +
   1.131 +  for(NodeIt v(G); v==INVALID; ++v) {
   1.132 +    check(bfs_test.reached(v),"Each node should be reached.");
   1.133 +    if ( bfs_test.predArc(v)!=INVALID ) {
   1.134 +      Arc e=bfs_test.predArc(v);
   1.135 +      Node u=G.source(e);
   1.136 +      check(u==bfs_test.predNode(v),"Wrong tree.");
   1.137 +      check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
   1.138 +	    "Wrong distance. Difference: " 
   1.139 +	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
   1.140 +			- 1));
   1.141 +    }
   1.142 +  }
   1.143 +}
   1.144 +