Skip to content

M-coloring Problem added #4282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 5, 2023
70 changes: 70 additions & 0 deletions src/main/java/com/thealgorithms/backtracking/MColoring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.thealgorithms.backtracking;

import java.io.*;
import java.util.*;

/**
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
class Node {
int color = 1;
Set<Integer> edges = new HashSet<Integer>();
}

public class MColoring {
static int possiblePaint(ArrayList<Node> nodes, int n, int m) {

// Create a visited array of n nodes
ArrayList<Integer> visited = new ArrayList<Integer>();
for (int i = 0; i < n + 1; i++) {
visited.add(0);
}

// maxColors used till now are 1 as
// all nodes are painted color 1
int maxColors = 1;

for (int sv = 1; sv <= n; sv++) {
if (visited.get(sv) > 0) {
continue;
}

// If the starting point is unvisited,
// mark it visited and push it in queue
visited.set(sv, 1);
Queue<Integer> q = new LinkedList<>();
q.add(sv);

// BFS
while (q.size() != 0) {
int top = q.peek();
q.remove();

// Checking all adjacent nodes
// to "top" edge in our queue
for (int it : nodes.get(top).edges) {

// If the color of the
// adjacent node is same, increase it by
// 1
if (nodes.get(top).color == nodes.get(it).color) {
nodes.get(it).color += 1;
}

// If number of colors used exceeds m,
// return 0
maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
if (maxColors > m) return 0;

// If the adjacent node is not visited,
// mark it visited and push it in queue
if (visited.get(it) == 0) {
visited.set(it, 1);
q.add(it);
}
}
}
}
return 1;
}
}
57 changes: 57 additions & 0 deletions src/test/java/com/thealgorithms/backtracking/MColoringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.thealgorithms.backtracking;

import static org.junit.jupiter.api.Assertions.*;

import java.util.*;
import org.junit.jupiter.api.Test;

/**
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
class MColoringTest {

@Test
void testGraphColoring1() {
int n = 4;
int[][] graph = {{0, 1, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 1}, {1, 0, 1, 0}};
int m = 3; // Number of colors

assertEquals(1, MColoring.possiblePaint(createGraph(graph), n, m));
}

@Test
void testGraphColoring2() {
int n = 5;
int[][] graph = {{0, 1, 1, 1, 0}, {1, 0, 0, 1, 0}, {1, 0, 0, 1, 1}, {1, 1, 1, 0, 1}, {0, 0, 1, 1, 0}};
int m = 2; // Number of colors

assertEquals(0, MColoring.possiblePaint(createGraph(graph), n, m));
}

@Test
void testGraphColoring3() {
int n = 3;
int[][] graph = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
int m = 2; // Number of colors

assertEquals(0, MColoring.possiblePaint(createGraph(graph), n, m));
}

private ArrayList<Node> createGraph(int[][] graph) {
int n = graph.length;
ArrayList<Node> nodes = new ArrayList<>(n + 1);
for (int i = 0; i <= n; i++) {
nodes.add(new Node());
}

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) { // Use j = i + 1 to avoid setting edges twice
if (graph[i][j] > 0) {
nodes.get(i + 1).edges.add(j + 1);
nodes.get(j + 1).edges.add(i + 1);
}
}
}
return nodes;
}
}