Search in sources :

Example 6 with Dijkstra

use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project graphdb by neo4j-attic.

the class DijkstraMultiplePathsTest method test7.

@Test
public void test7() {
    Relationship edgeAB = graph.makeEdge("a", "b");
    Relationship edgeBC = graph.makeEdge("b", "c");
    Relationship edgeCD = graph.makeEdge("c", "d");
    Relationship edgeDE = graph.makeEdge("d", "e");
    Relationship edgeAB2 = graph.makeEdge("a", "b2");
    Relationship edgeB2C = graph.makeEdge("b2", "c");
    Relationship edgeCD2 = graph.makeEdge("c", "d2");
    Relationship edgeD2E = graph.makeEdge("d2", "e");
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("e"), new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, new DoubleAdder(), new DoubleComparator(), Direction.OUTGOING, MyRelTypes.R1);
    // path discovery flags
    boolean pathBD = false;
    boolean pathB2D = false;
    boolean pathBD2 = false;
    boolean pathB2D2 = false;
    List<List<PropertyContainer>> paths = dijkstra.getPaths();
    assertTrue(paths.size() == 4);
    for (List<PropertyContainer> path : paths) {
        assertTrue(path.size() == 9);
        assertTrue(path.get(0).equals(graph.getNode("a")));
        assertTrue(path.get(4).equals(graph.getNode("c")));
        assertTrue(path.get(8).equals(graph.getNode("e")));
        // first choice
        if (path.get(2).equals(graph.getNode("b"))) {
            assertTrue(path.get(1).equals(edgeAB));
            assertTrue(path.get(3).equals(edgeBC));
        } else {
            assertTrue(path.get(1).equals(edgeAB2));
            assertTrue(path.get(2).equals(graph.getNode("b2")));
            assertTrue(path.get(3).equals(edgeB2C));
        }
        // second choice
        if (path.get(6).equals(graph.getNode("d"))) {
            assertTrue(path.get(5).equals(edgeCD));
            assertTrue(path.get(7).equals(edgeDE));
        } else {
            assertTrue(path.get(5).equals(edgeCD2));
            assertTrue(path.get(6).equals(graph.getNode("d2")));
            assertTrue(path.get(7).equals(edgeD2E));
        }
        // combinations
        if (path.get(2).equals(graph.getNode("b"))) {
            if (path.get(6).equals(graph.getNode("d"))) {
                pathBD = true;
            } else if (path.get(6).equals(graph.getNode("d2"))) {
                pathBD2 = true;
            }
        } else {
            if (path.get(6).equals(graph.getNode("d"))) {
                pathB2D = true;
            } else if (path.get(6).equals(graph.getNode("d2"))) {
                pathB2D2 = true;
            }
        }
    }
    assertTrue(pathBD);
    assertTrue(pathB2D);
    assertTrue(pathBD2);
    assertTrue(pathB2D2);
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) Direction(org.neo4j.graphdb.Direction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Relationship(org.neo4j.graphdb.Relationship) List(java.util.List) Test(org.junit.Test)

Example 7 with Dijkstra

use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project graphdb by neo4j-attic.

the class DijkstraMultiplePathsTest method test8.

@Test
public void test8() {
    Relationship edgeAB = graph.makeEdge("a", "b");
    Relationship edgeBC = graph.makeEdge("b", "c");
    Relationship edgeCD = graph.makeEdge("c", "d");
    Relationship edgeDE = graph.makeEdge("d", "e");
    Relationship edgeAB2 = graph.makeEdge("a", "b2");
    Relationship edgeB2C = graph.makeEdge("b2", "c");
    Relationship edgeCD2 = graph.makeEdge("c", "d2");
    Relationship edgeD2E = graph.makeEdge("d2", "e");
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("e"), new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, new DoubleAdder(), new DoubleComparator(), 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();
    assertTrue(paths.size() == 4);
    for (List<Relationship> path : paths) {
        assertTrue(path.size() == 4);
        // first choice
        if (path.get(0).equals(edgeAB)) {
            assertTrue(path.get(1).equals(edgeBC));
        } else {
            assertTrue(path.get(0).equals(edgeAB2));
            assertTrue(path.get(1).equals(edgeB2C));
        }
        // second choice
        if (path.get(2).equals(edgeCD)) {
            assertTrue(path.get(3).equals(edgeDE));
        } else {
            assertTrue(path.get(2).equals(edgeCD2));
            assertTrue(path.get(3).equals(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);
}
Also used : DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Relationship(org.neo4j.graphdb.Relationship) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) List(java.util.List) Direction(org.neo4j.graphdb.Direction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Test(org.junit.Test)

Example 8 with Dijkstra

use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project graphdb by neo4j-attic.

the class DijkstraDirectionTest method testDijkstraDirection3.

@Test
public void testDijkstraDirection3() {
    Relationship r1 = graph.makeEdge("start", "b");
    Relationship r2 = graph.makeEdge("c", "b");
    Relationship r3 = graph.makeEdge("c", "d");
    Relationship r4 = graph.makeEdge("e", "d");
    Relationship r5 = graph.makeEdge("e", "f");
    Relationship r6 = graph.makeEdge("g", "f");
    Relationship r7 = graph.makeEdge("g", "end");
    HashMap<Relationship, Direction> dirs = new HashMap<Relationship, Direction>();
    Dijkstra<Double> dijkstra = new Dijkstra<Double>((double) 0, graph.getNode("start"), graph.getNode("end"), new directionSavingCostEvaluator(dirs), new org.neo4j.graphalgo.impl.util.DoubleAdder(), new org.neo4j.graphalgo.impl.util.DoubleComparator(), 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));
}
Also used : HashMap(java.util.HashMap) Direction(org.neo4j.graphdb.Direction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 9 with Dijkstra

use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project graphdb by neo4j-attic.

the class DijkstraMultiplePathsTest method test6.

@Test
public void test6() {
    graph.makeEdgeChain("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,z", "cost", (double) 1);
    graph.makeEdge("a", "b2", "cost", (double) 4);
    graph.makeEdge("b2", "c", "cost", (double) -2);
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("z"), CommonEvaluators.doubleCostEvaluator("cost"), new org.neo4j.graphalgo.impl.util.DoubleAdder(), new org.neo4j.graphalgo.impl.util.DoubleComparator(), Direction.OUTGOING, MyRelTypes.R1);
    List<List<Node>> paths = dijkstra.getPathsAsNodes();
    assertTrue(paths.size() == 2);
}
Also used : DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) List(java.util.List) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Test(org.junit.Test)

Example 10 with Dijkstra

use of org.neo4j.graphalgo.impl.shortestpath.Dijkstra in project neo4j by neo4j.

the class DijkstraDirectionTest method testDijkstraDirection2.

@Test
void testDijkstraDirection2() {
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "a", "b");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "c", "d");
        graph.makeEdge(transaction, "d", "a");
        graph.makeEdge(transaction, "s", "a");
        graph.makeEdge(transaction, "b", "s");
        graph.makeEdge(transaction, "e", "c");
        graph.makeEdge(transaction, "d", "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();
    }
}
Also used : Test(org.junit.jupiter.api.Test) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Relationship(org.neo4j.graphdb.Relationship) Map(java.util.Map) Direction(org.neo4j.graphdb.Direction) HashMap(java.util.HashMap) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) Transaction(org.neo4j.graphdb.Transaction) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Transaction(org.neo4j.graphdb.Transaction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) Test(org.junit.jupiter.api.Test)

Aggregations

Dijkstra (org.neo4j.graphalgo.impl.shortestpath.Dijkstra)11 DoubleAdder (org.neo4j.graphalgo.impl.util.DoubleAdder)10 Relationship (org.neo4j.graphdb.Relationship)8 List (java.util.List)7 Test (org.junit.jupiter.api.Test)7 Transaction (org.neo4j.graphdb.Transaction)7 Direction (org.neo4j.graphdb.Direction)6 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DoubleComparator (org.neo4j.graphalgo.impl.util.DoubleComparator)3 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)2 Map (java.util.Map)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 CostEvaluator (org.neo4j.graphalgo.CostEvaluator)2 Entity (org.neo4j.graphdb.Entity)1 Node (org.neo4j.graphdb.Node)1 PropertyContainer (org.neo4j.graphdb.PropertyContainer)1