Skip to content

Commit 1efb04b

Browse files
Add missing graph.concurrentCopy in DegreeCentrality
Co-Authored-By: Max Kießling <[email protected]>
1 parent 827d8df commit 1efb04b

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

algo-common/src/main/java/org/neo4j/graphalgo/degree/DegreeCentrality.java renamed to alpha/alpha-algo/src/main/java/org/neo4j/graphalgo/centrality/degreecentrality/DegreeCentrality.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
20-
package org.neo4j.graphalgo.degree;
20+
package org.neo4j.graphalgo.centrality.degreecentrality;
2121

2222
import org.neo4j.graphalgo.Algorithm;
2323
import org.neo4j.graphalgo.api.Graph;
24+
import org.neo4j.graphalgo.api.RelationshipIterator;
2425
import org.neo4j.graphalgo.core.concurrency.ParallelUtil;
2526
import org.neo4j.graphalgo.core.utils.paged.AllocationTracker;
2627
import org.neo4j.graphalgo.core.utils.paged.HugeDoubleArray;
@@ -66,9 +67,9 @@ public DegreeCentrality compute() {
6667
for (int i = 0; i < taskCount; i++) {
6768
starts[i] = startNode;
6869
if (weighted) {
69-
tasks.add(new WeightedDegreeTask(starts[i], partitions[i]));
70+
tasks.add(new WeightedDegreeTask(graph.concurrentCopy(), starts[i], partitions[i]));
7071
} else {
71-
tasks.add(new DegreeTask(starts[i], partitions[i]));
72+
tasks.add(new DegreeTask(graph, starts[i], partitions[i]));
7273
}
7374
startNode += batchSize;
7475
}
@@ -96,11 +97,13 @@ public CentralityResult result() {
9697
}
9798

9899
private class DegreeTask implements Runnable {
100+
private final Graph graph;
99101
private final long startNodeId;
100102
private final double[] partition;
101103
private final long endNodeId;
102104

103-
DegreeTask(long start, double[] partition) {
105+
DegreeTask(Graph graph, long start, double[] partition) {
106+
this.graph = graph;
104107
this.startNodeId = start;
105108
this.partition = partition;
106109
this.endNodeId = Math.min(start + partition.length, nodeCount);
@@ -116,11 +119,17 @@ public void run() {
116119
}
117120

118121
private class WeightedDegreeTask implements Runnable {
122+
private final RelationshipIterator relationshipIterator;
119123
private final long startNodeId;
120124
private final double[] partition;
121125
private final long endNodeId;
122126

123-
WeightedDegreeTask(long start, double[] partition) {
127+
WeightedDegreeTask(
128+
RelationshipIterator relationshipIterator,
129+
long start,
130+
double[] partition
131+
) {
132+
this.relationshipIterator = relationshipIterator;
124133
this.startNodeId = start;
125134
this.partition = partition;
126135
this.endNodeId = Math.min(start + partition.length, nodeCount);
@@ -130,7 +139,7 @@ private class WeightedDegreeTask implements Runnable {
130139
public void run() {
131140
for (long nodeId = startNodeId; nodeId < endNodeId && running(); nodeId++) {
132141
int index = Math.toIntExact(nodeId - startNodeId);
133-
graph.forEachRelationship(nodeId, DEFAULT_WEIGHT, (sourceNodeId, targetNodeId, weight) -> {
142+
relationshipIterator.forEachRelationship(nodeId, DEFAULT_WEIGHT, (sourceNodeId, targetNodeId, weight) -> {
134143
if (weight > 0) {
135144
partition[index] += weight;
136145
}

algo-common/src/test/java/org/neo4j/graphalgo/degree/DegreeCentralityTest.java renamed to alpha/alpha-algo/src/test/java/org/neo4j/graphalgo/impl/degreecentrality/DegreeCentralityTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,33 @@
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
20-
package org.neo4j.graphalgo.degree;
20+
package org.neo4j.graphalgo.impl.degreecentrality;
2121

2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
2424
import org.neo4j.graphalgo.AlgoTestBase;
2525
import org.neo4j.graphalgo.Orientation;
2626
import org.neo4j.graphalgo.StoreLoaderBuilder;
27+
import org.neo4j.graphalgo.beta.generator.RandomGraphGenerator;
28+
import org.neo4j.graphalgo.beta.generator.RelationshipDistribution;
29+
import org.neo4j.graphalgo.beta.generator.RelationshipPropertyProducer;
30+
import org.neo4j.graphalgo.centrality.degreecentrality.DegreeCentrality;
31+
import org.neo4j.graphalgo.config.RandomGraphGeneratorConfig;
2732
import org.neo4j.graphalgo.core.Aggregation;
2833
import org.neo4j.graphalgo.core.concurrency.Pools;
34+
import org.neo4j.graphalgo.core.huge.HugeGraph;
2935
import org.neo4j.graphalgo.core.utils.paged.AllocationTracker;
36+
import org.neo4j.graphalgo.core.utils.paged.HugeDoubleArray;
37+
import org.neo4j.graphalgo.result.CentralityResult;
3038
import org.neo4j.graphdb.Label;
3139

3240
import java.util.HashMap;
3341
import java.util.Map;
42+
import java.util.Optional;
3443
import java.util.stream.IntStream;
3544

3645
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertTrue;
3747
import static org.neo4j.graphalgo.compat.GraphDatabaseApiProxy.runInTransaction;
3848

3949
final class DegreeCentralityTest extends AlgoTestBase {
@@ -104,6 +114,42 @@ void setupGraphDb() {
104114
runQuery(DB_CYPHER);
105115
}
106116

117+
@Test
118+
void shouldRunConcurrently() {
119+
int nodeCount = 20002;
120+
int averageDegree = 2;
121+
HugeGraph graph = new RandomGraphGenerator(
122+
nodeCount,
123+
averageDegree,
124+
RelationshipDistribution.POWER_LAW,
125+
0L,
126+
Optional.of(RelationshipPropertyProducer.random("similarity", 0, 1)),
127+
Aggregation.NONE,
128+
Orientation.NATURAL,
129+
RandomGraphGeneratorConfig.AllowSelfLoops.NO,
130+
AllocationTracker.EMPTY
131+
).generate();
132+
133+
DegreeCentrality degreeCentrality = new DegreeCentrality(
134+
graph,
135+
Pools.DEFAULT,
136+
2,
137+
true,
138+
AllocationTracker.EMPTY
139+
);
140+
141+
DegreeCentrality centrality = degreeCentrality.compute();
142+
HugeDoubleArray centralityResult = centrality.result().array();
143+
144+
double sum = 0;
145+
for (double v : centralityResult.toArray()) {
146+
sum += v;
147+
}
148+
149+
double expected = nodeCount * averageDegree * 0.5;
150+
assertEquals(expected, sum, expected * 0.1);
151+
}
152+
107153
@Test
108154
void outgoingCentrality() {
109155
final Label label = Label.label("Label1");

alpha/alpha-proc/src/main/java/org/neo4j/graphalgo/centrality/DegreeCentralityProc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.neo4j.graphalgo.core.CypherMapWrapper;
2828
import org.neo4j.graphalgo.core.concurrency.Pools;
2929
import org.neo4j.graphalgo.core.utils.paged.AllocationTracker;
30-
import org.neo4j.graphalgo.degree.DegreeCentrality;
30+
import org.neo4j.graphalgo.centrality.degreecentrality.DegreeCentrality;
3131
import org.neo4j.graphalgo.result.AbstractResultBuilder;
3232
import org.neo4j.graphalgo.results.CentralityScore;
3333
import org.neo4j.logging.Log;

0 commit comments

Comments
 (0)