コード例 #1
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);
        }
      }
    }
  }