1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    19 #include <lemon/concepts/digraph.h>
 
    20 #include <lemon/smart_graph.h>
 
    21 #include <lemon/list_graph.h>
 
    22 #include <lemon/lgf_reader.h>
 
    23 #include <lemon/bfs.h>
 
    24 #include <lemon/path.h>
 
    26 #include "graph_test.h"
 
    27 #include "test_tools.h"
 
    29 using namespace lemon;
 
    53 void checkBfsCompile()
 
    55   typedef concepts::Digraph Digraph;
 
    56   typedef Bfs<Digraph> BType;
 
    57   typedef Digraph::Node Node;
 
    58   typedef Digraph::Arc Arc;
 
    68   concepts::ReadMap<Node,bool> nm;
 
    72     const BType& const_bfs_test = bfs_test;
 
    79     bfs_test.addSource(s);
 
    80     n = bfs_test.processNextNode();
 
    81     n = bfs_test.processNextNode(t, b);
 
    82     n = bfs_test.processNextNode(nm, n);
 
    83     n = const_bfs_test.nextNode();
 
    84     b = const_bfs_test.emptyQueue();
 
    85     i = const_bfs_test.queueSize();
 
    91     l  = const_bfs_test.dist(t);
 
    92     e  = const_bfs_test.predArc(t);
 
    93     s  = const_bfs_test.predNode(t);
 
    94     b  = const_bfs_test.reached(t);
 
    95     d  = const_bfs_test.distMap();
 
    96     p  = const_bfs_test.predMap();
 
    97     pp = const_bfs_test.path(t);
 
   101       ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
 
   102       ::SetDistMap<concepts::ReadWriteMap<Node,int> >
 
   103       ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
 
   104       ::SetStandardProcessedMap
 
   105       ::SetProcessedMap<concepts::WriteMap<Node,bool> >
 
   106       ::Create bfs_test(G);
 
   108     concepts::ReadWriteMap<Node,Arc> pred_map;
 
   109     concepts::ReadWriteMap<Node,int> dist_map;
 
   110     concepts::ReadWriteMap<Node,bool> reached_map;
 
   111     concepts::WriteMap<Node,bool> processed_map;
 
   116       .reachedMap(reached_map)
 
   117       .processedMap(processed_map);
 
   124     bfs_test.addSource(s);
 
   125     n = bfs_test.processNextNode();
 
   126     n = bfs_test.processNextNode(t, b);
 
   127     n = bfs_test.processNextNode(nm, n);
 
   128     n = bfs_test.nextNode();
 
   129     b = bfs_test.emptyQueue();
 
   130     i = bfs_test.queueSize();
 
   136     l  = bfs_test.dist(t);
 
   137     e  = bfs_test.predArc(t);
 
   138     s  = bfs_test.predNode(t);
 
   139     b  = bfs_test.reached(t);
 
   140     pp = bfs_test.path(t);
 
   144 void checkBfsFunctionCompile()
 
   147   typedef concepts::Digraph Digraph;
 
   148   typedef Digraph::Arc Arc;
 
   149   typedef Digraph::Node Node;
 
   154   b=bfs(g).run(Node(),Node());
 
   157     .predMap(concepts::ReadWriteMap<Node,Arc>())
 
   158     .distMap(concepts::ReadWriteMap<Node,VType>())
 
   159     .reachedMap(concepts::ReadWriteMap<Node,bool>())
 
   160     .processedMap(concepts::WriteMap<Node,bool>())
 
   163     .predMap(concepts::ReadWriteMap<Node,Arc>())
 
   164     .distMap(concepts::ReadWriteMap<Node,VType>())
 
   165     .reachedMap(concepts::ReadWriteMap<Node,bool>())
 
   166     .processedMap(concepts::WriteMap<Node,bool>())
 
   167     .path(concepts::Path<Digraph>())
 
   171     .predMap(concepts::ReadWriteMap<Node,Arc>())
 
   172     .distMap(concepts::ReadWriteMap<Node,VType>())
 
   173     .reachedMap(concepts::ReadWriteMap<Node,bool>())
 
   174     .processedMap(concepts::WriteMap<Node,bool>())
 
   178 template <class Digraph>
 
   180   TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
 
   185   std::istringstream input(test_lgf);
 
   186   digraphReader(G, input).
 
   191   Bfs<Digraph> bfs_test(G);
 
   194   check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
 
   196   Path<Digraph> p = bfs_test.path(t);
 
   197   check(p.length()==2,"path() found a wrong path.");
 
   198   check(checkPath(G, p),"path() found a wrong path.");
 
   199   check(pathSource(G, p) == s,"path() found a wrong path.");
 
   200   check(pathTarget(G, p) == t,"path() found a wrong path.");
 
   203   for(ArcIt a(G); a!=INVALID; ++a) {
 
   206     check( !bfs_test.reached(u) ||
 
   207            (bfs_test.dist(v) <= bfs_test.dist(u)+1),
 
   208            "Wrong output. " << G.id(u) << "->" << G.id(v));
 
   211   for(NodeIt v(G); v!=INVALID; ++v) {
 
   212     if (bfs_test.reached(v)) {
 
   213       check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
 
   214       if (bfs_test.predArc(v)!=INVALID ) {
 
   215         Arc a=bfs_test.predArc(v);
 
   217         check(u==bfs_test.predNode(v),"Wrong tree.");
 
   218         check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
 
   219               "Wrong distance. Difference: "
 
   220               << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
 
   226     NullMap<Node,Arc> myPredMap;
 
   227     bfs(G).predMap(myPredMap).run(s);
 
   233   checkBfs<ListDigraph>();
 
   234   checkBfs<SmartDigraph>();