Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 7ab3b6f

Browse files
committed
Merge branch '3.4' of github.com:neo4j-contrib/neo4j-graph-algorithms into 3.4
2 parents 57d278a + 3aac6f6 commit 7ab3b6f

File tree

21 files changed

+878
-96
lines changed

21 files changed

+878
-96
lines changed

algo/src/main/java/org/neo4j/graphalgo/impl/Prim.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
public class Prim extends Algorithm<Prim> {
4747

48+
public static final int UNUSED = 42;
4849
private final RelationshipIterator relationshipIterator;
4950
private final RelationshipWeights weights;
5051
private final int nodeCount;
@@ -90,11 +91,16 @@ private SpanningTree prim(int startNode, boolean max) {
9091
if (visited.contains(t)) {
9192
return true;
9293
}
93-
// invert weight to calculate maximum
94+
// inverting the weight calculates maximum- instead of minimum spanning tree
9495
final double w = max ? -weights.weightOf(s, t) : weights.weightOf(s, t);
9596
if (w < cost.getOrDefault(t, Double.MAX_VALUE)) {
96-
cost.put(t, w);
97-
queue.add(t, -1.0);
97+
if (cost.containsKey(t)) {
98+
cost.put(t, w);
99+
queue.update(t);
100+
} else {
101+
cost.put(t, w);
102+
queue.add(t, w);
103+
}
98104
spanningTree.parent[t] = s;
99105
}
100106
return true;

algo/src/main/java/org/neo4j/graphalgo/impl/ShortestPathDijkstra.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ShortestPathDijkstra extends Algorithm<ShortestPathDijkstra> {
3939

4040
private static final int PATH_END = -1;
4141
public static final double NO_PATH_FOUND = -1.0;
42+
public static final int UNUSED = 42;
4243

4344
private Graph graph;
4445

@@ -152,28 +153,28 @@ private void run(int goal, Direction direction) {
152153
graph.forEachRelationship(
153154
node,
154155
direction, (source, target, relId, weight) -> {
155-
boolean oldCostChanged = updateCosts(source, target, weight + costs);
156-
if (!visited.get(target)) {
157-
if (oldCostChanged) {
158-
queue.update(target);
159-
} else {
160-
queue.add(target, 0);
161-
}
162-
}
156+
updateCosts(source, target, weight + costs);
163157
return true;
164158
});
165159
progressLogger.logProgress((double) node / (nodeCount - 1));
166160
}
167161
}
168162

169-
private boolean updateCosts(int source, int target, double newCosts) {
170-
double oldCosts = costs.getOrDefault(target, Double.MAX_VALUE);
171-
if (newCosts < oldCosts) {
172-
costs.put(target, newCosts);
173-
path.put(target, source);
174-
return oldCosts < Double.MAX_VALUE;
163+
private void updateCosts(int source, int target, double newCosts) {
164+
165+
if (costs.containsKey(target)) {
166+
if (newCosts < costs.getOrDefault(target, Double.MAX_VALUE)) {
167+
costs.put(target, newCosts);
168+
path.put(target, source);
169+
queue.update(target);
170+
}
171+
} else {
172+
if (newCosts < costs.getOrDefault(target, Double.MAX_VALUE)) {
173+
costs.put(target, newCosts);
174+
path.put(target, source);
175+
queue.add(target, newCosts);
176+
}
175177
}
176-
return false;
177178
}
178179

179180
@Override

algo/src/main/java/org/neo4j/graphalgo/impl/ShortestPaths.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void run() {
9999
final double targetCosts = this.costs.getOrDefault(target, Double.POSITIVE_INFINITY);
100100
if (weight + sourceCosts < targetCosts) {
101101
costs.put(target, weight + sourceCosts);
102-
queue.set(target, targetCosts);
102+
queue.set(target, weight + sourceCosts);
103103
}
104104
return true;
105105
});

algo/src/main/java/org/neo4j/graphalgo/impl/spanningTrees/KSpanningTree.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.neo4j.graphalgo.results.AbstractResultBuilder;
3030

3131
/**
32-
* Sequential Single-Source minimum weight spanning tree algorithm (PRIM).
33-
* <p>
3432
* The algorithm computes the MST by traversing all nodes from a given
3533
* startNodeId. It aggregates all transitions into a MinPriorityQueue
3634
* and visits each (unvisited) connected node by following only the

algo/src/main/java/org/neo4j/graphalgo/impl/spanningTrees/Prim.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ private SpanningTree prim(int startNode, boolean max) {
8686
// invert weight to calculate maximum
8787
final double w = max ? -weights.weightOf(s, t) : weights.weightOf(s, t);
8888
if (w < cost.getOrDefault(t, Double.MAX_VALUE)) {
89-
cost.put(t, w);
90-
queue.add(t, -1.0);
89+
if (cost.containsKey(t)) {
90+
cost.put(t, w);
91+
queue.update(t);
92+
} else {
93+
cost.put(t, w);
94+
queue.add(t, -1.0);
95+
}
9196
parent[t] = s;
9297
}
9398
return true;

algo/src/main/java/org/neo4j/graphalgo/impl/yens/Dijkstra.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.neo4j.graphalgo.core.utils.TerminationFlag;
2525
import org.neo4j.graphalgo.core.utils.queue.IntPriorityQueue;
2626
import org.neo4j.graphalgo.core.utils.queue.SharedIntPriorityQueue;
27-
import org.neo4j.graphalgo.core.utils.traverse.SimpleBitSet;
2827
import org.neo4j.graphdb.Direction;
2928

3029
import java.util.Arrays;
@@ -38,18 +37,19 @@
3837
public class Dijkstra {
3938

4039
// initial weighted path capacity
40+
4141
public static final int INITIAL_CAPACITY = 64;
4242

4343
private static final int PATH_END = -1;
44-
4544
private final Graph graph;
45+
4646
private final int nodeCount;
4747

4848
private TerminationFlag terminationFlag = TerminationFlag.RUNNING_TRUE;
49-
5049
// node to cost map
5150
private final IntDoubleMap costs;
5251
// next node priority queue
52+
5353
private final IntPriorityQueue queue;
5454
// auxiliary path map
5555
private final IntIntMap path;
@@ -157,7 +157,8 @@ private boolean dijkstra(int source, int target, Direction direction, int maxDep
157157
visited.clear();
158158
costs.put(source, 0.0);
159159
queue.add(source, 0.0);
160-
Arrays.fill(depth, 1);
160+
Arrays.fill(depth, 0);
161+
depth[source] = 1;
161162
while (!queue.isEmpty() && terminationFlag.running()) {
162163
int node = queue.pop();
163164
final int d = depth[node];
@@ -176,32 +177,49 @@ private boolean dijkstra(int source, int target, Direction direction, int maxDep
176177
return true;
177178
}
178179
final double w = graph.weightOf(s, t);
179-
final boolean updateCosts = updateCosts(s, t, w + costs);
180+
final UpdateResult updateCosts = updateCosts(s, t, w + costs);
180181
if (!visited.get(t)) {
181-
depth[t] = d + 1;
182-
if (updateCosts) {
183-
queue.update(t);
184-
} else {
185-
queue.add(t, w);
182+
switch (updateCosts) {
183+
case NO_PREVIOUS_COSTS:
184+
queue.add(t, w);
185+
break;
186+
case UPDATED_COST:
187+
queue.update(t);
188+
break;
189+
default:
190+
break;
186191
}
192+
depth[t] = depth[s] + 1;
187193
}
188194
return terminationFlag.running();
189195
});
190196
}
191197
return false;
192198
}
193199

200+
194201
/**
195202
* update cost map
196203
*/
197-
private boolean updateCosts(int source, int target, double newCosts) {
204+
private UpdateResult updateCosts(int source, int target, double newCosts) {
198205
double oldCosts = costs.getOrDefault(target, Double.MAX_VALUE);
206+
if (oldCosts == Double.MAX_VALUE) {
207+
if (!costs.containsKey(target)) {
208+
costs.put(target, newCosts);
209+
path.put(target, source);
210+
return UpdateResult.NO_PREVIOUS_COSTS;
211+
}
212+
}
199213
if (newCosts < oldCosts) {
200214
costs.put(target, newCosts);
201215
path.put(target, source);
202-
return oldCosts < Double.MAX_VALUE;
216+
return UpdateResult.UPDATED_COST;
203217
}
204-
return false;
218+
return UpdateResult.COST_NOT_COMPETITIVE;
219+
}
220+
221+
private enum UpdateResult {
222+
NO_PREVIOUS_COSTS, UPDATED_COST, COST_NOT_COMPETITIVE;
205223
}
206224

207225
}

algo/src/main/java/org/neo4j/graphalgo/similarity/CategoricalInput.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ SimilarityResult jaccard(double similarityCutoff, CategoricalInput e2) {
2626
if (jaccard < similarityCutoff) return null;
2727
return new SimilarityResult(id, e2.id, count1, count2, intersection, jaccard);
2828
}
29+
30+
SimilarityResult overlap(double similarityCutoff, CategoricalInput e2) {
31+
long intersection = Intersections.intersection3(targets, e2.targets);
32+
if (similarityCutoff >= 0d && intersection == 0) return null;
33+
int count1 = targets.length;
34+
int count2 = e2.targets.length;
35+
long denominator = Math.min(count1, count2);
36+
double overlap = denominator == 0 ? 0 : (double)intersection / denominator;
37+
if (overlap < similarityCutoff) return null;
38+
39+
if(count1 <= count2) {
40+
return new SimilarityResult(id, e2.id, count1, count2, intersection, overlap, false, false);
41+
} else {
42+
return new SimilarityResult(e2.id, id, count2, count1, intersection, overlap, false, true);
43+
}
44+
45+
}
2946
}

algo/src/main/java/org/neo4j/graphalgo/similarity/CosineProc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Stream<SimilaritySummaryResult> cosine(
8181

8282

8383
boolean write = configuration.isWriteFlag(false) && similarityCutoff > 0.0;
84-
return writeAndAggregateResults(configuration, stream, inputs.length, write);
84+
return writeAndAggregateResults(configuration, stream, inputs.length, write, "SIMILAR");
8585
}
8686

8787

algo/src/main/java/org/neo4j/graphalgo/similarity/EuclideanProc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Stream<SimilaritySummaryResult> euclidean(
8080
.map(SimilarityResult::squareRooted);
8181

8282
boolean write = configuration.isWriteFlag(false); // && similarityCutoff != 0.0;
83-
return writeAndAggregateResults(configuration, stream, inputs.length, write);
83+
return writeAndAggregateResults(configuration, stream, inputs.length, write, "SIMILAR");
8484
}
8585

8686

algo/src/main/java/org/neo4j/graphalgo/similarity/JaccardProc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Stream<SimilaritySummaryResult> jaccard(
6161
Stream<SimilarityResult> stream = topN(similarityStream(inputs, computer, configuration, similarityCutoff, getTopK(configuration)), getTopN(configuration));
6262

6363
boolean write = configuration.isWriteFlag(false) && similarityCutoff > 0.0;
64-
return writeAndAggregateResults(configuration, stream, inputs.length, write);
64+
return writeAndAggregateResults(configuration, stream, inputs.length, write, "SIMILAR");
6565
}
6666

6767

0 commit comments

Comments
 (0)