Skip to content

Commit b3c0ee6

Browse files
committed
feat: new recursion algo added
1 parent b11a6be commit b3c0ee6

File tree

15 files changed

+192
-34
lines changed

15 files changed

+192
-34
lines changed

src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
/**
9+
* Finds all permutations of given array
10+
* @author Tuhin Mondal (<a href="https://github.com/tuhinm2002">Git-Tuhin Mondal</a>)
11+
*/
12+
813
public final class GenerateSubsets {
914

1015
private GenerateSubsets() {
@@ -33,4 +38,4 @@ private static List<String> doRecursion(String p, String up) {
3338

3439
return left;
3540
}
36-
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.Recursion;
2+
3+
// program to find unique power set of a string
4+
5+
import java.util.*;
6+
7+
/**
8+
* Finds all permutations of given array
9+
* @author Tuhin Mondal (<a href="https://github.com/tuhinm2002">Git-Tuhin Mondal</a>)
10+
*/
11+
12+
public final class GenerateUniqueSubsets {
13+
14+
private GenerateUniqueSubsets() {
15+
throw new UnsupportedOperationException("Utility class");
16+
}
17+
18+
public static List<String> subsetRecursion(String str) {
19+
Set<String> ans = doRecursion("", str);
20+
List<String> a = new ArrayList<>(ans.stream().toList());
21+
Collections.sort(a);
22+
return a;
23+
}
24+
25+
private static Set<String> doRecursion(String p, String up) {
26+
if (up.isEmpty()) {
27+
Set<String> list = new HashSet<>();
28+
list.add(p);
29+
return list;
30+
}
31+
32+
// Taking the character
33+
char ch = up.charAt(0);
34+
// Adding the character in the recursion
35+
Set<String> left = doRecursion(p + ch, up.substring(1));
36+
// Not adding the character in the recursion
37+
Set<String> right = doRecursion(p, up.substring(1));
38+
39+
left.addAll(right);
40+
41+
return left;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.Recursion;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Finds all permutations of given array
8+
* @author Tuhin Mondal (<a href="https://github.com/tuhinm2002">Git-Tuhin Mondal</a>)
9+
*/
10+
11+
public final class TowerOfHanoi {
12+
private TowerOfHanoi() {
13+
throw new UnsupportedOperationException("Utility class");
14+
}
15+
16+
public static List<String> towerOfHanoi(int n) {
17+
List<String> arr = new ArrayList<>();
18+
recursionApproach(n, 'A', 'B', 'C', arr);
19+
return arr;
20+
}
21+
22+
public static void recursionApproach(int n, char a, char b, char c, List<String> list) {
23+
if (n == 1) {
24+
list.add("Take disk 1 from rod " + a + " to rod " + b);
25+
return;
26+
}
27+
28+
recursionApproach(n - 1, a, c, b, list);
29+
list.add("Take disk " + n + " from rod " + a + " to rod " + b);
30+
recursionApproach(n - 1, c, b, a, list);
31+
}
32+
}

src/main/java/com/thealgorithms/ciphers/DES.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void setKey(String key) {
3232
this.key = key;
3333
}
3434

35-
// Permutation table to convert initial 64-bit key to 56 bit key
35+
// GeneratePermutations table to convert initial 64-bit key to 56 bit key
3636
private static final int[] PC1 = {57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4};
3737

3838
// Lookup table used to shift the initial key, in order to generate the subkeys
@@ -66,7 +66,7 @@ public void setKey(String key) {
6666

6767
private static final int[][][] S = {S1, S2, S3, S4, S5, S6, S7, S8};
6868

69-
// Permutation table, used in the Feistel function post s-box usage
69+
// GeneratePermutations table, used in the Feistel function post s-box usage
7070
static final int[] PERMUTATION = {16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25};
7171

7272
// Table used for final inversion of the message box after 16 rounds of Feistel Function

src/main/java/com/thealgorithms/datastructures/crdt/GSet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public boolean lookup(T e) {
4444
}
4545

4646
/**
47-
* Compares the G-Set with another G-Set to check if it is a subset.
47+
* Compares the G-Set with another G-Set to check if it is a subsetRecursion.
4848
*
4949
* @param other the other G-Set to compare with
50-
* @return true if the current G-Set is a subset of the other, false otherwise
50+
* @return true if the current G-Set is a subsetRecursion of the other, false otherwise
5151
*/
5252
public boolean compare(GSet<T> other) {
5353
return other.elements.containsAll(elements);

src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ public boolean lookup(Element e) {
8888
}
8989

9090
/**
91-
* Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subset.
91+
* Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subsetRecursion.
9292
*
9393
* @param other The LWWElementSet to compare.
94-
* @return True if the set is subset, false otherwise.
94+
* @return True if the set is subsetRecursion, false otherwise.
9595
*/
9696
public boolean compare(LWWElementSet other) {
9797
return other.addSet.keySet().containsAll(addSet.keySet()) && other.removeSet.keySet().containsAll(removeSet.keySet());

src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ private String generateUniqueTag() {
132132
}
133133

134134
/**
135-
* Compares this Add-Wins OR-Set with another OR-Set to check if elements and tombstones are a subset.
135+
* Compares this Add-Wins OR-Set with another OR-Set to check if elements and tombstones are a subsetRecursion.
136136
*
137137
* @param other the other OR-Set to compare
138-
* @return true if the sets are subset, false otherwise
138+
* @return true if the sets are subsetRecursion, false otherwise
139139
*/
140140
public boolean compare(ORSet<T> other) {
141141
Set<Pair<T>> union = new HashSet<>(elements);

src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void remove(T element) {
6161
* Compares the current 2P-Set with another 2P-Set.
6262
*
6363
* @param otherSet The other 2P-Set to compare with.
64-
* @return True if both SetA and SetR are subset, otherwise false.
64+
* @return True if both SetA and SetR are subsetRecursion, otherwise false.
6565
*/
6666
public boolean compare(TwoPSet<T> otherSet) {
6767
return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR);

src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static class Graph {
6060
}
6161

6262
/**
63-
* Represents a subset for Union-Find operations
63+
* Represents a subsetRecursion for Union-Find operations
6464
*/
6565
private static class Component {
6666
int parent;
@@ -89,7 +89,7 @@ private static class BoruvkaState {
8989
/**
9090
* Adds the cheapest edges to the result list and performs Union operation on the subsets.
9191
*
92-
* @param cheapest Array containing the cheapest edge for each subset.
92+
* @param cheapest Array containing the cheapest edge for each subsetRecursion.
9393
*/
9494
void merge(final Edge[] cheapest) {
9595
for (int i = 0; i < graph.vertex; ++i) {
@@ -115,9 +115,9 @@ boolean hasMoreEdgesToAdd() {
115115
}
116116

117117
/**
118-
* Computes the cheapest edges for each subset in the Union-Find structure.
118+
* Computes the cheapest edges for each subsetRecursion in the Union-Find structure.
119119
*
120-
* @return an array containing the cheapest edge for each subset.
120+
* @return an array containing the cheapest edge for each subsetRecursion.
121121
*/
122122
private Edge[] computeCheapestEdges() {
123123
Edge[] cheapest = new Edge[graph.vertex];
@@ -153,11 +153,11 @@ private static Component[] initializeComponents(final Graph graph) {
153153
}
154154

155155
/**
156-
* Finds the parent of the subset using path compression
156+
* Finds the parent of the subsetRecursion using path compression
157157
*
158158
* @param components array of subsets
159-
* @param i index of the subset
160-
* @return the parent of the subset
159+
* @param i index of the subsetRecursion
160+
* @return the parent of the subsetRecursion
161161
*/
162162
static int find(final Component[] components, final int i) {
163163
if (components[i].parent != i) {
@@ -170,8 +170,8 @@ static int find(final Component[] components, final int i) {
170170
* Performs the Union operation for Union-Find
171171
*
172172
* @param components array of subsets
173-
* @param x index of the first subset
174-
* @param y index of the second subset
173+
* @param x index of the first subsetRecursion
174+
* @param y index of the second subsetRecursion
175175
*/
176176
static void union(Component[] components, final int x, final int y) {
177177
final int xroot = find(components, x);

src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.Arrays;
44

55
/*
6-
Given an array of non-negative integers , partition the array in two subset that
7-
difference in sum of elements for both subset minimum.
6+
Given an array of non-negative integers , partition the array in two subsetRecursion that
7+
difference in sum of elements for both subsetRecursion minimum.
88
Return the minimum difference in sum of these subsets you can achieve.
99
1010
Input: array[] = {1, 6, 11, 4}
@@ -35,7 +35,7 @@ public static int minimumSumPartition(final int[] array) {
3535
boolean[] dp = new boolean[sum / 2 + 1];
3636
dp[0] = true; // Base case , don't select any element from array
3737

38-
// Find the closest sum of subset array that we can achieve which is closest to half of sum of full array
38+
// Find the closest sum of subsetRecursion array that we can achieve which is closest to half of sum of full array
3939
int closestPartitionSum = 0;
4040

4141
for (int i = 0; i < array.length; i++) {

src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Description: The partition problem is a classic problem in computer science
55
* that asks whether a given set can be partitioned into two subsets such that
6-
* the sum of elements in each subset is the same.
6+
* the sum of elements in each subsetRecursion is the same.
77
*
88
* Example:
99
* Consider nums = {1, 2, 3}
@@ -24,17 +24,17 @@ private PartitionProblem() {
2424

2525
/**
2626
* Test if a set of integers can be partitioned into two subsets such that the sum of elements
27-
* in each subset is the same.
27+
* in each subsetRecursion is the same.
2828
*
2929
* @param nums the array contains integers.
30-
* @return {@code true} if two subset exists, otherwise {@code false}.
30+
* @return {@code true} if two subsetRecursion exists, otherwise {@code false}.
3131
*/
3232
public static boolean partition(int[] nums) {
3333
// calculate the sum of all the elements in the array
3434
int sum = Arrays.stream(nums).sum();
3535

3636
// it will return true if the sum is even and the array can be divided into two
37-
// subarrays/subset with equal sum. and here i reuse the SubsetSum class from dynamic
37+
// subarrays/subsetRecursion with equal sum. and here i reuse the SubsetSum class from dynamic
3838
// programming section to check if there is exists a subsetsum into nums[] array same as the
3939
// given sum
4040
return (sum & 1) == 0 && SubsetSum.subsetSum(nums, sum / 2);

src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ private SubsetCount() {
1515
* Method to find out the number of subsets present in the given array with a sum equal to
1616
* target. Time Complexity is O(n*target) and Space Complexity is O(n*target)
1717
* @param arr is the input array on which subsets are to searched
18-
* @param target is the sum of each element of the subset taken together
18+
* @param target is the sum of each element of the subsetRecursion taken together
1919
*
2020
*/
2121
public static int getCount(int[] arr, int target) {
2222
/*
23-
* Base Cases - If target becomes zero, we have reached the required sum for the subset
23+
* Base Cases - If target becomes zero, we have reached the required sum for the subsetRecursion
2424
* If we reach the end of the array arr then, either if target==arr[end], then we add one to
2525
* the final count Otherwise we add 0 to the final count
2626
*/
@@ -50,7 +50,7 @@ public static int getCount(int[] arr, int target) {
5050
* same problem This approach is a bit better in terms of Space Used Time Complexity is
5151
* O(n*target) and Space Complexity is O(target)
5252
* @param arr is the input array on which subsets are to searched
53-
* @param target is the sum of each element of the subset taken together
53+
* @param target is the sum of each element of the subsetRecursion taken together
5454
*/
5555
public static int getCountSO(int[] arr, int target) {
5656
int n = arr.length;

src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ private SubsetSum() {
55
}
66

77
/**
8-
* Test if a set of integers contains a subset that sums to a given integer.
8+
* Test if a set of integers contains a subsetRecursion that sums to a given integer.
99
*
1010
* @param arr the array containing integers.
11-
* @param sum the target sum of the subset.
12-
* @return {@code true} if a subset exists that sums to the given value, otherwise {@code false}.
11+
* @param sum the target sum of the subsetRecursion.
12+
* @return {@code true} if a subsetRecursion exists that sums to the given value, otherwise {@code false}.
1313
*/
1414
public static boolean subsetSum(int[] arr, int sum) {
1515
int n = arr.length;
1616
boolean[][] isSum = new boolean[n + 1][sum + 1];
1717

18-
// Initialize the first column to true since a sum of 0 can always be achieved with an empty subset.
18+
// Initialize the first column to true since a sum of 0 can always be achieved with an empty subsetRecursion.
1919
for (int i = 0; i <= n; i++) {
2020
isSum[i][0] = true;
2121
}
2222

23-
// Fill the subset sum matrix
23+
// Fill the subsetRecursion sum matrix
2424
for (int i = 1; i <= n; i++) {
2525
for (int j = 1; j <= sum; j++) {
2626
if (arr[i - 1] <= j) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.Recursion;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
public final class GenerateUniqueSubsetsTest {
9+
10+
@Test
11+
void subsetRecursionTestOne() {
12+
String str = "aba";
13+
String[] expected = new String[] {"", "a", "aa", "ab", "aba", "b", "ba"};
14+
15+
List<String> ans = GenerateUniqueSubsets.subsetRecursion(str);
16+
assertArrayEquals(ans.toArray(), expected);
17+
}
18+
19+
@Test
20+
void subsetRecursionTestTwo() {
21+
String str = "abba";
22+
String[] expected = new String[] {"", "a", "aa", "ab", "aba", "abb", "abba", "b", "ba", "bb", "bba"};
23+
24+
List<String> ans = GenerateUniqueSubsets.subsetRecursion(str);
25+
assertArrayEquals(ans.toArray(), expected);
26+
}
27+
28+
@Test
29+
void subsetRecursionTestThree() {
30+
String str = "aaa";
31+
String[] expected = new String[] {"", "a", "aa", "aaa"};
32+
33+
List<String> ans = GenerateUniqueSubsets.subsetRecursion(str);
34+
assertArrayEquals(ans.toArray(), expected);
35+
}
36+
}

0 commit comments

Comments
 (0)