alpar@1956
|
1 |
/* -*- C++ -*-
|
alpar@1956
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2553
|
5 |
* Copyright (C) 2003-2008
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1956
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@1956
|
8 |
*
|
alpar@1956
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@1956
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@1956
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@1956
|
12 |
*
|
alpar@1956
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1956
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1956
|
15 |
* purpose.
|
alpar@1956
|
16 |
*
|
alpar@1956
|
17 |
*/
|
deba@1206
|
18 |
|
deba@1187
|
19 |
#include <vector>
|
deba@1187
|
20 |
#include <algorithm>
|
deba@1187
|
21 |
|
deba@1187
|
22 |
#include <lemon/dijkstra.h>
|
deba@1187
|
23 |
|
deba@1187
|
24 |
class IntIntMap : public std::vector<int> {
|
deba@1187
|
25 |
public:
|
deba@1187
|
26 |
typedef std::vector<int> Parent;
|
deba@1187
|
27 |
|
deba@2269
|
28 |
typedef int Key;
|
deba@2269
|
29 |
typedef int Value;
|
deba@2269
|
30 |
|
deba@1187
|
31 |
IntIntMap() : Parent() {}
|
deba@1187
|
32 |
IntIntMap(int n) : Parent(n) {}
|
deba@1187
|
33 |
IntIntMap(int n, int v) : Parent(n, v) {}
|
deba@1187
|
34 |
|
deba@1187
|
35 |
void set(int key, int value) {
|
deba@1187
|
36 |
Parent::operator[](key) = value;
|
deba@1187
|
37 |
}
|
deba@1187
|
38 |
};
|
deba@1187
|
39 |
|
deba@1187
|
40 |
|
deba@1187
|
41 |
template <typename _Heap>
|
deba@1187
|
42 |
void heapSortTest(int n) {
|
deba@1187
|
43 |
typedef _Heap Heap;
|
deba@1187
|
44 |
IntIntMap map(n, -1);
|
deba@1187
|
45 |
|
deba@1187
|
46 |
Heap heap(map);
|
deba@1187
|
47 |
|
deba@1187
|
48 |
std::vector<int> v(n);
|
deba@1187
|
49 |
|
deba@1187
|
50 |
for (int i = 0; i < n; ++i) {
|
deba@2242
|
51 |
v[i] = rnd[1000];
|
deba@1187
|
52 |
heap.push(i, v[i]);
|
deba@1187
|
53 |
}
|
deba@1187
|
54 |
std::sort(v.begin(), v.end());
|
deba@1187
|
55 |
for (int i = 0; i < n; ++i) {
|
deba@1187
|
56 |
check(v[i] == heap.prio() ,"Wrong order in heap sort.");
|
deba@1187
|
57 |
heap.pop();
|
deba@1187
|
58 |
}
|
deba@1187
|
59 |
}
|
deba@1187
|
60 |
|
deba@1206
|
61 |
template <typename _Heap>
|
deba@1206
|
62 |
void heapIncreaseTest(int n) {
|
deba@1206
|
63 |
typedef _Heap Heap;
|
deba@1206
|
64 |
IntIntMap map(n, -1);
|
deba@1206
|
65 |
|
deba@1206
|
66 |
Heap heap(map);
|
deba@1206
|
67 |
|
deba@1206
|
68 |
std::vector<int> v(n);
|
deba@1206
|
69 |
|
deba@1206
|
70 |
for (int i = 0; i < n; ++i) {
|
deba@2242
|
71 |
v[i] = rnd[1000];
|
deba@1206
|
72 |
heap.push(i, v[i]);
|
deba@1206
|
73 |
}
|
deba@1206
|
74 |
for (int i = 0; i < n; ++i) {
|
deba@2242
|
75 |
v[i] += rnd[1000];
|
deba@1206
|
76 |
heap.increase(i, v[i]);
|
deba@1206
|
77 |
}
|
deba@1206
|
78 |
std::sort(v.begin(), v.end());
|
deba@1206
|
79 |
for (int i = 0; i < n; ++i) {
|
deba@1206
|
80 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
|
deba@1206
|
81 |
heap.pop();
|
deba@1206
|
82 |
}
|
deba@1206
|
83 |
}
|
deba@1206
|
84 |
|
deba@1206
|
85 |
|
deba@1206
|
86 |
|
deba@1187
|
87 |
template <typename _Graph, typename _LengthMap, typename _Heap>
|
deba@1187
|
88 |
void dijkstraHeapTest(_Graph& graph, _LengthMap& length,
|
deba@1187
|
89 |
typename _Graph::Node& start) {
|
deba@1187
|
90 |
|
deba@1187
|
91 |
typedef _Heap Heap;
|
deba@1187
|
92 |
typedef _Graph Graph;
|
deba@1187
|
93 |
typedef _LengthMap LengthMap;
|
deba@1187
|
94 |
|
deba@1187
|
95 |
typedef typename Graph::Node Node;
|
deba@1187
|
96 |
typedef typename Graph::Edge Edge;
|
deba@1187
|
97 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1187
|
98 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@1187
|
99 |
|
deba@1745
|
100 |
typename Dijkstra<Graph, LengthMap>::template DefStandardHeap<Heap>::
|
deba@1728
|
101 |
Create dijkstra(graph, length);
|
deba@1187
|
102 |
|
deba@1187
|
103 |
dijkstra.run(start);
|
deba@1187
|
104 |
|
deba@1187
|
105 |
for(EdgeIt e(graph); e!=INVALID; ++e) {
|
deba@1187
|
106 |
Node u=graph.source(e);
|
deba@1187
|
107 |
Node v=graph.target(e);
|
deba@1187
|
108 |
if (dijkstra.reached(u)) {
|
deba@1187
|
109 |
check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
|
deba@1728
|
110 |
"Error in a shortest path tree edge!");
|
deba@1187
|
111 |
}
|
deba@1187
|
112 |
}
|
deba@1187
|
113 |
|
deba@1187
|
114 |
for(NodeIt v(graph); v!=INVALID; ++v) {
|
deba@1763
|
115 |
if ( dijkstra.reached(v) && dijkstra.predEdge(v) != INVALID ) {
|
deba@1763
|
116 |
Edge e=dijkstra.predEdge(v);
|
deba@1187
|
117 |
Node u=graph.source(e);
|
deba@1187
|
118 |
check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
|
deba@1187
|
119 |
"Error in a shortest path tree edge!");
|
deba@1187
|
120 |
}
|
deba@1187
|
121 |
}
|
deba@1187
|
122 |
|
deba@1187
|
123 |
}
|