108 "Red",
"Blue",
"Green",
"Yellow",
"Orange",
109 "Purple",
"Cyan",
"Magenta",
"Brown",
"Gray"
117 std::snprintf(buf,
sizeof(buf),
"Color%zu", c);
124 cout <<
" Colors used: " <<
num_colors <<
"\n";
125 cout <<
" Assignment:\n";
126 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
128 Node *v = it.get_curr();
129 size_t c =
colors.find(v);
130 cout <<
" " << left << setw(20) << v->get_info()
131 <<
" -> " <<
color_name(c) <<
" (" << c <<
")\n";
139 cout <<
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
140 cout << title <<
"\n";
141 cout <<
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
156 separator(
"Part 1: Map Coloring — South America");
158 cout <<
"Each node is a country; edges represent shared borders.\n";
159 cout <<
"Goal: color the map so no two adjacent countries share a color.\n";
160 cout <<
"The Four Color Theorem guarantees χ(G) ≤ 4 for any planar map.\n\n";
197 cout <<
"▶ DSatur (best heuristic):\n";
220 cout <<
"Courses that share students cannot be scheduled at the same time.\n";
221 cout <<
"Each color represents an exam time slot.\n\n";
250 cout <<
"▶ Greedy (fast, may not be optimal):\n";
254 cout <<
"\n▶ Welsh-Powell (sort by degree first):\n";
258 cout <<
"\n▶ DSatur (adaptive, usually optimal):\n";
262 cout <<
"\n▶ Exact chromatic number:\n";
264 cout <<
" χ(G) = " <<
chi <<
" time slot(s) minimum\n";
284 separator(
"Part 3: Register Allocation (Compiler Optimization)");
286 cout <<
"Variables that are simultaneously live cannot share a register.\n";
287 cout <<
"Colors represent CPU registers (R0, R1, R2, ...).\n\n";
318 cout <<
"▶ DSatur coloring (minimum registers):\n";
320 cout <<
" Registers needed: " <<
k <<
"\n";
321 cout <<
" Assignment:\n";
322 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
324 Node *v = it.get_curr();
325 cout <<
" " << left << setw(4) << v->get_info()
326 <<
" -> R" <<
colors.find(v) <<
"\n";
328 cout <<
" Valid (no interference): "
331 cout <<
"\n▶ Exact chromatic number:\n";
333 cout <<
" Minimum registers needed: " <<
chi <<
"\n";
336 cout <<
" ✓ Fits in 3 registers — no spilling required!\n";
338 cout <<
" ✗ Needs more than 3 registers — some variables must be spilled.\n";
354 separator(
"Part 4: Algorithm Comparison — Petersen Graph");
356 cout <<
"The Petersen graph is 3-regular and has χ(G) = 3.\n";
357 cout <<
"It is a well-known benchmark for coloring heuristics.\n\n";
362 for (
int i = 0; i < 10; ++i)
366 for (
int i = 0; i < 5; ++i)
377 for (
int i = 0; i < 5; ++i)
385 cout <<
"▶ Greedy (order-dependent):\n";
387 cout <<
" Colors: " <<
k1 <<
" Valid: "
390 cout <<
"\n▶ Welsh-Powell (decreasing degree):\n";
392 cout <<
" Colors: " <<
k2 <<
" Valid: "
395 cout <<
"\n▶ DSatur (adaptive saturation):\n";
397 cout <<
" Colors: " <<
k3 <<
" Valid: "
401 cout <<
"\n▶ Exact chromatic number (branch-and-bound):\n";
403 cout <<
" χ(Petersen) = " <<
chi <<
"\n";
406 cout <<
"\n Summary:\n";
407 cout <<
" Greedy: " <<
k1 <<
" color(s)\n";
408 cout <<
" Welsh-Powell: " <<
k2 <<
" color(s)\n";
409 cout <<
" DSatur: " <<
k3 <<
" color(s)\n";
410 cout <<
" Exact (χ): " <<
chi <<
" color(s) ← optimal\n";
423 separator(
"Part 5: State Safety — Cookies Are Restored on Completion");
425 cout <<
"This implementation temporarily reuses NODE_COOKIE for coloring state,\n";
426 cout <<
"but restores any pre-existing cookie values when it finishes.\n";
427 cout <<
"It is not thread-safe, reentrant, or safe for nested/concurrent\n";
428 cout <<
"cookie users without external synchronization.\n\n";
444 cout <<
"Before coloring:\n";
451 cout <<
"After dsatur_coloring (" <<
k <<
" colors):\n";
467 cout <<
"╔══════════════════════════════════════════════════════════════════╗\n";
468 cout <<
"║ Graph Coloring Algorithms — Aleph-w Example ║\n";
469 cout <<
"╚══════════════════════════════════════════════════════════════════╝\n";
478 cout <<
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
479 cout <<
"Summary of algorithms in Graph_Coloring.H\n";
480 cout <<
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
481 cout <<
"┌─────────────────────────┬──────────────────────┬─────────────────┐\n";
482 cout <<
"│ Function │ Complexity │ Quality │\n";
483 cout <<
"├─────────────────────────┼──────────────────────┼─────────────────┤\n";
484 cout <<
"│ greedy_coloring │ O((V+E) log V) │ ≤ Δ+1 colors │\n";
485 cout <<
"│ welsh_powell_coloring │ O((V+E) log V) │ Often better │\n";
486 cout <<
"│ dsatur_coloring │ O(V^2 + E log V) │ Near-optimal │\n";
487 cout <<
"│ is_valid_coloring │ O(E log V) │ Validation │\n";
488 cout <<
"│ chromatic_number │ Exponential (≤64 V) │ Exact χ(G) │\n";
489 cout <<
"└─────────────────────────┴──────────────────────┴─────────────────┘\n\n";
490 cout <<
"All algorithms:\n";
491 cout <<
" • Return colors as DynMapTree<Node*, size_t> (0-based indices)\n";
492 cout <<
" • Temporarily reuse NODE_COOKIE, then restore prior values on completion\n";
493 cout <<
" • Are not thread-safe/reentrant for concurrent or nested cookie users\n";
494 cout <<
" • Work on List_Graph, List_SGraph, Array_Graph and digraph variants\n";
495 cout <<
" • Accept any arc filter via template parameter SA\n\n";
Graph coloring algorithms and heuristics.
WeightedDigraph::Node Node
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
static void demo_exam_scheduling()
Models exam scheduling as a graph coloring problem.
static void demo_algorithm_comparison()
The Petersen graph is a well-known benchmark in graph theory.
static void separator(const string &title)
static const char * color_name(size_t c)
static const char * color_names[]
static void print_coloring(const G &g, const ColorMap &colors, size_t num_colors)
static void demo_cookie_safety()
Shows that running one coloring algorithm does not alter cookies that were set previously by user cod...
static void demo_map_coloring()
Demonstrates graph coloring as a map-coloring problem.
static void demo_register_allocation()
Models register allocation as graph coloring.
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.
Main namespace for Aleph-w library functions.
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.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.