1 | /* -*- C++ -*- |
---|
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/dfs.h> |
---|
23 | #include <lemon/path.h> |
---|
24 | |
---|
25 | #include "graph_test.h" |
---|
26 | #include "test_tools.h" |
---|
27 | |
---|
28 | using namespace lemon; |
---|
29 | |
---|
30 | void checkDfsCompile() |
---|
31 | { |
---|
32 | typedef concepts::Digraph Digraph; |
---|
33 | typedef Dfs<Digraph> DType; |
---|
34 | |
---|
35 | Digraph G; |
---|
36 | Digraph::Node n; |
---|
37 | Digraph::Arc e; |
---|
38 | int l; |
---|
39 | bool b; |
---|
40 | DType::DistMap d(G); |
---|
41 | DType::PredMap p(G); |
---|
42 | // DType::PredNodeMap pn(G); |
---|
43 | |
---|
44 | DType dfs_test(G); |
---|
45 | |
---|
46 | dfs_test.run(n); |
---|
47 | |
---|
48 | l = dfs_test.dist(n); |
---|
49 | e = dfs_test.predArc(n); |
---|
50 | n = dfs_test.predNode(n); |
---|
51 | d = dfs_test.distMap(); |
---|
52 | p = dfs_test.predMap(); |
---|
53 | // pn = dfs_test.predNodeMap(); |
---|
54 | b = dfs_test.reached(n); |
---|
55 | |
---|
56 | Path<Digraph> pp = dfs_test.path(n); |
---|
57 | } |
---|
58 | |
---|
59 | void checkDfsFunctionCompile() |
---|
60 | { |
---|
61 | typedef int VType; |
---|
62 | typedef concepts::Digraph Digraph; |
---|
63 | typedef Digraph::Arc Arc; |
---|
64 | typedef Digraph::Node Node; |
---|
65 | |
---|
66 | Digraph g; |
---|
67 | dfs(g,Node()).run(); |
---|
68 | dfs(g).source(Node()).run(); |
---|
69 | dfs(g) |
---|
70 | .predMap(concepts::WriteMap<Node,Arc>()) |
---|
71 | .distMap(concepts::WriteMap<Node,VType>()) |
---|
72 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) |
---|
73 | .processedMap(concepts::WriteMap<Node,bool>()) |
---|
74 | .run(Node()); |
---|
75 | } |
---|
76 | |
---|
77 | template <class Digraph> |
---|
78 | void checkDfs() { |
---|
79 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
80 | |
---|
81 | Digraph G; |
---|
82 | Node s, t; |
---|
83 | PetStruct<Digraph> ps = addPetersen(G, 5); |
---|
84 | |
---|
85 | s=ps.outer[2]; |
---|
86 | t=ps.inner[0]; |
---|
87 | |
---|
88 | Dfs<Digraph> dfs_test(G); |
---|
89 | dfs_test.run(s); |
---|
90 | |
---|
91 | Path<Digraph> p = dfs_test.path(t); |
---|
92 | check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
---|
93 | check(checkPath(G, p),"path() found a wrong path."); |
---|
94 | check(pathSource(G, p) == s,"path() found a wrong path."); |
---|
95 | check(pathTarget(G, p) == t,"path() found a wrong path."); |
---|
96 | |
---|
97 | for(NodeIt v(G); v!=INVALID; ++v) { |
---|
98 | check(dfs_test.reached(v),"Each node should be reached."); |
---|
99 | if ( dfs_test.predArc(v)!=INVALID ) { |
---|
100 | Arc e=dfs_test.predArc(v); |
---|
101 | Node u=G.source(e); |
---|
102 | check(u==dfs_test.predNode(v),"Wrong tree."); |
---|
103 | check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
---|
104 | "Wrong distance. (" << dfs_test.dist(u) << "->" |
---|
105 | <<dfs_test.dist(v) << ')'); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | int main() |
---|
111 | { |
---|
112 | checkDfs<ListDigraph>(); |
---|
113 | checkDfs<SmartDigraph>(); |
---|
114 | return 0; |
---|
115 | } |
---|