Skip to content

Commit c9f7944

Browse files
authored
Merge branch 'master' into master
2 parents 1f39d7f + dab8ff3 commit c9f7944

File tree

4 files changed

+94
-43
lines changed

4 files changed

+94
-43
lines changed

DIRECTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@
561561
* [LongestNonRepetitiveSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java)
562562
* [LongestPalindromicSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java)
563563
* [Lower](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Lower.java)
564+
* [Manacher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Manacher.java)
564565
* [MyAtoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/MyAtoi.java)
565566
* [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Palindrome.java)
566567
* [Pangram](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Pangram.java)
@@ -848,6 +849,8 @@
848849
* [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
849850
* [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
850851
* misc
852+
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
853+
* [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
851854
* [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
852855
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
853856
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
@@ -859,6 +862,7 @@
859862
* [ArrayRightRotation](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotation.java)
860863
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)
861864
* [BestFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BestFitCPUTest.java)
865+
* [BFPRTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BFPRTTest.java)
862866
* [BoyerMooreTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BoyerMooreTest.java)
863867
* cn
864868
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java)
@@ -978,6 +982,7 @@
978982
* [LongestNonRepetitiveSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestNonRepetitiveSubstringTest.java)
979983
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java)
980984
* [LowerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LowerTest.java)
985+
* [ManacherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ManacherTest.java)
981986
* [MyAtoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/MyAtoiTest.java)
982987
* [PalindromeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PalindromeTest.java)
983988
* [PangramTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PangramTest.java)

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

Lines changed: 43 additions & 24 deletions
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/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,65 @@
44
import java.util.Arrays;
55
import java.util.Comparator;
66

7-
// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem
7+
// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem
88

99
public final class ActivitySelection {
10+
11+
// Private constructor to prevent instantiation of the utility class
1012
private ActivitySelection() {
1113
}
12-
// Function to perform activity selection
14+
15+
/**
16+
* Function to perform activity selection using a greedy approach.
17+
*
18+
* The goal is to select the maximum number of activities that don't overlap
19+
* with each other, based on their start and end times. Activities are chosen
20+
* such that no two selected activities overlap.
21+
*
22+
* @param startTimes Array containing the start times of the activities.
23+
* @param endTimes Array containing the end times of the activities.
24+
* @return A list of indices representing the selected activities that can be
25+
* performed without overlap.
26+
*/
1327
public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) {
1428
int n = startTimes.length;
15-
int[][] activities = new int[n][3];
1629

17-
// Create a 2D array to store activities and their start/end times.
18-
// Each row: [activity index, start time, end time]
30+
// Create a 2D array to store activity indices along with their start and end
31+
// times.
32+
// Each row represents an activity in the format: [activity index, start time,
33+
// end time].
34+
int[][] activities = new int[n][3];
1935

36+
// Populate the 2D array with the activity index, start time, and end time.
2037
for (int i = 0; i < n; i++) {
21-
activities[i][0] = i; // Assign activity index
22-
activities[i][1] = startTimes[i]; // Assign start time
23-
activities[i][2] = endTimes[i]; // Assign end time
38+
activities[i][0] = i; // Assign the activity index
39+
activities[i][1] = startTimes[i]; // Assign the start time of the activity
40+
activities[i][2] = endTimes[i]; // Assign the end time of the activity
2441
}
2542

26-
// Sort activities by their end times in ascending order.
43+
// Sort activities based on their end times in ascending order.
44+
// This ensures that we always try to finish earlier activities first.
2745
Arrays.sort(activities, Comparator.comparingDouble(activity -> activity[2]));
28-
int lastEndTime;
46+
int lastEndTime; // Variable to store the end time of the last selected activity
47+
// List to store the indices of selected activities
2948
ArrayList<Integer> selectedActivities = new ArrayList<>();
30-
selectedActivities.add(activities[0][0]);
31-
lastEndTime = activities[0][2];
3249

33-
// Iterate through sorted activities to select compatible ones.
50+
// Select the first activity (as it has the earliest end time after sorting)
51+
selectedActivities.add(activities[0][0]); // Add the first activity index to the result
52+
lastEndTime = activities[0][2]; // Keep track of the end time of the last selected activity
53+
54+
// Iterate over the sorted activities to select the maximum number of compatible
55+
// activities.
3456
for (int i = 1; i < n; i++) {
57+
// If the start time of the current activity is greater than or equal to the
58+
// end time of the last selected activity, it means there's no overlap.
3559
if (activities[i][1] >= lastEndTime) {
36-
selectedActivities.add(activities[i][0]);
37-
lastEndTime = activities[i][2];
60+
selectedActivities.add(activities[i][0]); // Select this activity
61+
lastEndTime = activities[i][2]; // Update the end time of the last selected activity
3862
}
3963
}
64+
65+
// Return the list of selected activity indices.
4066
return selectedActivities;
4167
}
4268
}

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

Lines changed: 5 additions & 4 deletions
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)