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