/**
     * Unit tests the {@code DepthFirstOrder} data type.
     *
     * @param args the command-line arguments
     */
    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();


    }
Exemple #2
0
    /**
     * Unit tests the {@code Bipartite} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int V1 = Integer.parseInt(args[0]);
        int V2 = Integer.parseInt(args[1]);
        int E  = Integer.parseInt(args[2]);
        int F  = Integer.parseInt(args[3]);

        // create random bipartite graph with V1 vertices on left side,
        // V2 vertices on right side, and E edges; then add F random edges
        Graph G = GraphGenerator.bipartite(V1, V2, E);
        for (int i = 0; i < F; i++) {
            int v = StdRandom.uniform(V1 + V2);
            int w = StdRandom.uniform(V1 + V2);
            G.addEdge(v, w);
        }

        StdOut.println(G);


        Bipartite b = new Bipartite(G);
        if (b.isBipartite()) {
            StdOut.println("Graph is bipartite");
            for (int v = 0; v < G.V(); v++) {
                StdOut.println(v + ": " + b.color(v));
            }
        }
        else {
            StdOut.print("Graph has an odd-length cycle: ");
            for (int x : b.oddCycle()) {
                StdOut.print(x + " ");
            }
            StdOut.println();
        }
    }
    /**/ public static void main(string[] strarr)
    {
        string text  = strarr[0];
        string text2 = strarr[1];

        char[]     charr       = java.lang.String.instancehelper_toCharArray(text);
        char[]     charr2      = java.lang.String.instancehelper_toCharArray(text2);
        BoyerMoore boyerMoore  = new BoyerMoore(text);
        BoyerMoore boyerMoore2 = new BoyerMoore(charr, 256);
        int        num         = boyerMoore.search(text2);
        int        num2        = boyerMoore2.search(charr2);

        StdOut.println(new StringBuilder().append("text:    ").append(text2).toString());
        StdOut.print("pattern: ");
        for (int i = 0; i < num; i++)
        {
            StdOut.print(" ");
        }
        StdOut.println(text);
        StdOut.print("pattern: ");
        for (int i = 0; i < num2; i++)
        {
            StdOut.print(" ");
        }
        StdOut.println(text);
    }
    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]);
        Digraph G = DigraphGenerator.dag(V, E);

        // add F extra edges
        for (int i = 0; i < F; i++) {
            int v = StdRandom.uniform(V);
            int w = StdRandom.uniform(V);
            G.addEdge(v, w);
        }

        StdOut.println(G);


        DirectedCycleX finder = new DirectedCycleX(G);
        if (finder.hasCycle()) {
            StdOut.print("Directed cycle: ");
            for (int v : finder.cycle()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }

        else {
            StdOut.println("No directed cycle");
        }
        StdOut.println();
    }
    /**/ public static void main(string[] strarr)
    {
        In  i  = new In(strarr[0]);
        int i2 = Integer.parseInt(strarr[1]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(i);
        AcyclicSP           acyclicSP           = new AcyclicSP(edgeWeightedDigraph, i2);

        for (int j = 0; j < edgeWeightedDigraph.V(); j++)
        {
            if (acyclicSP.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%.2f)  ", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j),
                    java.lang.Double.valueOf(acyclicSP.distTo(j))
                });
                Iterator iterator = acyclicSP.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    DirectedEdge obj = (DirectedEdge)iterator.next();
                    StdOut.print(new StringBuilder().append(obj).append("   ").toString());
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d         no path\n", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j)
                });
            }
        }
    }
Exemple #6
0
    /**
     * Unit tests the {@code GabowSCC} data type.
     *
     * @param args the command-line arguments
     */
    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();
        }

    }
Exemple #7
0
    /**
     * Unit tests the {@code FordFulkerson} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // create flow network with V vertices and E edges
        int V = Integer.parseInt(args[0]);
        int E = Integer.parseInt(args[1]);
        int s = 0, t = V-1;
        FlowNetwork G = new FlowNetwork(V, E);
        StdOut.println(G);

        // compute maximum flow and minimum cut
        FordFulkerson maxflow = new FordFulkerson(G, s, t);
        StdOut.println("Max flow from " + s + " to " + t);
        for (int v = 0; v < G.V(); v++) {
            for (FlowEdge e : G.adj(v)) {
                if ((v == e.from()) && e.flow() > 0)
                    StdOut.println("   " + e);
            }
        }

        // print min-cut
        StdOut.print("Min cut: ");
        for (int v = 0; v < G.V(); v++) {
            if (maxflow.inCut(v)) StdOut.print(v + " ");
        }
        StdOut.println();

        StdOut.println("Max flow value = " +  maxflow.value());
    }
Exemple #8
0
    /**
     * Unit tests the {@code FloydWarshall} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // random graph with V vertices and E edges, parallel edges allowed
        int V = Integer.parseInt(args[0]);
        int E = Integer.parseInt(args[1]);
        AdjMatrixEdgeWeightedDigraph G = new AdjMatrixEdgeWeightedDigraph(V);
        for (int i = 0; i < E; i++) {
            int v = StdRandom.uniform(V);
            int w = StdRandom.uniform(V);
            double weight = Math.round(100 * (StdRandom.uniform() - 0.15)) / 100.0;
            if (v == w) G.addEdge(new DirectedEdge(v, w, Math.abs(weight)));
            else G.addEdge(new DirectedEdge(v, w, weight));
        }

        StdOut.println(G);

        // run Floyd-Warshall algorithm
        FloydWarshall spt = new FloydWarshall(G);

        // print all-pairs shortest path distances
        StdOut.printf("  ");
        for (int v = 0; v < G.V(); v++) {
            StdOut.printf("%6d ", v);
        }
        StdOut.println();
        for (int v = 0; v < G.V(); v++) {
            StdOut.printf("%3d: ", v);
            for (int w = 0; w < G.V(); w++) {
                if (spt.hasPath(v, w)) StdOut.printf("%6.2f ", spt.dist(v, w));
                else StdOut.printf("  Inf ");
            }
            StdOut.println();
        }

        // print negative cycle
        if (spt.hasNegativeCycle()) {
            StdOut.println("Negative cost cycle:");
            for (DirectedEdge e : spt.negativeCycle())
                StdOut.println(e);
            StdOut.println();
        }

        // print all-pairs shortest paths
        else {
            for (int v = 0; v < G.V(); v++) {
                for (int w = 0; w < G.V(); w++) {
                    if (spt.hasPath(v, w)) {
                        StdOut.printf("%d to %d (%5.2f)  ", v, w, spt.dist(v, w));
                        for (DirectedEdge e : spt.path(v, w))
                            StdOut.print(e + "  ");
                        StdOut.println();
                    }
                    else {
                        StdOut.printf("%d to %d no path\n", v, w);
                    }
                }
            }
        }

    }
Exemple #9
0
    /**
     * Unit tests the {@code BellmanFordSP} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        In in = new In(args[0]);
        int s = Integer.parseInt(args[1]);
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(in);

        BellmanFordSP sp = new BellmanFordSP(G, s);

        // print negative cycle
        if (sp.hasNegativeCycle()) {
            for (DirectedEdge e : sp.negativeCycle())
                StdOut.println(e);
        }

        // print shortest paths
        else {
            for (int v = 0; v < G.V(); v++) {
                if (sp.hasPathTo(v)) {
                    StdOut.printf("%d to %d (%5.2f)  ", s, v, sp.distTo(v));
                    for (DirectedEdge e : sp.pathTo(v)) {
                        StdOut.print(e + "   ");
                    }
                    StdOut.println();
                }
                else {
                    StdOut.printf("%d to %d           no path\n", s, v);
                }
            }
        }

    }
Exemple #10
0
    /** 
     * Takes a pattern string and an input string as command-line arguments;
     * searches for the pattern string in the text string; and prints
     * the first occurrence of the pattern string in the text string.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        String pat = args[0];
        String txt = args[1];
        char[] pattern = pat.toCharArray();
        char[] text    = txt.toCharArray();

        KMP kmp1 = new KMP(pat);
        int offset1 = kmp1.search(txt);

        KMP kmp2 = new KMP(pattern, 256);
        int offset2 = kmp2.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);
    }
    /**/ public static void main(string[] strarr)
    {
        In                i                 = new In(strarr[0]);
        Digraph           digraph           = new Digraph(i);
        KosarajuSharirSCC kosarajuSharirSCC = new KosarajuSharirSCC(digraph);
        int               num               = kosarajuSharirSCC.count();

        StdOut.println(new StringBuilder().append(num).append(" components").toString());
        Queue[] array = (Queue[])new Queue[num];
        for (int j = 0; j < num; j++)
        {
            array[j] = new Queue();
        }
        for (int j = 0; j < digraph.V(); j++)
        {
            array[kosarajuSharirSCC.id(j)].enqueue(Integer.valueOf(j));
        }
        for (int j = 0; j < num; j++)
        {
            Iterator iterator = array[j].iterator();
            while (iterator.hasNext())
            {
                int i2 = ((Integer)iterator.next()).intValue();
                StdOut.print(new StringBuilder().append(i2).append(" ").toString());
            }
            StdOut.println();
        }
    }
    /**
     * Unit tests the {@code HopcroftKarp} data type.
     * Takes three command-line arguments {@code V1}, {@code V2}, and {@code E};
     * creates a random bipartite graph with {@code V1} + {@code V2} vertices
     * and {@code E} edges; computes a maximum matching and minimum vertex cover;
     * and prints the results.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int V1 = Integer.parseInt(args[0]);
        int V2 = Integer.parseInt(args[1]);
        int E  = Integer.parseInt(args[2]);
        Graph G = GraphGenerator.bipartite(V1, V2, E);

        if (G.V() < 1000) StdOut.println(G);

        BipartiteMatching matching = new BipartiteMatching(G);
        
        // print maximum matching
        StdOut.printf("Number of edges in max matching        = %d\n", matching.size());
        StdOut.printf("Number of vertices in min vertex cover = %d\n", matching.size());
        StdOut.printf("Graph has a perfect matching           = %b\n", matching.isPerfect());
        StdOut.println();

        if (G.V() >= 1000) return;

        StdOut.print("Max matching: ");
        for (int v = 0; v < G.V(); v++) {
            int w = matching.mate(v);
            if (matching.isMatched(v) && v < w)  // print each edge only once
                StdOut.print(v + "-" + w + " ");
        }
        StdOut.println();

        // print minimum vertex cover
        StdOut.print("Min vertex cover: ");
        for (int v = 0; v < G.V(); v++)
            if (matching.inMinVertexCover(v))
                StdOut.print(v + " ");
        StdOut.println();
    }
Exemple #13
0
 /**
  * Prints a 1D array of booleans to standard output.
  *
  * @param a the 1D array of booleans
  */
 public static void print(boolean[] a) {
     int n = a.length;
     StdOut.println(n);
     for (int i = 0; i < n; i++) {
         if (a[i]) StdOut.print("1 ");
         else      StdOut.print("0 ");
     }
     StdOut.println();
 }
Exemple #14
0
    /**
     * Unit tests the {@code TopologicalX} data type.
     *
     * @param args the command-line arguments
     */
    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]);

        Digraph G1 = DigraphGenerator.dag(V, E);

        // corresponding edge-weighted digraph
        EdgeWeightedDigraph G2 = new EdgeWeightedDigraph(V);
        for (int v = 0; v < G1.V(); v++)
            for (int w : G1.adj(v))
                G2.addEdge(new DirectedEdge(v, w, 0.0));

        // add F extra edges
        for (int i = 0; i < F; i++) {
            int v = StdRandom.uniform(V);
            int w = StdRandom.uniform(V);
            G1.addEdge(v, w);
            G2.addEdge(new DirectedEdge(v, w, 0.0));
        }

        StdOut.println(G1);
        StdOut.println();
        StdOut.println(G2);

        // find a directed cycle
        TopologicalX topological1 = new TopologicalX(G1);
        if (!topological1.hasOrder()) {
            StdOut.println("Not a DAG");
        }

        // or give topologial sort
        else {
            StdOut.print("Topological order: ");
            for (int v : topological1.order()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }

        // find a directed cycle
        TopologicalX topological2 = new TopologicalX(G2);
        if (!topological2.hasOrder()) {
            StdOut.println("Not a DAG");
        }

        // or give topologial sort
        else {
            StdOut.print("Topological order: ");
            for (int v : topological2.order()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }
    }
 /**
  * Unit tests the {@code NonrecursiveDFS} data type.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     In in = new In(args[0]);
     Graph G = new Graph(in);
     int s = Integer.parseInt(args[1]);
     NonrecursiveDFS dfs = new NonrecursiveDFS(G, s);
     for (int v = 0; v < G.V(); v++)
         if (dfs.marked(v))
             StdOut.print(v + " ");
     StdOut.println();
 }
Exemple #16
0
 /**
  * Unit tests the {@code GlobalMincut} data type.
  * 
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     In in = new In(args[0]);
     EdgeWeightedGraph G = new EdgeWeightedGraph(in);
     GlobalMincut mc = new GlobalMincut(G);
     StdOut.print("Min cut: ");
     for (int v = 0; v < G.V(); v++) {
         if (mc.cut(v)) StdOut.print(v + " ");
     }
     StdOut.println();
     StdOut.println("Min cut weight = " + mc.weight());
 }
Exemple #17
0
 /**
   * Prints a 2D array of booleans to standard output.
   *
   * @param a the 2D array of booleans
   */
  public static void print(boolean[][] a) {
      int m = a.length;
      int n = a[0].length;
      StdOut.println(m + " " + n);
      for (int i = 0; i < m; i++) {
          for (int j = 0; j < n; j++) {
              if (a[i][j]) StdOut.print("1 ");
              else         StdOut.print("0 ");
          }
          StdOut.println();
      }
  }
    /**/ public static void main(string[] strarr)
    {
        int num  = Integer.parseInt(strarr[0]);
        int num2 = Integer.parseInt(strarr[1]);
        int num3 = Integer.parseInt(strarr[2]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(num);

        int[] array = new int[num];
        for (int i = 0; i < num; i++)
        {
            array[i] = i;
        }
        StdRandom.shuffle(array);
        for (int i = 0; i < num2; i++)
        {
            int num4;
            int num5;
            do
            {
                num4 = StdRandom.uniform(num);
                num5 = StdRandom.uniform(num);
            }while (num4 >= num5);
            double d = java.lang.Math.random();
            edgeWeightedDigraph.addEdge(new DirectedEdge(num4, num5, d));
        }
        for (int i = 0; i < num3; i++)
        {
            int    num4 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            int    num5 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            double d    = java.lang.Math.random();
            edgeWeightedDigraph.addEdge(new DirectedEdge(num4, num5, d));
        }
        StdOut.println(edgeWeightedDigraph);
        EdgeWeightedDirectedCycle edgeWeightedDirectedCycle = new EdgeWeightedDirectedCycle(edgeWeightedDigraph);

        if (edgeWeightedDirectedCycle.hasCycle())
        {
            StdOut.print("Cycle: ");
            Iterator iterator = edgeWeightedDirectedCycle.cycle().iterator();
            while (iterator.hasNext())
            {
                DirectedEdge obj = (DirectedEdge)iterator.next();
                StdOut.print(new StringBuilder().append(obj).append(" ").toString());
            }
            StdOut.println();
        }
        else
        {
            StdOut.println("No directed cycle");
        }
    }
    /**
     * Unit tests the {@code DepthFirstSearch} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        In in = new In(args[0]);
        Graph G = new Graph(in);
        int s = Integer.parseInt(args[1]);
        DepthFirstSearch search = new DepthFirstSearch(G, s);
        for (int v = 0; v < G.V(); v++) {
            if (search.marked(v))
                StdOut.print(v + " ");
        }

        StdOut.println();
        if (search.count() != G.V()) StdOut.println("NOT connected");
        else                         StdOut.println("connected");
    }
Exemple #20
0
 /**
  * Unit tests the {@code Cycle} data type.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     In in = new In(args[0]);
     Graph G = new Graph(in);
     Cycle finder = new Cycle(G);
     if (finder.hasCycle()) {
         for (int v : finder.cycle()) {
             StdOut.print(v + " ");
         }
         StdOut.println();
     }
     else {
         StdOut.println("Graph is acyclic");
     }
 }
Exemple #21
0
    /**//**/ public static void main(string[] strarr)
    {
        int   num   = Integer.parseInt(strarr[0]);
        int   num2  = Integer.parseInt(strarr[1]);
        int   num3  = Integer.parseInt(strarr[2]);
        Graph graph = new Graph(num);

        int[] array = new int[num];
        for (int i = 0; i < num; i++)
        {
            array[i] = i;
        }
        StdRandom.shuffle(array);
        for (int i = 0; i < num2; i++)
        {
            int j    = StdRandom.uniform(num / 2);
            int num4 = StdRandom.uniform(num / 2);
            graph.addEdge(array[j], array[num / 2 + num4]);
        }
        for (int i = 0; i < num3; i++)
        {
            int j    = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            int num4 = ByteCodeHelper.d2i(java.lang.Math.random() * (double)num);
            graph.addEdge(j, num4);
        }
        StdOut.println(graph);
        Bipartite bipartite = new Bipartite(graph);

        if (bipartite.isBipartite())
        {
            StdOut.println("Graph is bipartite");
            for (int j = 0; j < graph.V(); j++)
            {
                StdOut.println(new StringBuilder().append(j).append(": ").append(bipartite.color(j)).toString());
            }
        }
        else
        {
            StdOut.print("Graph has an odd-length cycle: ");
            Iterator iterator = bipartite.oddCycle().iterator();
            while (iterator.hasNext())
            {
                int num4 = ((Integer)iterator.next()).intValue();
                StdOut.print(new StringBuilder().append(num4).append(" ").toString());
            }
            StdOut.println();
        }
    }
Exemple #22
0
    // merge together the sorted input streams and write the sorted result to standard output
    private static void merge(In[] streams) {
        int n = streams.length;
        IndexMinPQ<String> pq = new IndexMinPQ<String>(n);
        for (int i = 0; i < n; i++)
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());

        // Extract and print min and read next from its stream. 
        while (!pq.isEmpty()) {
            StdOut.print(pq.minKey() + " ");
            int i = pq.delMin();
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());
        }
        StdOut.println();
    }
Exemple #23
0
    /**
     * Unit tests the {@code EdgeWeightedDirectedCycle} data type.
     *
     * @param args the command-line arguments
     */
    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 = StdRandom.uniform();
            G.addEdge(new DirectedEdge(v, w, weight));
        }

        // add F extra edges
        for (int i = 0; i < F; i++) {
            int v = StdRandom.uniform(V);
            int w = StdRandom.uniform(V);
            double weight = StdRandom.uniform(0.0, 1.0);
            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");
        }
    }
Exemple #24
0
    /**
     * Unit tests the {@code DirectedCycle} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        In in = new In(args[0]);
        Digraph G = new Digraph(in);

        DirectedCycle finder = new DirectedCycle(G);
        if (finder.hasCycle()) {
            StdOut.print("Directed cycle: ");
            for (int v : finder.cycle()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }

        else {
            StdOut.println("No directed cycle");
        }
        StdOut.println();
    }
        /**/
        public static void main(string[] strarr)
        {
            string text  = strarr[0];
            string text2 = strarr[1];

            java.lang.String.instancehelper_toCharArray(text);
            java.lang.String.instancehelper_toCharArray(text2);
            RabinKarp rabinKarp = new RabinKarp(text);
            int       num       = rabinKarp.search(text2);

            StdOut.println(new StringBuilder().append("text:    ").append(text2).toString());
            StdOut.print("pattern: ");
            for (int i = 0; i < num; i++)
            {
                StdOut.print(" ");
            }
            StdOut.println(text);
        }
Exemple #26
0
    private static void unitTest(Graph G, String description) {
        StdOut.println(description);
        StdOut.println("-------------------------------------");
        StdOut.print(G);

        EulerianCycle euler = new EulerianCycle(G);

        StdOut.print("Eulerian cycle: ");
        if (euler.hasEulerianCycle()) {
            for (int v : euler.cycle()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }
        else {
            StdOut.println("none");
        }
        StdOut.println();
    }
Exemple #27
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output in binary, k bits per line,
     * where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bitsPerLine = 16;
        if (args.length == 1) {
            bitsPerLine = Integer.parseInt(args[0]);
        }

        int count;
        for (count = 0; !BinaryStdIn.isEmpty(); count++) {
            if (bitsPerLine == 0) {
                BinaryStdIn.readBoolean();
                continue;
            }
            else if (count != 0 && count % bitsPerLine == 0) StdOut.println();
            if (BinaryStdIn.readBoolean()) StdOut.print(1);
            else                           StdOut.print(0);
        }
        if (bitsPerLine != 0) StdOut.println();
        StdOut.println(count + " bits");
    }
    /**/ public static void main(string[] strarr)
    {
        In              i               = new In(strarr[0]);
        Digraph         digraph         = new Digraph(i);
        DepthFirstOrder depthFirstOrder = new DepthFirstOrder(digraph);

        StdOut.println("   v  pre post");
        StdOut.println("--------------");
        for (int j = 0; j < digraph.V(); j++)
        {
            StdOut.printf("%4d %4d %4d\n", new object[]
            {
                Integer.valueOf(j),
                Integer.valueOf(depthFirstOrder.pre(j)),
                Integer.valueOf(depthFirstOrder.post(j))
            });
        }
        StdOut.print("Preorder:  ");
        Iterator iterator = depthFirstOrder.pre().iterator();

        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Postorder: ");
        iterator = depthFirstOrder.post().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Reverse postorder: ");
        iterator = depthFirstOrder.reversePost().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
    }
Exemple #29
0
    private static void unitTest(Digraph G, String description) {
        StdOut.println(description);
        StdOut.println("-------------------------------------");
        StdOut.print(G);

        DirectedEulerianPath euler = new DirectedEulerianPath(G);

        StdOut.print("Eulerian path:  ");
        if (euler.hasEulerianPath()) {
            for (int v : euler.path()) {
                StdOut.print(v + " ");
            }
            StdOut.println();
        }
        else {
            StdOut.println("none");
        }
        StdOut.println();
    }
    /**/ public static void main(string[] strarr)
    {
        In                i                 = new In(strarr[0]);
        Graph             graph             = new Graph(i);
        int               num               = Integer.parseInt(strarr[1]);
        BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(graph, num);

        for (int j = 0; j < graph.V(); j++)
        {
            if (breadthFirstPaths.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%d):  ", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j),
                    Integer.valueOf(breadthFirstPaths.distTo(j))
                });
                Iterator iterator = breadthFirstPaths.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    int num2 = ((Integer)iterator.next()).intValue();
                    if (num2 == num)
                    {
                        StdOut.print(num2);
                    }
                    else
                    {
                        StdOut.print(new StringBuilder().append("-").append(num2).toString());
                    }
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d (-):  not connected\n", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j)
                });
            }
        }
    }