use of org.neo4j.graphalgo.PathFinder in project neo4j by neo4j.
the class DijkstraTest method canFindNeighbourMultipleCorrectPaths.
@Test
public void canFindNeighbourMultipleCorrectPaths() {
/*
* - 1.0 -
* / \
* (A) - 1 - (B)
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
graph.makeEdge("A", "B", "length", 1.0);
graph.makeEdge("A", "B", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
assertPaths(finder.findAllPaths(nodeA, nodeB), "A,B", "A,B");
}
use of org.neo4j.graphalgo.PathFinder in project neo4j by neo4j.
the class DijkstraTest method canFindNeighbourMultipleIncorrectPaths.
@Test
public void canFindNeighbourMultipleIncorrectPaths() {
/*
* - 2.0 -
* / \
* (A) - 1 - (B)
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
graph.makeEdge("A", "B", "length", 2.0);
graph.makeEdge("A", "B", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeB).iterator();
assertTrue("Expect at least one path", paths.hasNext());
WeightedPath path = paths.next();
assertPath(path, nodeA, nodeB);
assertTrue("Expect weight 1", path.weight() == 1);
assertFalse("Expected at most one path", paths.hasNext());
}
use of org.neo4j.graphalgo.PathFinder in project neo4j by neo4j.
the class DijkstraTest method canGetMultiplePathsInTriangleGraph.
@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
/* NODE (NAME/INDEX)
* ==> (two relationships)
*
* (A/0) ====== 1 =====> (B/1)
* \ /
* - 5 -> (C/2) <- 2 -
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
Node nodeC = graph.makeNode("C");
Set<Relationship> expectedFirsts = new HashSet<Relationship>();
expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d));
expectedFirsts.add(graph.makeEdge("A", "B", "length", 1));
Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2L);
graph.makeEdge("A", "C", "length", 5d);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
for (int i = 0; i < 2; i++) {
assertTrue("expected more paths", paths.hasNext());
Path path = paths.next();
assertPath(path, nodeA, nodeB, nodeC);
Iterator<Relationship> relationships = path.relationships().iterator();
assertTrue("found shorter path than expected", relationships.hasNext());
assertTrue("path contained unexpected relationship", expectedFirsts.remove(relationships.next()));
assertTrue("found shorter path than expected", relationships.hasNext());
assertEquals(expectedSecond, relationships.next());
assertFalse("found longer path than expected", relationships.hasNext());
}
assertFalse("expected at most two paths", paths.hasNext());
}
use of org.neo4j.graphalgo.PathFinder in project neo4j by neo4j.
the class DijkstraTest method shouldOnlyFindTheShortestPaths.
@Test
public void shouldOnlyFindTheShortestPaths() {
/*
*
* ----- (e) - 1 - (f) ---
* / \
* / ------- (a) -------- \
* 1 / \ \ 2
* | 2 0 0 |
* | / \ \|
* (s) - 1 - (c) - 1 - (d) - 1 - (t)
* \ /
* -- 1 - (b) - 1 -
*
*/
Node s = graph.makeNode("s");
Node t = graph.makeNode("t");
Node a = graph.makeNode("a");
Node b = graph.makeNode("b");
Node c = graph.makeNode("c");
graph.makeEdgeChain("s,e,f", "length", 1.0);
graph.makeEdge("f", "t", "length", 2);
graph.makeEdge("s", "a", "length", 2);
graph.makeEdge("a", "t", "length", 0);
graph.makeEdge("s", "c", "length", 1);
graph.makeEdge("c", "d", "length", 1);
graph.makeEdge("s", "b", "length", 1);
graph.makeEdge("b", "d", "length", 1);
graph.makeEdge("d", "a", "length", 0);
graph.makeEdge("d", "t", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(s, t).iterator();
for (int i = 1; i <= 3; i++) {
assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
assertTrue("Expected 3 paths of cost 2", NoneStrictMath.equals(paths.next().weight(), 2));
}
assertFalse("Expected exactly 3 paths", paths.hasNext());
}
use of org.neo4j.graphalgo.PathFinder in project neo4j by neo4j.
the class DijkstraTest method canKeepSearchingUntilFoundTrueShortest.
@Test
public void canKeepSearchingUntilFoundTrueShortest() {
/*
*
* 1 - (B) - 1 - (C) - 1 - (D) - 1 - (E) - 1
* | |
* (A) --- 1 --- (G) -- 2 -- (H) --- 1 --- (F)
*
*/
Node a = graph.makeNode("A");
Node b = graph.makeNode("B");
Node c = graph.makeNode("C");
Node d = graph.makeNode("D");
Node e = graph.makeNode("E");
Node f = graph.makeNode("F");
Node g = graph.makeNode("G");
Node h = graph.makeNode("H");
graph.makeEdgeChain("A,B,C,D,E,F", "length", 1);
graph.makeEdge("A", "G", "length", 1);
graph.makeEdge("G", "H", "length", 2);
graph.makeEdge("H", "F", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(a, f).iterator();
assertTrue("Expect at least one path", paths.hasNext());
WeightedPath path = paths.next();
assertPath(path, a, g, h, f);
assertTrue("Expect weight 1", path.weight() == 4);
assertFalse("Expected at most one path", paths.hasNext());
}
Aggregations