Aleph-w 3.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>
86 typename GT::Arc *arc)
87 {
88 for (typename DynDlist<typename GT::Arc *>::Iterator it(cut);
89 it.has_curr(); it.next_ne())
90 if (it.get_curr() == arc)
91 return true;
92 return false;
93 }
94
95 template <class GT>
96 void expect_cut_matches_partition(const GT &g,
100 size_t expected_cut_size)
101 {
102 EXPECT_EQ(l.size() + r.size(), g.get_num_nodes());
103
104 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
105 {
106 auto node = it.get_curr();
107 const bool in_l = l.contains(node);
108 const bool in_r = r.contains(node);
110 }
111
112 for (Arc_Iterator<GT> it(g); it.has_curr(); it.next_ne())
113 {
114 auto arc = it.get_curr();
115 auto src = g.get_src_node(arc);
116 auto tgt = g.get_tgt_node(arc);
117 const bool src_in_l = l.contains(src);
118 const bool tgt_in_l = l.contains(tgt);
119 const bool crosses = (src_in_l != tgt_in_l);
120 EXPECT_EQ(arc_in_cut<GT>(cut, arc), crosses);
121 }
122
123 for (typename DynDlist<typename GT::Arc *>::Iterator it(cut);
124 it.has_curr(); it.next_ne())
125 {
126 auto arc = it.get_curr();
127 auto src = g.get_src_node(arc);
128 auto tgt = g.get_tgt_node(arc);
129 const bool src_in_l = l.contains(src);
130 const bool tgt_in_l = l.contains(tgt);
132 }
133
135 }
136} // namespace
137
144
152
154{
155 Graph g;
156 auto nodes = make_nodes(g, 2);
157 g.insert_arc(nodes[0], nodes[1]);
159 EXPECT_EQ(k, 1L);
160}
161
163{
164 Graph g;
165 auto nodes = make_nodes(g, 4);
166 g.insert_arc(nodes[0], nodes[1]);
167 g.insert_arc(nodes[2], nodes[3]);
169 EXPECT_EQ(k, 0L);
170}
171
173{
174 Graph g;
175 auto nodes = make_nodes(g, 4);
176 make_cycle(g, nodes);
178 EXPECT_EQ(k, 2L);
179}
180
189
191{
192 Graph g;
193 auto nodes = make_nodes(g, 2);
194 g.insert_arc(nodes[0], nodes[1]);
195 g.insert_arc(nodes[0], nodes[1]);
197 EXPECT_EQ(k, 2L);
198}
199
211
224
226{
227 Graph g;
228 auto nodes = make_nodes(g, 2);
229 g.insert_arc(nodes[0], nodes[1]);
233
235 EXPECT_EQ(min_cut, 1L);
236 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
237}
238
240{
241 Graph g;
242 auto nodes = make_nodes(g, 4);
243 g.insert_arc(nodes[0], nodes[1]);
244 g.insert_arc(nodes[2], nodes[3]);
248
251 expect_cut_matches_partition(g, l, r, cut, 0U);
252}
253
255{
256 Graph g;
257 auto nodes = make_nodes(g, 4);
258 make_cycle(g, nodes);
262
264 EXPECT_EQ(min_cut, 2L);
265 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
266}
267
269{
270 Graph g;
271 auto nodes = make_nodes(g, 2);
272 g.insert_arc(nodes[0], nodes[1]);
273 g.insert_arc(nodes[0], nodes[1]);
277
279 EXPECT_EQ(min_cut, 2L);
280 expect_cut_matches_partition(g, l, r, cut, static_cast<size_t>(min_cut));
281}
282
289
297
299{
300 Graph g;
301 auto nodes = make_nodes(g, 2);
302 g.insert_arc(nodes[0], nodes[1]);
304 EXPECT_EQ(k, 1L);
305}
306
308{
309 Graph g;
310 auto nodes = make_nodes(g, 4);
311 g.insert_arc(nodes[0], nodes[1]);
312 g.insert_arc(nodes[2], nodes[3]);
314 EXPECT_EQ(k, 0L);
315}
316
318{
319 Graph g;
320 auto nodes = make_nodes(g, 4);
321 make_cycle(g, nodes);
323 EXPECT_EQ(k, 2L);
324}
325
334
336{
337 Graph g;
338 auto nodes = make_nodes(g, 5);
339 g.insert_arc(nodes[0], nodes[1]);
340 g.insert_arc(nodes[1], nodes[2]);
341 g.insert_arc(nodes[2], nodes[0]);
342 g.insert_arc(nodes[0], nodes[3]);
343 g.insert_arc(nodes[3], nodes[4]);
344 g.insert_arc(nodes[4], nodes[0]);
345
347 EXPECT_EQ(k, 1L);
348}
349
351{
352 TestDigraph g;
353 auto n1 = g.insert_node(1);
354 auto n2 = g.insert_node(2);
355 g.insert_arc(n1, n2);
356
360
361 auto edge_conn_call = [&]() {
363 };
364 auto min_cut_call = [&]() {
366 };
367 auto vertex_conn_call = [&]() {
369 };
370
371 EXPECT_THROW(edge_conn_call(), std::domain_error);
372 EXPECT_THROW(min_cut_call(), std::domain_error);
373 EXPECT_THROW(vertex_conn_call(), std::domain_error);
374}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
Iterator dynamic list.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
bool contains(const Key &key) const
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
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
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
#define TEST(name)
List_Digraph< IntNode, DoubleArc > TestDigraph
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:1864
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
K-connected graph structures and connectivity algorithms.
DynList< int > l