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