Search in sources :

Example 1 with SingleSourceShortestPathDijkstra

use of org.neo4j.graphalgo.impl.shortestpath.SingleSourceShortestPathDijkstra in project neo4j-apoc-procedures by neo4j-contrib.

the class Centrality method closeness.

@Deprecated
@Procedure("apoc.algo.closeness")
@Description("CALL apoc.algo.closeness(['TYPE',...],nodes, INCOMING) YIELD node, score - calculate closeness " + "centrality for given nodes")
public Stream<NodeScore> closeness(@Name("types") List<String> types, @Name("nodes") List<Node> nodes, @Name("direction") String direction) {
    assertParametersNotNull(types, nodes);
    try {
        RelationshipType[] relationshipTypes = types.isEmpty() ? Util.allRelationshipTypes(db) : Util.toRelTypes(types);
        SingleSourceShortestPath<Double> sssp = new SingleSourceShortestPathDijkstra<>(0.0, null, (relationship, dir) -> 1.0, new DoubleAdder(), new DoubleComparator(), Util.parseDirection(direction), relationshipTypes);
        ClosenessCentrality<Double> closenessCentrality = new ClosenessCentrality<>(sssp, new DoubleAdder(), 0.0, new HashSet<>(nodes), new CostDivider<Double>() {

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

            @Override
            public Double divideCost(Double c, Double d) {
                return c / d;
            }
        });
        return nodes.stream().map(node -> new NodeScore(node, closenessCentrality.getCentrality(node)));
    } catch (Exception e) {
        String errMsg = "Error encountered while calculating centrality";
        log.error(errMsg, e);
        throw new RuntimeException(errMsg, e);
    }
}
Also used : ClosenessCentrality(org.neo4j.graphalgo.impl.centrality.ClosenessCentrality) RelationshipType(org.neo4j.graphdb.RelationshipType) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) SingleSourceShortestPathDijkstra(org.neo4j.graphalgo.impl.shortestpath.SingleSourceShortestPathDijkstra) NodeScore(apoc.result.NodeScore)

Example 2 with SingleSourceShortestPathDijkstra

use of org.neo4j.graphalgo.impl.shortestpath.SingleSourceShortestPathDijkstra in project neo4j-apoc-procedures by neo4j-contrib.

the class Centrality method betweenness.

@Deprecated
@Procedure("apoc.algo.betweenness")
@Description("CALL apoc.algo.betweenness(['TYPE',...],nodes,BOTH) YIELD node, score - calculate betweenness " + "centrality for given nodes")
public Stream<NodeScore> betweenness(@Name("types") List<String> types, @Name("nodes") List<Node> nodes, @Name("direction") String direction) {
    assertParametersNotNull(types, nodes);
    try {
        RelationshipType[] relationshipTypes = types.isEmpty() ? Util.allRelationshipTypes(db) : Util.toRelTypes(types);
        SingleSourceShortestPath<Double> sssp = new SingleSourceShortestPathDijkstra<>(0.0, null, (relationship, dir) -> 1.0, new DoubleAdder(), new DoubleComparator(), Util.parseDirection(direction), relationshipTypes);
        BetweennessCentrality<Double> betweennessCentrality = new BetweennessCentrality<>(sssp, new HashSet<>(nodes));
        return nodes.stream().map(node -> new NodeScore(node, betweennessCentrality.getCentrality(node)));
    } catch (Exception e) {
        String errMsg = "Error encountered while calculating centrality";
        log.error(errMsg, e);
        throw new RuntimeException(errMsg, e);
    }
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) BetweennessCentrality(org.neo4j.graphalgo.impl.centrality.BetweennessCentrality) SingleSourceShortestPathDijkstra(org.neo4j.graphalgo.impl.shortestpath.SingleSourceShortestPathDijkstra) NodeScore(apoc.result.NodeScore)

Aggregations

NodeScore (apoc.result.NodeScore)2 SingleSourceShortestPathDijkstra (org.neo4j.graphalgo.impl.shortestpath.SingleSourceShortestPathDijkstra)2 DoubleAdder (org.neo4j.graphalgo.impl.util.DoubleAdder)2 DoubleComparator (org.neo4j.graphalgo.impl.util.DoubleComparator)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 BetweennessCentrality (org.neo4j.graphalgo.impl.centrality.BetweennessCentrality)1 ClosenessCentrality (org.neo4j.graphalgo.impl.centrality.ClosenessCentrality)1