1 | /* -*- C++ -*- |
---|
2 | * src/test/dijkstra_test.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #include "test_tools.h" |
---|
18 | #include <lemon/smart_graph.h> |
---|
19 | #include <lemon/dijkstra.h> |
---|
20 | #include <lemon/maps.h> |
---|
21 | #include <lemon/concept/graph.h> |
---|
22 | #include <lemon/concept/maps.h> |
---|
23 | using namespace lemon; |
---|
24 | |
---|
25 | const int PET_SIZE =5; |
---|
26 | |
---|
27 | |
---|
28 | void check_Dijkstra_BinHeap_Compile() |
---|
29 | { |
---|
30 | typedef int VType; |
---|
31 | typedef concept::StaticGraph Graph; |
---|
32 | |
---|
33 | typedef Graph::Edge Edge; |
---|
34 | typedef Graph::Node Node; |
---|
35 | typedef Graph::EdgeIt EdgeIt; |
---|
36 | typedef Graph::NodeIt NodeIt; |
---|
37 | typedef concept::ReadMap<Edge,VType> LengthMap; |
---|
38 | |
---|
39 | typedef Dijkstra<Graph, LengthMap> DType; |
---|
40 | |
---|
41 | Graph G; |
---|
42 | Node n; |
---|
43 | Edge e; |
---|
44 | VType l; |
---|
45 | bool b; |
---|
46 | DType::DistMap d(G); |
---|
47 | DType::PredMap p(G); |
---|
48 | // DType::PredNodeMap pn(G); |
---|
49 | LengthMap cap; |
---|
50 | |
---|
51 | DType dijkstra_test(G,cap); |
---|
52 | |
---|
53 | dijkstra_test.run(n); |
---|
54 | |
---|
55 | l = dijkstra_test.dist(n); |
---|
56 | e = dijkstra_test.pred(n); |
---|
57 | n = dijkstra_test.predNode(n); |
---|
58 | d = dijkstra_test.distMap(); |
---|
59 | p = dijkstra_test.predMap(); |
---|
60 | // pn = dijkstra_test.predNodeMap(); |
---|
61 | b = dijkstra_test.reached(n); |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | void check_Dijkstra_Function_Compile() |
---|
66 | { |
---|
67 | typedef int VType; |
---|
68 | typedef concept::StaticGraph Graph; |
---|
69 | |
---|
70 | typedef Graph::Edge Edge; |
---|
71 | typedef Graph::Node Node; |
---|
72 | typedef Graph::EdgeIt EdgeIt; |
---|
73 | typedef Graph::NodeIt NodeIt; |
---|
74 | typedef concept::ReadMap<Edge,VType> LengthMap; |
---|
75 | |
---|
76 | dijkstra(Graph(),LengthMap(),Node()).run(); |
---|
77 | dijkstra(Graph(),LengthMap()).source(Node()).run(); |
---|
78 | dijkstra(Graph(),LengthMap()) |
---|
79 | .predMap(concept::WriteMap<Node,Edge>()) |
---|
80 | .distMap(concept::WriteMap<Node,VType>()) |
---|
81 | .run(Node()); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | int main() |
---|
87 | { |
---|
88 | |
---|
89 | typedef SmartGraph Graph; |
---|
90 | |
---|
91 | typedef Graph::Edge Edge; |
---|
92 | typedef Graph::Node Node; |
---|
93 | typedef Graph::EdgeIt EdgeIt; |
---|
94 | typedef Graph::NodeIt NodeIt; |
---|
95 | typedef Graph::EdgeMap<int> LengthMap; |
---|
96 | |
---|
97 | Graph G; |
---|
98 | Node s, t; |
---|
99 | LengthMap cap(G); |
---|
100 | PetStruct<Graph> ps = addPetersen(G,PET_SIZE); |
---|
101 | |
---|
102 | for(int i=0;i<PET_SIZE;i++) { |
---|
103 | cap[ps.outcir[i]]=4; |
---|
104 | cap[ps.incir[i]]=1; |
---|
105 | cap[ps.chords[i]]=10; |
---|
106 | } |
---|
107 | s=ps.outer[0]; |
---|
108 | t=ps.inner[1]; |
---|
109 | |
---|
110 | Dijkstra<Graph, LengthMap> |
---|
111 | dijkstra_test(G, cap); |
---|
112 | dijkstra_test.run(s); |
---|
113 | |
---|
114 | check(dijkstra_test.dist(t)==13,"Dijkstra found a wrong path."); |
---|
115 | |
---|
116 | |
---|
117 | for(EdgeIt e(G); e!=INVALID; ++e) { |
---|
118 | Node u=G.source(e); |
---|
119 | Node v=G.target(e); |
---|
120 | check( !dijkstra_test.reached(u) || |
---|
121 | (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap[e]), |
---|
122 | "dist(target)-dist(source)- edge_length= " |
---|
123 | << dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
124 | - cap[e]); |
---|
125 | } |
---|
126 | |
---|
127 | ///\bug This works only for integer lengths |
---|
128 | for(NodeIt v(G); v!=INVALID; ++v){ |
---|
129 | check(dijkstra_test.reached(v),"Each node should be reached."); |
---|
130 | if ( dijkstra_test.pred(v)!=INVALID ) { |
---|
131 | Edge e=dijkstra_test.pred(v); |
---|
132 | Node u=G.source(e); |
---|
133 | check(u==dijkstra_test.predNode(v),"Wrong tree."); |
---|
134 | check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == cap[e], |
---|
135 | "Wrong distance! Difference: " |
---|
136 | << std::abs(dijkstra_test.dist(v) - dijkstra_test.dist(u) |
---|
137 | - cap[e])); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | { |
---|
143 | NullMap<Node,Edge> myPredMap; |
---|
144 | dijkstra(G,cap).predMap(myPredMap).run(s); |
---|
145 | } |
---|
146 | return 0; |
---|
147 | } |
---|