Beispiel #1
0
    /**
     *  Reads the currency exchange table from standard input and
     *  prints an arbitrage opportunity to standard output (if one exists).
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // V currencies
        int V = StdIn.readInt();
        String[] name = new String[V];

        // create complete network
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
        for (int v = 0; v < V; v++) {
            name[v] = StdIn.readString();
            for (int w = 0; w < V; w++) {
                double rate = StdIn.readDouble();
                DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate));
                G.addEdge(e);
            }
        }

        // find negative cycle
        BellmanFordSP spt = new BellmanFordSP(G, 0);
        if (spt.hasNegativeCycle()) {
            double stake = 1000.0;
            for (DirectedEdge e : spt.negativeCycle()) {
                StdOut.printf("%10.5f %s ", stake, name[e.from()]);
                stake *= Math.exp(-e.weight());
                StdOut.printf("= %10.5f %s\n", stake, name[e.to()]);
            }
        }
        else {
            StdOut.println("No arbitrage opportunity");
        }
    }
Beispiel #2
0
    /**
     * Unit tests the {@code Interval2D} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        double xmin = Double.parseDouble(args[0]);
        double xmax = Double.parseDouble(args[1]);
        double ymin = Double.parseDouble(args[2]);
        double ymax = Double.parseDouble(args[3]);
        int trials = Integer.parseInt(args[4]);

        Interval1D xInterval = new Interval1D(xmin, xmax);
        Interval1D yInterval = new Interval1D(ymin, ymax);
        Interval2D box = new Interval2D(xInterval, yInterval);
        box.draw();

        Counter counter = new Counter("hits");
        for (int t = 0; t < trials; t++) {
            double x = StdRandom.uniform(0.0, 1.0);
            double y = StdRandom.uniform(0.0, 1.0);
            Point2D point = new Point2D(x, y);

            if (box.contains(point)) counter.increment();
            else                     point.draw();
        }

        StdOut.println(counter);
        StdOut.printf("box area = %.2f\n", box.area());
    }
    /**/ 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)
                });
            }
        }
    }
Beispiel #4
0
        /**/
        public static void main(string[] strarr)
        {
            int num = Integer.parseInt(strarr[0]);

            if (strarr.Length == 1)
            {
                for (int i = 0; i < num; i++)
                {
                    double d = StdRandom.uniform();
                    StdOut.println(d);
                }
            }
            else
            {
                if (strarr.Length != 3)
                {
                    string arg_87_0 = "Invalid number of arguments";

                    throw new ArgumentException(arg_87_0);
                }
                double d2 = java.lang.Double.parseDouble(strarr[1]);
                double d3 = java.lang.Double.parseDouble(strarr[2]);
                for (int j = 0; j < num; j++)
                {
                    double d4 = StdRandom.uniform(d2, d3);
                    StdOut.printf("%.2f\n", new object[]
                    {
                        java.lang.Double.valueOf(d4)
                    });
                }
            }
        }
Beispiel #5
0
    /**
     * Unit tests the {@code SuffixArrayx} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        String s = StdIn.readAll().replaceAll("\n", " ").trim();
        SuffixArrayX suffix1 = new SuffixArrayX(s);
        SuffixArray suffix2 = new SuffixArray(s);
        boolean check = true;
        for (int i = 0; check && i < s.length(); i++) {
            if (suffix1.index(i) != suffix2.index(i)) {
                StdOut.println("suffix1(" + i + ") = " + suffix1.index(i));
                StdOut.println("suffix2(" + i + ") = " + suffix2.index(i));
                String ith = "\"" + s.substring(suffix1.index(i), Math.min(suffix1.index(i) + 50, s.length())) + "\"";
                String jth = "\"" + s.substring(suffix2.index(i), Math.min(suffix2.index(i) + 50, s.length())) + "\"";
                StdOut.println(ith);
                StdOut.println(jth);
                check = false;
            }
        }

        StdOut.println("  i ind lcp rnk  select");
        StdOut.println("---------------------------");

        for (int i = 0; i < s.length(); i++) {
            int index = suffix2.index(i);
            String ith = "\"" + s.substring(index, Math.min(index + 50, s.length())) + "\"";
            int rank = suffix2.rank(s.substring(index));
            assert s.substring(index).equals(suffix2.select(i));
            if (i == 0) {
                StdOut.printf("%3d %3d %3s %3d  %s\n", i, index, "-", rank, ith);
            }
            else {
                // int lcp  = suffix.lcp(suffix2.index(i), suffix2.index(i-1));
                int lcp  = suffix2.lcp(i);
                StdOut.printf("%3d %3d %3d %3d  %s\n", i, index, lcp, rank, ith);
            }
        }
    }
    /**
     * 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();
    }
Beispiel #7
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);
                }
            }
        }

    }
Beispiel #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);
                    }
                }
            }
        }

    }
Beispiel #9
0
    /**
     * 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();


    }
Beispiel #10
0
    /**
     * Reads in two command-line arguments lo and hi and prints n uniformly
     * random real numbers in [lo, hi) to standard output.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // command-line arguments
        int n = Integer.parseInt(args[0]);

        // for backward compatibility with Intro to Programming in Java version of RandomSeq
        if (args.length == 1) {
            // generate and print n numbers between 0.0 and 1.0
            for (int i = 0; i < n; i++) {
                double x = StdRandom.uniform();
                StdOut.println(x);
            }
        }

        else if (args.length == 3) {
            double lo = Double.parseDouble(args[1]);
            double hi = Double.parseDouble(args[2]);

            // generate and print n numbers between lo and hi
            for (int i = 0; i < n; i++) {
                double x = StdRandom.uniform(lo, hi);
                StdOut.printf("%.2f\n", x);
            }
        }

        else {
            throw new IllegalArgumentException("Invalid number of arguments");
        }
    }
    /**
     * Unit tests the {@code AssignmentProblem} data type.
     * Takes a command-line argument n; creates a random n-by-n matrix;
     * solves the n-by-n assignment problem; and prints the optimal
     * solution.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // create random n-by-n matrix
        int n = Integer.parseInt(args[0]);
        double[][] weight = new double[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                weight[i][j] = StdRandom.uniform(900) + 100;  // 3 digits
            }
        }

        // solve assignment problem
        AssignmentProblem assignment = new AssignmentProblem(weight);
        StdOut.printf("weight = %.0f\n", assignment.weight());
        StdOut.println();

        // print n-by-n matrix and optimal solution
        if (n >= 20) return;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j == assignment.sol(i))
                    StdOut.printf("*%.0f ", weight[i][j]);
                else
                    StdOut.printf(" %.0f ", weight[i][j]);
            }
            StdOut.println();
        }
    }
Beispiel #12
0
 /**
  * Prints an array of doubles to standard output.
  *
  * @param a the 1D array of doubles
  */
 public static void print(double[] a) {
     int n = a.length;
     StdOut.println(n);
     for (int i = 0; i < n; i++) {
         StdOut.printf("%9.5f ", a[i]);
     }
     StdOut.println();
 }
Beispiel #13
0
 /**
  * Prints table of running times to call {@code ThreeSum.count()}
  * for arrays of size 250, 500, 1000, 2000, and so forth, along
  * with ratios of running times between successive array sizes.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) { 
     double prev = timeTrial(125);
     for (int n = 250; true; n += n) {
         double time = timeTrial(n);
         StdOut.printf("%7d %7.1f %5.1f\n", n, time, time/prev);
         prev = time;
     } 
 } 
Beispiel #14
0
        /**/
        public static void main(string[] strarr)
        {
            string       text         = java.lang.String.instancehelper_trim(java.lang.String.instancehelper_replaceAll(StdIn.readAll(), "\n", " "));
            SuffixArrayX suffixArrayX = new SuffixArrayX(text);
            SuffixArray  suffixArray  = new SuffixArray(text);
            int          num          = 1;
            int          i            = 0;

            while (num != 0 && i < java.lang.String.instancehelper_length(text))
            {
                if (suffixArray.index(i) != suffixArrayX.index(i))
                {
                    StdOut.println(new StringBuilder().append("suffixReference(").append(i).append(") = ").append(suffixArray.index(i)).toString());
                    StdOut.println(new StringBuilder().append("suffix(").append(i).append(") = ").append(suffixArrayX.index(i)).toString());
                    string obj   = new StringBuilder().append("\"").append(java.lang.String.instancehelper_substring(text, suffixArrayX.index(i), java.lang.Math.min(suffixArrayX.index(i) + 50, java.lang.String.instancehelper_length(text)))).append("\"").toString();
                    string text2 = new StringBuilder().append("\"").append(java.lang.String.instancehelper_substring(text, suffixArray.index(i), java.lang.Math.min(suffixArray.index(i) + 50, java.lang.String.instancehelper_length(text)))).append("\"").toString();
                    StdOut.println(obj);
                    StdOut.println(text2);
                    num = 0;
                }
                i++;
            }
            StdOut.println("  i ind lcp rnk  select");
            StdOut.println("---------------------------");
            for (i = 0; i < java.lang.String.instancehelper_length(text); i++)
            {
                int    num2  = suffixArrayX.index(i);
                string text2 = new StringBuilder().append("\"").append(java.lang.String.instancehelper_substring(text, num2, java.lang.Math.min(num2 + 50, java.lang.String.instancehelper_length(text)))).append("\"").toString();
                int    i2    = suffixArrayX.rank(java.lang.String.instancehelper_substring(text, num2));
                if (!SuffixArrayX.s_assertionsDisabled && !java.lang.String.instancehelper_equals(java.lang.String.instancehelper_substring(text, num2), suffixArrayX.select(i)))
                {
                    throw new AssertionError();
                }
                if (i == 0)
                {
                    StdOut.printf("%3d %3d %3s %3d  %s\n", new object[]
                    {
                        Integer.valueOf(i),
                        Integer.valueOf(num2),
                        "-",
                        Integer.valueOf(i2),
                        text2
                    });
                }
                else
                {
                    int i3 = suffixArrayX.lcp(i);
                    StdOut.printf("%3d %3d %3d %3d  %s\n", new object[]
                    {
                        Integer.valueOf(i),
                        Integer.valueOf(num2),
                        Integer.valueOf(i3),
                        Integer.valueOf(i2),
                        text2
                    });
                }
            }
        }
Beispiel #15
0
 /**
  * Unit tests the {@code PrimMST} 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);
     PrimMST mst = new PrimMST(G);
     for (Edge e : mst.edges()) {
         StdOut.println(e);
     }
     StdOut.printf("%.5f\n", mst.weight());
 }
Beispiel #16
0
 /**
   * Unit tests {@code StdStats}.
   * Convert command-line arguments to array of doubles and call various methods.
   *
   * @param args the command-line arguments
   */
  public static void main(String[] args) {
      double[] a = StdArrayIO.readDouble1D();
      StdOut.printf("       min %10.3f\n", min(a));
      StdOut.printf("      mean %10.3f\n", mean(a));
      StdOut.printf("       max %10.3f\n", max(a));
      StdOut.printf("    stddev %10.3f\n", stddev(a));
      StdOut.printf("       var %10.3f\n", var(a));
      StdOut.printf("   stddevp %10.3f\n", stddevp(a));
      StdOut.printf("      varp %10.3f\n", varp(a));
  }
Beispiel #17
0
 /**
  * Print a 2D array of integers to standard output.
  *
  * @param a the 2D array of integers
  */
 public static void print(int[][] 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++) {
             StdOut.printf("%9d ", a[i][j]);
         }
         StdOut.println();
     }
 }
 // print the tableaux
 private void show() {
     for (int i = 0; i < n; i++) {
         for (int j = 0; j < n; j++) {
             StdOut.printf("%8.3f ", a[i][j]);
         }
         StdOut.printf("| ");
         for (int j = n; j < n+n; j++) {
             StdOut.printf("%8.3f ", a[i][j]);
         }
         StdOut.printf("| %8.3f\n", a[i][n+n]);
     }
     StdOut.println();
 }
 // print tableaux
 private void show() {
     StdOut.println("m = " + m);
     StdOut.println("n = " + n);
     for (int i = 0; i <= m; i++) {
         for (int j = 0; j <= m+n; j++) {
             StdOut.printf("%7.2f ", a[i][j]);
             // StdOut.printf("%10.7f ", a[i][j]);
         }
         StdOut.println();
     }
     StdOut.println("value = " + value());
     for (int i = 0; i < m; i++)
         if (basis[i] < n) StdOut.println("x_" + basis[i] + " = " + a[i][m+n]);
     StdOut.println();
 }
Beispiel #20
0
        /**/
        public static void main(string[] strarr)
        {
            int num = 250;

            while (true)
            {
                double d = DoublingTest.timeTrial(num);
                StdOut.printf("%7d %5.1f\n", new object[]
                {
                    Integer.valueOf(num),
                    java.lang.Double.valueOf(d)
                });
                num += num;
            }
        }
Beispiel #21
0
 /**
  * Unit tests the {@code GaussianElimination} data type.
  */
 private static void test(String name, double[][] A, double[] b) {
     StdOut.println("----------------------------------------------------");
     StdOut.println(name);
     StdOut.println("----------------------------------------------------");
     GaussianElimination gaussian = new GaussianElimination(A, b);
     double[] x = gaussian.primal();
     if (gaussian.isFeasible()) {
         for (int i = 0; i < x.length; i++) {
             StdOut.printf("%.6f\n", x[i]);
         }
     }
     else {
         StdOut.println("System is infeasible");
     }
     StdOut.println();
     StdOut.println();
 }
        /**/
        public static void main(string[] strarr)
        {

            In i = new In(strarr[0]);
            EdgeWeightedGraph ewg = new EdgeWeightedGraph(i);
            PrimMST primMST = new PrimMST(ewg);
            Iterator iterator = primMST.edges().iterator();
            while (iterator.hasNext())
            {
                Edge obj = (Edge)iterator.next();
                StdOut.println(obj);
            }
            StdOut.printf("%.5f\n", new object[]
            {
            java.lang.Double.valueOf(primMST.weight())
            });
        }
    // check that Ax = b or yA = 0, yb != 0
    private boolean certifySolution(double[][] A, double[] b) {

        // check that Ax = b
        if (isFeasible()) {
            double[] x = primal();
            for (int i = 0; i < n; i++) {
                double sum = 0.0;
                for (int j = 0; j < n; j++) {
                    sum += A[i][j] * x[j];
                }
                if (Math.abs(sum - b[i]) > EPSILON) {
                    StdOut.println("not feasible");
                    StdOut.printf("b[%d] = %8.3f, sum = %8.3f\n", i, b[i], sum);
                    return false;
                }
            }
            return true;
        }

        // or that yA = 0, yb != 0
        else {
            double[] y = dual();
            for (int j = 0; j < n; j++) {
                double sum = 0.0;
                for (int i = 0; i < n; i++) {
                    sum += A[i][j] * y[i];
                }
                if (Math.abs(sum) > EPSILON) {
                    StdOut.println("invalid certificate of infeasibility");
                    StdOut.printf("sum = %8.3f\n", sum);
                    return false;
                }
            }
            double sum = 0.0;
            for (int i = 0; i < n; i++) {
                sum += y[i] * b[i];
            }
            if (Math.abs(sum) < EPSILON) {
                StdOut.println("invalid certificate of infeasibility");
                StdOut.printf("yb  = %8.3f\n", sum);
                return false;
            }
            return true;
        }
    }
 // is the column vector y dual feasible?
 private boolean isDualFeasible() {
     double[] y = column();
     double sum = 0.0;
     for (int i = 0; i < m; i++) {
         if (y[i] < 0) {
             StdOut.println("column vector y[] is not a probability distribution");
             StdOut.printf("    y[%d] = %f\n", i, y[i]);
             return false;
         }
         sum += y[i];
     }
     if (Math.abs(sum - 1.0) > EPSILON) {
         StdOut.println("column vector not a probability distribution");
         StdOut.println("    sum = " + sum);
         return false;
     }
     return true;
 }
    /**************************************************************************
     *
     *  The code below is solely for testing correctness of the data type.
     *
     **************************************************************************/

    // is the row vector x primal feasible?
    private boolean isPrimalFeasible() {
        double[] x = row();
        double sum = 0.0;
        for (int j = 0; j < n; j++) {
            if (x[j] < 0) {
                StdOut.println("row vector not a probability distribution");
                StdOut.printf("    x[%d] = %f\n", j, x[j]);
                return false;
            }
            sum += x[j];
        }
        if (Math.abs(sum - 1.0) > EPSILON) {
            StdOut.println("row vector x[] is not a probability distribution");
            StdOut.println("    sum = " + sum);
            return false;
        }
        return true;
    }
    /**/ 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();
    }
    /**/ 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)
                });
            }
        }
    }
Beispiel #28
0
    /**
     * Unit tests the {@code AcyclicSP} 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);

        // find shortest path from s to each other vertex in DAG
        AcyclicSP sp = new AcyclicSP(G, s);
        for (int v = 0; v < G.V(); v++) {
            if (sp.hasPathTo(v)) {
                StdOut.printf("%d to %d (%.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);
            }
        }
    }
Beispiel #29
0
    /**
     * Unit tests the {@code AcyclicLP} 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);

        AcyclicLP lp = new AcyclicLP(G, s);

        for (int v = 0; v < G.V(); v++) {
            if (lp.hasPathTo(v)) {
                StdOut.printf("%d to %d (%.2f)  ", s, v, lp.distTo(v));
                for (DirectedEdge e : lp.pathTo(v)) {
                    StdOut.print(e + "   ");
                }
                StdOut.println();
            }
            else {
                StdOut.printf("%d to %d         no path\n", s, v);
            }
        }
    }
Beispiel #30
0
    /**/ public static void main(string[] strarr)
    {
        In      i       = new In(strarr[0]);
        Digraph digraph = new Digraph(i);
        int     num     = Integer.parseInt(strarr[1]);
        DepthFirstDirectedPaths depthFirstDirectedPaths = new DepthFirstDirectedPaths(digraph, num);

        for (int j = 0; j < digraph.V(); j++)
        {
            if (depthFirstDirectedPaths.hasPathTo(j))
            {
                StdOut.printf("%d to %d:  ", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j)
                });
                Iterator iterator = depthFirstDirectedPaths.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)
                });
            }
        }
    }