Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
warshall_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 <tpl_graph.H>
41#include <tpl_graph_utils.H>
42#include <warshall.H>
43
44using namespace Aleph;
45
47
55
61
63{
64 g.insert_node(0);
65
67
68 EXPECT_EQ(mat.get_num_nodes(), 1u);
69 EXPECT_EQ(mat(0L, 0L), 0);
70}
71
73{
74 auto n0 = g.insert_node(0);
75 g.insert_arc(n0, n0, 1);
76
78
79 EXPECT_EQ(mat.get_num_nodes(), 1u);
80 EXPECT_EQ(mat(0L, 0L), 1);
81}
82
84{
85 auto n0 = g.insert_node(0);
86 auto n1 = g.insert_node(1);
87 g.insert_arc(n0, n1, 1);
88
90
91 EXPECT_EQ(mat.get_num_nodes(), 2u);
92 EXPECT_EQ(mat(0L, 1L), 1);
93 EXPECT_EQ(mat(1L, 0L), 1);
94}
95
97{
98 g.insert_node(0);
99 g.insert_node(1);
100
102
103 EXPECT_EQ(mat.get_num_nodes(), 2u);
104 EXPECT_EQ(mat(0L, 1L), 0);
105 EXPECT_EQ(mat(1L, 0L), 0);
106 EXPECT_EQ(mat(0L, 0L), 0);
107 EXPECT_EQ(mat(1L, 1L), 0);
108}
109
111{
112 auto n0 = g.insert_node(0);
113 auto n1 = g.insert_node(1);
114 auto n2 = g.insert_node(2);
115 auto n3 = g.insert_node(3);
116
117 g.insert_arc(n0, n1, 1);
118 g.insert_arc(n1, n2, 1);
119 g.insert_arc(n2, n3, 1);
120
122
123 EXPECT_EQ(mat(0L, 1L), 1);
124 EXPECT_EQ(mat(1L, 2L), 1);
125 EXPECT_EQ(mat(2L, 3L), 1);
126
127 EXPECT_EQ(mat(0L, 2L), 1);
128 EXPECT_EQ(mat(0L, 3L), 1);
129 EXPECT_EQ(mat(1L, 3L), 1);
130}
131
133{
134 auto n0 = g.insert_node(0);
135 auto n1 = g.insert_node(1);
136 auto n2 = g.insert_node(2);
137
138 g.insert_arc(n0, n1, 1);
139 g.insert_arc(n1, n2, 1);
140 g.insert_arc(n2, n0, 1);
141
143
144 for (long i = 0; i < 3; ++i)
145 for (long j = 0; j < 3; ++j)
146 if (i != j)
147 EXPECT_EQ(mat(i, j), 1);
148}
149
151{
152 auto n0 = g.insert_node(0);
153 auto n1 = g.insert_node(1);
154 auto n2 = g.insert_node(2);
155 auto n3 = g.insert_node(3);
156
157 g.insert_arc(n0, n1, 1);
158 g.insert_arc(n0, n2, 1);
159 g.insert_arc(n0, n3, 1);
160 g.insert_arc(n1, n2, 1);
161 g.insert_arc(n1, n3, 1);
162 g.insert_arc(n2, n3, 1);
163
165
166 for (long i = 0; i < 4; ++i)
167 for (long j = 0; j < 4; ++j)
168 if (i != j)
169 EXPECT_EQ(mat(i, j), 1);
170}
171
173{
174 auto n0 = g.insert_node(0);
175 auto n1 = g.insert_node(1);
176 auto n2 = g.insert_node(2);
177 auto n3 = g.insert_node(3);
178
179 g.insert_arc(n0, n1, 1);
180 g.insert_arc(n2, n3, 1);
181
183
184 EXPECT_EQ(mat.get_num_nodes(), 4u);
185
186 EXPECT_EQ(mat(n0, n1), 1);
187 EXPECT_EQ(mat(n1, n0), 1);
188 EXPECT_EQ(mat(n2, n3), 1);
189 EXPECT_EQ(mat(n3, n2), 1);
190
191 EXPECT_EQ(mat(n0, n2), 0);
192 EXPECT_EQ(mat(n0, n3), 0);
193 EXPECT_EQ(mat(n1, n2), 0);
194 EXPECT_EQ(mat(n1, n3), 0);
195 EXPECT_EQ(mat(n2, n0), 0);
196 EXPECT_EQ(mat(n2, n1), 0);
197 EXPECT_EQ(mat(n3, n0), 0);
198 EXPECT_EQ(mat(n3, n1), 0);
199}
200
202{
203 auto n0 = g.insert_node(0);
204 auto n1 = g.insert_node(1);
205 auto n2 = g.insert_node(2);
206 auto n3 = g.insert_node(3);
207
208 g.insert_arc(n0, n1, 1);
209 g.insert_arc(n0, n2, 1);
210 g.insert_arc(n1, n3, 1);
211 g.insert_arc(n2, n3, 1);
212
214
215 EXPECT_EQ(mat(0L, 1L), 1);
216 EXPECT_EQ(mat(0L, 2L), 1);
217 EXPECT_EQ(mat(0L, 3L), 1);
218 EXPECT_EQ(mat(1L, 3L), 1);
219 EXPECT_EQ(mat(2L, 3L), 1);
220}
221
223{
224 constexpr int N = 50;
225 std::vector<TestGraph::Node *> nodes;
226
227 for (int i = 0; i < N; ++i)
228 nodes.push_back(g.insert_node(i));
229
230 for (int i = 0; i < N - 1; ++i)
231 g.insert_arc(nodes[i], nodes[i + 1], 1);
232
234
235 EXPECT_EQ(mat.get_num_nodes(), static_cast<size_t>(N));
236 EXPECT_EQ(mat(0L, static_cast<long>(N - 1)), 1);
237 EXPECT_EQ(mat(static_cast<long>(N - 1), 0L), 1);
238}
239
241{
242 auto n0 = g.insert_node(0);
243 auto n1 = g.insert_node(1);
244 auto n2 = g.insert_node(2);
245
246 g.insert_arc(n0, n1, 1);
247 g.insert_arc(n1, n2, 1);
248
249 warshall(g, mat);
250
251 EXPECT_EQ(mat(0L, 1L), 1);
252 EXPECT_EQ(mat(1L, 2L), 1);
253 EXPECT_EQ(mat(0L, 2L), 1);
254}
255
257{
258 auto n0 = g.insert_node(0);
259 auto n1 = g.insert_node(1);
260 auto n2 = g.insert_node(2);
261
262 g.insert_arc(n0, n1, 1);
263 g.insert_arc(n1, n2, 1);
264
267
270
271 for (size_t i = 0; i < mat1.get_num_nodes(); ++i)
272 for (size_t j = 0; j < mat1.get_num_nodes(); ++j)
273 EXPECT_EQ(mat1(static_cast<long>(i), static_cast<long>(j)),
274 mat2(static_cast<long>(i), static_cast<long>(j)));
275}
276
278{
279 auto center = g.insert_node(0);
280 std::vector<TestGraph::Node *> leaves;
281
282 for (int i = 1; i <= 5; ++i)
283 {
284 auto leaf = g.insert_node(i);
285 leaves.push_back(leaf);
286 g.insert_arc(center, leaf, 1);
287 }
288
290
291 for (long i = 1; i <= 5; ++i)
292 EXPECT_EQ(mat(0L, i), 1);
293
294 for (long i = 1; i <= 5; ++i)
295 {
296 EXPECT_EQ(mat(i, 0L), 1);
297 for (long j = 1; j <= 5; ++j)
298 if (i != j)
299 EXPECT_EQ(mat(i, j), 1);
300 }
301}
302
303int main(int argc, char ** argv)
304{
305 ::testing::InitGoogleTest(&argc, argv);
306 return RUN_ALL_TESTS();
307}
int main()
Bit matrix for graph connectivity.
Computes the transitive closure of a graph using Warshall's algorithm.
Definition warshall.H:114
Bit_Mat_Graph< TestGraph > mat
Warshall_Compute_Transitive_Clausure< TestGraph > warshall
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
void warshall_compute_transitive_clausure(GT &g, Bit_Mat_Graph< GT, SA > &mat)
Computes the transitive closure of a graph using Warshall's algorithm.
Definition warshall.H:80
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Warshall's algorithm for transitive closure.
TEST_F(WarshallTest, EmptyGraph)