[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/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" |
---|
| 53 | "target 5\n"; |
---|
| 54 | |
---|
[209] | 55 | void checkDfsCompile() |
---|
[100] | 56 | { |
---|
| 57 | typedef concepts::Digraph Digraph; |
---|
| 58 | typedef Dfs<Digraph> DType; |
---|
[209] | 59 | |
---|
[100] | 60 | Digraph G; |
---|
[171] | 61 | Digraph::Node n; |
---|
| 62 | Digraph::Arc e; |
---|
[100] | 63 | int l; |
---|
| 64 | bool b; |
---|
| 65 | DType::DistMap d(G); |
---|
| 66 | DType::PredMap p(G); |
---|
[209] | 67 | |
---|
[100] | 68 | DType dfs_test(G); |
---|
[209] | 69 | |
---|
[100] | 70 | dfs_test.run(n); |
---|
[209] | 71 | |
---|
[100] | 72 | l = dfs_test.dist(n); |
---|
| 73 | e = dfs_test.predArc(n); |
---|
| 74 | n = dfs_test.predNode(n); |
---|
| 75 | d = dfs_test.distMap(); |
---|
| 76 | p = dfs_test.predMap(); |
---|
| 77 | b = dfs_test.reached(n); |
---|
| 78 | |
---|
| 79 | Path<Digraph> pp = dfs_test.path(n); |
---|
| 80 | } |
---|
| 81 | |
---|
[209] | 82 | void checkDfsFunctionCompile() |
---|
[100] | 83 | { |
---|
| 84 | typedef int VType; |
---|
| 85 | typedef concepts::Digraph Digraph; |
---|
| 86 | typedef Digraph::Arc Arc; |
---|
| 87 | typedef Digraph::Node Node; |
---|
[209] | 88 | |
---|
[100] | 89 | Digraph g; |
---|
[278] | 90 | bool b; |
---|
| 91 | dfs(g).run(Node()); |
---|
| 92 | b=dfs(g).run(Node(),Node()); |
---|
| 93 | dfs(g).run(); |
---|
[100] | 94 | dfs(g) |
---|
[278] | 95 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 96 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
[100] | 97 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 98 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
[209] | 99 | .run(Node()); |
---|
[278] | 100 | b=dfs(g) |
---|
| 101 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 102 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 103 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 104 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 105 | .path(concepts::Path<Digraph>()) |
---|
| 106 | .dist(VType()) |
---|
| 107 | .run(Node(),Node()); |
---|
| 108 | dfs(g) |
---|
| 109 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
| 110 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
| 111 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
| 112 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
| 113 | .run(); |
---|
[100] | 114 | } |
---|
| 115 | |
---|
[171] | 116 | template <class Digraph> |
---|
| 117 | void checkDfs() { |
---|
| 118 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
[100] | 119 | |
---|
| 120 | Digraph G; |
---|
| 121 | Node s, t; |
---|
[209] | 122 | |
---|
[228] | 123 | std::istringstream input(test_lgf); |
---|
| 124 | digraphReader(input, G). |
---|
| 125 | node("source", s). |
---|
| 126 | node("target", t). |
---|
| 127 | run(); |
---|
[209] | 128 | |
---|
[100] | 129 | Dfs<Digraph> dfs_test(G); |
---|
[209] | 130 | dfs_test.run(s); |
---|
| 131 | |
---|
[100] | 132 | Path<Digraph> p = dfs_test.path(t); |
---|
[171] | 133 | check(p.length() == dfs_test.dist(t),"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 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
[228] | 139 | if (dfs_test.reached(v)) { |
---|
| 140 | check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
---|
| 141 | if (dfs_test.predArc(v)!=INVALID ) { |
---|
| 142 | Arc e=dfs_test.predArc(v); |
---|
| 143 | Node u=G.source(e); |
---|
| 144 | check(u==dfs_test.predNode(v),"Wrong tree."); |
---|
| 145 | check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
---|
| 146 | "Wrong distance. (" << dfs_test.dist(u) << "->" |
---|
[278] | 147 | << dfs_test.dist(v) << ")"); |
---|
[228] | 148 | } |
---|
[100] | 149 | } |
---|
| 150 | } |
---|
[278] | 151 | |
---|
| 152 | { |
---|
| 153 | NullMap<Node,Arc> myPredMap; |
---|
| 154 | dfs(G).predMap(myPredMap).run(s); |
---|
| 155 | } |
---|
[100] | 156 | } |
---|
| 157 | |
---|
[171] | 158 | int main() |
---|
| 159 | { |
---|
| 160 | checkDfs<ListDigraph>(); |
---|
| 161 | checkDfs<SmartDigraph>(); |
---|
| 162 | return 0; |
---|
| 163 | } |
---|