HARD-P2

Three groups as a formula

Practice 2 of 5Hard problems9 tests

The problem

Can the vertices of a graph be split into three groups, 0, 1 and 2, so that no edge joins two vertices of the same group? Do not answer the question: write it as a formula, the way lesson 2 reduces problems to SAT.

Use variable 3 * v + c + 1 for “vertex v is in group c”. Return a list of clauses; a clause is a list of literals, k for “variable k is True” and -k for “variable k is False”. The formula must be satisfiable exactly when the split exists, and every assignment that satisfies it must describe a split: each vertex has at least one True group variable, and giving each vertex any one of its True groups puts no edge inside a group.

colouring_formula(2, [(0, 1)])
# for example
# [[1, 2, 3], [4, 5, 6], [-1, -4], [-2, -5], [-3, -6]]

Limits

  • Up to 3,000 vertices. At most 4n + 3E clauses: the formula must be built in polynomial time and stay small.
  • Edges join two different vertices, and no edge is listed twice.

Builds on HARD-2 Reductions.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1One vertexNot run
2A triangleNot run
3Four, all joinedNot run
4An odd wheelNot run
5An even wheelNot run
6The Petersen graphNot run
7A path with no edges at one endNot run
8300 verticesNot run
93,000 verticesNot run