예제 #1
0
  /**
   * Reads in a social network from a file, and then repeatedly reads in individuals from standard
   * input and prints out their degrees of separation. Takes three command-line arguments: the name
   * of a file, a delimiter, and the name of the distinguished individual. Each line in the file
   * contains the name of a vertex, followed by a list of the names of the vertices adjacent to that
   * vertex, separated by the delimiter.
   */
  public static void main(String[] args) {
    String filename = args[0];
    String delimiter = args[1];
    String source = args[2];

    // StdOut.println("Source: " + source);

    SymbolGraph sg = new SymbolGraph(filename, delimiter);
    Graph G = sg.G();
    if (!sg.contains(source)) {
      StdOut.println(source + " not in database.");
      return;
    }

    int s = sg.index(source);
    BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

    while (!StdIn.isEmpty()) {
      String sink = StdIn.readLine();
      if (sg.contains(sink)) {
        int t = sg.index(sink);
        if (bfs.hasPathTo(t)) {
          for (int v : bfs.pathTo(t)) {
            StdOut.println("   " + sg.name(v));
          }
        } else {
          StdOut.println("Not connected");
        }
      } else {
        StdOut.println("   Not in database.");
      }
    }
  }
예제 #2
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();
    }
  }
예제 #3
0
파일: ErdosRenyi.java 프로젝트: enali/algs4
  public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    int T = Integer.parseInt(args[1]);
    int[] edges = new int[T];

    for (int t = 0; t < T; t++) edges[t] = count(N);

    StdOut.println("1/2 N ln N = " + 0.5 * N * Math.log(N));
    StdOut.println("mean       = " + StdStats.mean(edges));
    StdOut.println("stddev     = " + StdStats.stddev(edges));
  }
예제 #4
0
  public static void main(String[] args) {
    In in = new In(args[0]);
    EdgeWeightedGraph G;
    G = new EdgeWeightedGraph(in);

    LazyPrimMST mst = new LazyPrimMST(G);
    for (Edge e : mst.edges()) {
      StdOut.println(e);
    }
    StdOut.println(mst.weight());
  }
 /**
  * Reads in a sequence of pairs of integers (between 0 and N-1) from standard input, where each
  * integer represents some object; if the objects are in different components, merge the two
  * components and print the pair to standard output.
  */
 public static void main(String[] args) {
   int N = StdIn.readInt();
   WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);
   while (!StdIn.isEmpty()) {
     int p = StdIn.readInt();
     int q = StdIn.readInt();
     if (uf.connected(p, q)) continue;
     uf.union(p, q);
     StdOut.println(p + " " + q);
   }
   StdOut.println(uf.count() + " components");
 }
예제 #6
0
  public void testRandom() {
    int i = 1;
    while (i * i * i <= 1000) i++;
    StdOut.println("i=" + String.valueOf(i));

    i = 1;
    while (i * i * i <= 60000) i++;
    StdOut.println("i=" + String.valueOf(i));

    i = 1;
    while (i * i * i <= 3600000) i++;
    StdOut.println("i=" + String.valueOf(i));
  }
예제 #7
0
  public static void expand() {
    int n = BinaryStdIn.readInt();

    int w = DNA.lgR();

    for (int i = 0; i < n; i++) {
      char c = BinaryStdIn.readChar(w);
      BinaryStdOut.write(DNA.toChar(c));
    }
    BinaryStdOut.close();
    StdOut.println();
    StdOut.println();
  }
예제 #8
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] + " ");
    }
  }
예제 #9
0
  public static void main(String[] args) throws MalformedURLException {
    UrlSearcher searcher = new UrlSearcher();
    NanoStopwatch timer = new NanoStopwatch();

    URL[] linksWithTarget = searcher.search(new URL(INITIAL_LINK), TARGET.toLowerCase(), 1000);
    StdOut.println(
        searcher.getClass().getSimpleName()
            + " spent "
            + timer.elapsedTime()
            + " seconds to find \""
            + TARGET
            + "\" in these links: ");
    for (URL url : linksWithTarget) {
      StdOut.println(url.toString());
    }
  }
예제 #10
0
 /** Unit tests the <tt>ST</tt> data type. */
 public static void main(String[] args) {
   ST<String, Integer> st = new ST<String, Integer>();
   for (int i = 0; !StdIn.isEmpty(); i++) {
     String key = StdIn.readString();
     st.put(key, i);
   }
   for (String s : st.keys()) StdOut.println(s + " " + st.get(s));
 }
예제 #11
0
 public static void main(String[] args) {
   String regexp = "(.*" + args[0] + ".*)";
   NFA nfa = new NFA(regexp);
   while (StdIn.hasNextLine()) {
     String txt = StdIn.readLine();
     if (nfa.recognizes(txt)) {
       StdOut.println(txt);
     }
   }
 }
예제 #12
0
파일: DeDup.java 프로젝트: imace/zava
  public static void main(String[] args) {
    SET<String> set = new SET<String>();

    // read in strings and add to set
    while (!StdIn.isEmpty()) {
      String key = StdIn.readString();
      if (!set.contains(key)) {
        set.add(key);
        StdOut.println(key);
      }
    }
  }
예제 #13
0
  public static void main(String[] args) {
    String[] a = StdIn.readAllStrings();
    int N = a.length;

    // check that strings have fixed length
    int W = a[0].length();
    for (int i = 0; i < N; i++) assert a[i].length() == W : "Strings must have fixed length";

    // sort the strings
    sort(a, W);

    // print results
    for (int i = 0; i < N; i++) StdOut.println(a[i]);
  }
예제 #14
0
  /**
   * Reads the precedence constraints from standard input and prints a feasible schedule to standard
   * output.
   */
  public static void main(String[] args) {

    // number of jobs
    int N = StdIn.readInt();

    // source and sink
    int source = 2 * N;
    int sink = 2 * N + 1;

    // build network
    EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * N + 2);
    for (int i = 0; i < N; i++) {
      double duration = StdIn.readDouble();
      G.addEdge(new DirectedEdge(source, i, 0.0));
      G.addEdge(new DirectedEdge(i + N, sink, 0.0));
      G.addEdge(new DirectedEdge(i, i + N, duration));

      // precedence constraints
      int M = StdIn.readInt();
      for (int j = 0; j < M; j++) {
        int precedent = StdIn.readInt();
        G.addEdge(new DirectedEdge(N + i, precedent, 0.0));
      }
    }

    // compute longest path
    AcyclicLP lp = new AcyclicLP(G, source);

    // print results
    StdOut.println(" job   start  finish");
    StdOut.println("--------------------");
    for (int i = 0; i < N; i++) {
      StdOut.printf("%4d %7.1f %7.1f\n", i, lp.distTo(i), lp.distTo(i + N));
    }
    StdOut.printf("Finish time: %7.1f\n", lp.distTo(sink));
  }
예제 #15
0
  // check that pre() and post() are consistent with pre(v) and post(v)
  private boolean check(Digraph G) {

    // check that post(v) is consistent with post()
    int r = 0;
    for (int v : post()) {
      if (post(v) != r) {
        StdOut.println("post(v) and post() inconsistent");
        return false;
      }
      r++;
    }

    // check that pre(v) is consistent with pre()
    r = 0;
    for (int v : pre()) {
      if (pre(v) != r) {
        StdOut.println("pre(v) and pre() inconsistent");
        return false;
      }
      r++;
    }

    return true;
  }
예제 #16
0
  /**
   * Searches a target URL recursively for the target string, up to a max number of links and
   * returns all the links containing the target string as a word in the link or in its content
   * body.
   *
   * @param url The link to search from
   * @param target The word to search for
   * @param max Max number of links to open
   * @return An array of URLs containing all the links and pages matching the search
   */
  public URL[] search(URL url, String target, int max) {
    this.max = max;
    final ArrayList<URL> targetFoundAt = new ArrayList<>();
    queue.enqueue(url);

    while (!queue.isEmpty()) {
      final URL link = queue.dequeue();
      StdOut.println(discovered.size());
      discovered.add(url);
      if (this.scan(link, target)) {
        targetFoundAt.add(link);
      }
    }

    URL[] returnArray = new URL[targetFoundAt.size()];
    return targetFoundAt.toArray(returnArray);
  }
예제 #17
0
  /**
   * Reads a sequence of transactions from standard input; takes a command-line integer M; prints to
   * standard output the M largest transactions in descending order.
   */
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    MinPQ<Transaction> pq = new MinPQ<Transaction>(M + 1);

    while (StdIn.hasNextLine()) {
      // Create an entry from the next line and put on the PQ.
      String line = StdIn.readLine();
      Transaction transaction = new Transaction(line);
      pq.insert(transaction);

      // remove minimum if M+1 entries on the PQ
      if (pq.size() > M) pq.delMin();
    } // top M entries are on the PQ

    // print entries on PQ in reverse order
    Stack<Transaction> stack = new Stack<Transaction>();
    for (Transaction transaction : pq) stack.push(transaction);
    for (Transaction transaction : stack) StdOut.println(transaction);
  }
예제 #18
0
  /** Unit tests the date data type. */
  public static void main(String[] args) {
    Date today = new Date(2, 25, 2004);
    StdOut.println(today);
    for (int i = 0; i < 10; i++) {
      today = today.next();
      StdOut.println(today);
    }

    StdOut.println(today.isAfter(today.next()));
    StdOut.println(today.isAfter(today));
    StdOut.println(today.next().isAfter(today));

    Date birthday = new Date(10, 16, 1971);
    StdOut.println(birthday);
    for (int i = 0; i < 10; i++) {
      birthday = birthday.next();
      StdOut.println(birthday);
    }
  }
예제 #19
0
파일: BoyerMoore.java 프로젝트: imace/zava
  // 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);
  }
예제 #20
0
파일: InsertionX.java 프로젝트: imace/zava
 // print array to standard output
 private static void show(Comparable[] a) {
   for (int i = 0; i < a.length; i++) {
     StdOut.println(a[i]);
   }
 }
예제 #21
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();
  }