From 37bb532a28ee5369b18ccac413dae3660f66353a Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Oct 2024 17:11:26 +0530 Subject: [PATCH 01/28] LinkedList added --- .../LinkedList/LinkedListTraversal.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java diff --git a/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java b/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java new file mode 100644 index 000000000000..2c1dc4e538ec --- /dev/null +++ b/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java @@ -0,0 +1,55 @@ +package com.thealgorithms.LinkedList; + +public class LinkedListTraversal { + + private static class Node{ + private int data; + private Node next; + + public Node(int data){ + this.data = data; + } + + public Node(){ + } + } + + private Node head; + + public Node addElement(int val,Node node){ + + Node newNode = new Node(val); + + if (node == null){ + node = newNode; + head = node; + return head; + } + + Node temp = node; + while(temp.next != null){ + temp = temp.next; + } + + temp.next = new Node(val); + return node; + } + + public void traverse(Node node){ + while (node != null){ + System.out.println(node.data); + node = node.next; + } + } + + public static void main(String[] args) { + int[] arr = {1,7,3,4}; + LinkedListTraversal linkedList = new LinkedListTraversal(); + Node node = new Node(); + for (int i = 0; i < arr.length; i++) { + node = linkedList.addElement(arr[i],node); + } + + linkedList.traverse(node.next); + } +} From 17d0ca3cc3624a2a15d8bb5ab83f4c212b61ac4c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Oct 2024 17:45:28 +0530 Subject: [PATCH 02/28] LinkedList added --- .../thealgorithms/LinkedList/LinkedListTraversal.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java b/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java index 2c1dc4e538ec..f057a74086cf 100644 --- a/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java +++ b/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java @@ -2,9 +2,9 @@ public class LinkedListTraversal { - private static class Node{ - private int data; - private Node next; + public static class Node{ + public int data; + public Node next; public Node(int data){ this.data = data; @@ -14,7 +14,7 @@ public Node(){ } } - private Node head; + public Node head; public Node addElement(int val,Node node){ @@ -37,7 +37,7 @@ public Node addElement(int val,Node node){ public void traverse(Node node){ while (node != null){ - System.out.println(node.data); + System.out.print(node.data+"->"); node = node.next; } } From ee3cacbeb55c27a232963369d56293fff3e71362 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Oct 2024 19:50:45 +0530 Subject: [PATCH 03/28] feat: recursion subsets added --- .../LinkedList/LinkedListTraversal.java | 55 ------------------- .../com/thealgorithms/Recursion/Subsets.java | 28 ++++++++++ .../thealgorithms/Recursion/SubsetsTest.java | 28 ++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java create mode 100644 src/main/java/com/thealgorithms/Recursion/Subsets.java create mode 100644 src/test/java/com/thealgorithms/Recursion/SubsetsTest.java diff --git a/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java b/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java deleted file mode 100644 index f057a74086cf..000000000000 --- a/src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.thealgorithms.LinkedList; - -public class LinkedListTraversal { - - public static class Node{ - public int data; - public Node next; - - public Node(int data){ - this.data = data; - } - - public Node(){ - } - } - - public Node head; - - public Node addElement(int val,Node node){ - - Node newNode = new Node(val); - - if (node == null){ - node = newNode; - head = node; - return head; - } - - Node temp = node; - while(temp.next != null){ - temp = temp.next; - } - - temp.next = new Node(val); - return node; - } - - public void traverse(Node node){ - while (node != null){ - System.out.print(node.data+"->"); - node = node.next; - } - } - - public static void main(String[] args) { - int[] arr = {1,7,3,4}; - LinkedListTraversal linkedList = new LinkedListTraversal(); - Node node = new Node(); - for (int i = 0; i < arr.length; i++) { - node = linkedList.addElement(arr[i],node); - } - - linkedList.traverse(node.next); - } -} diff --git a/src/main/java/com/thealgorithms/Recursion/Subsets.java b/src/main/java/com/thealgorithms/Recursion/Subsets.java new file mode 100644 index 000000000000..fc3660b8957c --- /dev/null +++ b/src/main/java/com/thealgorithms/Recursion/Subsets.java @@ -0,0 +1,28 @@ +package com.thealgorithms.Recursion; + +// program to find power set of a string + +import java.util.ArrayList; + +public class Subsets { + public static void main(String[] args) { + String str = "abc"; + ArrayList res = new ArrayList<>(); + subset("",str,res); + System.out.println(res); + } + + public static void subset(String p,String up,ArrayList res){ + if(up.isEmpty()){ + res.add(p); + return; + } + + // Taking the character + char ch = up.charAt(0); + // Adding the character in the recursion + subset(p+ch,up.substring(1),res); + // Not adding the character in the recursion + subset(p,up.substring(1),res); + } +} diff --git a/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java new file mode 100644 index 000000000000..4aed1e9445f6 --- /dev/null +++ b/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.Recursion; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class SubsetsTest { + + @Test + void subset(){ + String str = "abc"; + ArrayList ans = new ArrayList<>(); + ArrayList actual = new ArrayList<>(); + ans.add("abc"); + ans.add("ab"); + ans.add("ac"); + ans.add("a"); + ans.add("bc"); + ans.add("b"); + ans.add("c"); + ans.add(""); + + Subsets.subset("",str,actual); + assertEquals(ans.toString(),actual.toString()); + } +} From 9e5057be8cbef970d00721d3ff6eb71d34ec34fc Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:52:49 +0530 Subject: [PATCH 04/28] Update and rename SubsetsTest.java to GenerateSubsetsTest.java --- .../Recursion/GenerateSubsetsTest.java | 36 +++++++++++++++++++ .../thealgorithms/Recursion/SubsetsTest.java | 28 --------------- 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java delete mode 100644 src/test/java/com/thealgorithms/Recursion/SubsetsTest.java diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java new file mode 100644 index 000000000000..301addd1b960 --- /dev/null +++ b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java @@ -0,0 +1,36 @@ +package com.thealgorithms.Recursion; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.List; +import org.junit.jupiter.api.Test; + +public class GenerateSubsetsTest { + + @Test + void subsetRecursionTestOne() { + String str = "abc"; + String[] expected = new String[] {"abc", "ab", "ac", "a", "bc", "b", "c", ""}; + + List ans = GenerateSubsets.subsetRecursion("", str); + assertArrayEquals(ans.toArray(), expected); + } + + @Test + void subsetRecursionTestTwo() { + String str = "cbf"; + String[] expected = new String[] {"cbf", "cb", "cf", "c", "bf", "b", "f", ""}; + + List ans = GenerateSubsets.subsetRecursion("", str); + assertArrayEquals(ans.toArray(), expected); + } + + @Test + void subsetRecursionTestThree() { + String str = "aba"; + String[] expected = new String[] {"aba", "ab", "aa", "a", "ba", "b", "a", ""}; + + List ans = GenerateSubsets.subsetRecursion("", str); + assertArrayEquals(ans.toArray(), expected); + } +} diff --git a/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java deleted file mode 100644 index 4aed1e9445f6..000000000000 --- a/src/test/java/com/thealgorithms/Recursion/SubsetsTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.thealgorithms.Recursion; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -public class SubsetsTest { - - @Test - void subset(){ - String str = "abc"; - ArrayList ans = new ArrayList<>(); - ArrayList actual = new ArrayList<>(); - ans.add("abc"); - ans.add("ab"); - ans.add("ac"); - ans.add("a"); - ans.add("bc"); - ans.add("b"); - ans.add("c"); - ans.add(""); - - Subsets.subset("",str,actual); - assertEquals(ans.toString(),actual.toString()); - } -} From e49a4142b172417c94217d63d25657c178da89e6 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:53:54 +0530 Subject: [PATCH 05/28] Update and rename Subsets.java to GenerateSubsets.java --- .../Recursion/GenerateSubsets.java | 28 +++++++++++++++++++ .../com/thealgorithms/Recursion/Subsets.java | 28 ------------------- 2 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java delete mode 100644 src/main/java/com/thealgorithms/Recursion/Subsets.java diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java new file mode 100644 index 000000000000..474daa38bef0 --- /dev/null +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -0,0 +1,28 @@ +package com.thealgorithms.Recursion; + +// program to find power set of a string + +import java.util.ArrayList; +import java.util.List; + +public class GenerateSubsets { + + public static List subsetRecursion(String p, String up) { + if (up.isEmpty()) { + List list = new ArrayList<>(); + list.add(p); + return list; + } + + // Taking the character + char ch = up.charAt(0); + // Adding the character in the recursion + List left = subsetRecursion(p + ch, up.substring(1)); + // Not adding the character in the recursion + List right = subsetRecursion(p, up.substring(1)); + + left.addAll(right); + + return left; + } +} diff --git a/src/main/java/com/thealgorithms/Recursion/Subsets.java b/src/main/java/com/thealgorithms/Recursion/Subsets.java deleted file mode 100644 index fc3660b8957c..000000000000 --- a/src/main/java/com/thealgorithms/Recursion/Subsets.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.thealgorithms.Recursion; - -// program to find power set of a string - -import java.util.ArrayList; - -public class Subsets { - public static void main(String[] args) { - String str = "abc"; - ArrayList res = new ArrayList<>(); - subset("",str,res); - System.out.println(res); - } - - public static void subset(String p,String up,ArrayList res){ - if(up.isEmpty()){ - res.add(p); - return; - } - - // Taking the character - char ch = up.charAt(0); - // Adding the character in the recursion - subset(p+ch,up.substring(1),res); - // Not adding the character in the recursion - subset(p,up.substring(1),res); - } -} From 89687c81ac90df23cb82f9d745a0f90c7f224cd6 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:06:58 +0530 Subject: [PATCH 06/28] Update GenerateSubsets.java --- src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index 474daa38bef0..b551e55588fa 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.List; -public class GenerateSubsets { +public final class GenerateSubsets { public static List subsetRecursion(String p, String up) { if (up.isEmpty()) { From eceb162b192703d3c1a99a7291b2c35c0aa2a97f Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:07:14 +0530 Subject: [PATCH 07/28] Update GenerateSubsetsTest.java --- .../java/com/thealgorithms/Recursion/GenerateSubsetsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java index 301addd1b960..337bf9cd97b1 100644 --- a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java +++ b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java @@ -5,7 +5,7 @@ import java.util.List; import org.junit.jupiter.api.Test; -public class GenerateSubsetsTest { +public final class GenerateSubsetsTest { @Test void subsetRecursionTestOne() { From 6d840a27b9451297e85f5055ad4ab46c0d2e0ed3 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:12:27 +0530 Subject: [PATCH 08/28] Update GenerateSubsets.java --- .../java/com/thealgorithms/Recursion/GenerateSubsets.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index b551e55588fa..f40a10d990c5 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -7,6 +7,10 @@ public final class GenerateSubsets { + private GenerateSubsets() { + throw new UnsupportedOperationException("Utility class"); + } + public static List subsetRecursion(String p, String up) { if (up.isEmpty()) { List list = new ArrayList<>(); From 5043123ef312baba6654e9b12eec467bb65db479 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:13:25 +0530 Subject: [PATCH 09/28] Update GenerateSubsets.java --- .../java/com/thealgorithms/Recursion/GenerateSubsets.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index f40a10d990c5..f930df791f9e 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -8,8 +8,8 @@ public final class GenerateSubsets { private GenerateSubsets() { - throw new UnsupportedOperationException("Utility class"); - } + throw new UnsupportedOperationException("Utility class"); + } public static List subsetRecursion(String p, String up) { if (up.isEmpty()) { From 61a81c98f8c225cd041f08644b735b585289791a Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:12:47 +0530 Subject: [PATCH 10/28] Update GenerateSubsets.java --- .../com/thealgorithms/Recursion/GenerateSubsets.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index f930df791f9e..c4eaeeb95b96 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -5,13 +5,17 @@ import java.util.ArrayList; import java.util.List; -public final class GenerateSubsets { +public class GenerateSubsets { private GenerateSubsets() { throw new UnsupportedOperationException("Utility class"); } - public static List subsetRecursion(String p, String up) { + public static List subsetRecursion(String str) { + return doRecursion("", str); + } + + private static List doRecursion(String p, String up) { if (up.isEmpty()) { List list = new ArrayList<>(); list.add(p); @@ -21,9 +25,9 @@ public static List subsetRecursion(String p, String up) { // Taking the character char ch = up.charAt(0); // Adding the character in the recursion - List left = subsetRecursion(p + ch, up.substring(1)); + List left = doRecursion(p + ch, up.substring(1)); // Not adding the character in the recursion - List right = subsetRecursion(p, up.substring(1)); + List right = doRecursion(p, up.substring(1)); left.addAll(right); From d3d154e9324aa04592483ddc60edc7a0f2bcfb7b Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:13:40 +0530 Subject: [PATCH 11/28] Update GenerateSubsetsTest.java --- .../com/thealgorithms/Recursion/GenerateSubsetsTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java index 337bf9cd97b1..f793b5f03325 100644 --- a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java +++ b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java @@ -5,14 +5,14 @@ import java.util.List; import org.junit.jupiter.api.Test; -public final class GenerateSubsetsTest { +public class GenerateSubsetsTest { @Test void subsetRecursionTestOne() { String str = "abc"; String[] expected = new String[] {"abc", "ab", "ac", "a", "bc", "b", "c", ""}; - List ans = GenerateSubsets.subsetRecursion("", str); + List ans = GenerateSubsets.subsetRecursion(str); assertArrayEquals(ans.toArray(), expected); } @@ -21,7 +21,7 @@ void subsetRecursionTestTwo() { String str = "cbf"; String[] expected = new String[] {"cbf", "cb", "cf", "c", "bf", "b", "f", ""}; - List ans = GenerateSubsets.subsetRecursion("", str); + List ans = GenerateSubsets.subsetRecursion(str); assertArrayEquals(ans.toArray(), expected); } @@ -30,7 +30,7 @@ void subsetRecursionTestThree() { String str = "aba"; String[] expected = new String[] {"aba", "ab", "aa", "a", "ba", "b", "a", ""}; - List ans = GenerateSubsets.subsetRecursion("", str); + List ans = GenerateSubsets.subsetRecursion(str); assertArrayEquals(ans.toArray(), expected); } } From 25874a93b1e011623e6d747b19f774825ed49186 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:15:59 +0530 Subject: [PATCH 12/28] Update GenerateSubsets.java --- src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index c4eaeeb95b96..417bf1307790 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.List; -public class GenerateSubsets { +public final class GenerateSubsets { private GenerateSubsets() { throw new UnsupportedOperationException("Utility class"); From ba81599a7abd6cb94b75392723cf66fa93517af9 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:16:16 +0530 Subject: [PATCH 13/28] Update GenerateSubsetsTest.java --- .../java/com/thealgorithms/Recursion/GenerateSubsetsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java index f793b5f03325..d4bc7e488f80 100644 --- a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java +++ b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java @@ -5,7 +5,7 @@ import java.util.List; import org.junit.jupiter.api.Test; -public class GenerateSubsetsTest { +public final class GenerateSubsetsTest { @Test void subsetRecursionTestOne() { From b3c0ee6e4de95119614c05615e6c94b58a7546ae Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 3 Oct 2024 17:31:52 +0530 Subject: [PATCH 14/28] feat: new recursion algo added --- .../Recursion/GenerateSubsets.java | 7 ++- .../Recursion/GenerateUniqueSubsets.java | 43 +++++++++++++++++++ .../thealgorithms/Recursion/TowerOfHanoi.java | 32 ++++++++++++++ .../java/com/thealgorithms/ciphers/DES.java | 4 +- .../datastructures/crdt/GSet.java | 4 +- .../datastructures/crdt/LWWElementSet.java | 4 +- .../datastructures/crdt/ORSet.java | 4 +- .../datastructures/crdt/TwoPSet.java | 2 +- .../graphs/BoruvkaAlgorithm.java | 18 ++++---- .../MinimumSumPartition.java | 6 +-- .../dynamicprogramming/PartitionProblem.java | 8 ++-- .../dynamicprogramming/SubsetCount.java | 6 +-- .../dynamicprogramming/SubsetSum.java | 10 ++--- .../Recursion/GenerateUniqueSubsetsTest.java | 36 ++++++++++++++++ .../Recursion/TowerOfHanoiTest.java | 42 ++++++++++++++++++ 15 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java create mode 100644 src/main/java/com/thealgorithms/Recursion/TowerOfHanoi.java create mode 100644 src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java create mode 100644 src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index 417bf1307790..e37bc319270d 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -5,6 +5,11 @@ import java.util.ArrayList; import java.util.List; +/** + * Finds all permutations of given array + * @author Tuhin Mondal (Git-Tuhin Mondal) + */ + public final class GenerateSubsets { private GenerateSubsets() { @@ -33,4 +38,4 @@ private static List doRecursion(String p, String up) { return left; } -} +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java new file mode 100644 index 000000000000..f9424947842e --- /dev/null +++ b/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java @@ -0,0 +1,43 @@ +package com.thealgorithms.Recursion; + +// program to find unique power set of a string + +import java.util.*; + +/** + * Finds all permutations of given array + * @author Tuhin Mondal (Git-Tuhin Mondal) + */ + +public final class GenerateUniqueSubsets { + + private GenerateUniqueSubsets() { + throw new UnsupportedOperationException("Utility class"); + } + + public static List subsetRecursion(String str) { + Set ans = doRecursion("", str); + List a = new ArrayList<>(ans.stream().toList()); + Collections.sort(a); + return a; + } + + private static Set doRecursion(String p, String up) { + if (up.isEmpty()) { + Set list = new HashSet<>(); + list.add(p); + return list; + } + + // Taking the character + char ch = up.charAt(0); + // Adding the character in the recursion + Set left = doRecursion(p + ch, up.substring(1)); + // Not adding the character in the recursion + Set right = doRecursion(p, up.substring(1)); + + left.addAll(right); + + return left; + } +} diff --git a/src/main/java/com/thealgorithms/Recursion/TowerOfHanoi.java b/src/main/java/com/thealgorithms/Recursion/TowerOfHanoi.java new file mode 100644 index 000000000000..f127ef74161d --- /dev/null +++ b/src/main/java/com/thealgorithms/Recursion/TowerOfHanoi.java @@ -0,0 +1,32 @@ +package com.thealgorithms.Recursion; + +import java.util.ArrayList; +import java.util.List; + +/** + * Finds all permutations of given array + * @author Tuhin Mondal (Git-Tuhin Mondal) + */ + +public final class TowerOfHanoi { + private TowerOfHanoi() { + throw new UnsupportedOperationException("Utility class"); + } + + public static List towerOfHanoi(int n) { + List arr = new ArrayList<>(); + recursionApproach(n, 'A', 'B', 'C', arr); + return arr; + } + + public static void recursionApproach(int n, char a, char b, char c, List list) { + if (n == 1) { + list.add("Take disk 1 from rod " + a + " to rod " + b); + return; + } + + recursionApproach(n - 1, a, c, b, list); + list.add("Take disk " + n + " from rod " + a + " to rod " + b); + recursionApproach(n - 1, c, b, a, list); + } +} diff --git a/src/main/java/com/thealgorithms/ciphers/DES.java b/src/main/java/com/thealgorithms/ciphers/DES.java index 7f3eed70f3c2..6e7064920bcc 100644 --- a/src/main/java/com/thealgorithms/ciphers/DES.java +++ b/src/main/java/com/thealgorithms/ciphers/DES.java @@ -32,7 +32,7 @@ public void setKey(String key) { this.key = key; } - // Permutation table to convert initial 64-bit key to 56 bit key + // GeneratePermutations table to convert initial 64-bit key to 56 bit key 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}; // Lookup table used to shift the initial key, in order to generate the subkeys @@ -66,7 +66,7 @@ public void setKey(String key) { private static final int[][][] S = {S1, S2, S3, S4, S5, S6, S7, S8}; - // Permutation table, used in the Feistel function post s-box usage + // GeneratePermutations table, used in the Feistel function post s-box usage 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}; // Table used for final inversion of the message box after 16 rounds of Feistel Function diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java index 2b8959ed0136..36ba84636209 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java @@ -44,10 +44,10 @@ public boolean lookup(T e) { } /** - * Compares the G-Set with another G-Set to check if it is a subset. + * Compares the G-Set with another G-Set to check if it is a subsetRecursion. * * @param other the other G-Set to compare with - * @return true if the current G-Set is a subset of the other, false otherwise + * @return true if the current G-Set is a subsetRecursion of the other, false otherwise */ public boolean compare(GSet other) { return other.elements.containsAll(elements); diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java index 2c6ce8a427d1..289e95c38769 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java @@ -88,10 +88,10 @@ public boolean lookup(Element e) { } /** - * Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subset. + * Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subsetRecursion. * * @param other The LWWElementSet to compare. - * @return True if the set is subset, false otherwise. + * @return True if the set is subsetRecursion, false otherwise. */ public boolean compare(LWWElementSet other) { return other.addSet.keySet().containsAll(addSet.keySet()) && other.removeSet.keySet().containsAll(removeSet.keySet()); diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java index a4cc2ffdd4a6..894be3df4146 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java @@ -132,10 +132,10 @@ private String generateUniqueTag() { } /** - * Compares this Add-Wins OR-Set with another OR-Set to check if elements and tombstones are a subset. + * Compares this Add-Wins OR-Set with another OR-Set to check if elements and tombstones are a subsetRecursion. * * @param other the other OR-Set to compare - * @return true if the sets are subset, false otherwise + * @return true if the sets are subsetRecursion, false otherwise */ public boolean compare(ORSet other) { Set> union = new HashSet<>(elements); diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java index c0ce17b2802b..b434f2686a05 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java @@ -61,7 +61,7 @@ public void remove(T element) { * Compares the current 2P-Set with another 2P-Set. * * @param otherSet The other 2P-Set to compare with. - * @return True if both SetA and SetR are subset, otherwise false. + * @return True if both SetA and SetR are subsetRecursion, otherwise false. */ public boolean compare(TwoPSet otherSet) { return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR); diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java index dcdb08ad133e..d47d7c7c5793 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java @@ -60,7 +60,7 @@ static class Graph { } /** - * Represents a subset for Union-Find operations + * Represents a subsetRecursion for Union-Find operations */ private static class Component { int parent; @@ -89,7 +89,7 @@ private static class BoruvkaState { /** * Adds the cheapest edges to the result list and performs Union operation on the subsets. * - * @param cheapest Array containing the cheapest edge for each subset. + * @param cheapest Array containing the cheapest edge for each subsetRecursion. */ void merge(final Edge[] cheapest) { for (int i = 0; i < graph.vertex; ++i) { @@ -115,9 +115,9 @@ boolean hasMoreEdgesToAdd() { } /** - * Computes the cheapest edges for each subset in the Union-Find structure. + * Computes the cheapest edges for each subsetRecursion in the Union-Find structure. * - * @return an array containing the cheapest edge for each subset. + * @return an array containing the cheapest edge for each subsetRecursion. */ private Edge[] computeCheapestEdges() { Edge[] cheapest = new Edge[graph.vertex]; @@ -153,11 +153,11 @@ private static Component[] initializeComponents(final Graph graph) { } /** - * Finds the parent of the subset using path compression + * Finds the parent of the subsetRecursion using path compression * * @param components array of subsets - * @param i index of the subset - * @return the parent of the subset + * @param i index of the subsetRecursion + * @return the parent of the subsetRecursion */ static int find(final Component[] components, final int i) { if (components[i].parent != i) { @@ -170,8 +170,8 @@ static int find(final Component[] components, final int i) { * Performs the Union operation for Union-Find * * @param components array of subsets - * @param x index of the first subset - * @param y index of the second subset + * @param x index of the first subsetRecursion + * @param y index of the second subsetRecursion */ static void union(Component[] components, final int x, final int y) { final int xroot = find(components, x); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java b/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java index 52308c23cf1c..7e910a40f229 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java @@ -3,8 +3,8 @@ import java.util.Arrays; /* -Given an array of non-negative integers , partition the array in two subset that -difference in sum of elements for both subset minimum. +Given an array of non-negative integers , partition the array in two subsetRecursion that +difference in sum of elements for both subsetRecursion minimum. Return the minimum difference in sum of these subsets you can achieve. Input: array[] = {1, 6, 11, 4} @@ -35,7 +35,7 @@ public static int minimumSumPartition(final int[] array) { boolean[] dp = new boolean[sum / 2 + 1]; dp[0] = true; // Base case , don't select any element from array - // Find the closest sum of subset array that we can achieve which is closest to half of sum of full array + // Find the closest sum of subsetRecursion array that we can achieve which is closest to half of sum of full array int closestPartitionSum = 0; for (int i = 0; i < array.length; i++) { diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java b/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java index 49c4a0a3a008..8613e5ddbdbe 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java @@ -3,7 +3,7 @@ * * Description: The partition problem is a classic problem in computer science * that asks whether a given set can be partitioned into two subsets such that - * the sum of elements in each subset is the same. + * the sum of elements in each subsetRecursion is the same. * * Example: * Consider nums = {1, 2, 3} @@ -24,17 +24,17 @@ private PartitionProblem() { /** * Test if a set of integers can be partitioned into two subsets such that the sum of elements - * in each subset is the same. + * in each subsetRecursion is the same. * * @param nums the array contains integers. - * @return {@code true} if two subset exists, otherwise {@code false}. + * @return {@code true} if two subsetRecursion exists, otherwise {@code false}. */ public static boolean partition(int[] nums) { // calculate the sum of all the elements in the array int sum = Arrays.stream(nums).sum(); // it will return true if the sum is even and the array can be divided into two - // subarrays/subset with equal sum. and here i reuse the SubsetSum class from dynamic + // subarrays/subsetRecursion with equal sum. and here i reuse the SubsetSum class from dynamic // programming section to check if there is exists a subsetsum into nums[] array same as the // given sum return (sum & 1) == 0 && SubsetSum.subsetSum(nums, sum / 2); diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java index 0c5bc2c5884d..91a8b19c752d 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java @@ -15,12 +15,12 @@ private SubsetCount() { * Method to find out the number of subsets present in the given array with a sum equal to * target. Time Complexity is O(n*target) and Space Complexity is O(n*target) * @param arr is the input array on which subsets are to searched - * @param target is the sum of each element of the subset taken together + * @param target is the sum of each element of the subsetRecursion taken together * */ public static int getCount(int[] arr, int target) { /* - * Base Cases - If target becomes zero, we have reached the required sum for the subset + * Base Cases - If target becomes zero, we have reached the required sum for the subsetRecursion * If we reach the end of the array arr then, either if target==arr[end], then we add one to * the final count Otherwise we add 0 to the final count */ @@ -50,7 +50,7 @@ public static int getCount(int[] arr, int target) { * same problem This approach is a bit better in terms of Space Used Time Complexity is * O(n*target) and Space Complexity is O(target) * @param arr is the input array on which subsets are to searched - * @param target is the sum of each element of the subset taken together + * @param target is the sum of each element of the subsetRecursion taken together */ public static int getCountSO(int[] arr, int target) { int n = arr.length; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java index 3dd41d2fdc0f..e8c6f97006bb 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java @@ -5,22 +5,22 @@ private SubsetSum() { } /** - * Test if a set of integers contains a subset that sums to a given integer. + * Test if a set of integers contains a subsetRecursion that sums to a given integer. * * @param arr the array containing integers. - * @param sum the target sum of the subset. - * @return {@code true} if a subset exists that sums to the given value, otherwise {@code false}. + * @param sum the target sum of the subsetRecursion. + * @return {@code true} if a subsetRecursion exists that sums to the given value, otherwise {@code false}. */ public static boolean subsetSum(int[] arr, int sum) { int n = arr.length; boolean[][] isSum = new boolean[n + 1][sum + 1]; - // Initialize the first column to true since a sum of 0 can always be achieved with an empty subset. + // Initialize the first column to true since a sum of 0 can always be achieved with an empty subsetRecursion. for (int i = 0; i <= n; i++) { isSum[i][0] = true; } - // Fill the subset sum matrix + // Fill the subsetRecursion sum matrix for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum; j++) { if (arr[i - 1] <= j) { diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java new file mode 100644 index 000000000000..af1a58cd9aaa --- /dev/null +++ b/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java @@ -0,0 +1,36 @@ +package com.thealgorithms.Recursion; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.List; +import org.junit.jupiter.api.Test; + +public final class GenerateUniqueSubsetsTest { + + @Test + void subsetRecursionTestOne() { + String str = "aba"; + String[] expected = new String[] {"", "a", "aa", "ab", "aba", "b", "ba"}; + + List ans = GenerateUniqueSubsets.subsetRecursion(str); + assertArrayEquals(ans.toArray(), expected); + } + + @Test + void subsetRecursionTestTwo() { + String str = "abba"; + String[] expected = new String[] {"", "a", "aa", "ab", "aba", "abb", "abba", "b", "ba", "bb", "bba"}; + + List ans = GenerateUniqueSubsets.subsetRecursion(str); + assertArrayEquals(ans.toArray(), expected); + } + + @Test + void subsetRecursionTestThree() { + String str = "aaa"; + String[] expected = new String[] {"", "a", "aa", "aaa"}; + + List ans = GenerateUniqueSubsets.subsetRecursion(str); + assertArrayEquals(ans.toArray(), expected); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java new file mode 100644 index 000000000000..c0ad5743b8c3 --- /dev/null +++ b/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java @@ -0,0 +1,42 @@ +package com.thealgorithms.Recursion; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.List; +import org.junit.jupiter.api.Test; + +public final class TowerOfHanoiTest { + + @Test + void hanoiTowerTestOne() { + + int n = 5; + String[] expected = {"Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B", + "Take disk 4 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", "Take disk 3 from rod B to rod C", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", + "Take disk 5 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 3 from rod C to rod A", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", + "Take disk 4 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B"}; + + List actual = TowerOfHanoi.towerOfHanoi(n); + assertArrayEquals(expected, actual.toArray()); + } + + @Test + void hanoiTowerTestTwo() { + + int n = 3; + String[] expected = {"Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B"}; + + List actual = TowerOfHanoi.towerOfHanoi(n); + assertArrayEquals(expected, actual.toArray()); + } + + @Test + void hanoiTowerTestThree() { + + int n = 1; + String[] expected = {"Take disk 1 from rod A to rod B"}; + + List actual = TowerOfHanoi.towerOfHanoi(n); + assertArrayEquals(expected, actual.toArray()); + } +} From 316ae837e18dd3dd2b583d985fd0e75f4ae77c17 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:44:14 +0530 Subject: [PATCH 15/28] Update GenerateSubsets.java --- src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java index e37bc319270d..5eefe3eee671 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java @@ -38,4 +38,4 @@ private static List doRecursion(String p, String up) { return left; } -} \ No newline at end of file +} From 372269044264b86ecd30da3d24fa139885e9ed5f Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:45:55 +0530 Subject: [PATCH 16/28] Update GenerateUniqueSubsetsTest.java --- .../com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java index af1a58cd9aaa..ceaa5081f0c5 100644 --- a/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java +++ b/src/test/java/com/thealgorithms/Recursion/GenerateUniqueSubsetsTest.java @@ -33,4 +33,4 @@ void subsetRecursionTestThree() { List ans = GenerateUniqueSubsets.subsetRecursion(str); assertArrayEquals(ans.toArray(), expected); } -} \ No newline at end of file +} From a3e34f79c6bbdfeb0b521d0d7ba752e95b54961e Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:46:41 +0530 Subject: [PATCH 17/28] Update TowerOfHanoiTest.java --- .../java/com/thealgorithms/Recursion/TowerOfHanoiTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java b/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java index c0ad5743b8c3..404c195ae400 100644 --- a/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java +++ b/src/test/java/com/thealgorithms/Recursion/TowerOfHanoiTest.java @@ -12,9 +12,9 @@ void hanoiTowerTestOne() { int n = 5; String[] expected = {"Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B", - "Take disk 4 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", "Take disk 3 from rod B to rod C", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", - "Take disk 5 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 3 from rod C to rod A", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", - "Take disk 4 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B"}; + "Take disk 4 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", "Take disk 3 from rod B to rod C", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", + "Take disk 5 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 3 from rod C to rod A", "Take disk 1 from rod B to rod C", "Take disk 2 from rod B to rod A", "Take disk 1 from rod C to rod A", + "Take disk 4 from rod C to rod B", "Take disk 1 from rod A to rod B", "Take disk 2 from rod A to rod C", "Take disk 1 from rod B to rod C", "Take disk 3 from rod A to rod B", "Take disk 1 from rod C to rod A", "Take disk 2 from rod C to rod B", "Take disk 1 from rod A to rod B"}; List actual = TowerOfHanoi.towerOfHanoi(n); assertArrayEquals(expected, actual.toArray()); From 283f35bea3baa0e4b574906c7733d45f216fcbcb Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:49:15 +0530 Subject: [PATCH 18/28] Update GenerateUniqueSubsets.java --- .../com/thealgorithms/Recursion/GenerateUniqueSubsets.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java index f9424947842e..9c8cffa59df8 100644 --- a/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java +++ b/src/main/java/com/thealgorithms/Recursion/GenerateUniqueSubsets.java @@ -2,7 +2,11 @@ // program to find unique power set of a string -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * Finds all permutations of given array From bebf28d0b3f993a0a58d1904c13f35da1600a208 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:38:06 +0530 Subject: [PATCH 19/28] Delete src/main/java/com/thealgorithms/ciphers/DES.java --- .../java/com/thealgorithms/ciphers/DES.java | 250 ------------------ 1 file changed, 250 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/ciphers/DES.java diff --git a/src/main/java/com/thealgorithms/ciphers/DES.java b/src/main/java/com/thealgorithms/ciphers/DES.java deleted file mode 100644 index 6e7064920bcc..000000000000 --- a/src/main/java/com/thealgorithms/ciphers/DES.java +++ /dev/null @@ -1,250 +0,0 @@ -package com.thealgorithms.ciphers; - -/** - * This class is build to demonstrate the application of the DES-algorithm - * (https://en.wikipedia.org/wiki/Data_Encryption_Standard) on a plain English message. The supplied - * key must be in form of a 64 bit binary String. - */ -public class DES { - - private String key; - private final String[] subKeys; - - private void sanitize(String key) { - int length = key.length(); - if (length != 64) { - throw new IllegalArgumentException("DES key must be supplied as a 64 character binary string"); - } - } - - DES(String key) { - sanitize(key); - this.key = key; - subKeys = getSubkeys(key); - } - - public String getKey() { - return this.key; - } - - public void setKey(String key) { - sanitize(key); - this.key = key; - } - - // GeneratePermutations table to convert initial 64-bit key to 56 bit key - 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}; - - // Lookup table used to shift the initial key, in order to generate the subkeys - private static final int[] KEY_SHIFTS = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}; - - // Table to convert the 56 bit subkeys to 48 bit subkeys - private static final int[] PC2 = {14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32}; - - // Initial permutation of each 64 but message block - private static final int[] IP = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7}; - - // Expansion table to convert right half of message blocks from 32 bits to 48 bits - private static final int[] EXPANSION = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1}; - - // The eight substitution boxes are defined below - private static final int[][] S1 = {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7}, {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8}, {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0}, {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}}; - - private static final int[][] S2 = {{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10}, {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5}, {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15}, {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}}; - - private static final int[][] S3 = {{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8}, {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1}, {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7}, {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}}; - - private static final int[][] S4 = {{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15}, {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9}, {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4}, {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}}; - - private static final int[][] S5 = {{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9}, {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6}, {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14}, {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}}; - - private static final int[][] S6 = {{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11}, {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8}, {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6}, {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}}; - - private static final int[][] S7 = {{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1}, {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6}, {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2}, {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}}; - - private static final int[][] S8 = {{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7}, {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2}, {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8}, {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}; - - private static final int[][][] S = {S1, S2, S3, S4, S5, S6, S7, S8}; - - // GeneratePermutations table, used in the Feistel function post s-box usage - 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}; - - // Table used for final inversion of the message box after 16 rounds of Feistel Function - static final int[] IP_INVERSE = {40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25}; - - private String[] getSubkeys(String originalKey) { - StringBuilder permutedKey = new StringBuilder(); // Initial permutation of keys via pc1 - int i; - int j; - for (i = 0; i < 56; i++) { - permutedKey.append(originalKey.charAt(PC1[i] - 1)); - } - String[] subKeys = new String[16]; - String initialPermutedKey = permutedKey.toString(); - String c0 = initialPermutedKey.substring(0, 28); - String d0 = initialPermutedKey.substring(28); - - // We will now operate on the left and right halves of the permutedKey - for (i = 0; i < 16; i++) { - String cN = c0.substring(KEY_SHIFTS[i]) + c0.substring(0, KEY_SHIFTS[i]); - String dN = d0.substring(KEY_SHIFTS[i]) + d0.substring(0, KEY_SHIFTS[i]); - subKeys[i] = cN + dN; - c0 = cN; // Re-assign the values to create running permutation - d0 = dN; - } - - // Let us shrink the keys to 48 bits (well, characters here) using pc2 - for (i = 0; i < 16; i++) { - String key = subKeys[i]; - permutedKey.setLength(0); - for (j = 0; j < 48; j++) { - permutedKey.append(key.charAt(PC2[j] - 1)); - } - subKeys[i] = permutedKey.toString(); - } - - return subKeys; - } - - private String xOR(String a, String b) { - int i; - int l = a.length(); - StringBuilder xor = new StringBuilder(); - for (i = 0; i < l; i++) { - int firstBit = a.charAt(i) - 48; // 48 is '0' in ascii - int secondBit = b.charAt(i) - 48; - xor.append((firstBit ^ secondBit)); - } - return xor.toString(); - } - - private String createPaddedString(String s, int desiredLength, char pad) { - int i; - int l = s.length(); - StringBuilder paddedString = new StringBuilder(); - int diff = desiredLength - l; - for (i = 0; i < diff; i++) { - paddedString.append(pad); - } - return paddedString.toString(); - } - - private String pad(String s, int desiredLength) { - return createPaddedString(s, desiredLength, '0') + s; - } - - private String padLast(String s, int desiredLength) { - return s + createPaddedString(s, desiredLength, '\u0000'); - } - - private String feistel(String messageBlock, String key) { - int i; - StringBuilder expandedKey = new StringBuilder(); - for (i = 0; i < 48; i++) { - expandedKey.append(messageBlock.charAt(EXPANSION[i] - 1)); - } - String mixedKey = xOR(expandedKey.toString(), key); - StringBuilder substitutedString = new StringBuilder(); - - // Let us now use the s-boxes to transform each 6 bit (length here) block to 4 bits - for (i = 0; i < 48; i += 6) { - String block = mixedKey.substring(i, i + 6); - int row = (block.charAt(0) - 48) * 2 + (block.charAt(5) - 48); - int col = (block.charAt(1) - 48) * 8 + (block.charAt(2) - 48) * 4 + (block.charAt(3) - 48) * 2 + (block.charAt(4) - 48); - String substitutedBlock = pad(Integer.toBinaryString(S[i / 6][row][col]), 4); - substitutedString.append(substitutedBlock); - } - - StringBuilder permutedString = new StringBuilder(); - for (i = 0; i < 32; i++) { - permutedString.append(substitutedString.charAt(PERMUTATION[i] - 1)); - } - - return permutedString.toString(); - } - - private String encryptBlock(String message, String[] keys) { - StringBuilder permutedMessage = new StringBuilder(); - int i; - for (i = 0; i < 64; i++) { - permutedMessage.append(message.charAt(IP[i] - 1)); - } - String e0 = permutedMessage.substring(0, 32); - String f0 = permutedMessage.substring(32); - - // Iterate 16 times - for (i = 0; i < 16; i++) { - String eN = f0; // Previous Right block - String fN = xOR(e0, feistel(f0, keys[i])); - e0 = eN; - f0 = fN; - } - - String combinedBlock = f0 + e0; // Reverse the 16th block - permutedMessage.setLength(0); - for (i = 0; i < 64; i++) { - permutedMessage.append(combinedBlock.charAt(IP_INVERSE[i] - 1)); - } - return permutedMessage.toString(); - } - - // To decode, we follow the same process as encoding, but with reversed keys - private String decryptBlock(String message, String[] keys) { - String[] reversedKeys = new String[keys.length]; - for (int i = 0; i < keys.length; i++) { - reversedKeys[i] = keys[keys.length - i - 1]; - } - return encryptBlock(message, reversedKeys); - } - - /** - * @param message Message to be encrypted - * @return The encrypted message, as a binary string - */ - public String encrypt(String message) { - StringBuilder encryptedMessage = new StringBuilder(); - int l = message.length(); - int i; - int j; - if (l % 8 != 0) { - int desiredLength = (l / 8 + 1) * 8; - l = desiredLength; - message = padLast(message, desiredLength); - } - - for (i = 0; i < l; i += 8) { - String block = message.substring(i, i + 8); - StringBuilder bitBlock = new StringBuilder(); - byte[] bytes = block.getBytes(); - for (j = 0; j < 8; j++) { - bitBlock.append(pad(Integer.toBinaryString(bytes[j]), 8)); - } - encryptedMessage.append(encryptBlock(bitBlock.toString(), subKeys)); - } - return encryptedMessage.toString(); - } - - /** - * @param message The encrypted string. Expects it to be a multiple of 64 bits, in binary format - * @return The decrypted String, in plain English - */ - public String decrypt(String message) { - StringBuilder decryptedMessage = new StringBuilder(); - int l = message.length(); - int i; - int j; - if (l % 64 != 0) { - throw new IllegalArgumentException("Encrypted message should be a multiple of 64 characters in length"); - } - for (i = 0; i < l; i += 64) { - String block = message.substring(i, i + 64); - String result = decryptBlock(block, subKeys); - byte[] res = new byte[8]; - for (j = 0; j < 64; j += 8) { - res[j / 8] = (byte) Integer.parseInt(result.substring(j, j + 8), 2); - } - decryptedMessage.append(new String(res)); - } - return decryptedMessage.toString().replace("\0", ""); // Get rid of the null bytes used for padding - } -} From 99a2e407b31fbb8d509da9ac18eba37ad7649435 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:38:16 +0530 Subject: [PATCH 20/28] Delete src/main/java/com/thealgorithms/datastructures/crdt/GSet.java --- .../datastructures/crdt/GSet.java | 65 ------------------- 1 file changed, 65 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/crdt/GSet.java diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java deleted file mode 100644 index 36ba84636209..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.thealgorithms.datastructures.crdt; - -import java.util.HashSet; -import java.util.Set; - -/** - * GSet (Grow-only Set) is a state-based CRDT (Conflict-free Replicated Data Type) - * that allows only the addition of elements and ensures that once an element is added, - * it cannot be removed. The merge operation of two G-Sets is their union. - * This implementation supports adding elements, looking up elements, comparing with other G-Sets, - * and merging with another G-Set to create a new G-Set containing all unique elements from both sets. - * (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) - * - * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) - */ - -public class GSet { - private final Set elements; - - /** - * Constructs an empty G-Set. - */ - public GSet() { - this.elements = new HashSet<>(); - } - - /** - * Adds an element to the G-Set. - * - * @param e the element to be added - */ - public void addElement(T e) { - elements.add(e); - } - - /** - * Checks if the given element is present in the G-Set. - * - * @param e the element to be checked - * @return true if the element is present, false otherwise - */ - public boolean lookup(T e) { - return elements.contains(e); - } - - /** - * Compares the G-Set with another G-Set to check if it is a subsetRecursion. - * - * @param other the other G-Set to compare with - * @return true if the current G-Set is a subsetRecursion of the other, false otherwise - */ - public boolean compare(GSet other) { - return other.elements.containsAll(elements); - } - - /** - * Merges the current G-Set with another G-Set, creating a new G-Set - * containing all unique elements from both sets. - * - * @param other the G-Set to merge with - */ - public void merge(GSet other) { - elements.addAll(other.elements); - } -} From 647845251cee946b1eac181319b778eb24e67572 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:38:34 +0530 Subject: [PATCH 21/28] Delete src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java --- .../dynamicprogramming/SubsetSum.java | 36 ------------------- 1 file changed, 36 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java deleted file mode 100644 index e8c6f97006bb..000000000000 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.thealgorithms.dynamicprogramming; - -public final class SubsetSum { - private SubsetSum() { - } - - /** - * Test if a set of integers contains a subsetRecursion that sums to a given integer. - * - * @param arr the array containing integers. - * @param sum the target sum of the subsetRecursion. - * @return {@code true} if a subsetRecursion exists that sums to the given value, otherwise {@code false}. - */ - public static boolean subsetSum(int[] arr, int sum) { - int n = arr.length; - boolean[][] isSum = new boolean[n + 1][sum + 1]; - - // Initialize the first column to true since a sum of 0 can always be achieved with an empty subsetRecursion. - for (int i = 0; i <= n; i++) { - isSum[i][0] = true; - } - - // Fill the subsetRecursion sum matrix - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= sum; j++) { - if (arr[i - 1] <= j) { - isSum[i][j] = isSum[i - 1][j] || isSum[i - 1][j - arr[i - 1]]; - } else { - isSum[i][j] = isSum[i - 1][j]; - } - } - } - - return isSum[n][sum]; - } -} From 127703ef402031fd8b2b296491e82b63c9819d02 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:38:56 +0530 Subject: [PATCH 22/28] Delete src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java --- .../MinimumSumPartition.java | 57 ------------------- 1 file changed, 57 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java b/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java deleted file mode 100644 index 7e910a40f229..000000000000 --- a/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.thealgorithms.dynamicprogramming; - -import java.util.Arrays; - -/* -Given an array of non-negative integers , partition the array in two subsetRecursion that -difference in sum of elements for both subsetRecursion minimum. -Return the minimum difference in sum of these subsets you can achieve. - -Input: array[] = {1, 6, 11, 4} -Output: 0 -Explanation: -Subset1 = {1, 4, 6}, sum of Subset1 = 11 -Subset2 = {11}, sum of Subset2 = 11 - -Input: array[] = {36, 7, 46, 40} -Output: 23 -Explanation: -Subset1 = {7, 46} ; sum of Subset1 = 53 -Subset2 = {36, 40} ; sum of Subset2 = 76 - */ -public final class MinimumSumPartition { - private MinimumSumPartition() { - } - - private static void throwIfInvalidInput(final int[] array) { - if (Arrays.stream(array).anyMatch(a -> a < 0)) { - throw new IllegalArgumentException("Input array should not contain negative number(s)."); - } - } - - public static int minimumSumPartition(final int[] array) { - throwIfInvalidInput(array); - int sum = Arrays.stream(array).sum(); - boolean[] dp = new boolean[sum / 2 + 1]; - dp[0] = true; // Base case , don't select any element from array - - // Find the closest sum of subsetRecursion array that we can achieve which is closest to half of sum of full array - int closestPartitionSum = 0; - - for (int i = 0; i < array.length; i++) { - for (int j = sum / 2; j > 0; j--) { - if (array[i] <= j) { - dp[j] = dp[j] || dp[j - array[i]]; - } - if (dp[j]) { - closestPartitionSum = Math.max(closestPartitionSum, j); - } - } - } - /* - Difference in sum = Big partition sum - Small partition sum - = ( Total sum - Small partition sum) - Small partition sum - */ - return sum - (2 * closestPartitionSum); - } -} From 33e3018cc83605351cf46d3f76d5ad255e966629 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:39:16 +0530 Subject: [PATCH 23/28] Delete src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java --- .../dynamicprogramming/PartitionProblem.java | 42 ------------------- 1 file changed, 42 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java b/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java deleted file mode 100644 index 8613e5ddbdbe..000000000000 --- a/src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @author Md Asif Joardar - * - * Description: The partition problem is a classic problem in computer science - * that asks whether a given set can be partitioned into two subsets such that - * the sum of elements in each subsetRecursion is the same. - * - * Example: - * Consider nums = {1, 2, 3} - * We can split the array "nums" into two partitions, where each having a sum of 3. - * nums1 = {1, 2} - * nums2 = {3} - * - * The time complexity of the solution is O(n × sum) and requires O(n × sum) space - */ - -package com.thealgorithms.dynamicprogramming; - -import java.util.Arrays; - -public final class PartitionProblem { - private PartitionProblem() { - } - - /** - * Test if a set of integers can be partitioned into two subsets such that the sum of elements - * in each subsetRecursion is the same. - * - * @param nums the array contains integers. - * @return {@code true} if two subsetRecursion exists, otherwise {@code false}. - */ - public static boolean partition(int[] nums) { - // calculate the sum of all the elements in the array - int sum = Arrays.stream(nums).sum(); - - // it will return true if the sum is even and the array can be divided into two - // subarrays/subsetRecursion with equal sum. and here i reuse the SubsetSum class from dynamic - // programming section to check if there is exists a subsetsum into nums[] array same as the - // given sum - return (sum & 1) == 0 && SubsetSum.subsetSum(nums, sum / 2); - } -} From dab10c607a27dc567ad1bdea83a4e67251398b23 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:39:31 +0530 Subject: [PATCH 24/28] Delete src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java --- .../dynamicprogramming/SubsetCount.java | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java deleted file mode 100644 index 91a8b19c752d..000000000000 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.thealgorithms.dynamicprogramming; - -/** - * Find the number of subsets present in the given array with a sum equal to target. - * Based on Solution discussed on - * StackOverflow - * @author Samrat Podder - */ -public final class SubsetCount { - private SubsetCount() { - } - - /** - * Dynamic Programming Implementation. - * Method to find out the number of subsets present in the given array with a sum equal to - * target. Time Complexity is O(n*target) and Space Complexity is O(n*target) - * @param arr is the input array on which subsets are to searched - * @param target is the sum of each element of the subsetRecursion taken together - * - */ - public static int getCount(int[] arr, int target) { - /* - * Base Cases - If target becomes zero, we have reached the required sum for the subsetRecursion - * If we reach the end of the array arr then, either if target==arr[end], then we add one to - * the final count Otherwise we add 0 to the final count - */ - int n = arr.length; - int[][] dp = new int[n][target + 1]; - for (int i = 0; i < n; i++) { - dp[i][0] = 1; - } - if (arr[0] <= target) { - dp[0][arr[0]] = 1; - } - for (int t = 1; t <= target; t++) { - for (int idx = 1; idx < n; idx++) { - int notpick = dp[idx - 1][t]; - int pick = 0; - if (arr[idx] <= t) { - pick += dp[idx - 1][target - t]; - } - dp[idx][target] = pick + notpick; - } - } - return dp[n - 1][target]; - } - - /** - * This Method is a Space Optimized version of the getCount(int[], int) method and solves the - * same problem This approach is a bit better in terms of Space Used Time Complexity is - * O(n*target) and Space Complexity is O(target) - * @param arr is the input array on which subsets are to searched - * @param target is the sum of each element of the subsetRecursion taken together - */ - public static int getCountSO(int[] arr, int target) { - int n = arr.length; - int[] prev = new int[target + 1]; - prev[0] = 1; - if (arr[0] <= target) { - prev[arr[0]] = 1; - } - for (int ind = 1; ind < n; ind++) { - int[] cur = new int[target + 1]; - cur[0] = 1; - for (int t = 1; t <= target; t++) { - int notTaken = prev[t]; - int taken = 0; - if (arr[ind] <= t) { - taken = prev[t - arr[ind]]; - } - cur[t] = notTaken + taken; - } - prev = cur; - } - return prev[target]; - } -} From 063b4728b5204be85f67d069c03e09309f7a607f Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:39:54 +0530 Subject: [PATCH 25/28] Delete src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java --- .../graphs/BoruvkaAlgorithm.java | 217 ------------------ 1 file changed, 217 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java deleted file mode 100644 index d47d7c7c5793..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java +++ /dev/null @@ -1,217 +0,0 @@ -package com.thealgorithms.datastructures.graphs; - -import java.util.ArrayList; -import java.util.List; - -/** - * Boruvka's algorithm to find Minimum Spanning Tree - * (https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm) - * - * @author itakurah (https://github.com/itakurah) - */ - -final class BoruvkaAlgorithm { - private BoruvkaAlgorithm() { - } - - /** - * Represents an edge in the graph - */ - static class Edge { - final int src; - final int dest; - final int weight; - - Edge(final int src, final int dest, final int weight) { - this.src = src; - this.dest = dest; - this.weight = weight; - } - } - - /** - * Represents the graph - */ - static class Graph { - final int vertex; - final List edges; - - /** - * Constructor for the graph - * - * @param vertex number of vertices - * @param edges list of edges - */ - Graph(final int vertex, final List edges) { - if (vertex < 0) { - throw new IllegalArgumentException("Number of vertices must be positive"); - } - if (edges == null || edges.isEmpty()) { - throw new IllegalArgumentException("Edges list must not be null or empty"); - } - for (final var edge : edges) { - checkEdgeVertices(edge.src, vertex); - checkEdgeVertices(edge.dest, vertex); - } - - this.vertex = vertex; - this.edges = edges; - } - } - - /** - * Represents a subsetRecursion for Union-Find operations - */ - private static class Component { - int parent; - int rank; - - Component(final int parent, final int rank) { - this.parent = parent; - this.rank = rank; - } - } - - /** - * Represents the state of Union-Find components and the result list - */ - private static class BoruvkaState { - List result; - Component[] components; - final Graph graph; - - BoruvkaState(final Graph graph) { - this.result = new ArrayList<>(); - this.components = initializeComponents(graph); - this.graph = graph; - } - - /** - * Adds the cheapest edges to the result list and performs Union operation on the subsets. - * - * @param cheapest Array containing the cheapest edge for each subsetRecursion. - */ - void merge(final Edge[] cheapest) { - for (int i = 0; i < graph.vertex; ++i) { - if (cheapest[i] != null) { - final var component1 = find(components, cheapest[i].src); - final var component2 = find(components, cheapest[i].dest); - - if (component1 != component2) { - result.add(cheapest[i]); - union(components, component1, component2); - } - } - } - } - - /** - * Checks if there are more edges to add to the result list - * - * @return true if there are more edges to add, false otherwise - */ - boolean hasMoreEdgesToAdd() { - return result.size() < graph.vertex - 1; - } - - /** - * Computes the cheapest edges for each subsetRecursion in the Union-Find structure. - * - * @return an array containing the cheapest edge for each subsetRecursion. - */ - private Edge[] computeCheapestEdges() { - Edge[] cheapest = new Edge[graph.vertex]; - for (final var edge : graph.edges) { - final var set1 = find(components, edge.src); - final var set2 = find(components, edge.dest); - - if (set1 != set2) { - if (cheapest[set1] == null || edge.weight < cheapest[set1].weight) { - cheapest[set1] = edge; - } - if (cheapest[set2] == null || edge.weight < cheapest[set2].weight) { - cheapest[set2] = edge; - } - } - } - return cheapest; - } - - /** - * Initializes subsets for Union-Find - * - * @param graph the graph - * @return the initialized subsets - */ - private static Component[] initializeComponents(final Graph graph) { - Component[] components = new Component[graph.vertex]; - for (int v = 0; v < graph.vertex; ++v) { - components[v] = new Component(v, 0); - } - return components; - } - } - - /** - * Finds the parent of the subsetRecursion using path compression - * - * @param components array of subsets - * @param i index of the subsetRecursion - * @return the parent of the subsetRecursion - */ - static int find(final Component[] components, final int i) { - if (components[i].parent != i) { - components[i].parent = find(components, components[i].parent); - } - return components[i].parent; - } - - /** - * Performs the Union operation for Union-Find - * - * @param components array of subsets - * @param x index of the first subsetRecursion - * @param y index of the second subsetRecursion - */ - static void union(Component[] components, final int x, final int y) { - final int xroot = find(components, x); - final int yroot = find(components, y); - - if (components[xroot].rank < components[yroot].rank) { - components[xroot].parent = yroot; - } else if (components[xroot].rank > components[yroot].rank) { - components[yroot].parent = xroot; - } else { - components[yroot].parent = xroot; - components[xroot].rank++; - } - } - - /** - * Boruvka's algorithm to find the Minimum Spanning Tree - * - * @param graph the graph - * @return list of edges in the Minimum Spanning Tree - */ - static List boruvkaMST(final Graph graph) { - var boruvkaState = new BoruvkaState(graph); - - while (boruvkaState.hasMoreEdgesToAdd()) { - final var cheapest = boruvkaState.computeCheapestEdges(); - boruvkaState.merge(cheapest); - } - return boruvkaState.result; - } - - /** - * Checks if the edge vertices are in a valid range - * - * @param vertex the vertex to check - * @param upperBound the upper bound for the vertex range - */ - private static void checkEdgeVertices(final int vertex, final int upperBound) { - if (vertex < 0 || vertex >= upperBound) { - throw new IllegalArgumentException("Edge vertex out of range"); - } - } -} From c4e5e180683f058a81f3a8d8ba33a4d040d774e8 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:40:13 +0530 Subject: [PATCH 26/28] Delete src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java --- .../datastructures/crdt/LWWElementSet.java | 138 ------------------ 1 file changed, 138 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java deleted file mode 100644 index 289e95c38769..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.thealgorithms.datastructures.crdt; - -import java.util.HashMap; -import java.util.Map; - -/** - * Last-Write-Wins Element Set (LWWElementSet) is a state-based CRDT (Conflict-free Replicated Data Type) - * designed for managing sets in a distributed and concurrent environment. It supports the addition and removal - * of elements, using timestamps to determine the order of operations. The set is split into two subsets: - * the add set for elements to be added and the remove set for elements to be removed. - * - * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) - * @see Conflict-free_replicated_data_type - * @see itakurah (Niklas Hoefflin) - */ - -class Element { - String key; - int timestamp; - Bias bias; - - /** - * Constructs a new Element with the specified key, timestamp and bias. - * - * @param key The key of the element. - * @param timestamp The timestamp associated with the element. - * @param bias The bias of the element (ADDS or REMOVALS). - */ - Element(String key, int timestamp, Bias bias) { - this.key = key; - this.timestamp = timestamp; - this.bias = bias; - } -} - -enum Bias { - /** - * ADDS bias for the add set. - * REMOVALS bias for the remove set. - */ - ADDS, - REMOVALS -} - -class LWWElementSet { - private final Map addSet; - private final Map removeSet; - - /** - * Constructs an empty LWWElementSet. - */ - LWWElementSet() { - this.addSet = new HashMap<>(); - this.removeSet = new HashMap<>(); - } - - /** - * Adds an element to the addSet. - * - * @param e The element to be added. - */ - public void add(Element e) { - addSet.put(e.key, e); - } - - /** - * Removes an element from the removeSet. - * - * @param e The element to be removed. - */ - public void remove(Element e) { - if (lookup(e)) { - removeSet.put(e.key, e); - } - } - - /** - * Checks if an element is in the LWWElementSet by comparing timestamps in the addSet and removeSet. - * - * @param e The element to be checked. - * @return True if the element is present, false otherwise. - */ - public boolean lookup(Element e) { - Element inAddSet = addSet.get(e.key); - Element inRemoveSet = removeSet.get(e.key); - - return (inAddSet != null && (inRemoveSet == null || inAddSet.timestamp > inRemoveSet.timestamp)); - } - - /** - * Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subsetRecursion. - * - * @param other The LWWElementSet to compare. - * @return True if the set is subsetRecursion, false otherwise. - */ - public boolean compare(LWWElementSet other) { - return other.addSet.keySet().containsAll(addSet.keySet()) && other.removeSet.keySet().containsAll(removeSet.keySet()); - } - - /** - * Merges another LWWElementSet into this set by resolving conflicts based on timestamps. - * - * @param other The LWWElementSet to merge. - */ - public void merge(LWWElementSet other) { - for (Element e : other.addSet.values()) { - if (!addSet.containsKey(e.key) || compareTimestamps(addSet.get(e.key), e)) { - addSet.put(e.key, e); - } - } - - for (Element e : other.removeSet.values()) { - if (!removeSet.containsKey(e.key) || compareTimestamps(removeSet.get(e.key), e)) { - removeSet.put(e.key, e); - } - } - } - - /** - * Compares timestamps of two elements based on their bias (ADDS or REMOVALS). - * - * @param e The first element. - * @param other The second element. - * @return True if the first element's timestamp is greater or the bias is ADDS and timestamps are equal. - */ - public boolean compareTimestamps(Element e, Element other) { - if (e.bias != other.bias) { - throw new IllegalArgumentException("Invalid bias value"); - } - Bias bias = e.bias; - int timestampComparison = Integer.compare(e.timestamp, other.timestamp); - - if (timestampComparison == 0) { - return bias != Bias.ADDS; - } - return timestampComparison < 0; - } -} From 8de38cbaf49c59b474f886b9b6d5d80592914cce Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:40:28 +0530 Subject: [PATCH 27/28] Delete src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java --- .../datastructures/crdt/TwoPSet.java | 84 ------------------- 1 file changed, 84 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java deleted file mode 100644 index b434f2686a05..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.thealgorithms.datastructures.crdt; - -import java.util.HashSet; -import java.util.Set; - -/** - * TwoPhaseSet (2P-Set) is a state-based CRDT (Conflict-free Replicated Data Type) designed for managing sets - * with support for both addition and removal operations in a distributed and concurrent environment. - * It combines two G-Sets (grow-only sets) - one set for additions and another set (tombstone set) for removals. - * Once an element is removed and placed in the tombstone set, it cannot be re-added, adhering to "remove-wins" semantics. - * This implementation supports querying the presence of elements, adding elements, removing elements, - * comparing with other 2P-Sets, and merging two 2P-Sets while preserving the remove-wins semantics. - * (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) - * - * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) - */ - -public class TwoPSet { - private final Set setA; - private final Set setR; - - /** - * Constructs an empty Two-Phase Set. - */ - public TwoPSet() { - this.setA = new HashSet<>(); - this.setR = new HashSet<>(); - } - - /** - * Checks if an element is in the set and has not been removed. - * - * @param element The element to be checked. - * @return True if the element is in the set and has not been removed, otherwise false. - */ - public boolean lookup(T element) { - return setA.contains(element) && !setR.contains(element); - } - - /** - * Adds an element to the set. - * - * @param element The element to be added. - */ - public void add(T element) { - setA.add(element); - } - - /** - * Removes an element from the set. The element will be placed in the tombstone set. - * - * @param element The element to be removed. - */ - public void remove(T element) { - if (lookup(element)) { - setR.add(element); - } - } - - /** - * Compares the current 2P-Set with another 2P-Set. - * - * @param otherSet The other 2P-Set to compare with. - * @return True if both SetA and SetR are subsetRecursion, otherwise false. - */ - public boolean compare(TwoPSet otherSet) { - return otherSet.setA.containsAll(setA) && otherSet.setR.containsAll(setR); - } - - /** - * Merges the current 2P-Set with another 2P-Set. - * - * @param otherSet The other 2P-Set to merge with. - * @return A new 2P-Set containing the merged elements. - */ - public TwoPSet merge(TwoPSet otherSet) { - TwoPSet mergedSet = new TwoPSet<>(); - mergedSet.setA.addAll(this.setA); - mergedSet.setA.addAll(otherSet.setA); - mergedSet.setR.addAll(this.setR); - mergedSet.setR.addAll(otherSet.setR); - return mergedSet; - } -} From 09638b5881e114ff73137a0e09f010317d0d6606 Mon Sep 17 00:00:00 2001 From: Tuhinm2002 <75078694+Tuhinm2002@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:40:50 +0530 Subject: [PATCH 28/28] Delete src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java --- .../datastructures/crdt/ORSet.java | 191 ------------------ 1 file changed, 191 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java deleted file mode 100644 index 894be3df4146..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java +++ /dev/null @@ -1,191 +0,0 @@ -package com.thealgorithms.datastructures.crdt; - -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -/** - * ORSet (Observed-Removed Set) is a state-based CRDT (Conflict-free Replicated Data Type) - * that supports both addition and removal of elements. This particular implementation follows - * the Add-Wins strategy, meaning that in case of conflicting add and remove operations, - * the add operation takes precedence. The merge operation of two OR-Sets ensures that - * elements added at any replica are eventually observed at all replicas. Removed elements, - * once observed, are never reintroduced. - * This OR-Set implementation provides methods for adding elements, removing elements, - * checking for element existence, retrieving the set of elements, comparing with other OR-Sets, - * and merging with another OR-Set to create a new OR-Set containing all unique elements - * from both sets. - * - * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) - * @see Conflict-free_replicated_data_type - * @see itakurah (Niklas Hoefflin) - */ - -public class ORSet { - - private final Set> elements; - private final Set> tombstones; - - /** - * Constructs an empty OR-Set. - */ - public ORSet() { - this.elements = new HashSet<>(); - this.tombstones = new HashSet<>(); - } - - /** - * Checks if the set contains the specified element. - * - * @param element the element to check for - * @return true if the set contains the element, false otherwise - */ - public boolean contains(T element) { - return elements.stream().anyMatch(pair -> pair.getElement().equals(element)); - } - - /** - * Retrieves the elements in the set. - * - * @return a set containing the elements - */ - public Set elements() { - Set result = new HashSet<>(); - elements.forEach(pair -> result.add(pair.getElement())); - return result; - } - - /** - * Adds the specified element to the set. - * - * @param element the element to add - */ - public void add(T element) { - String n = prepare(); - effect(element, n); - } - - /** - * Removes the specified element from the set. - * - * @param element the element to remove - */ - public void remove(T element) { - Set> pairsToRemove = prepare(element); - effect(pairsToRemove); - } - - /** - * Collect all pairs with the specified element. - * - * @param element the element to collect pairs for - * @return a set of pairs with the specified element to be removed - */ - private Set> prepare(T element) { - Set> pairsToRemove = new HashSet<>(); - for (Pair pair : elements) { - if (pair.getElement().equals(element)) { - pairsToRemove.add(pair); - } - } - return pairsToRemove; - } - - /** - * Generates a unique tag for the element. - * - * @return the unique tag - */ - private String prepare() { - return generateUniqueTag(); - } - - /** - * Adds the element with the specified unique tag to the set. - * - * @param element the element to add - * @param n the unique tag associated with the element - */ - private void effect(T element, String n) { - Pair pair = new Pair<>(element, n); - elements.add(pair); - elements.removeAll(tombstones); - } - - /** - * Removes the specified pairs from the set. - * - * @param pairsToRemove the pairs to remove - */ - private void effect(Set> pairsToRemove) { - elements.removeAll(pairsToRemove); - tombstones.addAll(pairsToRemove); - } - - /** - * Generates a unique tag. - * - * @return the unique tag - */ - private String generateUniqueTag() { - return UUID.randomUUID().toString(); - } - - /** - * Compares this Add-Wins OR-Set with another OR-Set to check if elements and tombstones are a subsetRecursion. - * - * @param other the other OR-Set to compare - * @return true if the sets are subsetRecursion, false otherwise - */ - public boolean compare(ORSet other) { - Set> union = new HashSet<>(elements); - union.addAll(tombstones); - - Set> otherUnion = new HashSet<>(other.elements); - otherUnion.addAll(other.tombstones); - - return otherUnion.containsAll(union) && other.tombstones.containsAll(tombstones); - } - - /** - * Merges this Add-Wins OR-Set with another OR-Set. - * - * @param other the other OR-Set to merge - */ - public void merge(ORSet other) { - elements.removeAll(other.tombstones); - other.elements.removeAll(tombstones); - elements.addAll(other.elements); - tombstones.addAll(other.tombstones); - } - - /** - * Represents a pair containing an element and a unique tag. - * - * @param the type of the element in the pair - */ - public static class Pair { - private final T element; - private final String uniqueTag; - - /** - * Constructs a pair with the specified element and unique tag. - * - * @param element the element in the pair - * @param uniqueTag the unique tag associated with the element - */ - public Pair(T element, String uniqueTag) { - this.element = element; - this.uniqueTag = uniqueTag; - } - - /** - * Gets the element from the pair. - * - * @return the element - */ - public T getElement() { - return element; - } - } -}