test/bfs_test.cc
changeset 104 cdbba181b786
child 171 02f4d5d9bfd7
equal deleted inserted replaced
-1:000000000000 0:c80a3233e455
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2008
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 #include "test_tools.h"
       
    20 //#include <lemon/smart_graph.h>
       
    21 #include <lemon/list_graph.h>
       
    22 #include <lemon/bfs.h>
       
    23 #include <lemon/path.h>
       
    24 #include<lemon/concepts/digraph.h>
       
    25 
       
    26 using namespace lemon;
       
    27 
       
    28 const int PET_SIZE =5;
       
    29 
       
    30 
       
    31 void check_Bfs_Compile() 
       
    32 {
       
    33   typedef concepts::Digraph Digraph;
       
    34 
       
    35   typedef Digraph::Arc Arc;
       
    36   typedef Digraph::Node Node;
       
    37   typedef Digraph::ArcIt ArcIt;
       
    38   typedef Digraph::NodeIt NodeIt;
       
    39  
       
    40   typedef Bfs<Digraph> BType;
       
    41   
       
    42   Digraph G;
       
    43   Node n;
       
    44   Arc e;
       
    45   int l;
       
    46   bool b;
       
    47   BType::DistMap d(G);
       
    48   BType::PredMap p(G);
       
    49   //  BType::PredNodeMap pn(G);
       
    50   
       
    51   BType bfs_test(G);
       
    52   
       
    53   bfs_test.run(n);
       
    54   
       
    55   l  = bfs_test.dist(n);
       
    56   e  = bfs_test.predArc(n);
       
    57   n  = bfs_test.predNode(n);
       
    58   d  = bfs_test.distMap();
       
    59   p  = bfs_test.predMap();
       
    60   //  pn = bfs_test.predNodeMap();
       
    61   b  = bfs_test.reached(n);
       
    62 
       
    63   Path<Digraph> pp = bfs_test.path(n);
       
    64 }
       
    65 
       
    66 void check_Bfs_Function_Compile() 
       
    67 {
       
    68   typedef int VType;
       
    69   typedef concepts::Digraph Digraph;
       
    70 
       
    71   typedef Digraph::Arc Arc;
       
    72   typedef Digraph::Node Node;
       
    73   typedef Digraph::ArcIt ArcIt;
       
    74   typedef Digraph::NodeIt NodeIt;
       
    75   typedef concepts::ReadMap<Arc,VType> LengthMap;
       
    76    
       
    77   Digraph g;
       
    78   bfs(g,Node()).run();
       
    79   bfs(g).source(Node()).run();
       
    80   bfs(g)
       
    81     .predMap(concepts::WriteMap<Node,Arc>())
       
    82     .distMap(concepts::WriteMap<Node,VType>())
       
    83     .reachedMap(concepts::ReadWriteMap<Node,bool>())
       
    84     .processedMap(concepts::WriteMap<Node,bool>())
       
    85     .run(Node());
       
    86   
       
    87 }
       
    88 
       
    89 int main()
       
    90 {
       
    91     
       
    92   // typedef SmartDigraph Digraph;
       
    93   typedef ListDigraph Digraph;
       
    94 
       
    95   typedef Digraph::Arc Arc;
       
    96   typedef Digraph::Node Node;
       
    97   typedef Digraph::ArcIt ArcIt;
       
    98   typedef Digraph::NodeIt NodeIt;
       
    99   typedef Digraph::ArcMap<int> LengthMap;
       
   100 
       
   101   Digraph G;
       
   102   Node s, t;
       
   103   PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
       
   104    
       
   105   s=ps.outer[2];
       
   106   t=ps.inner[0];
       
   107   
       
   108   Bfs<Digraph> bfs_test(G);
       
   109   bfs_test.run(s);
       
   110   
       
   111   check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
       
   112 
       
   113   Path<Digraph> p = bfs_test.path(t);
       
   114   check(p.length()==3,"getPath() found a wrong path.");
       
   115   check(checkPath(G, p),"path() found a wrong path.");
       
   116   check(pathSource(G, p) == s,"path() found a wrong path.");
       
   117   check(pathTarget(G, p) == t,"path() found a wrong path.");
       
   118   
       
   119 
       
   120   for(ArcIt e(G); e==INVALID; ++e) {
       
   121     Node u=G.source(e);
       
   122     Node v=G.target(e);
       
   123     check( !bfs_test.reached(u) ||
       
   124 	   (bfs_test.dist(v) > bfs_test.dist(u)+1),
       
   125 	   "Wrong output.");
       
   126   }
       
   127 
       
   128   for(NodeIt v(G); v==INVALID; ++v) {
       
   129     check(bfs_test.reached(v),"Each node should be reached.");
       
   130     if ( bfs_test.predArc(v)!=INVALID ) {
       
   131       Arc e=bfs_test.predArc(v);
       
   132       Node u=G.source(e);
       
   133       check(u==bfs_test.predNode(v),"Wrong tree.");
       
   134       check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
       
   135 	    "Wrong distance. Difference: " 
       
   136 	    << std::abs(bfs_test.dist(v) - bfs_test.dist(u) 
       
   137 			- 1));
       
   138     }
       
   139   }
       
   140 }
       
   141