Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
archeap_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <algorithm>
41#include <random>
42#include <unordered_map>
43#include <vector>
44
45#include <tpl_graph.H>
46#include <archeap.H>
47
48using namespace Aleph;
49
50namespace
51{
53 using Node = Graph::Node;
54 using Arc = Graph::Arc;
55
56 std::vector<Node *> make_nodes(Graph &g, int n)
57 {
58 std::vector<Node *> nodes;
59 nodes.reserve(n);
60 for (int i = 0; i < n; ++i)
61 nodes.push_back(g.insert_node(i));
62 return nodes;
63 }
64
65 struct ArcWeightDist
66 {
67 int operator()(Arc *a) const noexcept
68 {
69 return a->get_info();
70 }
71 };
72
74 using HeapNode = typename BinHeapType::Node;
75
76 struct MapAccess
77 {
78 std::unordered_map<Node *, HeapNode *> *map = nullptr;
79
80 MapAccess() = default;
81
82 explicit MapAccess(std::unordered_map<Node *, HeapNode *> &m) noexcept
83 : map(&m)
84 { }
85
86 HeapNode *& operator()(Node *p)
87 {
88 return (*map)[p];
89 }
90 };
91
93} // namespace
94
96{
97 Graph g;
98 auto nodes = make_nodes(g, 2);
99 Arc *a = g.insert_arc(nodes[0], nodes[1], 5);
100
101 std::unordered_map<Node *, HeapNode *> mapping;
102 MapAccess access(mapping);
103 TestArcHeap heap(ArcWeightDist{}, access);
104
105 heap.put_arc(a, nodes[1]);
106
107 ASSERT_EQ(mapping.size(), 1U);
108 ASSERT_NE(mapping[nodes[1]], nullptr);
109
110 Arc *min_arc = heap.get_min_arc();
111 EXPECT_EQ(min_arc, a);
112 EXPECT_EQ(mapping[nodes[1]], nullptr);
113}
114
116{
117 Graph g;
118 auto nodes = make_nodes(g, 2);
119 Arc *a1 = g.insert_arc(nodes[0], nodes[1], 10);
120 Arc *a2 = g.insert_arc(nodes[0], nodes[1], 3);
121
122 std::unordered_map<Node *, HeapNode *> mapping;
123 MapAccess access(mapping);
124 TestArcHeap heap(ArcWeightDist{}, access);
125
126 heap.put_arc(a1, nodes[1]);
127 heap.put_arc(a2, nodes[1]);
128
129 Arc *min_arc = heap.get_min_arc();
130 EXPECT_EQ(min_arc, a2);
131 EXPECT_EQ(mapping[nodes[1]], nullptr);
132}
133
135{
136 Graph g;
137 auto nodes = make_nodes(g, 2);
138 Arc *a1 = g.insert_arc(nodes[0], nodes[1], 2);
139 Arc *a2 = g.insert_arc(nodes[0], nodes[1], 7);
140
141 std::unordered_map<Node *, HeapNode *> mapping;
142 MapAccess access(mapping);
143 TestArcHeap heap(ArcWeightDist{}, access);
144
145 heap.put_arc(a1, nodes[1]);
146 heap.put_arc(a2, nodes[1]);
147
148 Arc *min_arc = heap.get_min_arc();
149 EXPECT_EQ(min_arc, a1);
150 EXPECT_EQ(mapping[nodes[1]], nullptr);
151}
152
154{
155 Graph g;
156 auto nodes = make_nodes(g, 4);
157 Arc *a1 = g.insert_arc(nodes[0], nodes[1], 5);
158 Arc *a2 = g.insert_arc(nodes[0], nodes[2], 1);
159 Arc *a3 = g.insert_arc(nodes[0], nodes[3], 3);
160
161 std::unordered_map<Node *, HeapNode *> mapping;
162 MapAccess access(mapping);
163 TestArcHeap heap(ArcWeightDist{}, access);
164
165 heap.put_arc(a1, nodes[1]);
166 heap.put_arc(a2, nodes[2]);
167 heap.put_arc(a3, nodes[3]);
168
169 std::vector<int> extracted;
170 extracted.reserve(3);
171 extracted.push_back(heap.get_min_arc()->get_info());
172 extracted.push_back(heap.get_min_arc()->get_info());
173 extracted.push_back(heap.get_min_arc()->get_info());
174
175 EXPECT_EQ(extracted.size(), 3U);
176 EXPECT_TRUE(std::is_sorted(extracted.begin(), extracted.end()));
177}
178
180{
181 Graph g;
182 auto nodes = make_nodes(g, 4);
183 Arc *a1 = g.insert_arc(nodes[0], nodes[1], 4);
184 Arc *a2 = g.insert_arc(nodes[0], nodes[2], 2);
185 Arc *a3 = g.insert_arc(nodes[0], nodes[3], 6);
186
187 std::unordered_map<Node *, HeapNode *> mapping;
188 MapAccess access(mapping);
189 TestArcHeap heap(ArcWeightDist{}, access);
190
191 heap.put_arc(a1, nodes[1]);
192 heap.put_arc(a2, nodes[2]);
193 heap.put_arc(a3, nodes[3]);
194
195 EXPECT_EQ(mapping.size(), 3U);
196 EXPECT_NE(mapping[nodes[1]], nullptr);
197 EXPECT_NE(mapping[nodes[2]], nullptr);
198 EXPECT_NE(mapping[nodes[3]], nullptr);
199
200 (void)heap.get_min_arc();
201 (void)heap.get_min_arc();
202 (void)heap.get_min_arc();
203
204 EXPECT_EQ(mapping[nodes[1]], nullptr);
205 EXPECT_EQ(mapping[nodes[2]], nullptr);
206 EXPECT_EQ(mapping[nodes[3]], nullptr);
207}
208
210{
211 std::mt19937 rng(123456);
212 std::uniform_int_distribution<int> node_count(2, 8);
213 std::uniform_int_distribution<int> weight_dist(0, 100);
214
215 for (int iter = 0; iter < 100; ++iter)
216 {
217 Graph g;
218 const int n = node_count(rng);
219 auto nodes = make_nodes(g, n);
220
221 std::unordered_map<Node *, HeapNode *> mapping;
222 MapAccess access(mapping);
223 TestArcHeap heap(ArcWeightDist{}, access);
224
225 std::vector<int> weights;
226 for (int i = 1; i < n; ++i)
227 {
228 const int w = weight_dist(rng);
229 Arc *a = g.insert_arc(nodes[0], nodes[i], w);
230 heap.put_arc(a, nodes[i]);
231 weights.push_back(w);
232 }
233
234 if (weights.empty())
235 continue;
236
237 std::vector<int> extracted;
238 extracted.reserve(weights.size());
239 for (size_t i = 0; i < weights.size(); ++i)
240 extracted.push_back(heap.get_min_arc()->get_info());
241
242 EXPECT_TRUE(std::is_sorted(extracted.begin(), extracted.end()));
243 }
244}
Arc heap for graph algorithms.
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
long double w
Definition btreepic.C:153
void empty() noexcept
empty the list
Definition htlist.H:1689
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Node Node
The graph type.
Definition tpl_graph.H:432
Arc Arc
The node class type.
Definition tpl_graph.H:433
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
static mt19937 rng
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Node heap without virtual destructor.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.