All problems
Chromatic Number Check
Problem
Compute the chromatic number of a small undirected graph: the fewest colors needed for a proper coloring.
Try k = 1, 2, 3, ... and use backtracking with symmetry breaking (a node may only use colors up to the highest already used plus one). Return the smallest feasible k. edges is a list of [u, v] pairs and n is small.
Hint
Symmetry breaking avoids trying color permutations of the same partial coloring.
Test cases
case 1
in: [3,[[0,1],[1,2],[2,0]]]
expected: 3
case 2
in: [4,[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]]
expected: 4
case 3
in: [4,[[0,1],[1,2],[2,3],[3,0]]]
expected: 2
case 4
in: [5,[[0,1],[1,2],[2,3],[3,4]]]
expected: 2
case 5
in: [3,[]]
expected: 1
Starter codePython
def chromatic_number(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor