Search in sources :

Example 91 with Relationship

use of org.neo4j.graphdb.Relationship 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 92 with Relationship

use of org.neo4j.graphdb.Relationship 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 93 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class StressCentrality method getAndUpdateNodeStress.

/**
     * This recursively updates the node stress (number of paths through a
     * node).
     * @param node
     *            The start node
     * @param skipFirstNode
     *            If true, the start node is not updated. Useful, since the
     *            first node in any path doesnt need to be updated.
     * @param successors
     * @param counter
     *            Object that can return the number of paths from the initial
     *            start node to any node.
     * @param stresses
     *            A map used to limit the recursion where possible (dynamic
     *            programming)
     * @return
     */
protected Double getAndUpdateNodeStress(Node node, boolean skipFirstNode, Map<Node, List<Relationship>> successors, PathCounter counter, Map<Node, Double> stresses) {
    Double stress = stresses.get(node);
    if (stress != null) {
        return stress;
    }
    stress = (double) 0;
    List<Relationship> succs = successors.get(node);
    if (succs == null || succs.size() == 0) {
        return (double) 0;
    }
    for (Relationship relationship : succs) {
        Node otherNode = relationship.getOtherNode(node);
        Double otherStress = getAndUpdateNodeStress(otherNode, false, successors, counter, stresses);
        stress += (otherStress + 1) * counter.getNumberOfPathsToNode(node);
    }
    if (!skipFirstNode) {
        stresses.put(node, stress);
        // When adding to the final result (and only then), take the global
        // factor into account.
        addCentralityToNode(node, stress * globalFactor);
    }
    return stress;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node)

Example 94 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 95 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class ShortestPath method hitsToPaths.

private static Iterable<Path> hitsToPaths(Collection<Hit> depthHits, Node start, Node end) {
    Collection<Path> paths = new ArrayList<Path>();
    for (Hit hit : depthHits) {
        Iterable<LinkedList<Relationship>> startPaths = getPaths(hit, hit.start);
        Iterable<LinkedList<Relationship>> endPaths = getPaths(hit, hit.end);
        for (LinkedList<Relationship> startPath : startPaths) {
            PathImpl.Builder startBuilder = toBuilder(start, startPath);
            for (LinkedList<Relationship> endPath : endPaths) {
                PathImpl.Builder endBuilder = toBuilder(end, endPath);
                Path path = startBuilder.build(endBuilder);
                paths.add(path);
            }
        }
    }
    return paths;
}
Also used : Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) ArrayList(java.util.ArrayList) PathImpl(org.neo4j.graphalgo.impl.util.PathImpl) LinkedList(java.util.LinkedList) Builder(org.neo4j.graphalgo.impl.util.PathImpl.Builder)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)490 Node (org.neo4j.graphdb.Node)370 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 LinkedList (java.util.LinkedList)48 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 RelationshipType (org.neo4j.graphdb.RelationshipType)38 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)26 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 Map (java.util.Map)14 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11