Пример #1
0
  /** Unit tests the <tt>GabowSCC</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);
    GabowSCC scc = new GabowSCC(G);

    // number of connected components
    int M = scc.count();
    StdOut.println(M + " components");

    // compute list of vertices in each strong component
    Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M];
    for (int i = 0; i < M; i++) {
      components[i] = new Queue<Integer>();
    }
    for (int v = 0; v < G.V(); v++) {
      components[scc.id(v)].enqueue(v);
    }

    // print results
    for (int i = 0; i < M; i++) {
      for (int v : components[i]) {
        StdOut.print(v + " ");
      }
      StdOut.println();
    }
  }
Пример #2
0
  public static void main(String[] args) {
    Integer[] a = new Integer[20];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (StdRandom.uniform() * 100);
      StdOut.print(a[i] + " ");
    }
    StdOut.println();

    //		Quick.sort(a);    //快速排序
    Merge.sort(a);
    //		InsertionX.sort(a);

    for (int i = 0; i < a.length; i++) {
      StdOut.print(a[i] + " ");
    }
  }
Пример #3
0
  /** Unit tests the <tt>DepthFirstOrder</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);

    DepthFirstOrder dfs = new DepthFirstOrder(G);
    StdOut.println("   v  pre post");
    StdOut.println("--------------");
    for (int v = 0; v < G.V(); v++) {
      StdOut.printf("%4d %4d %4d\n", v, dfs.pre(v), dfs.post(v));
    }

    StdOut.print("Preorder:  ");
    for (int v : dfs.pre()) {
      StdOut.print(v + " ");
    }
    StdOut.println();

    StdOut.print("Postorder: ");
    for (int v : dfs.post()) {
      StdOut.print(v + " ");
    }
    StdOut.println();

    StdOut.print("Reverse postorder: ");
    for (int v : dfs.reversePost()) {
      StdOut.print(v + " ");
    }
    StdOut.println();
  }
Пример #4
0
  // test client
  public static void main(String[] args) {
    String pat = args[0];
    String txt = args[1];
    char[] pattern = pat.toCharArray();
    char[] text = txt.toCharArray();

    BoyerMoore boyermoore1 = new BoyerMoore(pat);
    BoyerMoore boyermoore2 = new BoyerMoore(pattern, 256);
    int offset1 = boyermoore1.search(txt);
    int offset2 = boyermoore2.search(text);

    // print results
    StdOut.println("text:    " + txt);

    StdOut.print("pattern: ");
    for (int i = 0; i < offset1; i++) StdOut.print(" ");
    StdOut.println(pat);

    StdOut.print("pattern: ");
    for (int i = 0; i < offset2; i++) StdOut.print(" ");
    StdOut.println(pat);
  }