Add M-coloring Problem (#4282)

Co-authored-by: BamaCharanChhandogi <b.c.chhandogi@gmailcom>
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Bama Charan Chhandogi 2023-08-06 00:30:26 +05:30 committed by GitHub
parent c4a9ef1566
commit 1ef5208b75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 1 deletions

View File

@ -13,6 +13,7 @@
* [FloodFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/FloodFill.java)
* [KnightsTour](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/KnightsTour.java)
* [MazeRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java)
* [MColoring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MColoring.java)
* [NQueens](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/NQueens.java)
* [Permutation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/Permutation.java)
* [PowerSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/PowerSum.java)
@ -181,7 +182,7 @@
* [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java)
* [VerticalOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java)
* [ZigzagTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/ZigzagTraversal.java)
* devutils
* utils
* entities
* [ProcessDetails](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java)
* nodes
@ -522,6 +523,7 @@
* [CombinationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/CombinationTest.java)
* [FloodFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java)
* [MazeRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java)
* [MColoringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/MColoringTest.java)
* [PermutationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/PermutationTest.java)
* [PowerSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/PowerSumTest.java)
* [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java)
@ -645,6 +647,7 @@
* [FrizzyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FrizzyNumberTest.java)
* [GaussianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GaussianTest.java)
* [GCDTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GCDTest.java)
* [GenericRootTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GenericRootTest.java)
* [HarshadNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java)
* [HeronsFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java)
* [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
@ -665,6 +668,7 @@
* [PerfectSquareTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectSquareTest.java)
* [PerimeterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerimeterTest.java)
* [PollardRhoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PollardRhoTest.java)
* [PowerOfTwoOrNotTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PowerOfTwoOrNotTest.java)
* [PrimeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PrimeCheckTest.java)
* [PrimeFactorizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PrimeFactorizationTest.java)
* [PronicNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PronicNumberTest.java)

View File

@ -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;
}
}

View File

@ -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;
}
}