Skip to content

Introduce 2P-Set (Two-Phase Set) for both addition and removal operations in distributed systems #4977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 121 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
d1d2da9
added Boruvka's algorithm to find Minimum Spanning Tree
itakurah Nov 19, 2023
0236655
clang format
itakurah Nov 19, 2023
0cb6011
clang format
itakurah Nov 19, 2023
1d0717d
clang format
itakurah Nov 19, 2023
e8b51c9
clang format
itakurah Nov 19, 2023
55078e4
clang format
itakurah Nov 22, 2023
0da8bad
clang format
itakurah Nov 22, 2023
f0d8192
added JUnit tests
itakurah Nov 22, 2023
262648d
clang format
itakurah Nov 22, 2023
4a8864b
clang format
itakurah Nov 22, 2023
37be803
clang format
itakurah Nov 22, 2023
78f0a96
clang format
itakurah Nov 22, 2023
0162767
clang format
itakurah Nov 22, 2023
54196ba
clang format
itakurah Nov 22, 2023
9ce77b0
clang format
itakurah Nov 22, 2023
9399d24
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
3b4e6d6
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
3f1b5ab
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
54745a4
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
7ca5cba
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
8af1152
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
3dce6ee
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
76ee92b
removed compareTo
itakurah Nov 22, 2023
bb6dbca
renamed member fields
itakurah Nov 22, 2023
e7ea4e1
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 22, 2023
b41c3a1
changed constructor
itakurah Nov 23, 2023
40b5b25
updated test cases
itakurah Nov 23, 2023
5c34f3b
clang format
itakurah Nov 23, 2023
a62ef7e
added GCounter for tracking counts in a distributed and concurrent en…
itakurah Nov 23, 2023
c748099
added GCounter for tracking counts in a distributed and concurrent en…
itakurah Nov 23, 2023
623be46
clang format
itakurah Nov 23, 2023
9a3587d
clang format
itakurah Nov 23, 2023
92ce6b1
clang format
itakurah Nov 23, 2023
f59213e
clang format
itakurah Nov 23, 2023
1c1f78c
clang format
itakurah Nov 23, 2023
fa96251
clang format
itakurah Nov 23, 2023
f275ed1
clang format
itakurah Nov 23, 2023
f2d9a93
clang format
itakurah Nov 23, 2023
1c14657
clang format
itakurah Nov 23, 2023
bcf8dbe
clang format
itakurah Nov 23, 2023
d0cae1f
clang format
itakurah Nov 23, 2023
5496ffc
clang format
itakurah Nov 23, 2023
8543d83
clang format
itakurah Nov 23, 2023
0e6e7de
clang format
itakurah Nov 24, 2023
bd04ed7
added test cases for vertices and edges
itakurah Nov 24, 2023
2a16c18
clang format
itakurah Nov 24, 2023
277b823
clang format
itakurah Nov 24, 2023
f6c71dd
Merge branch 'master' into master
vil02 Nov 26, 2023
b67ef33
Merge remote-tracking branch 'origin/crdt'
itakurah Nov 27, 2023
ac8d8df
added separate methods for logic
itakurah Nov 27, 2023
d191a27
added new test cases
itakurah Nov 27, 2023
fc11ba7
Merge remote-tracking branch 'origin/master'
itakurah Nov 27, 2023
45803d0
Update src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
0aa71e8
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
1e3b255
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
f905321
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
8f41341
Update src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
5aaec56
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
7fef71d
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
7b230f6
refactor
itakurah Nov 27, 2023
c47a7e9
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
7b9fd37
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
ed3ad77
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
cd07ca6
Update src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
0989353
Update src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 27, 2023
268eb89
refactor
itakurah Nov 27, 2023
9471b29
refactor
itakurah Nov 27, 2023
8b81224
Merge branch 'TheAlgorithms:master' into pncounter
itakurah Nov 28, 2023
2cf09c6
added computeTotalWeight() and some refactoring
itakurah Nov 28, 2023
e0637b6
refactor
itakurah Nov 28, 2023
81ca459
clang format
itakurah Nov 28, 2023
8f59ab8
Merge branch 'master' into master
vil02 Nov 28, 2023
9897ddc
refactor
itakurah Nov 29, 2023
5c37c5c
refactor
itakurah Nov 29, 2023
d380a0c
Update directory
Nov 29, 2023
3a23e3d
clang format
itakurah Nov 29, 2023
708b8f0
Merge remote-tracking branch 'origin/master'
itakurah Nov 29, 2023
be3eb66
clang format
itakurah Nov 29, 2023
05241ca
clang format
itakurah Nov 29, 2023
df7314a
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 29, 2023
c47c59a
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 29, 2023
b2757b5
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 29, 2023
48001bf
Update src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaA…
itakurah Nov 29, 2023
bdab7cb
refactor
itakurah Nov 29, 2023
63a53d7
refactor
itakurah Nov 29, 2023
61f80ad
refactor
itakurah Nov 29, 2023
0821de0
refactor
itakurah Nov 29, 2023
d9dcead
refactor
itakurah Nov 29, 2023
73b22d1
refactor
itakurah Nov 29, 2023
1cd65fe
refactor
itakurah Nov 29, 2023
1b90810
refactor
itakurah Nov 30, 2023
eec8d1b
Merge branch 'TheAlgorithms:master' into crdt
itakurah Dec 2, 2023
30aa66e
Merge branch 'pncounter'
itakurah Dec 2, 2023
dfc9f3a
Merge remote-tracking branch 'origin/master'
itakurah Dec 2, 2023
aba0e75
Merge remote-tracking branch 'origin/crdt'
itakurah Dec 2, 2023
03d5dce
Merge remote-tracking branch 'origin/pncounter' into pncounter
itakurah Dec 2, 2023
f706fe8
added PN-Counter
itakurah Dec 2, 2023
3afb28e
added PN-Counter tests
itakurah Dec 2, 2023
edd511a
Update directory
Dec 2, 2023
f8b3c55
clang format
itakurah Dec 2, 2023
88ac39b
Update directory
Dec 2, 2023
c418184
clang format
itakurah Dec 2, 2023
03377e2
clang format
itakurah Dec 2, 2023
af7d944
clang format
itakurah Dec 2, 2023
0a72e19
Merge branch 'master' into pn
itakurah Dec 3, 2023
b3976bc
Merge remote-tracking branch 'origin/pncounter' into pn
itakurah Dec 3, 2023
dba8296
Update src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.…
itakurah Dec 3, 2023
4db306e
added checks for different node size of counters when comparing and m…
itakurah Dec 3, 2023
58ad1b9
Merge remote-tracking branch 'origin/pn' into pn
itakurah Dec 3, 2023
b0076f3
spelling
itakurah Dec 4, 2023
70ee950
Merge remote-tracking branch 'origin/master' into gset
itakurah Dec 5, 2023
65e4e75
added GSet for adding elements in a distributed and concurrent enviro…
itakurah Dec 5, 2023
a444a2f
added GSet test cases
itakurah Dec 5, 2023
d44178b
Update directory
Dec 5, 2023
f26f702
clang format
itakurah Dec 5, 2023
4fcf392
Merge remote-tracking branch 'origin/gset' into gset
itakurah Dec 5, 2023
84e854d
added 2P-Set
itakurah Dec 7, 2023
7cfede2
added 2P-Set tests
itakurah Dec 7, 2023
5b69ffa
Update directory
Dec 7, 2023
a16eb6d
Merge branch 'TheAlgorithms:master' into 2pset
itakurah Dec 7, 2023
79d9061
Update directory
Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
* [GCounter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java)
* [GSet](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java)
* [PNCounter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java)
* [TwoPSet](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java)
* disjointsetunion
* [DisjointSetUnion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java)
* [Node](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/disjointsetunion/Node.java)
Expand Down Expand Up @@ -528,7 +529,7 @@
* [InfixToPostfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/InfixToPostfix.java)
* [LargestRectangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/LargestRectangle.java)
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
* [NextGraterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGraterElement.java)
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
Expand Down Expand Up @@ -621,6 +622,7 @@
* [GCounterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/GCounterTest.java)
* [GSetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/GSetTest.java)
* [PNCounterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/PNCounterTest.java)
* [TwoPSetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/TwoPSetTest.java)
* disjointsetunion
* [DisjointSetUnionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnionTest.java)
* graphs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 Set<String> setA;
private Set<String> 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(String element) {
return setA.contains(element) && !setR.contains(element);
}

/**
* Adds an element to the set.
*
* @param element The element to be added.
*/
public void add(String 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(String 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 subset, 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.thealgorithms.datastructures.crdt;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TwoPSetTest {

private TwoPSet set;

@BeforeEach
void setUp() {
set = new TwoPSet();
}

@Test
void testLookup() {
set.add("A");
assertTrue(set.lookup("A"));
assertFalse(set.lookup("B"));
set.remove("A");
assertFalse(set.lookup("A"));
}

@Test
void testAdd() {
set.add("A");
assertTrue(set.lookup("A"));
}

@Test
void testRemove() {
set.add("A");
set.remove("A");
assertFalse(set.lookup("A"));
}

@Test
void testCompare() {
TwoPSet set1 = new TwoPSet();
set1.add("A");
set1.add("B");
TwoPSet set2 = new TwoPSet();
set2.add("A");
assertFalse(set1.compare(set2));
set2.add("B");
assertTrue(set1.compare(set2));
set1.remove("A");
assertFalse(set1.compare(set2));
set2.remove("A");
assertTrue(set1.compare(set2));
}

@Test
void testMerge() {
TwoPSet set1 = new TwoPSet();
set1.add("A");
set1.add("B");
TwoPSet set2 = new TwoPSet();
set2.add("B");
set2.add("C");
TwoPSet mergedSet = set1.merge(set2);
assertTrue(mergedSet.lookup("A"));
assertTrue(mergedSet.lookup("B"));
assertTrue(mergedSet.lookup("C"));
}
}