[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[100] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[100] | 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 | |
---|
[171] | 19 | #include <lemon/concepts/digraph.h> |
---|
| 20 | #include <lemon/smart_graph.h> |
---|
[100] | 21 | #include <lemon/list_graph.h> |
---|
[228] | 22 | #include <lemon/lgf_reader.h> |
---|
[100] | 23 | #include <lemon/bfs.h> |
---|
| 24 | #include <lemon/path.h> |
---|
[171] | 25 | |
---|
| 26 | #include "graph_test.h" |
---|
| 27 | #include "test_tools.h" |
---|
[100] | 28 | |
---|
| 29 | using namespace lemon; |
---|
| 30 | |
---|
[228] | 31 | char test_lgf[] = |
---|
| 32 | "@nodes\n" |
---|
| 33 | "label\n" |
---|
| 34 | "0\n" |
---|
| 35 | "1\n" |
---|
| 36 | "2\n" |
---|
| 37 | "3\n" |
---|
| 38 | "4\n" |
---|
| 39 | "5\n" |
---|
| 40 | "@arcs\n" |
---|
| 41 | " label\n" |
---|
| 42 | "0 1 0\n" |
---|
| 43 | "1 2 1\n" |
---|
| 44 | "2 3 2\n" |
---|
| 45 | "3 4 3\n" |
---|
| 46 | "0 3 4\n" |
---|
| 47 | "0 3 5\n" |
---|
| 48 | "5 2 6\n" |
---|
| 49 | "@attributes\n" |
---|
| 50 | "source 0\n" |
---|
| 51 | "target 4\n"; |
---|
| 52 | |
---|
[209] | 53 | void checkBfsCompile() |
---|
[100] | 54 | { |
---|
| 55 | typedef concepts::Digraph Digraph; |
---|
| 56 | typedef Bfs<Digraph> BType; |
---|
[209] | 57 | |
---|
[100] | 58 | Digraph G; |
---|
[171] | 59 | Digraph::Node n; |
---|
| 60 | Digraph::Arc e; |
---|
[100] | 61 | int l; |
---|
| 62 | bool b; |
---|
| 63 | BType::DistMap d(G); |
---|
| 64 | BType::PredMap p(G); |
---|
[209] | 65 | |
---|
[100] | 66 | BType bfs_test(G); |
---|
[209] | 67 | |
---|
[100] | 68 | bfs_test.run(n); |
---|
[209] | 69 | |
---|
[100] | 70 | l = bfs_test.dist(n); |
---|
| 71 | e = bfs_test.predArc(n); |
---|
| 72 | n = bfs_test.predNode(n); |
---|
| 73 | d = bfs_test.distMap(); |
---|
| 74 | p = bfs_test.predMap(); |
---|
| 75 | b = bfs_test.reached(n); |
---|
| 76 | |
---|
| 77 | Path<Digraph> pp = bfs_test.path(n); |
---|
| 78 | } |
---|
| 79 | |
---|
[209] | 80 | void checkBfsFunctionCompile() |
---|
[100] | 81 | { |
---|
| 82 | typedef int VType; |
---|
| 83 | typedef concepts::Digraph Digraph; |
---|
| 84 | typedef Digraph::Arc Arc; |
---|
| 85 | typedef Digraph::Node Node; |
---|
[209] | 86 | |
---|
[100] | 87 | Digraph g; |
---|
[278] | 88 | bool b; |
---|
| 89 | bfs(g).run(Node()); |
---|
| 90 | b=bfs(g).run(Node(),Node()); |
---|
| 91 | bfs(g).run(); |
---|
[100] | 92 | bfs(g) |
---|
[278] | 93 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 94 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
[100] | 95 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 96 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 97 | .run(Node()); |
---|
[278] | 98 | b=bfs(g) |
---|
| 99 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 100 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 101 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 102 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 103 | .path(concepts::Path<Digraph>()) |
---|
| 104 | .dist(VType()) |
---|
| 105 | .run(Node(),Node()); |
---|
| 106 | bfs(g) |
---|
| 107 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 108 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 109 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 110 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 111 | .run(); |
---|
[100] | 112 | } |
---|
| 113 | |
---|
[171] | 114 | template <class Digraph> |
---|
| 115 | void checkBfs() { |
---|
| 116 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
[100] | 117 | |
---|
| 118 | Digraph G; |
---|
| 119 | Node s, t; |
---|
[209] | 120 | |
---|
[228] | 121 | std::istringstream input(test_lgf); |
---|
| 122 | digraphReader(input, G). |
---|
| 123 | node("source", s). |
---|
| 124 | node("target", t). |
---|
| 125 | run(); |
---|
[209] | 126 | |
---|
[100] | 127 | Bfs<Digraph> bfs_test(G); |
---|
| 128 | bfs_test.run(s); |
---|
[209] | 129 | |
---|
[278] | 130 | check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
---|
[100] | 131 | |
---|
| 132 | Path<Digraph> p = bfs_test.path(t); |
---|
[228] | 133 | check(p.length()==2,"path() found a wrong path."); |
---|
[100] | 134 | check(checkPath(G, p),"path() found a wrong path."); |
---|
| 135 | check(pathSource(G, p) == s,"path() found a wrong path."); |
---|
| 136 | check(pathTarget(G, p) == t,"path() found a wrong path."); |
---|
[209] | 137 | |
---|
[100] | 138 | |
---|
[228] | 139 | for(ArcIt a(G); a!=INVALID; ++a) { |
---|
| 140 | Node u=G.source(a); |
---|
| 141 | Node v=G.target(a); |
---|
[100] | 142 | check( !bfs_test.reached(u) || |
---|
[222] | 143 | (bfs_test.dist(v) <= bfs_test.dist(u)+1), |
---|
[278] | 144 | "Wrong output. " << G.id(u) << "->" << G.id(v)); |
---|
[100] | 145 | } |
---|
| 146 | |
---|
[222] | 147 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
[228] | 148 | if (bfs_test.reached(v)) { |
---|
| 149 | check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); |
---|
| 150 | if (bfs_test.predArc(v)!=INVALID ) { |
---|
| 151 | Arc a=bfs_test.predArc(v); |
---|
| 152 | Node u=G.source(a); |
---|
| 153 | check(u==bfs_test.predNode(v),"Wrong tree."); |
---|
| 154 | check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
---|
| 155 | "Wrong distance. Difference: " |
---|
[278] | 156 | << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
---|
[228] | 157 | } |
---|
[100] | 158 | } |
---|
| 159 | } |
---|
[278] | 160 | |
---|
| 161 | { |
---|
| 162 | NullMap<Node,Arc> myPredMap; |
---|
| 163 | bfs(G).predMap(myPredMap).run(s); |
---|
| 164 | } |
---|
[100] | 165 | } |
---|
| 166 | |
---|
[171] | 167 | int main() |
---|
| 168 | { |
---|
| 169 | checkBfs<ListDigraph>(); |
---|
| 170 | checkBfs<SmartDigraph>(); |
---|
| 171 | return 0; |
---|
| 172 | } |
---|