Ejemplo n.º 1
0
  /** Testing that using path validator that accepts all requests finds full paths. */
  public void testAllowAll() {
    int size = 5;
    SimpleGraph<String, DefaultEdge> clique = buildCliqueGraph(size);
    for (int i = 0; i < size; i++) {
      KShortestPaths<String, DefaultEdge> ksp =
          new KShortestPaths<String, DefaultEdge>(
              clique,
              String.valueOf(i),
              30,
              Integer.MAX_VALUE,
              new PathValidator<String, DefaultEdge>() {

                @Override
                public boolean isValidPath(
                    AbstractPathElement<String, DefaultEdge> prevPathElement, DefaultEdge edge) {
                  // block all paths
                  return true;
                }
              });

      for (int j = 0; j < size; j++) {
        if (j == i) {
          continue;
        }
        List<GraphPath<String, DefaultEdge>> paths = ksp.getPaths(String.valueOf(j));
        assertNotNull(paths);
        assertEquals(16, paths.size());
      }
    }
  }
Ejemplo n.º 2
0
  /** Testing a ring with only single path allowed between two vertices. */
  public void testRing() {
    int size = 10;
    SimpleGraph<Integer, DefaultEdge> ring = buildRingGraph(size);
    for (int i = 0; i < size; i++) {
      KShortestPaths<Integer, DefaultEdge> ksp =
          new KShortestPaths<Integer, DefaultEdge>(
              ring,
              i,
              2,
              Integer.MAX_VALUE,
              new PathValidator<Integer, DefaultEdge>() {

                @Override
                public boolean isValidPath(
                    AbstractPathElement<Integer, DefaultEdge> prevPathElement, DefaultEdge edge) {
                  if (prevPathElement == null) {
                    return true;
                  }
                  return Math.abs(
                          prevPathElement.getVertex()
                              - Graphs.getOppositeVertex(ring, edge, prevPathElement.getVertex()))
                      == 1;
                }
              });

      for (int j = 0; j < size; j++) {
        if (j == i) {
          continue;
        }
        List<GraphPath<Integer, DefaultEdge>> paths = ksp.getPaths(j);
        assertNotNull(paths);
        assertEquals(1, paths.size());
      }
    }
  }
Ejemplo n.º 3
0
  /**
   * Testing a graph where the validator denies the request to go on an edge which cutting it makes
   * the graph disconnected
   */
  public void testDisconnected() {
    int cliqueSize = 5;
    // generate graph of two cliques connected by single edge
    SimpleGraph<Integer, DefaultEdge> graph = buildGraphForTestDisconnected(cliqueSize);
    for (int i = 0; i < graph.vertexSet().size(); i++) {
      KShortestPaths<Integer, DefaultEdge> ksp =
          new KShortestPaths<Integer, DefaultEdge>(
              graph,
              i,
              100,
              Integer.MAX_VALUE,
              new PathValidator<Integer, DefaultEdge>() {

                @Override
                public boolean isValidPath(
                    AbstractPathElement<Integer, DefaultEdge> prevPathElement, DefaultEdge edge) {
                  // accept all requests but the one to pass through the edge connecting
                  // the two cliques.
                  DefaultEdge connectingEdge = graph.getEdge(cliqueSize - 1, cliqueSize);
                  return connectingEdge != edge;
                }
              });

      for (int j = 0; j < graph.vertexSet().size(); j++) {
        if (j == i) {
          continue;
        }
        List<GraphPath<Integer, DefaultEdge>> paths = ksp.getPaths(j);
        if ((i < cliqueSize && j < cliqueSize) || (i >= cliqueSize && j >= cliqueSize)) {
          // within the clique - path should exist
          assertNotNull(paths);
          assertTrue(paths.size() > 0);
        } else {
          // else - should not
          assertNull(paths);
        }
      }
    }
  }