Skip to content

Commit 833a482

Browse files
committed
Improve comments in Mcoloring.java
1 parent 1e8abf1 commit 833a482

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

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

+43-21
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,91 @@
99
/**
1010
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1111
*/
12+
13+
/**
14+
* Node class represents a graph node. Each node is associated with a color
15+
* (initially 1) and contains a set of edges representing its adjacent nodes.
16+
*/
1217
class Node {
13-
int color = 1;
14-
Set<Integer> edges = new HashSet<Integer>();
18+
int color = 1; // Initial color for each node
19+
Set<Integer> edges = new HashSet<Integer>(); // Set of edges representing adjacent nodes
1520
}
1621

22+
/**
23+
* MColoring class solves the M-Coloring problem where the goal is to determine
24+
* if it's possible to color a graph using at most M colors such that no two
25+
* adjacent nodes have the same color.
26+
*/
1727
public final class MColoring {
28+
1829
private MColoring() {
19-
}
30+
} // Prevent instantiation of utility class
31+
32+
/**
33+
* Determines whether it is possible to color the graph using at most M colors.
34+
*
35+
* @param nodes List of nodes representing the graph.
36+
* @param n The total number of nodes in the graph.
37+
* @param m The maximum number of allowed colors.
38+
* @return 1 if the graph can be colored using M colors, otherwise 0.
39+
*/
2040
static int possiblePaint(ArrayList<Node> nodes, int n, int m) {
2141

22-
// Create a visited array of n nodes
42+
// Visited array keeps track of whether each node has been processed.
2343
ArrayList<Integer> visited = new ArrayList<Integer>();
2444
for (int i = 0; i < n + 1; i++) {
25-
visited.add(0);
45+
visited.add(0); // Initialize all nodes as unvisited (0)
2646
}
2747

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

52+
// Loop through all the nodes to ensure every node is visited, in case the
53+
// graph is disconnected.
3254
for (int sv = 1; sv <= n; sv++) {
3355
if (visited.get(sv) > 0) {
34-
continue;
56+
continue; // Skip nodes that are already visited
3557
}
3658

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

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

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

52-
// If the color of the
53-
// adjacent node is same, increase it by
54-
// 1
72+
// If the adjacent node has the same color as the current node, increment its
73+
// color to avoid conflict.
5574
if (nodes.get(top).color == nodes.get(it).color) {
5675
nodes.get(it).color += 1;
5776
}
5877

59-
// If number of colors used exceeds m,
60-
// return 0
78+
// Keep track of the maximum number of colors used so far
6179
maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
80+
81+
// If the number of colors used exceeds the allowed limit M, return 0 (failure).
6282
if (maxColors > m) {
6383
return 0;
6484
}
6585

66-
// If the adjacent node is not visited,
67-
// mark it visited and push it in queue
86+
// If the adjacent node hasn't been visited yet, mark it as visited and add it
87+
// to the queue for further processing.
6888
if (visited.get(it) == 0) {
6989
visited.set(it, 1);
7090
q.add(it);
7191
}
7292
}
7393
}
7494
}
95+
96+
// Return 1 if it's possible to color the entire graph with M or fewer colors.
7597
return 1;
7698
}
7799
}

0 commit comments

Comments
 (0)