Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

M-Coloring Problem Editorial

DSA Editorial, Solution and Code

Practice Problem Link: M-Coloring Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given an undirected graph with n nodes and an integer m. Find if it is possible to color all the vertices of the graph using at most m colors, such that no two adjacent vertices have the same color.

Naive Approach

We can use recursive brute force to solve this problem naively. We try to generate all the possible configurations of colors. Then we check if any node and its neighbors have their colors equal. Once we find a valid configuration, we can return true. Else, after exhaustively searching for all the possible configurations we return false.

Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(n)

Implementation

C++
bool isSafe(vector<vector<int>> adjMatrix, int color[], int n) {
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if(adjMatrix[i][j] && color[j] == color[i]) {
				return false;
			}
		}
	}
	return true;
}
bool colorGraph(vector<vector<int>> adjMatrix, int m, int index, int color[], int n) {
	if(index == n) {
		return ((isSafe(adjMatrix, color, n));
	}
	for(int j = 1; j <= m; j++) {
		color[index] = j;
		if(colorGraph(adjMatrix, m, index + 1, color, n)) {
			return true;
		}
		color[index] = 0;
	}
	return false;
}
bool isColoringPossible(vector<vector<int>> adjMatrix, int m) {
	int n = adjMatrix.size();
    int color[n];
	memset(color, 0, sizeof(color));
	for(int i = 0; i < n; i++) {
		if(adjMatrix[i][i]) {
			return false;
		}
	}
	return colorGraph(adjMatrix, m, 0, color, n);
}
Java
class Solution {
	boolean isSafe(int[][] adjMatrix, int[] color, int n) {
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if(adjMatrix[i][j] == 1 && color[j] == color[i]) {
					return false;
				}
			}
		}
		return true;
	}
	boolean colorGraph(int[][] adjMatrix, int m, int index, int[] color, int n) {
		if(index == n) {
			return ((isSafe(adjMatrix, color, n));
		}
		for(int j = 1; j <= m; j++) {
			color[index] = j;
			if(colorGraph(adjMatrix, m, index + 1, color, n)) {
				return true;
			}
			color[index] = 0;
		}
		return false;
	}
	boolean isColoringPossible(int[][] adjMatrix, int m) {
    	int n = adjMatrix.length;
		int color[] = new int[n];
		for(int i = 0; i < n; i++) {
			if(adjMatrix[i][i] == 1) {
				return false;
			}
		}
		return colorGraph(adjMatrix, m, 0, color, n);
	}
}

Optimal Approach

We approach the problem with a backtracking-based approach, trying to prune through many impossible cases. The idea is to assign colors to the vertexes one by one starting from vertex 0. Before assignment of the current color, we check if applying the current color is valid, else we backtrack through the configuration to find another valid configuration. If no valid configuration is found, we return false.

Analysis:

  • Time Complexity: O(mn) // The average-case complexity will be much improved upon.
  • Space Complexity: O(n)

Implementation:

C++
bool isSafe(vector<vector<int>> adjMatrix, int color[], int n, int c, int v) {
	for (int i = 0; i < n; i++) {
		if(adjMatrix[v][i] && c == color[i]) {
			return false;
		}
	}
	return true;
}
bool colorGraph(vector<vector<int>> adjMatrix, int m, int index, int color[], int n, int v) {
	if(v == n) {
		return true;
	}
	for (int j = 1; j <= m; j++) {
		if(isSafe(adjMatrix, color, n, j, v)) {
			color[v] = j;
			if(colorGraph(adjMatrix, m, index, color, n, v + 1)) {
				return true;
			}
			color[v] = 0;
		}
	}
	return false;
}
bool isColoringPossible(vector<vector<int>> adjMatrix, int m) {
	int n = adjMatrix.size();
    int color[n];
	memset(color, 0, sizeof(color));
	for(int i = 0; i < n; i++) {
		if(adjMatrix[i][i]) {
			return false;
		}
	}
	return colorGraph(adjMatrix, m, 0, color, n, 0);
}
Java
class Solution {
	boolean isSafe(int[][] adjMatrix, int[] color, int n, int c, int v) {
		for (int i = 0; i < n; i++) {
			if(adjMatrix[v][i] == 1 && c == color[i]) {
				return false;
			}
		}
		return true;
	}
	boolean colorGraph(int[][] adjMatrix, int m, int index, int[] color, int n, int v) {
		if(v == n) {
			return true;
		}
		for (int j = 1; j <= m; j++) {
			if(isSafe(adjMatrix, color, n, j, v)) {
				color[v] = j;
				if(colorGraph(adjMatrix, m, index, color, n, v + 1)) {
					return true;
				}
				color[v] = 0;
			}
		}
		return false;
	}
	boolean isColoringPossible(int[][] adjMatrix, int m) {
    	int n = adjMatrix.length;
		int color[] = new int[n];
		for(int i = 0; i < n; i++) {
			if(adjMatrix[i][i] == 1) {
				return false;
			}
		}
		return colorGraph(adjMatrix, m, 0, color, n, 0);
	}
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
Number of Islands
Stepping Numbers
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet