Skip to content

Added PN-Counter (Positive-Negative Counter) for tracking counts in distributed systems #4974

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 110 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
110 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
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
itakurah committed Nov 29, 2023
commit 5c37c5c7fc8521c44e3e0cc57016eafde53f79cd
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@ static class Component {
static class BoruvkaState {
List<Edge> result;
Component[] components;
Graph graph;

BoruvkaState(List<Edge> result, Component[] components) {
this.result = result;
this.components = components;
BoruvkaState(Graph graph) {
this.result = new ArrayList<>();
this.components = initializeSubsets(graph);
this.graph = graph;
}

/**
* Adds the cheapest edges to the result list and performs Union operation on the subsets.
*
* @param graph the graph
* @param cheapest Array containing the cheapest edge for each subset.
*/
void merge(Graph graph, Edge[] cheapest) {
void merge(Edge[] cheapest) {
for (int i = 0; i < graph.vertex; ++i) {
if (cheapest[i] != null) {
final var set1 = find(components, cheapest[i].src);
Expand All @@ -103,6 +104,38 @@ void merge(Graph graph, Edge[] cheapest) {
}
}
}

/**
* 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 subset in the Union-Find structure.
*
* @return an array containing the cheapest edge for each subset.
*/
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;
}
}

/**
Expand Down Expand Up @@ -147,15 +180,13 @@ static void union(Component[] components, final int x, final int y) {
* @return list of edges in the Minimum Spanning Tree
*/
static List<Edge> boruvkaMST(final Graph graph) {
List<Edge> result = new ArrayList<>();
Component[] components = initializeSubsets(graph);
BoruvkaState boruvkaState = new BoruvkaState(result, components);
var boruvkaState = new BoruvkaState(graph);

while (result.size() < graph.vertex - 1) {
final var cheapest = computeCheapestEdges(graph, boruvkaState.components);
boruvkaState.merge(graph, cheapest);
while (boruvkaState.hasMoreEdgesToAdd()) {
final var cheapest = boruvkaState.computeCheapestEdges();
boruvkaState.merge(cheapest);
}
return result;
return boruvkaState.result;
}

/**
Expand All @@ -172,30 +203,6 @@ private static Component[] initializeSubsets(final Graph graph) {
return components;
}

/**
* Computes the cheapest edges for each subset in the Union-Find structure.
*
* @param graph the graph
* @param components Array of subsets used for Union-Find operations.
* @return an array containing the cheapest edge for each subset.
*/
private static Edge[] computeCheapestEdges(final Graph graph, final Component[] components) {
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;
}

/**
* Checks if the edge vertices are in a valid range
Expand All @@ -208,18 +215,4 @@ private static void checkEdgeVertices(final int vertex, final int upperBound) {
throw new IllegalArgumentException("Edge vertex out of range");
}
}

/**
* Computes the total weight of the Minimum Spanning Tree
*
* @param result list of edges in the Minimum Spanning Tree
* @return the total weight of the Minimum Spanning Tree
*/
public static int computeTotalWeight(final List<Edge> result) {
int totalWeight = 0;
for (final var edge : result) {
totalWeight += edge.weight;
}
return totalWeight;
}
}