Graph degree as solution for undirected graph paint

Posted on


The problem is:

Given an undirected graph represented as an adjacency matrix and an
integer k, write a function to determine whether each vertex in the
graph can be coloured such that no two adjacent vertices share the same
colour using at most k colours.

The proposed solution uses a backtracking algorithm (, but I would like to know if just evaluating the vertice degree is enough (source:

    boolean canBeColored(int[][] adjacencyMatrix, int colors) {

    for (int row = 0; row < adjacencyMatrix.length; row++) {
        int degree = 0;
        for (int column = 0; column < adjacencyMatrix[row].length; column++) {
            if (adjacencyMatrix[row][column] == 1) {

        if (degree > colors) {
            return false;

    return true;


It is not. Consider a triangle graph which has tree nodes and three edges. All vertices has degree two but three colors are required to color it. Your function would fail for that input.

In fact, the problem is NP-complete meaning that it is not known if an efficient algorithm can be constructed for solving it. So if you can do it, you’ll win fame and fortune and also a very large monetary prize.

Leave a Reply

Your email address will not be published. Required fields are marked *