Skip to content

Commit 68e45ad

Browse files
Fixed longest path with zero weights
Co-authored-by: Ioannis Panagiotas <[email protected]>
1 parent 414f022 commit 68e45ad

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

algo/src/main/java/org/neo4j/gds/dag/longestPath/DagLongestPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
import org.neo4j.gds.collections.haa.HugeAtomicLongArray;
3131
import org.neo4j.gds.core.concurrency.ExecutorServiceUtil;
3232
import org.neo4j.gds.core.concurrency.ParallelUtil;
33-
import org.neo4j.gds.termination.TerminationFlag;
3433
import org.neo4j.gds.core.utils.paged.ParalleLongPageCreator;
3534
import org.neo4j.gds.core.utils.partition.PartitionUtils;
3635
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
3736
import org.neo4j.gds.paths.ImmutablePathResult;
3837
import org.neo4j.gds.paths.PathResult;
3938
import org.neo4j.gds.paths.delta.TentativeDistances;
4039
import org.neo4j.gds.paths.dijkstra.PathFindingResult;
40+
import org.neo4j.gds.termination.TerminationFlag;
4141

4242
import java.util.Optional;
4343
import java.util.concurrent.ConcurrentHashMap;
@@ -74,7 +74,7 @@ public class DagLongestPath extends Algorithm<PathFindingResult> {
7474
this.nodeCount = graph.nodeCount();
7575
this.concurrency = concurrency;
7676
this.inDegrees = HugeAtomicLongArray.of(nodeCount, ParalleLongPageCreator.passThrough(this.concurrency));
77-
this.parentsAndDistances = TentativeDistances.distanceAndPredecessors(nodeCount, concurrency, Double.MIN_VALUE, (a, b) -> Double.compare(a, b) < 0);
77+
this.parentsAndDistances = TentativeDistances.distanceAndPredecessors(nodeCount, concurrency, -Double.MIN_VALUE, (a, b) -> Double.compare(a, b) < 0);
7878
}
7979

8080
@Override

algo/src/test/java/org/neo4j/gds/dag/longestPath/WeightedDagLongestPathTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,83 @@ void shouldWorkWithMultipleSources() {
202202
assertThat(counter).isEqualTo(7L);
203203
}
204204
}
205+
206+
@GdlExtension
207+
@Nested
208+
class GraphWithZeroWeights {
209+
@GdlGraph
210+
private static final String DB_QUERY =
211+
"CREATE" +
212+
" (n0)" +
213+
", (n1)" +
214+
", (n2)" +
215+
", (n3)" +
216+
", (n0)-[:T {prop: 0.0}]->(n1)" +
217+
", (n1)-[:T {prop: 0.0}]->(n2)" +
218+
", (n2)-[:T {prop: 8.0}]->(n3)";
219+
220+
@Inject
221+
private TestGraph graph;
222+
223+
@Test
224+
void shouldWorkWithZeroWeights() {
225+
IdFunction idFunction = graph::toMappedNodeId;
226+
227+
long[] a = new long[]{
228+
idFunction.of("n0"),
229+
idFunction.of("n1"),
230+
idFunction.of("n2"),
231+
idFunction.of("n3"),
232+
};
233+
234+
var longestPath = new DagLongestPathFactory().build(
235+
graph,
236+
CONFIG,
237+
ProgressTracker.NULL_TRACKER
238+
);
239+
240+
PathFindingResult result = longestPath.compute();
241+
242+
long[][] EXPECTED_PATHS = new long[4][];
243+
EXPECTED_PATHS[(int) a[0]] = new long[]{a[0]};
244+
EXPECTED_PATHS[(int) a[1]] = new long[]{a[0], a[1]};
245+
EXPECTED_PATHS[(int) a[2]] = new long[]{a[0], a[1], a[2]};
246+
EXPECTED_PATHS[(int) a[3]] = new long[]{a[0], a[1], a[2], a[3]};
247+
double[] EXPECTED_COSTS = new double[4];
248+
EXPECTED_COSTS[(int) a[0]] = 0;
249+
EXPECTED_COSTS[(int) a[1]] = 0;
250+
EXPECTED_COSTS[(int) a[2]] = 0;
251+
EXPECTED_COSTS[(int) a[3]] = 8;
252+
253+
long counter = 0;
254+
for (var path : result.pathSet()) {
255+
counter++;
256+
int currentTargetNode = (int) path.targetNode();
257+
assertThat(path.nodeIds()).isEqualTo(EXPECTED_PATHS[currentTargetNode]);
258+
assertThat(path.totalCost()).isEqualTo(EXPECTED_COSTS[currentTargetNode]);
259+
}
260+
assertThat(counter).isEqualTo(4L);
261+
}
262+
263+
@Test
264+
void shouldLogProgress() {
265+
var lpFactory = new DagLongestPathFactory<>();
266+
var progressTask = lpFactory.progressTask(graph, CONFIG);
267+
var log = Neo4jProxy.testLog();
268+
var testTracker = new TestProgressTracker(
269+
progressTask,
270+
log,
271+
CONFIG.concurrency(),
272+
EmptyTaskRegistryFactory.INSTANCE
273+
);
274+
275+
var lp = lpFactory.build(graph, CONFIG, testTracker);
276+
lp.compute().pathSet();
277+
278+
String taskName = lpFactory.taskName();
279+
280+
assertTrue(log.containsMessage(TestLog.INFO, taskName + " :: Start"));
281+
assertTrue(log.containsMessage(TestLog.INFO, taskName + " :: Finished"));
282+
}
283+
}
205284
}

proc/path-finding/src/test/java/org/neo4j/gds/paths/dag/longestPath/DagLongestPathStreamProcTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,5 @@ void testStreamWithWeights() {
134134
}
135135
}
136136
);
137-
138137
}
139138
}

0 commit comments

Comments
 (0)