Search in sources :

Example 1 with DoubleAdder

use of org.neo4j.graphalgo.impl.util.DoubleAdder in project neo4j by neo4j.

the class ParallellCentralityCalculationTest method testPlusShape.

@Test
public void testPlusShape() {
    // Make graph
    graph.makeEdgeChain("a,b,c");
    graph.makeEdgeChain("d,b,e");
    SingleSourceShortestPath<Double> singleSourceShortestPath = getSingleSourceShortestPath();
    ParallellCentralityCalculation<Double> pcc = new ParallellCentralityCalculation<Double>(singleSourceShortestPath, graph.getAllNodes());
    BetweennessCentrality<Double> betweennessCentrality = new BetweennessCentrality<Double>(singleSourceShortestPath, graph.getAllNodes());
    StressCentrality<Double> stressCentrality = new StressCentrality<Double>(singleSourceShortestPath, graph.getAllNodes());
    ClosenessCentrality<Double> closenessCentrality = new ClosenessCentrality<Double>(singleSourceShortestPath, new DoubleAdder(), 0.0, graph.getAllNodes(), new CostDivider<Double>() {

        public Double divideByCost(Double d, Double c) {
            return d / c;
        }

        public Double divideCost(Double c, Double d) {
            return c / d;
        }
    });
    pcc.addCalculation(betweennessCentrality);
    pcc.addCalculation(stressCentrality);
    pcc.addCalculation(closenessCentrality);
    pcc.calculate();
    assertCentrality(betweennessCentrality, "a", 0.0);
    assertCentrality(betweennessCentrality, "b", 6.0);
    assertCentrality(betweennessCentrality, "c", 0.0);
    assertCentrality(betweennessCentrality, "d", 0.0);
    assertCentrality(betweennessCentrality, "e", 0.0);
    assertCentrality(stressCentrality, "a", 0.0);
    assertCentrality(stressCentrality, "b", 6.0);
    assertCentrality(stressCentrality, "c", 0.0);
    assertCentrality(stressCentrality, "d", 0.0);
    assertCentrality(stressCentrality, "e", 0.0);
    assertCentrality(closenessCentrality, "a", 1.0 / 7);
    assertCentrality(closenessCentrality, "b", 1.0 / 4);
    assertCentrality(closenessCentrality, "c", 1.0 / 7);
    assertCentrality(closenessCentrality, "d", 1.0 / 7);
    assertCentrality(closenessCentrality, "e", 1.0 / 7);
}
Also used : ClosenessCentrality(org.neo4j.graphalgo.impl.centrality.ClosenessCentrality) ParallellCentralityCalculation(org.neo4j.graphalgo.impl.centrality.ParallellCentralityCalculation) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) BetweennessCentrality(org.neo4j.graphalgo.impl.centrality.BetweennessCentrality) StressCentrality(org.neo4j.graphalgo.impl.centrality.StressCentrality) Test(org.junit.Test)

Example 2 with DoubleAdder

use of org.neo4j.graphalgo.impl.util.DoubleAdder in project neo4j by neo4j.

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 3 with DoubleAdder

use of org.neo4j.graphalgo.impl.util.DoubleAdder in project neo4j by neo4j.

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 4 with DoubleAdder

use of org.neo4j.graphalgo.impl.util.DoubleAdder 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 5 with DoubleAdder

use of org.neo4j.graphalgo.impl.util.DoubleAdder 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)

Aggregations

Test (org.junit.Test)6 DoubleAdder (org.neo4j.graphalgo.impl.util.DoubleAdder)6 List (java.util.List)4 Dijkstra (org.neo4j.graphalgo.impl.shortestpath.Dijkstra)4 DoubleComparator (org.neo4j.graphalgo.impl.util.DoubleComparator)4 Direction (org.neo4j.graphdb.Direction)4 Relationship (org.neo4j.graphdb.Relationship)4 BetweennessCentrality (org.neo4j.graphalgo.impl.centrality.BetweennessCentrality)2 ClosenessCentrality (org.neo4j.graphalgo.impl.centrality.ClosenessCentrality)2 ParallellCentralityCalculation (org.neo4j.graphalgo.impl.centrality.ParallellCentralityCalculation)2 StressCentrality (org.neo4j.graphalgo.impl.centrality.StressCentrality)2 PropertyContainer (org.neo4j.graphdb.PropertyContainer)2