Skip to content

Commit dab8ff3

Browse files
authored
Improve comments in Mcoloring.java (#5484)
1 parent 0bd86b3 commit dab8ff3

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

src/main/java/com/thealgorithms/backtracking/MColoring.java

+43-24
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,90 @@
77
import java.util.Set;
88

99
/**
10+
* Node class represents a graph node. Each node is associated with a color
11+
* (initially 1) and contains a set of edges representing its adjacent nodes.
12+
*
1013
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1114
*/
1215
class Node {
13-
int color = 1;
14-
Set<Integer> edges = new HashSet<Integer>();
16+
int color = 1; // Initial color for each node
17+
Set<Integer> edges = new HashSet<Integer>(); // Set of edges representing adjacent nodes
1518
}
1619

20+
/**
21+
* MColoring class solves the M-Coloring problem where the goal is to determine
22+
* if it's possible to color a graph using at most M colors such that no two
23+
* adjacent nodes have the same color.
24+
*/
1725
public final class MColoring {
26+
1827
private MColoring() {
19-
}
20-
static int possiblePaint(ArrayList<Node> nodes, int n, int m) {
28+
} // Prevent instantiation of utility class
2129

22-
// Create a visited array of n nodes
30+
/**
31+
* Determines whether it is possible to color the graph using at most M colors.
32+
*
33+
* @param nodes List of nodes representing the graph.
34+
* @param n The total number of nodes in the graph.
35+
* @param m The maximum number of allowed colors.
36+
* @return true if the graph can be colored using M colors, false otherwise.
37+
*/
38+
static boolean isColoringPossible(ArrayList<Node> nodes, int n, int m) {
39+
40+
// Visited array keeps track of whether each node has been processed.
2341
ArrayList<Integer> visited = new ArrayList<Integer>();
2442
for (int i = 0; i < n + 1; i++) {
25-
visited.add(0);
43+
visited.add(0); // Initialize all nodes as unvisited (0)
2644
}
2745

28-
// maxColors used till now are 1 as
29-
// all nodes are painted color 1
46+
// The number of colors used so far (initially set to 1, since all nodes
47+
// start with color 1).
3048
int maxColors = 1;
3149

50+
// Loop through all the nodes to ensure every node is visited, in case the
51+
// graph is disconnected.
3252
for (int sv = 1; sv <= n; sv++) {
3353
if (visited.get(sv) > 0) {
34-
continue;
54+
continue; // Skip nodes that are already visited
3555
}
3656

37-
// If the starting point is unvisited,
38-
// mark it visited and push it in queue
57+
// If the node is unvisited, mark it as visited and add it to the queue for BFS.
3958
visited.set(sv, 1);
4059
Queue<Integer> q = new LinkedList<>();
4160
q.add(sv);
4261

43-
// BFS
62+
// Perform BFS to process all nodes and their adjacent nodes
4463
while (q.size() != 0) {
45-
int top = q.peek();
64+
int top = q.peek(); // Get the current node from the queue
4665
q.remove();
4766

48-
// Checking all adjacent nodes
49-
// to "top" edge in our queue
67+
// Check all adjacent nodes of the current node
5068
for (int it : nodes.get(top).edges) {
5169

52-
// If the color of the
53-
// adjacent node is same, increase it by
54-
// 1
70+
// If the adjacent node has the same color as the current node, increment its
71+
// color to avoid conflict.
5572
if (nodes.get(top).color == nodes.get(it).color) {
5673
nodes.get(it).color += 1;
5774
}
5875

59-
// If number of colors used exceeds m,
60-
// return 0
76+
// Keep track of the maximum number of colors used so far
6177
maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
78+
79+
// If the number of colors used exceeds the allowed limit M, return false.
6280
if (maxColors > m) {
63-
return 0;
81+
return false;
6482
}
6583

66-
// If the adjacent node is not visited,
67-
// mark it visited and push it in queue
84+
// If the adjacent node hasn't been visited yet, mark it as visited and add it
85+
// to the queue for further processing.
6886
if (visited.get(it) == 0) {
6987
visited.set(it, 1);
7088
q.add(it);
7189
}
7290
}
7391
}
7492
}
75-
return 1;
93+
94+
return true; // Possible to color the entire graph with M or fewer colors.
7695
}
7796
}

src/test/java/com/thealgorithms/backtracking/MColoringTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
45

56
import java.util.ArrayList;
67
import org.junit.jupiter.api.Test;
@@ -16,7 +17,7 @@ void testGraphColoring1() {
1617
int[][] graph = {{0, 1, 1, 1}, {1, 0, 1, 0}, {1, 1, 0, 1}, {1, 0, 1, 0}};
1718
int m = 3; // Number of colors
1819

19-
assertEquals(1, MColoring.possiblePaint(createGraph(graph), n, m));
20+
assertTrue(MColoring.isColoringPossible(createGraph(graph), n, m));
2021
}
2122

2223
@Test
@@ -25,7 +26,7 @@ void testGraphColoring2() {
2526
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}};
2627
int m = 2; // Number of colors
2728

28-
assertEquals(0, MColoring.possiblePaint(createGraph(graph), n, m));
29+
assertFalse(MColoring.isColoringPossible(createGraph(graph), n, m));
2930
}
3031

3132
@Test
@@ -34,7 +35,7 @@ void testGraphColoring3() {
3435
int[][] graph = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
3536
int m = 2; // Number of colors
3637

37-
assertEquals(0, MColoring.possiblePaint(createGraph(graph), n, m));
38+
assertFalse(MColoring.isColoringPossible(createGraph(graph), n, m));
3839
}
3940

4041
private ArrayList<Node> createGraph(int[][] graph) {

0 commit comments

Comments
 (0)