66#ifndef GRAPH_COLORING_H
67#define GRAPH_COLORING_H
81namespace graph_coloring_detail {
85 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(c + 1));
90 return cookie !=
nullptr;
95 return static_cast<size_t>(
reinterpret_cast<uintptr_t
>(cookie)) - 1;
101 while (used.
search(c) !=
nullptr)
112template <AlephGraph GT, ArcFilter<GT> SA>
120 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
125 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
129 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
131 auto *arc = it.get_curr();
138 neighbor_sets[u].
insert(v);
139 neighbor_sets[v].
insert(u);
143 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
145 Node *u = it.get_curr();
146 auto &adj_list = adj[u];
147 auto &nbrs = neighbor_sets[u];
148 for (
auto sit = nbrs.get_it(); sit.has_curr(); sit.next_ne())
149 adj_list.
append(sit.get_curr());
153template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
156 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
158 auto *arc = it.get_curr();
160 <<
"graph coloring does not support self-loops";
198template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
203 using namespace graph_coloring_detail;
209 validate_no_self_loops<GT, SA>(g, sa);
212 build_undirected_adj<GT, SA>(g, adj, sa);
215 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
218 size_t num_colors = 0;
219 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
221 Node *v = it.get_curr();
224 for (
auto nit = adj[v].get_it(); nit.has_curr(); nit.next_ne())
228 size_t c = smallest_available(used);
232 if (c + 1 > num_colors)
268template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
273 using namespace graph_coloring_detail;
279 validate_no_self_loops<GT, SA>(g, sa);
282 build_undirected_adj<GT, SA>(g, adj, sa);
286 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
288 Node *p = it.get_curr();
295 return adj[a].
size() > adj[b].
size();
298 size_t num_colors = 0;
299 for (
auto nit =
nodes.get_it(); nit.has_curr(); nit.next_ne())
301 Node *v = nit.get_curr();
304 for (
auto ait = adj[v].get_it(); ait.has_curr(); ait.next_ne())
306 Node *
w = ait.get_curr();
311 size_t c = smallest_available(used);
315 if (c + 1 > num_colors)
353template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
358 using namespace graph_coloring_detail;
364 validate_no_self_loops<GT, SA>(g, sa);
367 build_undirected_adj<GT, SA>(g, adj, sa);
373 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
375 Node *v = it.get_curr();
377 uncolored_nodes.
insert(v);
381 size_t num_colors = 0;
382 while (not uncolored_nodes.
is_empty())
384 Node *best =
nullptr;
389 for (
auto it = uncolored_nodes.
get_it(); it.has_curr(); it.next_ne())
391 Node *v = it.get_curr();
392 size_t sat = saturation_sets[v].
size();
393 size_t deg = adj[v].
size();
395 if (best ==
nullptr or sat > best_sat or (sat == best_sat
and deg > best_deg))
403 size_t c = smallest_available(saturation_sets[best]);
406 uncolored_nodes.
remove(best);
408 if (c + 1 > num_colors)
412 for (
auto it = adj[best].get_it(); it.has_curr(); it.next_ne())
414 saturation_sets[
w].insert(c);
449template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
459 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
460 if (colors.
search(it.get_curr()) ==
nullptr)
463 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
465 auto *arc = it.get_curr();
472 auto *pu = colors.
search(u);
473 auto *pv = colors.
search(v);
474 if (pu ==
nullptr or pv ==
nullptr)
476 if (pu->second == pv->second)
517template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
522 using namespace graph_coloring_detail;
525 <<
"chromatic_number: graph has " << g.
get_num_nodes() <<
" nodes (max 64)";
531 validate_no_self_loops<GT, SA>(g, sa);
534 size_t upper = dsatur_coloring<GT, SA>(g, best_colors, sa);
538 colors = std::move(best_colors);
543 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
544 node_list.
append(it.get_curr());
546 const size_t n = node_list.
size();
549 for (
auto it = node_list.
get_it(); it.has_curr(); it.next_ne())
550 nodes[idx++] = it.get_curr();
555 for (
size_t i = 0; i < n; ++i)
556 NODE_COOKIE(
nodes[i]) =
reinterpret_cast<void *
>(
static_cast<uintptr_t
>(i));
558 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
560 auto *arc = it.get_curr();
567 adj_idx[i].append(j);
568 adj_idx[j].append(i);
574 auto reset_coloring = [&]()
576 for (
size_t i = 0; i < n; ++i)
580 auto try_k_coloring = [&](
size_t k,
auto &self,
size_t node_idx) ->
bool
584 for (
size_t c = 0; c <
k; ++c)
586 bool feasible =
true;
587 for (
auto jit = adj_idx[node_idx].get_it(); jit.has_curr(); jit.next_ne())
589 size_t j = jit.get_curr();
590 if (j < node_idx
and coloring[j] == c)
598 coloring[node_idx] = c;
599 if (self(
k, self, node_idx + 1))
603 coloring[node_idx] = 0;
607 size_t lo = 2, hi = upper;
610 size_t mid = (lo + hi) / 2;
612 if (try_k_coloring(mid, try_k_coloring, 0))
621 try_k_coloring(lo, try_k_coloring, 0);
623 for (
size_t i = 0; i < n; ++i)
627 colors = std::move(best_colors);
644template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
661 return greedy_coloring<GT, SA>(g, colors,
sa);
672template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
689 return welsh_powell_coloring<GT, SA>(g, colors,
sa);
700template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
717 return dsatur_coloring<GT, SA>(g, colors,
sa);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
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.
DSatur_Coloring(SA __sa=SA())
Keeps sa, the filter of the arcs.
void insert(Dlink *node) noexcept
Insert node after this.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * append(const Key &key, const Data &data)
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.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
bool is_empty() const
returns true if the set is empty
Functor wrapper for greedy_coloring.
Greedy_Coloring(SA __sa=SA())
Keeps sa, the filter of the arcs.
size_t operator()(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors) const
Execute greedy coloring.
Graph_Node< Node_Info > Node
The graph type.
Functor wrapper for welsh_powell_coloring.
Welsh_Powell_Coloring(SA __sa=SA())
Keeps sa, the filter of the arcs.
size_t operator()(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors) const
Execute Welsh-Powell coloring.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
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)
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()
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 chromatic_number(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Computes the exact chromatic number of a small graph.
size_t greedy_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Greedy graph coloring in iteration order.
bool is_valid_coloring(const GT &g, const DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Validates a graph coloring.
#define NODE_COOKIE(p)
Return the node cookie
size_t dsatur_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
DSatur graph coloring (saturation-degree heuristic).
size_t welsh_powell_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Welsh-Powell graph coloring.
size_t decode_color(void *cookie) noexcept
bool is_colored(void *cookie) noexcept
size_t smallest_available(const DynSetTree< size_t > &used)
void build_undirected_adj(const GT &g, DynMapTree< typename GT::Node *, DynList< typename GT::Node * > > &adj, SA &sa)
Internal helper to pre-build undirected adjacency lists.
void * encode_color(size_t c) noexcept
void validate_no_self_loops(const GT &g, SA &sa)
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
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.