Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_kgraph_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
43#include <gtest/gtest.h>
44
45#include <vector>
46
47#include <tpl_graph.H>
48#include <tpl_kgraph.H>
49
50using namespace Aleph;
51
52namespace
53{
56 using Digraph = TestDigraph;
57
58 std::vector<Graph::Node *> make_nodes(Graph &g, int n)
59 {
60 std::vector<Graph::Node *> nodes;
61 nodes.reserve(n);
62 for (int i = 0; i < n; ++i)
63 nodes.push_back(g.insert_node(i));
64 return nodes;
65 }
66
67 void make_cycle(Graph &g, const std::vector<Graph::Node *> &nodes)
68 {
69 if (nodes.empty())
70 return;
71
72 const size_t n = nodes.size();
73 for (size_t i = 0; i < n; ++i)
74 g.insert_arc(nodes[i], nodes[(i + 1) % n]);
75 }
76
77 void make_complete(Graph &g, const std::vector<Graph::Node *> &nodes)
78 {
79 for (size_t i = 0; i < nodes.size(); ++i)
80 for (size_t j = i + 1; j < nodes.size(); ++j)
81 g.insert_arc(nodes[i], nodes[j]);
82 }
83
84 template <class GT>
85 bool arc_in_cut(const DynDlist<typename GT::Arc *> &cut,
86 typename GT::Arc *arc)
87 {
88 for (auto it = cut.get_it(); it.has_curr(); it.next_ne())
89 if (it.get_curr() == arc)
90 return true;
91 return false;
92 }
93
94 template <class GT>
95 void expect_cut_matches_partition(const GT &g,
99 size_t expected_cut_size)
100 {
101 EXPECT_EQ(l.size() + r.size(), g.get_num_nodes());
102
103 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
104 {
105 auto node = it.get_curr();
106 const bool in_l = l.contains(node);
107 const bool in_r = r.contains(node);
108 EXPECT_NE(in_l, in_r);
109 }
110
111 for (Arc_Iterator<GT> it(g); it.has_curr(); it.next_ne())
112 {
113 auto arc = it.get_curr();
114 auto src = g.get_src_node(arc);
115 auto tgt = g.get_tgt_node(arc);
116 const bool src_in_l = l.contains(src);
117 const bool tgt_in_l = l.contains(tgt);
118 const bool crosses = (src_in_l != tgt_in_l);
119 EXPECT_EQ(arc_in_cut<GT>(cut, arc), crosses);
120 }
121
122 for (auto it = cut.get_it(); it.has_curr(); it.next_ne())
123 {
124 auto arc = it.get_curr();
125 auto src = g.get_src_node(arc);
126 auto tgt = g.get_tgt_node(arc);
127 const bool src_in_l = l.contains(src);
128 const bool tgt_in_l = l.contains(tgt);
129 EXPECT_NE(src_in_l, tgt_in_l);
130 }
131
132 EXPECT_EQ(cut.size(), expected_cut_size);
133 }
134} // namespace
135
136TEST(KGraphEdgeConnectivity, EmptyGraphReturnsZero)
137{
138 Graph g;
139 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
140 EXPECT_EQ(k, 0L);
141}
142
143TEST(KGraphEdgeConnectivity, SingleNodeReturnsZero)
144{
145 Graph g;
146 g.insert_node(1);
147 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
148 EXPECT_EQ(k, 0L);
149}
150
151TEST(KGraphEdgeConnectivity, SingleEdgeReturnsOne)
152{
153 Graph g;
154 auto nodes = make_nodes(g, 2);
155 g.insert_arc(nodes[0], nodes[1]);
156 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
157 EXPECT_EQ(k, 1L);
158}
159
160TEST(KGraphEdgeConnectivity, DisconnectedGraphReturnsZero)
161{
162 Graph g;
163 auto nodes = make_nodes(g, 4);
164 g.insert_arc(nodes[0], nodes[1]);
165 g.insert_arc(nodes[2], nodes[3]);
166 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
167 EXPECT_EQ(k, 0L);
168}
169
170TEST(KGraphEdgeConnectivity, CycleReturnsTwo)
171{
172 Graph g;
173 auto nodes = make_nodes(g, 4);
174 make_cycle(g, nodes);
175 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
176 EXPECT_EQ(k, 2L);
177}
178
179TEST(KGraphEdgeConnectivity, CompleteGraphReturnsNMinusOne)
180{
181 Graph g;
182 auto nodes = make_nodes(g, 4);
183 make_complete(g, nodes);
184 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
185 EXPECT_EQ(k, 3L);
186}
187
188TEST(KGraphEdgeConnectivity, ParallelEdgesIncreaseConnectivity)
189{
190 Graph g;
191 auto nodes = make_nodes(g, 2);
192 g.insert_arc(nodes[0], nodes[1]);
193 g.insert_arc(nodes[0], nodes[1]);
194 const auto k = edge_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
195 EXPECT_EQ(k, 2L);
196}
197
198TEST(KGraphMinCut, EmptyGraphProducesEmptyPartition)
199{
200 Graph g;
204
205 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
206 EXPECT_EQ(min_cut, 0L);
207 expect_cut_matches_partition(g, l, r, cut, 0U);
208}
209
210TEST(KGraphMinCut, SingleNodeTrivialCut)
211{
212 Graph g;
213 g.insert_node(1);
217
218 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
219 EXPECT_EQ(min_cut, 0L);
220 expect_cut_matches_partition(g, l, r, cut, 0U);
221}
222
223TEST(KGraphMinCut, SingleEdgeCut)
224{
225 Graph g;
226 auto nodes = make_nodes(g, 2);
227 g.insert_arc(nodes[0], nodes[1]);
231
232 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
233 EXPECT_EQ(min_cut, 1L);
234 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
235}
236
237TEST(KGraphMinCut, DisconnectedGraphReturnsZero)
238{
239 Graph g;
240 auto nodes = make_nodes(g, 4);
241 g.insert_arc(nodes[0], nodes[1]);
242 g.insert_arc(nodes[2], nodes[3]);
246
247 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
248 EXPECT_EQ(min_cut, 0L);
249 expect_cut_matches_partition(g, l, r, cut, 0U);
250}
251
252TEST(KGraphMinCut, CycleReturnsTwo)
253{
254 Graph g;
255 auto nodes = make_nodes(g, 4);
256 make_cycle(g, nodes);
260
261 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
262 EXPECT_EQ(min_cut, 2L);
263 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
264}
265
266TEST(KGraphMinCut, ParallelEdgesReturnFullCut)
267{
268 Graph g;
269 auto nodes = make_nodes(g, 2);
270 g.insert_arc(nodes[0], nodes[1]);
271 g.insert_arc(nodes[0], nodes[1]);
275
276 const long min_cut = compute_min_cut<Graph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
277 EXPECT_EQ(min_cut, 2L);
278 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
279}
280
281TEST(KGraphVertexConnectivity, EmptyGraphReturnsZero)
282{
283 Graph g;
284 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
285 EXPECT_EQ(k, 0L);
286}
287
288TEST(KGraphVertexConnectivity, SingleNodeReturnsZero)
289{
290 Graph g;
291 g.insert_node(1);
292 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
293 EXPECT_EQ(k, 0L);
294}
295
296TEST(KGraphVertexConnectivity, TwoNodesOneEdgeReturnsOne)
297{
298 Graph g;
299 auto nodes = make_nodes(g, 2);
300 g.insert_arc(nodes[0], nodes[1]);
301 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
302 EXPECT_EQ(k, 1L);
303}
304
305TEST(KGraphVertexConnectivity, DisconnectedGraphReturnsZero)
306{
307 Graph g;
308 auto nodes = make_nodes(g, 4);
309 g.insert_arc(nodes[0], nodes[1]);
310 g.insert_arc(nodes[2], nodes[3]);
311 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
312 EXPECT_EQ(k, 0L);
313}
314
315TEST(KGraphVertexConnectivity, CycleReturnsTwo)
316{
317 Graph g;
318 auto nodes = make_nodes(g, 4);
319 make_cycle(g, nodes);
320 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
321 EXPECT_EQ(k, 2L);
322}
323
324TEST(KGraphVertexConnectivity, CompleteGraphReturnsNMinusOne)
325{
326 Graph g;
327 auto nodes = make_nodes(g, 4);
328 make_complete(g, nodes);
329 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
330 EXPECT_EQ(k, 3L);
331}
332
333TEST(KGraphVertexConnectivity, BowtieGraphHasConnectivityOne)
334{
335 Graph g;
336 auto nodes = make_nodes(g, 5);
337 g.insert_arc(nodes[0], nodes[1]);
338 g.insert_arc(nodes[1], nodes[2]);
339 g.insert_arc(nodes[2], nodes[0]);
340 g.insert_arc(nodes[0], nodes[3]);
341 g.insert_arc(nodes[3], nodes[4]);
342 g.insert_arc(nodes[4], nodes[0]);
343
344 const auto k = vertex_connectivity<Graph, Heap_Preflow_Maximum_Flow>(g);
345 EXPECT_EQ(k, 1L);
346}
347
348TEST(KGraphPreconditions, RejectsDigraphs)
349{
350 TestDigraph g;
351 auto n1 = g.insert_node(1);
352 auto n2 = g.insert_node(2);
353 g.insert_arc(n1, n2);
354
358
359 auto edge_conn_call = [&]() {
360 return edge_connectivity<TestDigraph, Heap_Preflow_Maximum_Flow>(g);
361 };
362 auto min_cut_call = [&]() {
363 return compute_min_cut<TestDigraph, Heap_Preflow_Maximum_Flow>(g, l, r, cut);
364 };
365 auto vertex_conn_call = [&]() {
366 return vertex_connectivity<TestDigraph, Heap_Preflow_Maximum_Flow>(g);
367 };
368
369 EXPECT_THROW(edge_conn_call(), std::domain_error);
370 EXPECT_THROW(min_cut_call(), std::domain_error);
371 EXPECT_THROW(vertex_conn_call(), std::domain_error);
372}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Dynamic set backed by balanced binary search trees with automatic memory management.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:1074
#define TEST(name)
List_Digraph< IntNode, DoubleArc > TestDigraph
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Definition tpl_net.H:2790
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
static int * k
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
K-connected graph structures and connectivity algorithms.
DynList< int > l