Example #1
0
  private static List<Edge> createPath(MyBreadthFirstIterator iter, Object endVertex) {
    List<Edge> path = new ArrayList<Edge>();

    while (true) {
      Edge edge = iter.getSpanningTreeEdge(endVertex);

      if (edge == null) {
        break;
      }

      path.add(edge);
      endVertex = edge.oppositeVertex(endVertex);
    }

    Collections.reverse(path);

    return path;
  }
Example #2
0
  private List lazyFindBiconnectedSets() {

    if (biconnectedSets == null) {
      biconnectedSets = new ArrayList();

      Iterator connectedSets = new ConnectivityInspector(graph).connectedSets().iterator();

      while (connectedSets.hasNext()) {
        Set connectedSet = (Set) connectedSets.next();
        if (connectedSet.size() == 1) {
          continue;
        }

        Graph subgraph = new Subgraph(graph, connectedSet, null);

        // do DFS

        // Stack for the DFS
        Stack vertexStack = new Stack();

        Set visitedVertices = new HashSet();
        Map parent = new HashMap();
        List dfsVertices = new ArrayList();

        Set treeEdges = new HashSet();

        Object currentVertex = subgraph.vertexSet().toArray()[0];

        vertexStack.push(currentVertex);
        visitedVertices.add(currentVertex);

        while (!vertexStack.isEmpty()) {

          currentVertex = vertexStack.pop();

          Object parentVertex = parent.get(currentVertex);

          if (parentVertex != null) {
            Edge edge = subgraph.getEdge(parentVertex, currentVertex);

            // tree edge
            treeEdges.add(edge);
          }

          visitedVertices.add(currentVertex);

          dfsVertices.add(currentVertex);

          Iterator edges = subgraph.edgesOf(currentVertex).iterator();
          while (edges.hasNext()) {
            // find a neighbour vertex of the current vertex
            Edge edge = (Edge) edges.next();

            if (!treeEdges.contains(edge)) {

              Object nextVertex = edge.oppositeVertex(currentVertex);

              if (!visitedVertices.contains(nextVertex)) {

                vertexStack.push(nextVertex);

                parent.put(nextVertex, currentVertex);

              } else {
                // non-tree edge
              }
            }
          }
        }

        // DFS is finished. Now create the auxiliary graph h
        // Add all the tree edges as vertices in h
        SimpleGraph h = new SimpleGraph();

        h.addAllVertices(treeEdges);

        visitedVertices.clear();

        Set connected = new HashSet();

        for (Iterator it = dfsVertices.iterator(); it.hasNext(); ) {
          Object v = it.next();

          visitedVertices.add(v);

          // find all adjacent non-tree edges
          for (Iterator adjacentEdges = subgraph.edgesOf(v).iterator(); adjacentEdges.hasNext(); ) {
            Edge l = (Edge) adjacentEdges.next();
            if (!treeEdges.contains(l)) {
              h.addVertex(l);
              Object u = l.oppositeVertex(v);

              // we need to check if (u,v) is a back-edge
              if (!visitedVertices.contains(u)) {

                while (u != v) {
                  Object pu = parent.get(u);
                  Edge f = subgraph.getEdge(u, pu);

                  h.addEdge(f, l);

                  if (!connected.contains(f)) {
                    connected.add(f);
                    u = pu;
                  } else {
                    u = v;
                  }
                }
              }
            }
          }
        }

        ConnectivityInspector connectivityInspector = new ConnectivityInspector(h);

        biconnectedSets.addAll(connectivityInspector.connectedSets());
      }
    }

    return biconnectedSets;
  }