コード例 #1
0
ファイル: SparseVector.java プロジェクト: ekis/algos
 // test client
 public static void main(String[] args) {
   SparseVector a = new SparseVector(10);
   SparseVector b = new SparseVector(10);
   a.put(3, 0.50);
   a.put(9, 0.75);
   a.put(6, 0.11);
   a.put(6, 0.00);
   b.put(3, 0.60);
   b.put(4, 0.90);
   StdOut.println("a = " + a);
   StdOut.println("b = " + b);
   StdOut.println("a dot b = " + a.dot(b));
   StdOut.println("a + b   = " + a.plus(b));
 }
コード例 #2
0
  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");
    }
  }
コード例 #3
0
ファイル: HexDump.java プロジェクト: ekis/algos
  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");
  }