1
4
0
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/invalid.h> |
27 | 27 |
|
28 | 28 |
namespace lemon { |
29 | 29 |
|
30 | 30 |
namespace concepts { |
31 | 31 |
|
32 | 32 |
/// \addtogroup concept |
33 | 33 |
/// @{ |
34 | 34 |
|
35 | 35 |
/// \brief The heap concept. |
36 | 36 |
/// |
37 | 37 |
/// Concept class describing the main interface of heaps. |
38 | 38 |
template <typename Priority, typename ItemIntMap> |
39 | 39 |
class Heap { |
40 | 40 |
public: |
41 | 41 |
|
42 | 42 |
/// Type of the items stored in the heap. |
43 | 43 |
typedef typename ItemIntMap::Key Item; |
44 | 44 |
|
45 | 45 |
/// Type of the priorities. |
46 | 46 |
typedef Priority Prio; |
47 | 47 |
|
48 | 48 |
/// \brief Type to represent the states of the items. |
49 | 49 |
/// |
50 | 50 |
/// Each item has a state associated to it. It can be "in heap", |
51 | 51 |
/// "pre heap" or "post heap". The later two are indifferent |
52 | 52 |
/// from the point of view of the heap, but may be useful for |
53 | 53 |
/// the user. |
54 | 54 |
/// |
55 | 55 |
/// The \c ItemIntMap must be initialized in such a way, that it |
56 | 56 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
57 | 57 |
enum State { |
58 | 58 |
IN_HEAP = 0, |
59 | 59 |
PRE_HEAP = -1, |
60 | 60 |
POST_HEAP = -2 |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
/// \brief The constructor. |
64 | 64 |
/// |
65 | 65 |
/// The constructor. |
66 | 66 |
/// \param map A map that assigns \c int values to keys of type |
67 | 67 |
/// \c Item. It is used internally by the heap implementations to |
68 | 68 |
/// handle the cross references. The assigned value must be |
69 | 69 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
70 | 70 |
explicit Heap(ItemIntMap &map) {} |
71 | 71 |
|
72 | 72 |
/// \brief The number of items stored in the heap. |
73 | 73 |
/// |
74 | 74 |
/// Returns the number of items stored in the heap. |
75 | 75 |
int size() const { return 0; } |
76 | 76 |
|
77 | 77 |
/// \brief Checks if the heap is empty. |
78 | 78 |
/// |
79 | 79 |
/// Returns \c true if the heap is empty. |
80 | 80 |
bool empty() const { return false; } |
81 | 81 |
|
82 | 82 |
/// \brief Makes the heap empty. |
83 | 83 |
/// |
84 | 84 |
/// Makes the heap empty. |
85 | 85 |
void clear(); |
86 | 86 |
|
87 | 87 |
/// \brief Inserts an item into the heap with the given priority. |
88 | 88 |
/// |
89 | 89 |
/// Inserts the given item into the heap with the given priority. |
90 | 90 |
/// \param i The item to insert. |
91 | 91 |
/// \param p The priority of the item. |
92 | 92 |
void push(const Item &i, const Prio &p) {} |
93 | 93 |
|
94 | 94 |
/// \brief Returns the item having minimum priority. |
95 | 95 |
/// |
96 | 96 |
/// Returns the item having minimum priority. |
97 | 97 |
/// \pre The heap must be non-empty. |
98 | 98 |
Item top() const {} |
99 | 99 |
|
100 | 100 |
/// \brief The minimum priority. |
101 | 101 |
/// |
102 | 102 |
/// Returns the minimum priority. |
103 | 103 |
/// \pre The heap must be non-empty. |
104 | 104 |
Prio prio() const {} |
105 | 105 |
|
106 | 106 |
/// \brief Removes the item having minimum priority. |
107 | 107 |
/// |
108 | 108 |
/// Removes the item having minimum priority. |
109 | 109 |
/// \pre The heap must be non-empty. |
110 | 110 |
void pop() {} |
111 | 111 |
|
112 | 112 |
/// \brief Removes an item from the heap. |
113 | 113 |
/// |
114 | 114 |
/// Removes the given item from the heap if it is already stored. |
115 | 115 |
/// \param i The item to delete. |
116 | 116 |
void erase(const Item &i) {} |
117 | 117 |
|
118 | 118 |
/// \brief The priority of an item. |
119 | 119 |
/// |
120 | 120 |
/// Returns the priority of the given item. |
121 | 121 |
/// \pre \c i must be in the heap. |
122 | 122 |
/// \param i The item. |
123 | 123 |
Prio operator[](const Item &i) const {} |
124 | 124 |
|
125 | 125 |
/// \brief Sets the priority of an item or inserts it, if it is |
126 | 126 |
/// not stored in the heap. |
127 | 127 |
/// |
128 | 128 |
/// This method sets the priority of the given item if it is |
129 | 129 |
/// already stored in the heap. |
130 | 130 |
/// Otherwise it inserts the given item with the given priority. |
131 | 131 |
/// |
132 | 132 |
/// It may throw an \ref UnderflowPriorityException. |
133 | 133 |
/// \param i The item. |
134 | 134 |
/// \param p The priority. |
135 | 135 |
void set(const Item &i, const Prio &p) {} |
136 | 136 |
|
137 | 137 |
/// \brief Decreases the priority of an item to the given value. |
138 | 138 |
/// |
139 | 139 |
/// Decreases the priority of an item to the given value. |
140 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
141 | 141 |
/// \param i The item. |
142 | 142 |
/// \param p The priority. |
143 | 143 |
void decrease(const Item &i, const Prio &p) {} |
144 | 144 |
|
145 | 145 |
/// \brief Increases the priority of an item to the given value. |
146 | 146 |
/// |
147 | 147 |
/// Increases the priority of an item to the given value. |
148 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
149 | 149 |
/// \param i The item. |
150 | 150 |
/// \param p The priority. |
151 | 151 |
void increase(const Item &i, const Prio &p) {} |
152 | 152 |
|
153 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
154 | 154 |
/// never been in the heap. |
155 | 155 |
/// |
156 | 156 |
/// This method returns \c PRE_HEAP if the given item has never |
157 | 157 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
158 | 158 |
/// and \c POST_HEAP otherwise. |
159 | 159 |
/// In the latter case it is possible that the item will get back |
160 | 160 |
/// to the heap again. |
161 | 161 |
/// \param i The item. |
162 | 162 |
State state(const Item &i) const {} |
163 | 163 |
|
164 | 164 |
/// \brief Sets the state of an item in the heap. |
165 | 165 |
/// |
166 | 166 |
/// Sets the state of the given item in the heap. It can be used |
167 | 167 |
/// to manually clear the heap when it is important to achive the |
168 | 168 |
/// better time complexity. |
169 | 169 |
/// \param i The item. |
170 | 170 |
/// \param st The state. It should not be \c IN_HEAP. |
171 | 171 |
void state(const Item& i, State st) {} |
172 | 172 |
|
173 | 173 |
|
174 | 174 |
template <typename _Heap> |
175 | 175 |
struct Constraints { |
176 | 176 |
public: |
177 | 177 |
void constraints() { |
178 | 178 |
typedef typename _Heap::Item OwnItem; |
179 | 179 |
typedef typename _Heap::Prio OwnPrio; |
180 | 180 |
typedef typename _Heap::State OwnState; |
181 | 181 |
|
182 | 182 |
Item item; |
183 | 183 |
Prio prio; |
184 |
State state; |
|
185 | 184 |
item=Item(); |
186 | 185 |
prio=Prio(); |
187 | 186 |
ignore_unused_variable_warning(item); |
188 | 187 |
ignore_unused_variable_warning(prio); |
189 |
ignore_unused_variable_warning(state); |
|
190 | 188 |
|
191 | 189 |
OwnItem own_item; |
192 | 190 |
OwnPrio own_prio; |
193 | 191 |
OwnState own_state; |
194 | 192 |
own_item=Item(); |
195 | 193 |
own_prio=Prio(); |
196 | 194 |
ignore_unused_variable_warning(own_item); |
197 | 195 |
ignore_unused_variable_warning(own_prio); |
198 | 196 |
ignore_unused_variable_warning(own_state); |
199 | 197 |
|
200 | 198 |
_Heap heap1(map); |
201 | 199 |
_Heap heap2 = heap1; |
202 | 200 |
ignore_unused_variable_warning(heap1); |
203 | 201 |
ignore_unused_variable_warning(heap2); |
204 | 202 |
|
205 | 203 |
int s = heap.size(); |
204 |
ignore_unused_variable_warning(s); |
|
206 | 205 |
bool e = heap.empty(); |
206 |
ignore_unused_variable_warning(e); |
|
207 | 207 |
|
208 | 208 |
prio = heap.prio(); |
209 | 209 |
item = heap.top(); |
210 | 210 |
prio = heap[item]; |
211 | 211 |
own_prio = heap.prio(); |
212 | 212 |
own_item = heap.top(); |
213 | 213 |
own_prio = heap[own_item]; |
214 | 214 |
|
215 | 215 |
heap.push(item, prio); |
216 | 216 |
heap.push(own_item, own_prio); |
217 | 217 |
heap.pop(); |
218 | 218 |
|
219 | 219 |
heap.set(item, prio); |
220 | 220 |
heap.decrease(item, prio); |
221 | 221 |
heap.increase(item, prio); |
222 | 222 |
heap.set(own_item, own_prio); |
223 | 223 |
heap.decrease(own_item, own_prio); |
224 | 224 |
heap.increase(own_item, own_prio); |
225 | 225 |
|
226 | 226 |
heap.erase(item); |
227 | 227 |
heap.erase(own_item); |
228 | 228 |
heap.clear(); |
229 | 229 |
|
230 |
state = heap.state(item); |
|
231 |
heap.state(item, state); |
|
232 |
|
|
230 |
own_state = heap.state(own_item); |
|
233 | 231 |
heap.state(own_item, own_state); |
234 | 232 |
|
235 |
state = _Heap::PRE_HEAP; |
|
236 |
state = _Heap::IN_HEAP; |
|
237 |
state = _Heap::POST_HEAP; |
|
238 | 233 |
own_state = _Heap::PRE_HEAP; |
239 | 234 |
own_state = _Heap::IN_HEAP; |
240 | 235 |
own_state = _Heap::POST_HEAP; |
241 | 236 |
} |
242 | 237 |
|
243 | 238 |
_Heap& heap; |
244 | 239 |
ItemIntMap& map; |
245 | 240 |
}; |
246 | 241 |
}; |
247 | 242 |
|
248 | 243 |
/// @} |
249 | 244 |
} // namespace lemon |
250 | 245 |
} |
251 | 246 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
include_directories (${LEMON_SOURCE_DIR}) |
2 | 2 |
|
3 | 3 |
link_directories (${LEMON_BINARY_DIR}/lemon) |
4 | 4 |
|
5 | 5 |
set (TESTS |
6 | 6 |
bfs_test |
7 | 7 |
counter_test |
8 | 8 |
dfs_test |
9 | 9 |
digraph_test |
10 | 10 |
dijkstra_test |
11 | 11 |
dim_test |
12 | 12 |
error_test |
13 | 13 |
graph_copy_test |
14 | 14 |
graph_test |
15 | 15 |
graph_utils_test |
16 |
heap_test |
|
16 | 17 |
kruskal_test |
17 | 18 |
maps_test |
18 | 19 |
path_test |
19 | 20 |
random_test |
20 | 21 |
time_measure_test |
21 | 22 |
unionfind_test) |
22 | 23 |
|
23 | 24 |
foreach (TEST_NAME ${TESTS}) |
24 | 25 |
add_executable (${TEST_NAME} ${TEST_NAME}.cc) |
25 | 26 |
target_link_libraries (${TEST_NAME} lemon) |
26 | 27 |
add_test(${TEST_NAME} ${TEST_NAME}) |
27 | 28 |
endforeach (TEST_NAME) |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
test/CMakeLists.txt |
3 | 3 |
|
4 | 4 |
noinst_HEADERS += \ |
5 | 5 |
test/graph_test.h \ |
6 |
test/heap_test.h \ |
|
7 | 6 |
test/graph_maps_test.h \ |
8 | 7 |
test/test_tools.h |
9 | 8 |
|
10 | 9 |
check_PROGRAMS += \ |
11 | 10 |
test/bfs_test \ |
12 | 11 |
test/counter_test \ |
13 | 12 |
test/dfs_test \ |
14 | 13 |
test/digraph_test \ |
15 | 14 |
test/dijkstra_test \ |
16 | 15 |
test/dim_test \ |
17 | 16 |
test/error_test \ |
18 | 17 |
test/graph_copy_test \ |
19 | 18 |
test/graph_test \ |
20 | 19 |
test/graph_utils_test \ |
20 |
test/heap_test \ |
|
21 | 21 |
test/kruskal_test \ |
22 | 22 |
test/maps_test \ |
23 | 23 |
test/random_test \ |
24 | 24 |
test/path_test \ |
25 | 25 |
test/test_tools_fail \ |
26 | 26 |
test/test_tools_pass \ |
27 | 27 |
test/time_measure_test \ |
28 | 28 |
test/unionfind_test |
29 | 29 |
|
30 | 30 |
TESTS += $(check_PROGRAMS) |
31 | 31 |
XFAIL_TESTS += test/test_tools_fail$(EXEEXT) |
32 | 32 |
|
33 | 33 |
test_bfs_test_SOURCES = test/bfs_test.cc |
34 | 34 |
test_counter_test_SOURCES = test/counter_test.cc |
35 | 35 |
test_dfs_test_SOURCES = test/dfs_test.cc |
36 | 36 |
test_digraph_test_SOURCES = test/digraph_test.cc |
37 | 37 |
test_dijkstra_test_SOURCES = test/dijkstra_test.cc |
38 | 38 |
test_dim_test_SOURCES = test/dim_test.cc |
39 | 39 |
test_error_test_SOURCES = test/error_test.cc |
40 | 40 |
test_graph_copy_test_SOURCES = test/graph_copy_test.cc |
41 | 41 |
test_graph_test_SOURCES = test/graph_test.cc |
42 | 42 |
test_graph_utils_test_SOURCES = test/graph_utils_test.cc |
43 |
|
|
43 |
test_heap_test_SOURCES = test/heap_test.cc |
|
44 | 44 |
test_kruskal_test_SOURCES = test/kruskal_test.cc |
45 | 45 |
test_maps_test_SOURCES = test/maps_test.cc |
46 | 46 |
test_path_test_SOURCES = test/path_test.cc |
47 | 47 |
test_random_test_SOURCES = test/random_test.cc |
48 | 48 |
test_test_tools_fail_SOURCES = test/test_tools_fail.cc |
49 | 49 |
test_test_tools_pass_SOURCES = test/test_tools_pass.cc |
50 | 50 |
test_time_measure_test_SOURCES = test/time_measure_test.cc |
51 | 51 |
test_unionfind_test_SOURCES = test/unionfind_test.cc |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <iostream> |
20 | 20 |
#include <fstream> |
21 | 21 |
#include <string> |
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
#include <lemon/concept_check.h> |
25 | 25 |
#include <lemon/concepts/heap.h> |
26 | 26 |
|
27 |
#include <lemon/ |
|
27 |
#include <lemon/smart_graph.h> |
|
28 | 28 |
|
29 |
#include <lemon/ |
|
29 |
#include <lemon/lgf_reader.h> |
|
30 |
#include <lemon/dijkstra.h> |
|
31 |
#include <lemon/maps.h> |
|
30 | 32 |
|
31 | 33 |
#include <lemon/bin_heap.h> |
32 |
#include <lemon/fib_heap.h> |
|
33 |
#include <lemon/radix_heap.h> |
|
34 |
#include <lemon/bucket_heap.h> |
|
35 | 34 |
|
36 | 35 |
#include "test_tools.h" |
37 | 36 |
|
38 |
#include "heap_test.h" |
|
39 |
|
|
40 |
#include <lemon/time_measure.h> |
|
41 |
|
|
42 | 37 |
using namespace lemon; |
43 | 38 |
using namespace lemon::concepts; |
44 | 39 |
|
40 |
typedef ListDigraph Digraph; |
|
41 |
DIGRAPH_TYPEDEFS(Digraph); |
|
42 |
|
|
43 |
char test_lgf[] = |
|
44 |
"@nodes\n" |
|
45 |
"label\n" |
|
46 |
"0\n" |
|
47 |
"1\n" |
|
48 |
"2\n" |
|
49 |
"3\n" |
|
50 |
"4\n" |
|
51 |
"5\n" |
|
52 |
"6\n" |
|
53 |
"7\n" |
|
54 |
"8\n" |
|
55 |
"9\n" |
|
56 |
"@arcs\n" |
|
57 |
" label capacity\n" |
|
58 |
"0 5 0 94\n" |
|
59 |
"3 9 1 11\n" |
|
60 |
"8 7 2 83\n" |
|
61 |
"1 2 3 94\n" |
|
62 |
"5 7 4 35\n" |
|
63 |
"7 4 5 84\n" |
|
64 |
"9 5 6 38\n" |
|
65 |
"0 4 7 96\n" |
|
66 |
"6 7 8 6\n" |
|
67 |
"3 1 9 27\n" |
|
68 |
"5 2 10 77\n" |
|
69 |
"5 6 11 69\n" |
|
70 |
"6 5 12 41\n" |
|
71 |
"4 6 13 70\n" |
|
72 |
"3 2 14 45\n" |
|
73 |
"7 9 15 93\n" |
|
74 |
"5 9 16 50\n" |
|
75 |
"9 0 17 94\n" |
|
76 |
"9 6 18 67\n" |
|
77 |
"0 9 19 86\n" |
|
78 |
"@attributes\n" |
|
79 |
"source 3\n"; |
|
80 |
|
|
81 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34}; |
|
82 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37}; |
|
83 |
|
|
84 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
|
85 |
|
|
86 |
template <typename Heap> |
|
87 |
void heapSortTest() { |
|
88 |
RangeMap<int> map(test_len, -1); |
|
89 |
|
|
90 |
Heap heap(map); |
|
91 |
|
|
92 |
std::vector<int> v(test_len); |
|
93 |
|
|
94 |
for (int i = 0; i < test_len; ++i) { |
|
95 |
v[i] = test_seq[i]; |
|
96 |
heap.push(i, v[i]); |
|
97 |
} |
|
98 |
std::sort(v.begin(), v.end()); |
|
99 |
for (int i = 0; i < test_len; ++i) { |
|
100 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
|
101 |
heap.pop(); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
template <typename Heap> |
|
106 |
void heapIncreaseTest() { |
|
107 |
RangeMap<int> map(test_len, -1); |
|
108 |
|
|
109 |
Heap heap(map); |
|
110 |
|
|
111 |
std::vector<int> v(test_len); |
|
112 |
|
|
113 |
for (int i = 0; i < test_len; ++i) { |
|
114 |
v[i] = test_seq[i]; |
|
115 |
heap.push(i, v[i]); |
|
116 |
} |
|
117 |
for (int i = 0; i < test_len; ++i) { |
|
118 |
v[i] += test_inc[i]; |
|
119 |
heap.increase(i, v[i]); |
|
120 |
} |
|
121 |
std::sort(v.begin(), v.end()); |
|
122 |
for (int i = 0; i < test_len; ++i) { |
|
123 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
|
124 |
heap.pop(); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
template <typename Heap> |
|
131 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
|
132 |
Node source) { |
|
133 |
|
|
134 |
typename Dijkstra<Digraph, IntArcMap>::template DefStandardHeap<Heap>:: |
|
135 |
Create dijkstra(digraph, length); |
|
136 |
|
|
137 |
dijkstra.run(source); |
|
138 |
|
|
139 |
for(ArcIt a(digraph); a != INVALID; ++a) { |
|
140 |
Node s = digraph.source(a); |
|
141 |
Node t = digraph.target(a); |
|
142 |
if (dijkstra.reached(s)) { |
|
143 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
|
144 |
"Error in a shortest path tree!"); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
for(NodeIt n(digraph); n != INVALID; ++n) { |
|
149 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) { |
|
150 |
Arc a = dijkstra.predArc(n); |
|
151 |
Node s = digraph.source(a); |
|
152 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
|
153 |
"Error in a shortest path tree!"); |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
|
|
45 | 159 |
int main() { |
46 | 160 |
|
47 | 161 |
typedef int Item; |
48 | 162 |
typedef int Prio; |
49 |
typedef |
|
163 |
typedef RangeMap<int> ItemIntMap; |
|
164 |
|
|
165 |
Digraph digraph; |
|
166 |
IntArcMap length(digraph); |
|
167 |
Node source; |
|
50 | 168 |
|
51 |
typedef ListDigraph Digraph; |
|
52 |
|
|
53 |
typedef Digraph::Arc Arc; |
|
54 |
typedef Digraph::Node Node; |
|
55 |
typedef Digraph::ArcIt ArcIt; |
|
56 |
typedef Digraph::NodeIt NodeIt; |
|
57 |
typedef Digraph::ArcMap<int> LengthMap; |
|
58 |
|
|
59 |
Digraph digraph; |
|
60 |
LengthMap length(digraph); |
|
61 |
Node start; |
|
62 |
|
|
63 |
/// \todo create own test digraph |
|
64 |
|
|
65 |
std::string f_name; |
|
66 |
if( getenv("srcdir") ) |
|
67 |
f_name = std::string(getenv("srcdir")); |
|
68 |
else f_name = "."; |
|
69 |
f_name += "/test/dijkstra_test.lgf"; |
|
70 |
|
|
71 |
std::ifstream input(f_name.c_str()); |
|
72 |
check(input, "Input file '" << f_name << "' not found."); |
|
73 |
DigraphReader<Digraph>(input, digraph). |
|
74 |
readArcMap("capacity", length). |
|
75 |
|
|
169 |
std::istringstream input(test_lgf); |
|
170 |
digraphReader(input, digraph). |
|
171 |
arcMap("capacity", length). |
|
172 |
node("source", source). |
|
76 | 173 |
run(); |
77 | 174 |
|
78 | 175 |
{ |
79 |
std::cout << "Checking Bin Heap" << std::endl; |
|
80 |
|
|
81 | 176 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
82 | 177 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
83 |
heapSortTest<IntHeap>(100); |
|
84 |
heapIncreaseTest<IntHeap>(100); |
|
178 |
heapSortTest<IntHeap>(); |
|
179 |
heapIncreaseTest<IntHeap>(); |
|
85 | 180 |
|
86 |
typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap; |
|
87 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
88 |
Timer timer; |
|
89 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
90 |
std::cout << timer << std::endl; |
|
91 |
} |
|
92 |
{ |
|
93 |
std::cout << "Checking Fib Heap" << std::endl; |
|
94 |
|
|
95 |
typedef FibHeap<Prio, ItemIntMap> IntHeap; |
|
96 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
97 |
heapSortTest<IntHeap>(100); |
|
98 |
heapIncreaseTest<IntHeap>(100); |
|
99 |
|
|
100 |
typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap; |
|
101 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
102 |
Timer timer; |
|
103 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
104 |
std::cout << timer << std::endl; |
|
105 |
} |
|
106 |
{ |
|
107 |
std::cout << "Checking Radix Heap" << std::endl; |
|
108 |
|
|
109 |
typedef RadixHeap<ItemIntMap> IntHeap; |
|
110 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
111 |
heapSortTest<IntHeap>(100); |
|
112 |
heapIncreaseTest<IntHeap>(100); |
|
113 |
|
|
114 |
typedef RadixHeap<Digraph::NodeMap<int> > NodeHeap; |
|
115 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
116 |
Timer timer; |
|
117 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
118 |
std::cout << timer << std::endl; |
|
119 |
} |
|
120 |
|
|
121 |
{ |
|
122 |
std::cout << "Checking Bucket Heap" << std::endl; |
|
123 |
|
|
124 |
typedef BucketHeap<ItemIntMap> IntHeap; |
|
125 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
126 |
heapSortTest<IntHeap>(100); |
|
127 |
heapIncreaseTest<IntHeap>(100); |
|
128 |
|
|
129 |
typedef BucketHeap<Digraph::NodeMap<int> > NodeHeap; |
|
130 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
131 |
Timer timer; |
|
132 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
133 |
std::cout << timer << std::endl; |
|
181 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
|
182 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
|
183 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
|
134 | 184 |
} |
135 | 185 |
|
136 | 186 |
return 0; |
137 | 187 |
} |
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 <vector> |
|
20 |
#include <algorithm> |
|
21 |
|
|
22 |
#include <lemon/dijkstra.h> |
|
23 |
|
|
24 |
class IntIntMap : public std::vector<int> { |
|
25 |
public: |
|
26 |
typedef std::vector<int> Parent; |
|
27 |
|
|
28 |
typedef int Key; |
|
29 |
typedef int Value; |
|
30 |
|
|
31 |
IntIntMap() : Parent() {} |
|
32 |
IntIntMap(int n) : Parent(n) {} |
|
33 |
IntIntMap(int n, int v) : Parent(n, v) {} |
|
34 |
|
|
35 |
void set(int key, int value) { |
|
36 |
Parent::operator[](key) = value; |
|
37 |
} |
|
38 |
}; |
|
39 |
|
|
40 |
|
|
41 |
template <typename _Heap> |
|
42 |
void heapSortTest(int n) { |
|
43 |
typedef _Heap Heap; |
|
44 |
IntIntMap map(n, -1); |
|
45 |
|
|
46 |
Heap heap(map); |
|
47 |
|
|
48 |
std::vector<int> v(n); |
|
49 |
|
|
50 |
for (int i = 0; i < n; ++i) { |
|
51 |
v[i] = rnd[1000]; |
|
52 |
heap.push(i, v[i]); |
|
53 |
} |
|
54 |
std::sort(v.begin(), v.end()); |
|
55 |
for (int i = 0; i < n; ++i) { |
|
56 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
|
57 |
heap.pop(); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
template <typename _Heap> |
|
62 |
void heapIncreaseTest(int n) { |
|
63 |
typedef _Heap Heap; |
|
64 |
IntIntMap map(n, -1); |
|
65 |
|
|
66 |
Heap heap(map); |
|
67 |
|
|
68 |
std::vector<int> v(n); |
|
69 |
|
|
70 |
for (int i = 0; i < n; ++i) { |
|
71 |
v[i] = rnd[1000]; |
|
72 |
heap.push(i, v[i]); |
|
73 |
} |
|
74 |
for (int i = 0; i < n; ++i) { |
|
75 |
v[i] += rnd[1000]; |
|
76 |
heap.increase(i, v[i]); |
|
77 |
} |
|
78 |
std::sort(v.begin(), v.end()); |
|
79 |
for (int i = 0; i < n; ++i) { |
|
80 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
|
81 |
heap.pop(); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
template <typename _Digraph, typename _LengthMap, typename _Heap> |
|
88 |
void dijkstraHeapTest(_Digraph& digraph, _LengthMap& length, |
|
89 |
typename _Digraph::Node& start) { |
|
90 |
|
|
91 |
typedef _Heap Heap; |
|
92 |
typedef _Digraph Digraph; |
|
93 |
typedef _LengthMap LengthMap; |
|
94 |
|
|
95 |
typedef typename Digraph::Node Node; |
|
96 |
typedef typename Digraph::Arc Arc; |
|
97 |
typedef typename Digraph::NodeIt NodeIt; |
|
98 |
typedef typename Digraph::ArcIt ArcIt; |
|
99 |
|
|
100 |
typename Dijkstra<Digraph, LengthMap>::template DefStandardHeap<Heap>:: |
|
101 |
Create dijkstra(digraph, length); |
|
102 |
|
|
103 |
dijkstra.run(start); |
|
104 |
|
|
105 |
for(ArcIt e(digraph); e!=INVALID; ++e) { |
|
106 |
Node u=digraph.source(e); |
|
107 |
Node v=digraph.target(e); |
|
108 |
if (dijkstra.reached(u)) { |
|
109 |
check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e], |
|
110 |
"Error in a shortest path tree arc!"); |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
for(NodeIt v(digraph); v!=INVALID; ++v) { |
|
115 |
if ( dijkstra.reached(v) && dijkstra.predArc(v) != INVALID ) { |
|
116 |
Arc e=dijkstra.predArc(v); |
|
117 |
Node u=digraph.source(e); |
|
118 |
check( dijkstra.dist(v) - dijkstra .dist(u) == length[e], |
|
119 |
"Error in a shortest path tree arc!"); |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
} |
0 comments (0 inline)