@Override
  public TraversalGraph<V, E> reconstructTraversalGraph() {

    if (currentStartNode == null) {
      throw new IllegalStateException(
          "You must call #calculate before " + "reconstructing the traversal graph.");
    }

    TraversalGraph<V, E> traversalGraph =
        new TraversalGraph<V, E>(graph.getEdgeFactory(), currentStartNode);
    for (V v : graph.vertexSet()) {
      Set<E> predEdges = (Set<E>) v.getPredecessorEdges();
      for (E e : predEdges) {
        V source = graph.getEdgeSource(e);
        V target = graph.getEdgeTarget(e);
        traversalGraph.addVertex(source);
        traversalGraph.addVertex(target);
        if (v.equals(source)) {
          traversalGraph.addEdge(target, source).setBaseGraphEdge(e);
        } else if (v.equals(target)) {
          traversalGraph.addEdge(source, target).setBaseGraphEdge(e);
        } else {
          throw new IllegalStateException(
              "A vertex has a predecessor " + "edge not ending on itself.");
        }
      }
    }

    return traversalGraph;
  }
示例#2
0
  @Test
  public void testBFSFromVertexEight() {

    bfs.calculate(v8);

    assertTrue(graph.getVertex(8).getDistance() == 0);
    assertTrue(graph.getVertex(5).getDistance() == 1);
    assertTrue(graph.getVertex(6).getDistance() == 1);
    assertTrue(graph.getVertex(7).getDistance() == 1);
    assertTrue(graph.getVertex(4).getDistance() == 2);
    assertTrue(graph.getVertex(3).getDistance() == 3);
    assertTrue(graph.getVertex(2).getDistance() == 4);
    assertTrue(graph.getVertex(1).getDistance() == 5);

    sPT = bfs.reconstructTraversalGraph();
    assertTrue(sPT.getRoot().equals(v8));
    assertTrue(sPT.edgeSet().size() == 8);
    assertTrue(sPT.containsEdge(v8, v7));
    assertTrue(sPT.containsEdge(v8, v5));
    assertTrue(sPT.containsEdge(v5, v4));
    assertTrue(sPT.containsEdge(v8, v6));
    assertTrue(sPT.containsEdge(v6, v4));
    assertTrue(sPT.containsEdge(v4, v3));
    assertTrue(sPT.containsEdge(v3, v2));
    assertTrue(sPT.containsEdge(v2, v1));
  }