Repository: TheAlgorithms/Java
Pull request #4282 (2023-08-04 19:43:34)

M-coloring Problem added
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

Added question

debasishbsws: Fix the formating mentioned in the Clang format linter
https://github.com/TheAlgorithms/Java/actions/runs/5766002343/job/15633103083?pr=4282#step:4:12:~:text=11-,%2D%2D%2D%20./src/test/java/com/thealgorithms/backtracking/MColoringTest.java%09(original),%7D,-Post%20Run%20actions
BamaCharanChhandogi: > Fix the formating mentioned in the Clang format linter https://github.com/TheAlgorithms/Java/actions/runs/5766002343/job/15633103083?pr=4282#step:4:12:~:text=11-,%2D%2D%2D%20./src/test/java/com/thealgorithms/backtracking/MColoringTest.java%09(original),%7D,-Post%20Run%20actions

Can You please tell me more? I don't understand this issue.
BamaCharanChhandogi: Hey, @debasishbsws The Whole code is running successfully. 
 I tried a lot but I don't understand what is actually problem!
 can you please check?

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 931e7695..e1a6297c 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
diff --git a/src/main/java/com/thealgorithms/backtracking/MColoring.java b/src/main/java/com/thealgorithms/backtracking/MColoring.java
new file mode 100644
index 00000000..c9bc0200
--- /dev/null
+++ b/src/main/java/com/thealgorithms/backtracking/MColoring.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/backtracking/MColoringTest.java b/src/test/java/com/thealgorithms/backtracking/MColoringTest.java
new file mode 100644
index 00000000..606193b0
--- /dev/null
+++ b/src/test/java/com/thealgorithms/backtracking/MColoringTest.java
@@ -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;
+    }
+}