public static void main(String[] args) {
    DiGraph g = new DiGraph(13);
    g.addEdge(0, 5);
    g.addEdge(4, 3);
    g.addEdge(5, 4);
    g.addEdge(3, 5);
    CycleDetect detectCycle = new CycleDetect(g);

    Stack<Integer> st = (Stack<Integer>) detectCycle.cycle();
    while (!st.isEmpty()) {
      System.out.print(st.pop() + " ");
    }
  }
 public static void main(String args[]) {
   DiGraph g = new DiGraph(4);
   g.addEdge(0, 1);
   g.addEdge(0, 3);
   g.addEdge(1, 0);
   g.addEdge(1, 2);
   g.addEdge(2, 1);
   g.addEdge(2, 3);
   g.addEdge(3, 0);
   g.addEdge(3, 2);
   BipartiteGraph bipartiteGraph = new BipartiteGraph(g);
   if (bipartiteGraph.isBipartite(0)) System.out.println("Graph is Bipartite");
   else System.out.println("Graph is not Bipartite");
 }
Пример #3
0
  /**
   * Retorna un Digraph que es la clausura transitiva de este DiGraph calculada usando el algoritmo
   * Roy-Warshal.
   *
   * <p>Este metodo no altera este grafo <i>this</i>
   *
   * @return un Digraph que es la clausura transitiva de este DiGraph calculada usando el algoritmo
   *     Roy-Warshal
   */
  public DiGraph royWarshall() {
    DiGraph ret = null;

    ret = (DiGraph) this.clone();

    // Se agrega la identidad
    for (int i = 0; i < numNodes; ++i) {
      ret.addArc(i, i);
    }

    for (int k = 0; k < numNodes; ++k) {
      for (int i = 0; i < numNodes; ++i) {
        if ((i != k) && ret.isArc(i, k)) {
          for (int j = 0; j < numNodes; ++j) {
            if (ret.isArc(k, j)) {
              ret.addArc(i, j);
            }
          }
        }
      }
    }

    return ret;
  }
 public void dfs(DiGraph g, int s) {
   marked[s] = true;
   onStack[s] = true;
   for (int w : g.adj(s))
     if (this.hasCycle()) return;
     else if (!marked(w)) {
       edgeTo[w] = s;
       dfs(g, w);
     } else if (onStack[w]) {
       cycle = new Stack<Integer>();
       for (int x = s; x != w; x = edgeTo[x]) cycle.push(x);
       cycle.push(w);
       cycle.push(s);
     }
   onStack[s] = false;
 }
 BipartiteGraph(DiGraph diGraph) {
   adjList = diGraph.getAdjList();
   vertices = diGraph.getV();
 }
 public CycleDetect(DiGraph g) {
   marked = new boolean[g.V()];
   onStack = new boolean[g.V()];
   edgeTo = new int[g.V()];
   for (int v = 0; v < g.V(); v++) if (!marked(v)) dfs(g, v);
 }