🧩 Constraint Solving POTD:Problem of the Day: Graph Coloring #50239
Closed
Replies: 1 comment
|
This discussion has been marked as outdated by Constraint Solving — Problem of the Day. A newer discussion is available at Discussion #50541. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Problem Statement
Assign a color to each vertex of a graph such that no two adjacent vertices share the same color, using the minimum number of colors possible (the graph's chromatic number).
Concrete Instance:
Consider a map with 5 regions arranged like this:
Input: Graph
G = (V, E)whereVis a set of vertices andEis a set of edges.Output: A function
color: V → {1, 2, ..., k}such that for all edges(u, v) ∈ E,color(u) ≠ color(v), andkis minimized.Why It Matters
Map Coloring & Geographic Planning: Cartographers and urban planners use graph coloring to ensure adjacent regions have distinct visual colors for clarity. The famous Four Color Theorem guarantees planar maps need at most 4 colors.
Register Allocation: Compiler optimizers solve graph coloring to assign processor registers to program variables, minimizing memory spills and improving code performance.
Frequency Assignment: Telecommunications engineers color a graph of towers to assign radio frequencies such that interfering towers (adjacent nodes) don't use overlapping bands.
Timetabling: Universities color a graph of exam courses, where edges connect courses sharing students. Adjacent courses (conflicting students) must have different exam time slots.
Modeling Approaches
Approach 1: Constraint Programming (Domain-based)
Decision Variables:
color[v] ∈ {1, 2, ..., k}for each vertexv ∈ V, wherekis an upper bound (e.g.,|V|or a known upper bound like Δ+1, where Δ is max degree).Constraints:
color[u] ≠ color[v]for all(u, v) ∈ E(stated as a disequality constraint)color[v1] ≤ color[v2]ifv1 < v2(in some ordering)Strengths: Declarative, exploits domain consistency and specialized global constraints like
all_different. Efficient propagation can prune the search space dramatically.Weaknesses: Simple formulation can be symmetric (many equivalent colorings swap color labels), requiring symmetry-breaking constraints. Naive all_different propagation alone may not be tight enough for dense graphs.
Approach 2: Integer Linear Programming (ILP)
Decision Variables:
x[v, c] ∈ {0, 1}: true if vertexvis assigned colorck ≥ 0: the number of colors used (to minimize)Constraints:
Σ_c x[v, c] = 1for allv ∈ Vx[u, c] + x[v, c] ≤ 1for all(u, v) ∈ Eand allc(adjacent vertices cannot share a color)k ≥ c · (1/|V|) · Σ_v x[v, c]or use a big-M formulation to count active colorsObjective: Minimize
kStrengths: Leverages mature LP/MIP solvers (CPLEX, Gurobi). Can incorporate side constraints easily (e.g., costs per color, preferred colors for certain vertices).
Weaknesses: Linear relaxation is loose for chromatic number. Requires a large number of binary variables (O(|V| · k)), making it expensive for large graphs. Less natural representation than CP.
Approach 3: SAT-based Encoding
Propositional Variables:
x[v, c]∈ {true, false}: vertexvhas colorcClauses (in CNF):
(x[v, 1] ∨ x[v, 2] ∨ ... ∨ x[v, k]) ∧ Π_{c1 < c2} (¬x[v, c1] ∨ ¬x[v, c2])(¬x[u, c] ∨ ¬x[v, c])for all(u, v) ∈ Eand allc(a clause saying "not bothuandvare colorc")Search: Run a SAT solver (MiniSat, Glucose, CaDiCaL). Iteratively increase
kfrom a lower bound until SAT returns satisfiable.Strengths: Automatic conflict analysis and clause learning lead to powerful pruning. Modern SAT solvers are highly optimized.
Weaknesses: Encoding overhead; requires iterating on
k. Less intuitive than CP for those new to SAT.Example: CP Model (Pseudo-code)
This model is compact and directly expresses the problem. The solver uses arc-consistency and specialized global constraint propagators to prune infeasible branches.
Key Techniques
1. Arc Consistency (AC-3 Algorithm)
Arc consistency iteratively removes values from variable domains that have no supporting assignment in a neighboring variable. For graph coloring, this means: if a vertex
vis assigned colorc, all neighbors' domains shrink to excludec. Specialized propagators likeall_differentachieve stronger consistency (domain consistency or bound consistency) efficiently.2. Greedy Heuristics & Ordering
A simple greedy coloring (e.g., "color vertices in order of descending degree") often gives a good upper bound quickly. In branch-and-bound, this upper bound prunes many branches early. More sophisticated heuristics like the Largest Degree First (LDF) or Saturation Degree ordering (color the vertex whose neighbors use the most colors) guide search toward tight lower bounds.
3. Symmetry-Breaking & Normalization
Graph coloring is highly symmetric: swapping color labels 1 and 2 yields an equivalent solution. CP solvers mitigate this by imposing ordering constraints (e.g., vertex 1 always gets color 1) or channeling variables to a canonical form. SAT solvers use clause learning and restart strategies to recover from thrashing on symmetric branches.
4. Lower Bounds via Cliques & Fractional Chromatic Number
A clique (complete subgraph) of size
ωrequires at leastωcolors. Computing a maximum clique provides a lower bound. The fractional chromatic number (via LP relaxation) provides a tighter bound but is expensive to compute. These bounds drive branch-and-bound or integer programming algorithms.Challenge Corner
Open Questions for You:
Symmetry Reduction: The concretely encoded CP model above uses
color[i] <= color[i+1] + 1to break color-label symmetry. Can you generalize this to a more efficient formulation that doesn't enforce a total order on vertices but still eliminates most symmetric solutions?Hybrid Reasoning: What if you first compute a lower bound using a maximum clique algorithm, then feed that into a SAT solver as a clause? How would such a hybrid strategy compare to pure SAT or pure CP?
Parameterized Complexity: Graph coloring is NP-hard in general. However, if the graph's treewidth is bounded (a structural parameter), the problem becomes tractable. How would you exploit treewidth decomposition to solve coloring more efficiently?
Real-World Twist: In the register allocation application, each vertex (variable) has a lifetime interval during which it must reside in a register. Vertices with non-overlapping lifetimes don't conflict. How would you model this constraint efficiently? Does it change the complexity?
References
Rossi, F., van Beek, P., & Walsh, T. (Eds.). Handbook of Constraint Programming (Chapter 2–3 on Constraint Propagation and CSP Techniques). Elsevier, 2006.
→ Essential reference for CP fundamentals and global constraints.
Jensen, T. R., & Toft, B. Graph Coloring Problems. Wiley, 1995.
→ Comprehensive monograph on algorithmic and theoretical aspects of chromatic numbers.
Geoff Knottenbelt (École Polytechnique). Satisfiability and SAT Solvers (Course Notes & Survey).
→ Accessible introduction to SAT-based encodings of CSP problems.
Wikipedia: Graph Coloring & Four Color Theorem (accessible starting point)
→ Good visual examples and references to planarity, applications, and historical context.
What coloring techniques do you find most effective? Share your solver implementations, benchmarks, or real-world applications in the discussion below!
All reactions