Search in sources :

Example 11 with EigenvectorCentrality

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

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);
    // 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 : 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 12 with EigenvectorCentrality

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

the class EigenvectorCentralityTest method testRun.

@Test
public void testRun() {
    graph.makeEdgeChain("a,b,c,d");
    graph.makeEdges("b,a,c,a");
    EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.OUTGOING, new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, graph.getAllNodes(), graph.getAllEdges(), 0.01);
    // eigenvectorCentrality.setMaxIterations( 100 );
    assertApproximateCentrality(eigenvectorCentrality, "a", 0.693, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "b", 0.523, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "c", 0.395, 0.01);
    assertApproximateCentrality(eigenvectorCentrality, "d", 0.298, 0.01);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) EigenvectorCentrality(org.neo4j.graphalgo.impl.centrality.EigenvectorCentrality) Direction(org.neo4j.graphdb.Direction) 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