Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hamiltonian.H
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
80# ifndef HAMILTONIAN_H
81# define HAMILTONIAN_H
82
83# include <algorithm>
84# include <limits>
85# include <utility>
86
87# include <ah-graph-concepts.H>
88
89# include <tpl_dynMapTree.H>
90# include <tpl_dynSetTree.H>
91# include <tpl_graph.H>
92
93namespace Aleph
94{
95
96namespace hamiltonian_detail
97{
106 template <class GT, class SA>
108 {
109 using Node = typename GT::Node;
110
113
114 Simple_Graph(const GT & g, SA & sa)
115 {
116 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
117 {
118 out.insert(it.get_curr(), DynSetTree<Node *>());
119 in.insert(it.get_curr(), 0);
120 }
121 for (Node_Iterator<GT> it(g); it.has_curr(); it.next_ne())
122 {
123 Node * p = it.get_curr();
124 auto & to = out.find(p);
125 for (Node_Arc_Iterator<GT, SA> a(p, sa); a.has_curr(); a.next_ne())
126 if (Node * q = a.get_tgt_node_ne(); q != p and to.insert(q) != nullptr)
127 ++in.find(q);
128 }
129 }
130
132 size_t degree(Node * p) const { return out.find(p).size(); }
133
135 size_t in_degree(Node * p) const { return in.find(p); }
136
138 bool joined(Node * p, Node * q) const { return out.find(p).contains(q); }
139 };
140} // namespace hamiltonian_detail
141
181template <AlephGraph GT,
182 NodeFilter<GT> SN = Dft_Show_Node<GT>,
183 ArcFilter<GT> SA = Dft_Show_Arc<GT> >
185{
188
199 bool test_graph(GT & g)
200 {
201 assert(not g.is_digraph());
202
203 const size_t n = g.get_num_nodes();
204
205 // Ore's theorem requires n >= 3
206 if (n < 3)
207 return false;
208
209 const hamiltonian_detail::Simple_Graph<GT, SA> simple(g, sa.get());
210
211 // Check all pairs of distinct vertices
212 for (Node_Iterator<GT, SN> i(g, sn); i.has_curr(); i.next_ne())
213 {
214 typename GT::Node * src = i.get_curr();
215
216 // Start j from the node after i to avoid duplicate pairs
218 j.next_ne();
219
220 for (; j.has_curr(); j.next_ne())
221 {
222 typename GT::Node * tgt = j.get_curr();
223
224 // Only check NON-ADJACENT pairs (Ore's theorem condition)
225 if (simple.joined(src, tgt))
226 continue;
227
228 // For non-adjacent pairs, check if deg(u) + deg(v) >= n
229 if (simple.degree(src) + simple.degree(tgt) < n)
230 return false;
231 }
232 }
233
234 return true;
235 }
236
249 bool test_digraph(GT & g)
250 {
251 assert(g.is_digraph());
252
253 const size_t n = g.get_num_nodes();
254
255 // Requires n >= 3
256 if (n < 3)
257 return false;
258
259 const hamiltonian_detail::Simple_Graph<GT, SA> simple(g, sa.get());
260
261 // Check all ordered pairs (src, tgt) where src != tgt
262 for (Node_Iterator<GT, SN> i(g, sn); i.has_curr(); i.next_ne())
263 {
264 typename GT::Node * src = i.get_curr();
265 for (Node_Iterator<GT, SN> j(g, sn); j.has_curr(); j.next_ne())
266 {
267 typename GT::Node * tgt = j.get_curr();
268 if (src == tgt or simple.joined(src, tgt))
269 continue;
270
271 // A pair without an arc that does not satisfy the condition
272 if (simple.degree(src) + simple.in_degree(tgt) < n)
273 return false;
274 }
275 }
276
277 return true;
278 }
279
280public:
281
288 Test_Hamiltonian_Sufficiency(SN && __sn = SN(), SA && __sa = SA())
289 : sn(std::move(__sn)), sa(std::move(__sa))
290 {
291 // empty
292 }
293
306 bool operator () (GT & g)
307 {
308 if (g.is_digraph())
309 return test_digraph(g);
310 else
311 return test_graph(g);
312 }
313};
314
354template <AlephGraph GT,
355 NodeFilter<GT> SN = Dft_Show_Node<GT>,
356 ArcFilter<GT> SA = Dft_Show_Arc<GT> >
358{
361
371 bool test_graph(GT & g)
372 {
373 assert(not g.is_digraph());
374
375 const size_t n = g.get_num_nodes();
376
377 // Dirac's theorem requires n >= 3
378 if (n < 3)
379 return false;
380
381 const size_t min_degree = (n + 1) / 2; // Ceiling of n/2
382 const hamiltonian_detail::Simple_Graph<GT, SA> simple(g, sa.get());
383
384 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
385 if (simple.degree(it.get_curr()) < min_degree)
386 return false;
387
388 return true;
389 }
390
400 bool test_digraph(GT & g)
401 {
402 assert(g.is_digraph());
403
404 const size_t n = g.get_num_nodes();
405 if (n < 3)
406 return false;
407
408 const size_t min_degree = (n + 1) / 2;
409 const hamiltonian_detail::Simple_Graph<GT, SA> simple(g, sa.get());
410
411 // Both must be at least n/2
412 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
413 {
414 typename GT::Node * p = it.get_curr();
415 if (simple.degree(p) < min_degree or simple.in_degree(p) < min_degree)
416 return false;
417 }
418
419 return true;
420 }
421
422public:
423
430 Test_Dirac_Condition(SN && __sn = SN(), SA && __sa = SA())
431 : sn(std::move(__sn)), sa(std::move(__sa))
432 {
433 // empty
434 }
435
444 bool operator () (GT & g)
445 {
446 if (g.is_digraph())
447 return test_digraph(g);
448 else
449 return test_graph(g);
450 }
451
458 size_t min_required_degree(GT & g) const
459 {
460 return (g.get_num_nodes() + 1) / 2;
461 }
462
474 std::pair<size_t, typename GT::Node*> find_min_degree_vertex(GT & g)
475 {
476 size_t min_deg = std::numeric_limits<size_t>::max();
477 typename GT::Node * min_node = nullptr;
478 const hamiltonian_detail::Simple_Graph<GT, SA> simple(g, sa.get());
479
480 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
481 {
482 typename GT::Node * p = it.get_curr();
483 size_t deg = simple.degree(p);
484 if (g.is_digraph())
485 deg = std::min(deg, simple.in_degree(p));
486 if (deg < min_deg)
487 {
488 min_deg = deg;
489 min_node = p;
490 }
491 }
492
493 return {min_deg, min_node};
494 }
495};
496
497} // end namespace Aleph
498
499# endif // HAMILTONIAN_H
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
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.
A filter that an algorithm object keeps: one given as an lvalue is used where it is,...
Definition tpl_graph.H:1249
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Tests Dirac's sufficient condition for Hamiltonian graphs.
bool test_graph(GT &g)
Test Dirac's condition for undirected graphs.
size_t min_required_degree(GT &g) const
Get the minimum degree required by Dirac's condition.
bool test_digraph(GT &g)
Test Dirac-like condition for directed graphs.
std::pair< size_t, typename GT::Node * > find_min_degree_vertex(GT &g)
Find the vertex with minimum degree.
Test_Dirac_Condition(SN &&__sn=SN(), SA &&__sa=SA())
Construct a Dirac condition tester.
bool operator()(GT &g)
Test if a graph satisfies Dirac's Hamiltonian condition.
Tests Ore's sufficient condition for Hamiltonian graphs.
bool test_graph(GT &g)
Test Ore's condition for undirected graphs.
bool operator()(GT &g)
Test if a graph satisfies Ore's Hamiltonian condition.
Test_Hamiltonian_Sufficiency(SN &&__sn=SN(), SA &&__sa=SA())
Construct a Hamiltonian sufficiency tester.
bool test_digraph(GT &g)
Test Ore-like condition for directed graphs.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Default filter for the graph nodes.
Definition tpl_graph.H:1228
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
The simple graph that the arcs that a filter shows give.
DynMapTree< Node *, DynSetTree< Node * > > out
bool joined(Node *p, Node *q) const
Whether p and q are joined, or p goes to q in a digraph.
size_t degree(Node *p) const
The neighbors of p, or the nodes that it goes to in a digraph.
DynMapTree< Node *, size_t > in
size_t in_degree(Node *p) const
The nodes that come to p, in a digraph.
void test_graph()
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.