Example #1
0
  public static int[] dfs(final IGraph g, final int vertex) {
    final int len = g.getNumVertices();
    pred = new int[len];
    color = new int[len];

    for (int i = 0; i < len; i++) {
      pred[i] = i;
      color[i] = WHITE;
    }

    visit(g, vertex);

    for (final int unvisited : g.getVertices()) {
      if (WHITE != color[unvisited]) {
        continue;
      }
      visit(g, unvisited);
    }

    return pred;
  }
Example #2
0
  private static void visit(final IGraph g, final int vertex) {
    color[vertex] = GREY;

    for (final int neighbor : g.getNeighbors(vertex)) {
      if (WHITE != color[neighbor]) {
        continue;
      }
      pred[neighbor] = vertex;
      visit(g, neighbor);
    }

    color[vertex] = BLACK;
  }