Skip to content

Commit 2c86dd4

Browse files
committed
Merge branch 'sum_of_subset_improvement' of https://github.com/Hardvan/Java into sum_of_subset_improvement
2 parents e963638 + 29e5853 commit 2c86dd4

File tree

10 files changed

+670
-39
lines changed

10 files changed

+670
-39
lines changed

DIRECTORY.md

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
* [ConnectedComponent](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/ConnectedComponent.java)
108108
* [Cycles](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Cycles.java)
109109
* [DijkstraAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java)
110+
* [EdmondsBlossomAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithm.java)
110111
* [FloydWarshall](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FloydWarshall.java)
111112
* [FordFulkerson](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java)
112113
* [Graphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java)
@@ -265,6 +266,7 @@
265266
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
266267
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
267268
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
269+
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
268270
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
269271
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
270272
* io
@@ -449,6 +451,8 @@
449451
* [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TowerOfHanoi.java)
450452
* [TwoPointers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TwoPointers.java)
451453
* [Verhoeff](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Verhoeff.java)
454+
* Recursion
455+
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
452456
* scheduling
453457
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
454458
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
@@ -658,6 +662,7 @@
658662
* graphs
659663
* [BoruvkaAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java)
660664
* [DijkstraAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java)
665+
* [EdmondsBlossomAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithmTest.java)
661666
* [FordFulkersonTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java)
662667
* [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java)
663668
* [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java)
@@ -749,6 +754,7 @@
749754
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
750755
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
751756
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
757+
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
752758
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
753759
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
754760
* io
@@ -889,6 +895,8 @@
889895
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
890896
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
891897
* [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
898+
* Recursion
899+
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
892900
* scheduling
893901
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
894902
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.Recursion;
2+
3+
// program to find power set of a string
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public final class GenerateSubsets {
9+
10+
private GenerateSubsets() {
11+
throw new UnsupportedOperationException("Utility class");
12+
}
13+
14+
public static List<String> subsetRecursion(String str) {
15+
return doRecursion("", str);
16+
}
17+
18+
private static List<String> doRecursion(String p, String up) {
19+
if (up.isEmpty()) {
20+
List<String> list = new ArrayList<>();
21+
list.add(p);
22+
return list;
23+
}
24+
25+
// Taking the character
26+
char ch = up.charAt(0);
27+
// Adding the character in the recursion
28+
List<String> left = doRecursion(p + ch, up.substring(1));
29+
// Not adding the character in the recursion
30+
List<String> right = doRecursion(p, up.substring(1));
31+
32+
left.addAll(right);
33+
34+
return left;
35+
}
36+
}

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
}

0 commit comments

Comments
 (0)