use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.
the class DijkstraDirectionTest method testDijkstraDirection1.
@Test
void testDijkstraDirection1() {
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "s", "e");
Dijkstra<Double> dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
assertEquals(Direction.OUTGOING, direction);
return 1.0;
}, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
dijkstra.getCost();
dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
assertEquals(Direction.INCOMING, direction);
return 1.0;
}, new DoubleAdder(), Double::compareTo, Direction.INCOMING, MyRelTypes.R1);
dijkstra.getCost();
transaction.commit();
}
}
use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.
the class DijkstraDirectionTest method testDijkstraDirection3.
// This saves the first direction observed
@Test
void testDijkstraDirection3() {
try (Transaction transaction = graphDb.beginTx()) {
Relationship r1 = graph.makeEdge(transaction, "start", "b");
Relationship r2 = graph.makeEdge(transaction, "c", "b");
Relationship r3 = graph.makeEdge(transaction, "c", "d");
Relationship r4 = graph.makeEdge(transaction, "e", "d");
Relationship r5 = graph.makeEdge(transaction, "e", "f");
Relationship r6 = graph.makeEdge(transaction, "g", "f");
Relationship r7 = graph.makeEdge(transaction, "g", "end");
Map<Relationship, Direction> dirs = new HashMap<>();
Dijkstra<Double> dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "start"), graph.getNode(transaction, "end"), new DirectionSavingCostEvaluator(dirs), new DoubleAdder(), Double::compareTo, Direction.BOTH, MyRelTypes.R1);
dijkstra.getCost();
assertEquals(Direction.OUTGOING, dirs.get(r1));
assertEquals(Direction.INCOMING, dirs.get(r2));
assertEquals(Direction.OUTGOING, dirs.get(r3));
assertEquals(Direction.INCOMING, dirs.get(r4));
assertEquals(Direction.OUTGOING, dirs.get(r5));
assertEquals(Direction.INCOMING, dirs.get(r6));
assertEquals(Direction.OUTGOING, dirs.get(r7));
transaction.commit();
}
}
use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.
the class DijkstraMultiplePathsTest method test7.
@Test
void test7() {
try (Transaction transaction = graphDb.beginTx()) {
Relationship edgeAB = graph.makeEdge(transaction, "a", "b");
Relationship edgeBC = graph.makeEdge(transaction, "b", "c");
Relationship edgeCD = graph.makeEdge(transaction, "c", "d");
Relationship edgeDE = graph.makeEdge(transaction, "d", "e");
Relationship edgeAB2 = graph.makeEdge(transaction, "a", "b2");
Relationship edgeB2C = graph.makeEdge(transaction, "b2", "c");
Relationship edgeCD2 = graph.makeEdge(transaction, "c", "d2");
Relationship edgeD2E = graph.makeEdge(transaction, "d2", "e");
Dijkstra<Double> dijkstra = new Dijkstra<>(0.0, graph.getNode(transaction, "a"), graph.getNode(transaction, "e"), (relationship, direction) -> 1.0, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
// path discovery flags
boolean pathBD = false;
boolean pathB2D = false;
boolean pathBD2 = false;
boolean pathB2D2 = false;
List<List<Entity>> paths = dijkstra.getPaths();
assertEquals(4, paths.size());
for (List<Entity> path : paths) {
assertEquals(9, path.size());
assertEquals(path.get(0), graph.getNode(transaction, "a"));
assertEquals(path.get(4), graph.getNode(transaction, "c"));
assertEquals(path.get(8), graph.getNode(transaction, "e"));
// first choice
if (path.get(2).equals(graph.getNode(transaction, "b"))) {
assertEquals(path.get(1), edgeAB);
assertEquals(path.get(3), edgeBC);
} else {
assertEquals(path.get(1), edgeAB2);
assertEquals(path.get(2), graph.getNode(transaction, "b2"));
assertEquals(path.get(3), edgeB2C);
}
// second choice
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
assertEquals(path.get(5), edgeCD);
assertEquals(path.get(7), edgeDE);
} else {
assertEquals(path.get(5), edgeCD2);
assertEquals(path.get(6), graph.getNode(transaction, "d2"));
assertEquals(path.get(7), edgeD2E);
}
// combinations
if (path.get(2).equals(graph.getNode(transaction, "b"))) {
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
pathBD = true;
} else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
pathBD2 = true;
}
} else {
if (path.get(6).equals(graph.getNode(transaction, "d"))) {
pathB2D = true;
} else if (path.get(6).equals(graph.getNode(transaction, "d2"))) {
pathB2D2 = true;
}
}
}
assertTrue(pathBD);
assertTrue(pathB2D);
assertTrue(pathBD2);
assertTrue(pathB2D2);
transaction.commit();
}
}
use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.
the class DijkstraMultiplePathsTest method test8.
@Test
void test8() {
try (Transaction transaction = graphDb.beginTx()) {
Relationship edgeAB = graph.makeEdge(transaction, "a", "b");
Relationship edgeBC = graph.makeEdge(transaction, "b", "c");
Relationship edgeCD = graph.makeEdge(transaction, "c", "d");
Relationship edgeDE = graph.makeEdge(transaction, "d", "e");
Relationship edgeAB2 = graph.makeEdge(transaction, "a", "b2");
Relationship edgeB2C = graph.makeEdge(transaction, "b2", "c");
Relationship edgeCD2 = graph.makeEdge(transaction, "c", "d2");
Relationship edgeD2E = graph.makeEdge(transaction, "d2", "e");
Dijkstra<Double> dijkstra = new Dijkstra<>(0.0, graph.getNode(transaction, "a"), graph.getNode(transaction, "e"), (relationship, direction) -> 1.0, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
// path discovery flags
boolean pathBD = false;
boolean pathB2D = false;
boolean pathBD2 = false;
boolean pathB2D2 = false;
List<List<Relationship>> paths = dijkstra.getPathsAsRelationships();
assertEquals(4, paths.size());
for (List<Relationship> path : paths) {
assertEquals(4, path.size());
// first choice
if (path.get(0).equals(edgeAB)) {
assertEquals(path.get(1), edgeBC);
} else {
assertEquals(path.get(0), edgeAB2);
assertEquals(path.get(1), edgeB2C);
}
// second choice
if (path.get(2).equals(edgeCD)) {
assertEquals(path.get(3), edgeDE);
} else {
assertEquals(path.get(2), edgeCD2);
assertEquals(path.get(3), edgeD2E);
}
// combinations
if (path.get(0).equals(edgeAB)) {
if (path.get(2).equals(edgeCD)) {
pathBD = true;
} else if (path.get(2).equals(edgeCD2)) {
pathBD2 = true;
}
} else {
if (path.get(2).equals(edgeCD)) {
pathB2D = true;
} else if (path.get(2).equals(edgeCD2)) {
pathB2D2 = true;
}
}
}
assertTrue(pathBD);
assertTrue(pathB2D);
assertTrue(pathBD2);
assertTrue(pathB2D2);
transaction.commit();
}
}
use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.
the class DijkstraMultiplePathsTest method test9.
/**
* Should generate three paths. The three paths must have the prefix: a, b, and c. The three paths must have the sufix: f and g.
* All the edges have cost 0.
*/
@Test
public void test9() {
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "a", "b", "cost", (double) 0);
graph.makeEdge(transaction, "b", "c", "cost", (double) 0);
graph.makeEdge(transaction, "c", "d", "cost", (double) 0);
graph.makeEdge(transaction, "d", "e", "cost", (double) 0);
graph.makeEdge(transaction, "e", "f", "cost", (double) 0);
graph.makeEdge(transaction, "f", "g", "cost", (double) 0);
graph.makeEdge(transaction, "d", "j", "cost", (double) 0);
graph.makeEdge(transaction, "j", "k", "cost", (double) 0);
graph.makeEdge(transaction, "k", "f", "cost", (double) 0);
graph.makeEdge(transaction, "c", "h", "cost", (double) 0);
graph.makeEdge(transaction, "h", "i", "cost", (double) 0);
graph.makeEdge(transaction, "i", "e", "cost", (double) 0);
Dijkstra<Double> dijkstra = new Dijkstra<>(0.0, graph.getNode(transaction, "a"), graph.getNode(transaction, "g"), (relationship, direction) -> .0, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
List<List<Node>> paths = dijkstra.getPathsAsNodes();
assertEquals(3, paths.size());
String[] commonPrefix = { "a", "b", "c" };
String[] commonSuffix = { "f", "g" };
for (List<Node> path : paths) {
/**
* Check if the prefixes are all correct.
*/
for (int j = 0; j < commonPrefix.length; j++) {
assertEquals(path.get(j), graph.getNode(transaction, commonPrefix[j]));
}
int pathSize = path.size();
/**
* Check if the suffixes are all correct.
*/
for (int j = 0; j < commonSuffix.length; j++) {
assertEquals(path.get(pathSize - j - 1), graph.getNode(transaction, commonSuffix[commonSuffix.length - j - 1]));
}
}
transaction.commit();
}
}
Aggregations