66#ifndef GRAPH_COLORING_H
67#define GRAPH_COLORING_H
79namespace graph_coloring_detail {
83 return reinterpret_cast<void *
>(
static_cast<uintptr_t>(c + 1));
88 return cookie !=
nullptr;
93 return static_cast<size_t>(
reinterpret_cast<uintptr_t>(cookie)) - 1;
99 while (
used.search(c) !=
nullptr)
110template <
class GT,
class SA>
117 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
122 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
126 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
128 auto *arc = it.get_curr();
140 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
142 Node *u = it.get_curr();
145 for (
auto sit =
nbrs.get_it();
sit.has_curr();
sit.next_ne())
150template <
class GT,
class SA = Dft_Show_Arc<GT>>
153 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
155 auto *arc = it.get_curr();
157 <<
"graph coloring does not support self-loops";
194template <
class GT,
class SA = Dft_Show_Arc<GT>>
198 using namespace graph_coloring_detail;
210 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
214 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
216 Node *v = it.get_curr();
219 for (
auto nit = adj[v].get_it();
nit.has_curr();
nit.next_ne())
223 size_t c = smallest_available(
used);
262template <
class GT,
class SA = Dft_Show_Arc<GT>>
266 using namespace graph_coloring_detail;
279 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
281 Node *p = it.get_curr();
288 return adj[a].
size() > adj[b].
size();
297 for (
auto ait = adj[v].get_it();
ait.has_curr();
ait.next_ne())
304 size_t c = smallest_available(
used);
345template <
class GT,
class SA = Dft_Show_Arc<GT>>
349 using namespace graph_coloring_detail;
364 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
366 Node *v = it.get_curr();
382 Node *v = it.get_curr();
403 for (
auto it = adj[
best].get_it(); it.has_curr(); it.next_ne())
439template <
class GT,
class SA = Dft_Show_Arc<GT>>
448 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
449 if (
colors.search(it.get_curr()) ==
nullptr)
452 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
454 auto *arc = it.get_curr();
463 if (
pu ==
nullptr or pv ==
nullptr)
465 if (
pu->second ==
pv->second)
505template <
class GT,
class SA = Dft_Show_Arc<GT>>
509 using namespace graph_coloring_detail;
512 <<
"chromatic_number: graph has " << g.
get_num_nodes() <<
" nodes (max 64)";
530 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
531 node_list.
append(it.get_curr());
533 const size_t n = node_list.
size();
536 for (
auto it = node_list.
get_it(); it.has_curr(); it.next_ne())
537 nodes[idx++] = it.get_curr();
542 for (
size_t i = 0; i < n; ++i)
545 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
547 auto *arc = it.get_curr();
563 for (
size_t i = 0; i < n; ++i)
571 for (
size_t c = 0; c <
k; ++c)
573 bool feasible =
true;
576 size_t j =
jit.get_curr();
594 size_t lo = 2, hi =
upper;
597 size_t mid = (lo + hi) / 2;
610 for (
size_t i = 0; i < n; ++i)
631template <
class GT,
class SA = Dft_Show_Arc<GT>>
654template <
class GT,
class SA = Dft_Show_Arc<GT>>
677template <
class GT,
class SA = Dft_Show_Arc<GT>>
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
RAII guard that saves and restores graph cookies.
Functor wrapper for dsatur_coloring.
size_t operator()(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors) const
Execute DSatur coloring.
void insert(Dlink *node) noexcept
Insert node after this.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
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.
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Functor wrapper for greedy_coloring.
size_t operator()(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors) const
Execute greedy coloring.
size_t size() const noexcept
Count the number of elements of the list.
Graph_Node< Node_Info > Node
The graph type.
Functor wrapper for welsh_powell_coloring.
size_t operator()(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors) const
Execute Welsh-Powell coloring.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
RAII guards for graph node/arc cookies.
DynArray< Graph::Node * > nodes
size_t dsatur_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors)
DSatur graph coloring (saturation-degree heuristic).
bool is_valid_coloring(const GT &g, const DynMapTree< typename GT::Node *, size_t > &colors)
Validates a graph coloring.
size_t chromatic_number(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors)
Computes the exact chromatic number of a small graph.
#define NODE_COOKIE(p)
Return the node cookie
size_t greedy_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors)
Greedy graph coloring in iteration order.
size_t welsh_powell_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors)
Welsh-Powell graph coloring.
size_t decode_color(void *cookie) noexcept
bool is_colored(void *cookie) noexcept
void build_undirected_adj(const GT &g, DynMapTree< typename GT::Node *, DynList< typename GT::Node * > > &adj)
Internal helper to pre-build undirected adjacency lists.
size_t smallest_available(const DynSetTree< size_t > &used)
void validate_no_self_loops(const GT &g)
void * encode_color(size_t c) noexcept
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
size_t size(Node *root) noexcept
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Dynamic array container with automatic resizing.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.