Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@
* [LongestNonRepetitiveSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java)
* [LongestPalindromicSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java)
* [Lower](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Lower.java)
* [Manacher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Manacher.java)
* [MyAtoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/MyAtoi.java)
* [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Palindrome.java)
* [Pangram](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Pangram.java)
Expand Down Expand Up @@ -846,6 +847,8 @@
* [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
* [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
* misc
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
* [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
* [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
Expand All @@ -857,6 +860,7 @@
* [ArrayRightRotation](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotation.java)
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)
* [BestFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BestFitCPUTest.java)
* [BFPRTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BFPRTTest.java)
* [BoyerMooreTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BoyerMooreTest.java)
* cn
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java)
Expand Down Expand Up @@ -976,6 +980,7 @@
* [LongestNonRepetitiveSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestNonRepetitiveSubstringTest.java)
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java)
* [LowerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LowerTest.java)
* [ManacherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ManacherTest.java)
* [MyAtoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/MyAtoiTest.java)
* [PalindromeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PalindromeTest.java)
* [PangramTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PangramTest.java)
Expand Down
62 changes: 41 additions & 21 deletions src/main/java/com/thealgorithms/backtracking/MColoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,91 @@
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() {
}
} // Prevent instantiation of utility class

/**
* 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 1 if the graph can be colored using M colors, otherwise 0.
*/
static int possiblePaint(ArrayList<Node> nodes, int n, int m) {

// Create a visited array of n nodes
// 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 0 (failure).
if (maxColors > m) {
return 0;
}

// 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);
}
}
}
}

// Return 1 if it's possible to color the entire graph with M or fewer colors.
return 1;
}
}