public static void main(String[] args) {

    // create random DAG with V vertices and E edges; then add F random edges
    int V = Integer.parseInt(args[0]);
    int E = Integer.parseInt(args[1]);
    int F = Integer.parseInt(args[2]);
    EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < E; i++) {
      int v, w;
      do {
        v = StdRandom.uniform(V);
        w = StdRandom.uniform(V);
      } while (v >= w);
      double weight = Math.random();
      G.addEdge(new DirectedEdge(v, w, weight));
    }

    // add F extra edges
    for (int i = 0; i < F; i++) {
      int v = (int) (Math.random() * V);
      int w = (int) (Math.random() * V);
      double weight = Math.random();
      G.addEdge(new DirectedEdge(v, w, weight));
    }

    StdOut.println(G);

    // find a directed cycle
    EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);
    if (finder.hasCycle()) {
      StdOut.print("Cycle: ");
      for (DirectedEdge e : finder.cycle()) {
        StdOut.print(e + " ");
      }
      StdOut.println();
    }

    // or give topologial sort
    else {
      StdOut.println("No directed cycle");
    }
  }
Example #2
0
  public static void main(String[] args) {
    int BYTES_PER_LINE = 16;
    if (args.length == 1) {
      BYTES_PER_LINE = Integer.parseInt(args[0]);
    }

    int i;
    for (i = 0; !BinaryStdIn.isEmpty(); i++) {
      if (BYTES_PER_LINE == 0) {
        BinaryStdIn.readChar();
        continue;
      }
      if (i == 0) StdOut.printf("");
      else if (i % BYTES_PER_LINE == 0) StdOut.printf("\n", i);
      else StdOut.print(" ");
      char c = BinaryStdIn.readChar();
      StdOut.printf("%02x", c & 0xff);
    }
    if (BYTES_PER_LINE != 0) StdOut.println();
    StdOut.println((i * 8) + " bits");
  }