[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 | * |
---|
[877] | 5 | * Copyright (C) 2003-2010 |
---|
[100] | 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/dfs.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 | "6\n" |
---|
| 41 | "@arcs\n" |
---|
| 42 | " label\n" |
---|
| 43 | "0 1 0\n" |
---|
| 44 | "1 2 1\n" |
---|
| 45 | "2 3 2\n" |
---|
| 46 | "1 4 3\n" |
---|
| 47 | "4 2 4\n" |
---|
| 48 | "4 5 5\n" |
---|
| 49 | "5 0 6\n" |
---|
| 50 | "6 3 7\n" |
---|
| 51 | "@attributes\n" |
---|
| 52 | "source 0\n" |
---|
[906] | 53 | "target 5\n" |
---|
| 54 | "source1 6\n" |
---|
| 55 | "target1 3\n"; |
---|
| 56 | |
---|
[228] | 57 | |
---|
[209] | 58 | void checkDfsCompile() |
---|
[100] | 59 | { |
---|
| 60 | typedef concepts::Digraph Digraph; |
---|
| 61 | typedef Dfs<Digraph> DType; |
---|
[286] | 62 | typedef Digraph::Node Node; |
---|
| 63 | typedef Digraph::Arc Arc; |
---|
[209] | 64 | |
---|
[100] | 65 | Digraph G; |
---|
[286] | 66 | Node s, t; |
---|
| 67 | Arc e; |
---|
[585] | 68 | int l, i; |
---|
[100] | 69 | bool b; |
---|
[1083] | 70 | ::lemon::ignore_unused_variable_warning(l,i,b); |
---|
[1007] | 71 | |
---|
[100] | 72 | DType::DistMap d(G); |
---|
| 73 | DType::PredMap p(G); |
---|
[286] | 74 | Path<Digraph> pp; |
---|
[585] | 75 | concepts::ReadMap<Arc,bool> am; |
---|
[209] | 76 | |
---|
[286] | 77 | { |
---|
| 78 | DType dfs_test(G); |
---|
[585] | 79 | const DType& const_dfs_test = dfs_test; |
---|
[209] | 80 | |
---|
[286] | 81 | dfs_test.run(s); |
---|
| 82 | dfs_test.run(s,t); |
---|
| 83 | dfs_test.run(); |
---|
[209] | 84 | |
---|
[585] | 85 | dfs_test.init(); |
---|
| 86 | dfs_test.addSource(s); |
---|
| 87 | e = dfs_test.processNextArc(); |
---|
| 88 | e = const_dfs_test.nextArc(); |
---|
| 89 | b = const_dfs_test.emptyQueue(); |
---|
| 90 | i = const_dfs_test.queueSize(); |
---|
[877] | 91 | |
---|
[585] | 92 | dfs_test.start(); |
---|
| 93 | dfs_test.start(t); |
---|
| 94 | dfs_test.start(am); |
---|
| 95 | |
---|
| 96 | l = const_dfs_test.dist(t); |
---|
| 97 | e = const_dfs_test.predArc(t); |
---|
| 98 | s = const_dfs_test.predNode(t); |
---|
| 99 | b = const_dfs_test.reached(t); |
---|
| 100 | d = const_dfs_test.distMap(); |
---|
| 101 | p = const_dfs_test.predMap(); |
---|
| 102 | pp = const_dfs_test.path(t); |
---|
[286] | 103 | } |
---|
| 104 | { |
---|
| 105 | DType |
---|
| 106 | ::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
---|
| 107 | ::SetDistMap<concepts::ReadWriteMap<Node,int> > |
---|
| 108 | ::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
---|
[585] | 109 | ::SetStandardProcessedMap |
---|
[286] | 110 | ::SetProcessedMap<concepts::WriteMap<Node,bool> > |
---|
| 111 | ::Create dfs_test(G); |
---|
[100] | 112 | |
---|
[585] | 113 | concepts::ReadWriteMap<Node,Arc> pred_map; |
---|
| 114 | concepts::ReadWriteMap<Node,int> dist_map; |
---|
| 115 | concepts::ReadWriteMap<Node,bool> reached_map; |
---|
| 116 | concepts::WriteMap<Node,bool> processed_map; |
---|
[877] | 117 | |
---|
[585] | 118 | dfs_test |
---|
| 119 | .predMap(pred_map) |
---|
| 120 | .distMap(dist_map) |
---|
| 121 | .reachedMap(reached_map) |
---|
| 122 | .processedMap(processed_map); |
---|
| 123 | |
---|
[286] | 124 | dfs_test.run(s); |
---|
| 125 | dfs_test.run(s,t); |
---|
| 126 | dfs_test.run(); |
---|
[585] | 127 | dfs_test.init(); |
---|
| 128 | |
---|
| 129 | dfs_test.addSource(s); |
---|
| 130 | e = dfs_test.processNextArc(); |
---|
| 131 | e = dfs_test.nextArc(); |
---|
| 132 | b = dfs_test.emptyQueue(); |
---|
| 133 | i = dfs_test.queueSize(); |
---|
[877] | 134 | |
---|
[585] | 135 | dfs_test.start(); |
---|
| 136 | dfs_test.start(t); |
---|
| 137 | dfs_test.start(am); |
---|
[286] | 138 | |
---|
| 139 | l = dfs_test.dist(t); |
---|
| 140 | e = dfs_test.predArc(t); |
---|
| 141 | s = dfs_test.predNode(t); |
---|
| 142 | b = dfs_test.reached(t); |
---|
| 143 | pp = dfs_test.path(t); |
---|
| 144 | } |
---|
[100] | 145 | } |
---|
| 146 | |
---|
[209] | 147 | void checkDfsFunctionCompile() |
---|
[100] | 148 | { |
---|
| 149 | typedef int VType; |
---|
| 150 | typedef concepts::Digraph Digraph; |
---|
| 151 | typedef Digraph::Arc Arc; |
---|
| 152 | typedef Digraph::Node Node; |
---|
[209] | 153 | |
---|
[100] | 154 | Digraph g; |
---|
[278] | 155 | bool b; |
---|
[1083] | 156 | ::lemon::ignore_unused_variable_warning(b); |
---|
[1007] | 157 | |
---|
[278] | 158 | dfs(g).run(Node()); |
---|
| 159 | b=dfs(g).run(Node(),Node()); |
---|
| 160 | dfs(g).run(); |
---|
[100] | 161 | dfs(g) |
---|
[278] | 162 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 163 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
[100] | 164 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 165 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
[209] | 166 | .run(Node()); |
---|
[278] | 167 | b=dfs(g) |
---|
| 168 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 169 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 170 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 171 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 172 | .path(concepts::Path<Digraph>()) |
---|
| 173 | .dist(VType()) |
---|
| 174 | .run(Node(),Node()); |
---|
| 175 | dfs(g) |
---|
| 176 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 177 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 178 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 179 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 180 | .run(); |
---|
[100] | 181 | } |
---|
| 182 | |
---|
[171] | 183 | template <class Digraph> |
---|
| 184 | void checkDfs() { |
---|
| 185 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
[100] | 186 | |
---|
| 187 | Digraph G; |
---|
| 188 | Node s, t; |
---|
[906] | 189 | Node s1, t1; |
---|
[209] | 190 | |
---|
[228] | 191 | std::istringstream input(test_lgf); |
---|
[293] | 192 | digraphReader(G, input). |
---|
[228] | 193 | node("source", s). |
---|
| 194 | node("target", t). |
---|
[906] | 195 | node("source1", s1). |
---|
| 196 | node("target1", t1). |
---|
[228] | 197 | run(); |
---|
[209] | 198 | |
---|
[100] | 199 | Dfs<Digraph> dfs_test(G); |
---|
[209] | 200 | dfs_test.run(s); |
---|
| 201 | |
---|
[100] | 202 | Path<Digraph> p = dfs_test.path(t); |
---|
[171] | 203 | check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
---|
[100] | 204 | check(checkPath(G, p),"path() found a wrong path."); |
---|
| 205 | check(pathSource(G, p) == s,"path() found a wrong path."); |
---|
| 206 | check(pathTarget(G, p) == t,"path() found a wrong path."); |
---|
[209] | 207 | |
---|
[100] | 208 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
[228] | 209 | if (dfs_test.reached(v)) { |
---|
| 210 | check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
---|
| 211 | if (dfs_test.predArc(v)!=INVALID ) { |
---|
| 212 | Arc e=dfs_test.predArc(v); |
---|
| 213 | Node u=G.source(e); |
---|
| 214 | check(u==dfs_test.predNode(v),"Wrong tree."); |
---|
| 215 | check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
---|
| 216 | "Wrong distance. (" << dfs_test.dist(u) << "->" |
---|
[278] | 217 | << dfs_test.dist(v) << ")"); |
---|
[228] | 218 | } |
---|
[100] | 219 | } |
---|
| 220 | } |
---|
[278] | 221 | |
---|
| 222 | { |
---|
[906] | 223 | Dfs<Digraph> dfs(G); |
---|
| 224 | check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6."); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | { |
---|
[278] | 228 | NullMap<Node,Arc> myPredMap; |
---|
| 229 | dfs(G).predMap(myPredMap).run(s); |
---|
| 230 | } |
---|
[100] | 231 | } |
---|
| 232 | |
---|
[171] | 233 | int main() |
---|
| 234 | { |
---|
| 235 | checkDfs<ListDigraph>(); |
---|
| 236 | checkDfs<SmartDigraph>(); |
---|
| 237 | return 0; |
---|
| 238 | } |
---|