|
7 | 7 | import java.util.Set;
|
8 | 8 |
|
9 | 9 | /**
|
| 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 | + * |
10 | 13 | * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
11 | 14 | */
|
12 | 15 | 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 |
15 | 18 | }
|
16 | 19 |
|
| 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 | + */ |
17 | 25 | public final class MColoring {
|
| 26 | + |
18 | 27 | private MColoring() {
|
19 |
| - } |
20 |
| - static int possiblePaint(ArrayList<Node> nodes, int n, int m) { |
| 28 | + } // Prevent instantiation of utility class |
21 | 29 |
|
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. |
23 | 41 | ArrayList<Integer> visited = new ArrayList<Integer>();
|
24 | 42 | for (int i = 0; i < n + 1; i++) {
|
25 |
| - visited.add(0); |
| 43 | + visited.add(0); // Initialize all nodes as unvisited (0) |
26 | 44 | }
|
27 | 45 |
|
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). |
30 | 48 | int maxColors = 1;
|
31 | 49 |
|
| 50 | + // Loop through all the nodes to ensure every node is visited, in case the |
| 51 | + // graph is disconnected. |
32 | 52 | for (int sv = 1; sv <= n; sv++) {
|
33 | 53 | if (visited.get(sv) > 0) {
|
34 |
| - continue; |
| 54 | + continue; // Skip nodes that are already visited |
35 | 55 | }
|
36 | 56 |
|
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. |
39 | 58 | visited.set(sv, 1);
|
40 | 59 | Queue<Integer> q = new LinkedList<>();
|
41 | 60 | q.add(sv);
|
42 | 61 |
|
43 |
| - // BFS |
| 62 | + // Perform BFS to process all nodes and their adjacent nodes |
44 | 63 | while (q.size() != 0) {
|
45 |
| - int top = q.peek(); |
| 64 | + int top = q.peek(); // Get the current node from the queue |
46 | 65 | q.remove();
|
47 | 66 |
|
48 |
| - // Checking all adjacent nodes |
49 |
| - // to "top" edge in our queue |
| 67 | + // Check all adjacent nodes of the current node |
50 | 68 | for (int it : nodes.get(top).edges) {
|
51 | 69 |
|
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. |
55 | 72 | if (nodes.get(top).color == nodes.get(it).color) {
|
56 | 73 | nodes.get(it).color += 1;
|
57 | 74 | }
|
58 | 75 |
|
59 |
| - // If number of colors used exceeds m, |
60 |
| - // return 0 |
| 76 | + // Keep track of the maximum number of colors used so far |
61 | 77 | 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. |
62 | 80 | if (maxColors > m) {
|
63 |
| - return 0; |
| 81 | + return false; |
64 | 82 | }
|
65 | 83 |
|
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. |
68 | 86 | if (visited.get(it) == 0) {
|
69 | 87 | visited.set(it, 1);
|
70 | 88 | q.add(it);
|
71 | 89 | }
|
72 | 90 | }
|
73 | 91 | }
|
74 | 92 | }
|
75 |
| - return 1; |
| 93 | + |
| 94 | + return true; // Possible to color the entire graph with M or fewer colors. |
76 | 95 | }
|
77 | 96 | }
|
0 commit comments