1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
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 | |
---|
19 | #include <lemon/concepts/digraph.h> |
---|
20 | #include <lemon/smart_graph.h> |
---|
21 | #include <lemon/list_graph.h> |
---|
22 | #include <lemon/lgf_reader.h> |
---|
23 | #include <lemon/bfs.h> |
---|
24 | #include <lemon/path.h> |
---|
25 | |
---|
26 | #include "graph_test.h" |
---|
27 | #include "test_tools.h" |
---|
28 | |
---|
29 | using namespace lemon; |
---|
30 | |
---|
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 | |
---|
53 | void checkBfsCompile() |
---|
54 | { |
---|
55 | typedef concepts::Digraph Digraph; |
---|
56 | typedef Bfs<Digraph> BType; |
---|
57 | |
---|
58 | Digraph G; |
---|
59 | Digraph::Node n; |
---|
60 | Digraph::Arc e; |
---|
61 | int l; |
---|
62 | bool b; |
---|
63 | BType::DistMap d(G); |
---|
64 | BType::PredMap p(G); |
---|
65 | |
---|
66 | BType bfs_test(G); |
---|
67 | |
---|
68 | bfs_test.run(n); |
---|
69 | |
---|
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 | |
---|
80 | void checkBfsFunctionCompile() |
---|
81 | { |
---|
82 | typedef int VType; |
---|
83 | typedef concepts::Digraph Digraph; |
---|
84 | typedef Digraph::Arc Arc; |
---|
85 | typedef Digraph::Node Node; |
---|
86 | |
---|
87 | Digraph g; |
---|
88 | bool b; |
---|
89 | bfs(g).run(Node()); |
---|
90 | b=bfs(g).run(Node(),Node()); |
---|
91 | bfs(g).run(); |
---|
92 | bfs(g) |
---|
93 | .predMap(concepts::ReadWriteMap<Node,Arc>()) |
---|
94 | .distMap(concepts::ReadWriteMap<Node,VType>()) |
---|
95 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
96 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
97 | .run(Node()); |
---|
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(); |
---|
112 | } |
---|
113 | |
---|
114 | template <class Digraph> |
---|
115 | void checkBfs() { |
---|
116 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
117 | |
---|
118 | Digraph G; |
---|
119 | Node s, t; |
---|
120 | |
---|
121 | std::istringstream input(test_lgf); |
---|
122 | digraphReader(input, G). |
---|
123 | node("source", s). |
---|
124 | node("target", t). |
---|
125 | run(); |
---|
126 | |
---|
127 | Bfs<Digraph> bfs_test(G); |
---|
128 | bfs_test.run(s); |
---|
129 | |
---|
130 | check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
---|
131 | |
---|
132 | Path<Digraph> p = bfs_test.path(t); |
---|
133 | check(p.length()==2,"path() found a wrong path."); |
---|
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."); |
---|
137 | |
---|
138 | |
---|
139 | for(ArcIt a(G); a!=INVALID; ++a) { |
---|
140 | Node u=G.source(a); |
---|
141 | Node v=G.target(a); |
---|
142 | check( !bfs_test.reached(u) || |
---|
143 | (bfs_test.dist(v) <= bfs_test.dist(u)+1), |
---|
144 | "Wrong output. " << G.id(u) << "->" << G.id(v)); |
---|
145 | } |
---|
146 | |
---|
147 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
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: " |
---|
156 | << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | { |
---|
162 | NullMap<Node,Arc> myPredMap; |
---|
163 | bfs(G).predMap(myPredMap).run(s); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | int main() |
---|
168 | { |
---|
169 | checkBfs<ListDigraph>(); |
---|
170 | checkBfs<SmartDigraph>(); |
---|
171 | return 0; |
---|
172 | } |
---|