/** Test case for extracting paths */
 @Test
 public void testPath() {
   graph.makeEdge("a", "b", "cost", (double) 1);
   graph.makeEdge("b", "c", "cost", (double) 1);
   graph.makeEdge("c", "d", "cost", (double) 1);
   graph.makeEdge("d", "e", "cost", (double) 1);
   graph.makeEdge("e", "f", "cost", (double) 1);
   FloydWarshall<Double> floydWarshall =
       new FloydWarshall<Double>(
           0.0,
           Double.MAX_VALUE,
           Direction.OUTGOING,
           CommonEvaluators.doubleCostEvaluator("cost"),
           new org.neo4j.graphalgo.impl.util.DoubleAdder(),
           new org.neo4j.graphalgo.impl.util.DoubleComparator(),
           graph.getAllNodes(),
           graph.getAllEdges());
   List<Node> path = floydWarshall.getPath(graph.getNode("a"), graph.getNode("f"));
   assertTrue(path.size() == 6);
   assertTrue(path.get(0).equals(graph.getNode("a")));
   assertTrue(path.get(1).equals(graph.getNode("b")));
   assertTrue(path.get(2).equals(graph.getNode("c")));
   assertTrue(path.get(3).equals(graph.getNode("d")));
   assertTrue(path.get(4).equals(graph.getNode("e")));
   assertTrue(path.get(5).equals(graph.getNode("f")));
 }
예제 #2
0
  @Test
  public void canGetMultiplePathsInTriangleGraph() throws Exception {
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    Set<Relationship> expectedFirsts = new HashSet<Relationship>();
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d));
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d));
    Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2d);
    graph.makeEdge("A", "C", "length", 5d);

    Dijkstra algo =
        new Dijkstra(
            PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length"));

    Iterator<WeightedPath> paths = algo.findAllPaths(nodeA, nodeC).iterator();
    for (int i = 0; i < 2; i++) {
      assertTrue("expected more paths", paths.hasNext());
      Path path = paths.next();
      assertPath(path, nodeA, nodeB, nodeC);

      Iterator<Relationship> relationships = path.relationships().iterator();
      assertTrue("found shorter path than expected", relationships.hasNext());
      assertTrue(
          "path contained unexpected relationship", expectedFirsts.remove(relationships.next()));
      assertTrue("found shorter path than expected", relationships.hasNext());
      assertEquals(expectedSecond, relationships.next());
      assertFalse("found longer path than expected", relationships.hasNext());
    }
    assertFalse("expected at most two paths", paths.hasNext());
  }
예제 #3
0
    private PathFinder<? extends Path> getAlgorithm(
        String algorithm, RelationshipExpander expander, int maxDepth) {
      if (algorithm.equals("shortestPath")) {
        return GraphAlgoFactory.shortestPath(expander, maxDepth);
      } else if (algorithm.equals("allSimplePaths")) {
        return GraphAlgoFactory.allSimplePaths(expander, maxDepth);
      } else if (algorithm.equals("allPaths")) {
        return GraphAlgoFactory.allPaths(expander, maxDepth);
      } else if (algorithm.equals("dijkstra")) {
        String costProperty = (String) map.get("cost_property");
        Number defaultCost = (Number) map.get("default_cost");
        CostEvaluator<Double> costEvaluator =
            defaultCost == null
                ? CommonEvaluators.doubleCostEvaluator(costProperty)
                : CommonEvaluators.doubleCostEvaluator(costProperty, defaultCost.doubleValue());
        representationCreator = WEIGHTED_PATH_REPRESENTATION_CREATOR;
        return GraphAlgoFactory.dijkstra(expander, costEvaluator);
      }

      throw new RuntimeException("Failed to find matching algorithm");
    }
예제 #4
0
  @Test
  public void canGetPathsInTriangleGraph() throws Exception {
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3d);
    graph.makeEdge("A", "C", "length", 10d);

    Dijkstra algo =
        new Dijkstra(
            PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length"));

    Iterator<WeightedPath> paths = algo.findAllPaths(nodeA, nodeC).iterator();
    assertTrue("expected at least one path", paths.hasNext());
    assertPath(paths.next(), nodeA, nodeB, nodeC);
    assertFalse("expected at most one path", paths.hasNext());

    assertPath(algo.findSinglePath(nodeA, nodeC), nodeA, nodeB, nodeC);
  }
예제 #5
0
  @Test
  public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    Node nodeD = graph.makeNode("D");
    Node nodeE = graph.makeNode("E");
    Node nodeF = graph.makeNode("F");
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "C", "length", 2.5d);
    graph.makeEdge("C", "D", "length", 7.3d);
    graph.makeEdge("B", "D", "length", 2.5d);
    graph.makeEdge("D", "E", "length", 3d);
    graph.makeEdge("C", "E", "length", 5d);
    graph.makeEdge("E", "F", "length", 5d);
    graph.makeEdge("C", "F", "length", 12d);
    graph.makeEdge("A", "F", "length", 25d);

    Dijkstra algo =
        new Dijkstra(
            PathExpanders.allTypesAndDirections(), CommonEvaluators.doubleCostEvaluator("length"));

    // Try the search in both directions.
    for (Node[] nodes : new Node[][] {{nodeA, nodeF}, {nodeF, nodeA}}) {
      int found = 0;
      Iterator<WeightedPath> paths = algo.findAllPaths(nodes[0], nodes[1]).iterator();
      for (int i = 0; i < 2; i++) {
        assertTrue("expected more paths", paths.hasNext());
        Path path = paths.next();
        if (path.length() != found && path.length() == 3) {
          assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
        } else if (path.length() != found && path.length() == 4) {
          assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
        } else {
          fail("unexpected path length: " + path.length());
        }
        found = path.length();
      }
      assertFalse("expected at most two paths", paths.hasNext());
    }
  }
 /** Test case for paths of length 0 and 1, and an impossible path */
 @Test
 public void testMinimal() {
   graph.makeEdge("a", "b", "cost", (double) 1);
   graph.makeEdge("a", "c", "cost", (double) 1);
   graph.makeEdge("a", "d", "cost", (double) 1);
   graph.makeEdge("a", "e", "cost", (double) 1);
   graph.makeEdge("b", "c", "cost", (double) 1);
   graph.makeEdge("c", "d", "cost", (double) 1);
   graph.makeEdge("d", "e", "cost", (double) 1);
   graph.makeEdge("e", "b", "cost", (double) 1);
   FloydWarshall<Double> floydWarshall =
       new FloydWarshall<Double>(
           0.0,
           Double.MAX_VALUE,
           Direction.OUTGOING,
           CommonEvaluators.doubleCostEvaluator("cost"),
           new org.neo4j.graphalgo.impl.util.DoubleAdder(),
           new org.neo4j.graphalgo.impl.util.DoubleComparator(),
           graph.getAllNodes(),
           graph.getAllEdges());
   assertTrue(floydWarshall.getCost(graph.getNode("a"), graph.getNode("a")) == 0.0);
   assertTrue(floydWarshall.getCost(graph.getNode("a"), graph.getNode("b")) == 1.0);
   assertTrue(floydWarshall.getCost(graph.getNode("b"), graph.getNode("a")) == Double.MAX_VALUE);
 }