Search in sources :

Example 1 with EigenvectorCentrality

use of org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality in project neo4j by neo4j.

the class EigenvectorCentralityTest method testWeight.

/**
     * Some weighted relationships.
     */
@Test
public void testWeight() {
    /*
         * Layout
         *      ------- 0.1 ---------
         *     /                     \
         *    /            --- 0.1 ---
         *   v            v           \
         * (a) - 1.0 -> (b) - 1.0 -> (c) - 1.0 -> (d)
         */
    graph.makeEdgeChain("a,b", "cost", 1.0);
    graph.makeEdgeChain("b,c", "cost", 1.0);
    graph.makeEdgeChain("c,d", "cost", 1.0);
    graph.makeEdgeChain("c,b", "cost", 0.1);
    graph.makeEdgeChain("c,a", "cost", 0.1);
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.OUTGOING, CommonEvaluators.doubleCostEvaluator("cost"), graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.02);
}
Also used : EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Test(org.junit.Test)

Example 2 with EigenvectorCentrality

use of org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality in project neo4j by neo4j.

the class EigenvectorCentralityTest method shouldHandleTargetNodeBeingOrphan.

@Test
public void shouldHandleTargetNodeBeingOrphan() {
    graph.makeNode("o");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {

        @Override
        public Double getCost(Relationship relationship, Direction direction) {
            return 1d;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "o", 0d, 0.02);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 3 with EigenvectorCentrality

use of org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality in project neo4j by neo4j.

the class EigenvectorCentralityTest method testWeightAndDirection.

/**
     * Same network as above, but with direction BOTH and weights in different
     * directions are given by a map.
     */
@Test
public void testWeightAndDirection() {
    graph.makeEdgeChain("a,b");
    graph.makeEdgeChain("b,c");
    graph.makeEdgeChain("c,d");
    graph.makeEdgeChain("c,a");
    final Map<String, Double> costs = new HashMap<String, Double>();
    costs.put("a,b", 1.0);
    costs.put("b,c", 1.0);
    costs.put("c,d", 1.0);
    costs.put("c,b", 0.1);
    costs.put("c,a", 0.1);
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            String start = graph.getNodeId(relationship.getStartNode());
            String end = graph.getNodeId(relationship.getEndNode());
            if (direction == Direction.INCOMING) {
                // swap
                String tmp = end;
                end = start;
                start = tmp;
            }
            Double value = costs.get(start + "," + end);
            if (value == null) {
                return 0.0;
            }
            return value;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.02);
}
Also used : HashMap(java.util.HashMap) Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 4 with EigenvectorCentrality

use of org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality in project neo4j by neo4j.

the class EigenvectorCentralityTest method testDirection.

/**
     * Same as above, but inverted direction.
     */
@Test
public void testDirection() {
    graph.makeEdgeChain("d,c,b,a");
    graph.makeEdges("a,b,a,c");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.INCOMING, new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.693, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.523, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.395, 0.02);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.298, 0.02);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test)

Example 5 with EigenvectorCentrality

use of org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality in project graphdb by neo4j-attic.

the class EigenvectorCentralityTest method testWeight.

/**
     * Some weighted relationships.
     */
@Test
public void testWeight() {
    graph.makeEdgeChain("a,b", "cost", 1.0);
    graph.makeEdgeChain("b,c", "cost", 1.0);
    graph.makeEdgeChain("c,d", "cost", 1.0);
    graph.makeEdgeChain("c,b", "cost", 0.1);
    graph.makeEdgeChain("c,a", "cost", 0.1);
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.OUTGOING, CommonEvaluators.doubleCostEvaluator("cost"), graph.getAllNodes(), graph.getAllEdges(), 0.01);
    // eigenvectorCentrality.setMaxIterations( 100 );
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.01);
}
Also used : EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 EigenvectorCentrality (org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality)12 Direction (org.neo4j.graphdb.Direction)10 Relationship (org.neo4j.graphdb.Relationship)10 HashSet (java.util.HashSet)3 Node (org.neo4j.graphdb.Node)3 HashMap (java.util.HashMap)2