All problems
Greedy Coloring by Degree
Problem
Color an undirected graph greedily in descending degree order.
Sort nodes by (-degree, index), then give each node the smallest non-negative color not used by an already colored neighbor. edges is a simple undirected [u, v] list. Return colors in node-index order.
Hint
High-degree nodes go first, which usually keeps the palette small.
Test cases
case 1
in: [3,[[0,1],[1,2]]]
expected: [1,0,1]
case 2
in: [4,[[0,1],[0,2],[0,3]]]
expected: [0,1,1,1]
case 3
in: [3,[[0,1],[1,2],[2,0]]]
expected: [0,1,2]
case 4
in: [3,[[0,1],[0,2]]]
expected: [0,1,1]
case 5
in: [4,[[0,1],[1,2],[2,0],[2,3]]]
expected: [1,2,0,1]
Starter codePython
def greedy_coloring_by_degree(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor