Tutorials

11. Color a graph

Assign each vertex an integer representing a color. Bound the available colors and require the endpoints of every edge to differ. A satisfying assignment is then a valid coloring.

Prerequisites: integer constraints and results and models.

Model a triangle

All three vertices in a triangle are adjacent. Its colors must therefore be pairwise distinct. Two available colors are insufficient; three suffice.

coloring.mojo Download
"""Color a triangle with a bounded integer assigned to each vertex."""

from std.testing import assert_equal, assert_true
from cvc5_mojo import TermManager, Solver, Term, Kind


def check_triangle(tm: TermManager, colors: Int) raises:
    var solver = Solver(tm)
    solver.setLogic("QF_LIA")
    solver.setOption("produce-models", "true")
    var vertices = List[Term]()
    for i in range(3):
        vertices.append(tm.mkConst(tm.getIntegerSort(), "v" + String(i)))
        solver.assertFormula(tm.mkTerm(Kind.GEQ, vertices[i], tm.mkInteger(0)))
        solver.assertFormula(tm.mkTerm(Kind.LT, vertices[i], tm.mkInteger(colors)))
    var edges = tm.mkTerm(Kind.DISTINCT, vertices[0], vertices[1], vertices[2])
    solver.assertFormula(edges)
    var result = solver.checkSat()
    if colors == 2:
        assert_true(result.isUnsat())
    else:
        assert_true(result.isSat())
        assert_equal(String(solver.getValue(edges)), "true")
        for vertex in vertices:
            var in_range = tm.mkTerm(Kind.AND,
                tm.mkTerm(Kind.GEQ, vertex, tm.mkInteger(0)),
                tm.mkTerm(Kind.LT, vertex, tm.mkInteger(colors)))
            assert_equal(String(solver.getValue(in_range)), "true")
    print(colors, "colors:", result)


def main() raises:
    var tm = TermManager()
    check_triangle(tm, 2)
    check_triangle(tm, 3)

Run from the repository root pixi run --locked mojo run -I src docs/examples/coloring.mojo

Output

2 colors: unsat
3 colors: sat

The function builds a fresh solver for each color budget. Its loop creates three symbolic integers with bounds 0 <= vertex < colors. The all-distinct constraint expresses the three edges compactly for this particular graph.

The successful case checks the returned model's bounds and edge condition, rather than requiring a particular permutation of color numbers. Color labels have no intrinsic meaning here: swapping red and blue changes the printed assignment without changing the validity of the coloring.

Generalize to an edge list

For a general graph, create one term per vertex, then loop over the edge list and assert a disequality for each pair of endpoints. Do not assert that every vertex has a different color. Nonadjacent vertices may share a color, and forbidding that would solve a different problem.

The Mojo loops construct a finite collection of formulas. They are not quantifiers inside the solver. The graph's size is known to the program constructing the model, and each edge becomes an explicit assertion.

Fixing one vertex's color can remove equivalent assignments caused by renaming colors. Such a symmetry restriction is justified only when the color labels really are interchangeable. If “red” represents a resource with special restrictions, arbitrarily fixing a vertex to red can remove valid solutions.

Exercise: remove an edge

What happens if the triangle becomes a path with two edges?

Worked answer. Two colors now suffice: the endpoint vertices can share one color while the middle vertex uses the other. Replace the all-distinct constraint with just the two edge disequalities. This demonstrates why the graph's edge list, rather than an all-distinct shortcut, must define the general encoding.

Next: scheduling.

cvc5_mojo v1.4.1.1 for Mojo 1.2 and cvc5 1.4.1 · Apache License 2.0 with LLVM Exceptions · Source on GitHub