Repository: TheAlgorithms/Java
Pull request #5484 (2024-09-30 16:55:24)

Improve comments in Mcoloring.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5484?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 52.29%. Comparing base [(`1e8abf1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/1e8abf1ddf257e9b1396a1156333ef948917416c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`e4fabf2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e4fabf2f70de57be2749687e5b7900a59699ee0c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #5484   +/-   ##
=========================================
  Coverage     52.29%   52.29%           
  Complexity     3256     3256           
=========================================
  Files           525      525           
  Lines         15184    15184           
  Branches       2887     2887           
=========================================
  Hits           7940     7940           
  Misses         6920     6920           
  Partials        324      324           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5484?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/backtracking/MColoring.java b/src/main/java/com/thealgorithms/backtracking/MColoring.java
index f069e46c..d0188dfd 100644
--- a/src/main/java/com/thealgorithms/backtracking/MColoring.java
+++ b/src/main/java/com/thealgorithms/backtracking/MColoring.java
@@ -7,64 +7,82 @@ import java.util.Queue;
 import java.util.Set;
 
 /**
+ * Node class represents a graph node. Each node is associated with a color
+ * (initially 1) and contains a set of edges representing its adjacent nodes.
+ *
  * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
  */
 class Node {
-    int color = 1;
-    Set<Integer> edges = new HashSet<Integer>();
+    int color = 1; // Initial color for each node
+    Set<Integer> edges = new HashSet<Integer>(); // Set of edges representing adjacent nodes
 }
 
+/**
+ * MColoring class solves the M-Coloring problem where the goal is to determine
+ * if it's possible to color a graph using at most M colors such that no two
+ * adjacent nodes have the same color.
+ */
 public final class MColoring {
+
     private MColoring() {
-    }
-    static int possiblePaint(ArrayList<Node> nodes, int n, int m) {
+    } // Prevent instantiation of utility class
 
-        // Create a visited array of n nodes
+    /**
+     * Determines whether it is possible to color the graph using at most M colors.
+     *
+     * @param nodes List of nodes representing the graph.
+     * @param n     The total number of nodes in the graph.
+     * @param m     The maximum number of allowed colors.
+     * @return true if the graph can be colored using M colors, false otherwise.
+     */
+    static boolean isColoringPossible(ArrayList<Node> nodes, int n, int m) {
+
+        // Visited array keeps track of whether each node has been processed.
         ArrayList<Integer> visited = new ArrayList<Integer>();
         for (int i = 0; i < n + 1; i++) {
-            visited.add(0);
+            visited.add(0); // Initialize all nodes as unvisited (0)
         }
 
-        // maxColors used till now are 1 as
-        // all nodes are painted color 1
+        // The number of colors used so far (initially set to 1, since all nodes
+        // start with color 1).
         int maxColors = 1;
 
+        // Loop through all the nodes to ensure every node is visited, in case the
+        // graph is disconnected.
         for (int sv = 1; sv <= n; sv++) {
             if (visited.get(sv) > 0) {
-                continue;
+                continue; // Skip nodes that are already visited
             }
 
-            // If the starting point is unvisited,
-            // mark it visited and push it in queue
+            // If the node is unvisited, mark it as visited and add it to the queue for BFS.
             visited.set(sv, 1);
             Queue<Integer> q = new LinkedList<>();
             q.add(sv);
 
-            // BFS
+            // Perform BFS to process all nodes and their adjacent nodes
             while (q.size() != 0) {
-                int top = q.peek();
+                int top = q.peek(); // Get the current node from the queue
                 q.remove();
 
-                // Checking all adjacent nodes
-                // to "top" edge in our queue
+                // Check all adjacent nodes of the current node
                 for (int it : nodes.get(top).edges) {
 
-                    // If the color of the
-                    // adjacent node is same, increase it by
-                    // 1
+                    // If the adjacent node has the same color as the current node, increment its
+                    // color to avoid conflict.
                     if (nodes.get(top).color == nodes.get(it).color) {
                         nodes.get(it).color += 1;
                     }
 
-                    // If number of colors used exceeds m,
-                    // return 0
+                    // Keep track of the maximum number of colors used so far
                     maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
+
+                    // If the number of colors used exceeds the allowed limit M, return false.
                     if (maxColors > m) {
-                        return 0;
+                        return false;
                     }
 
-                    // If the adjacent node is not visited,
-                    // mark it visited and push it in queue
+                    // If the adjacent node hasn't been visited yet, mark it as visited and add it
+                    // to the queue for further processing.
                     if (visited.get(it) == 0) {
                         visited.set(it, 1);
                         q.add(it);
@@ -72,6 +90,7 @@ public final class MColoring {
                 }
             }
         }
-        return 1;
+
+        return true; // Possible to color the entire graph with M or fewer colors.
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/backtracking/MColoringTest.java b/src/test/java/com/thealgorithms/backtracking/MColoringTest.java
index 8b505abb..f3f25933 100644
--- a/src/test/java/com/thealgorithms/backtracking/MColoringTest.java
+++ b/src/test/java/com/thealgorithms/backtracking/MColoringTest.java
@@ -1,6 +1,7 @@
 package com.thealgorithms.backtracking;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.ArrayList;
 import org.junit.jupiter.api.Test;
@@ -16,7 +17,7 @@ class MColoringTest {
         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));
+        assertTrue(MColoring.isColoringPossible(createGraph(graph), n, m));
     }
 
     @Test
@@ -25,7 +26,7 @@ class MColoringTest {
         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));
+        assertFalse(MColoring.isColoringPossible(createGraph(graph), n, m));
     }
 
     @Test
@@ -34,7 +35,7 @@ class MColoringTest {
         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));
+        assertFalse(MColoring.isColoringPossible(createGraph(graph), n, m));
     }
 
     private ArrayList<Node> createGraph(int[][] graph) {