コード例 #1
0
        public static void MainTest(string[] args)
        {
            GaussianElimination.test1();
            GaussianElimination.test2();
            GaussianElimination.test3();
            GaussianElimination.test4();
            GaussianElimination.test5();
            GaussianElimination.test6();
            GaussianElimination.test7();
            GaussianElimination.test8();
            GaussianElimination.test9();

            // N-by-N random system
            int N = int.Parse(args[0]);

            // build augmented matrix
            double[][] A = new double[N][];

            for (int i = 0; i < N; i++)
            {
                A[i] = new double[N];
                for (int j = 0; j < N; j++)
                {
                    A[i][j] = StdRandom.Uniform(1000);
                }
            }

            double[] b = new double[N];
            for (int i = 0; i < N; i++)
            {
                b[i] = StdRandom.Uniform(1000);
            }

            GaussianElimination.test(N + "-by-" + N + " (probably nonsingular)", A, b);
        }
コード例 #2
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a uniformly random <c>k</c>-regular graph on <c>V</c> vertices
        /// (not necessarily simple). The graph is simple with probability only about e^(-k^2/4),
        /// which is tiny when k = 14.</summary>
        /// <param name="V">the number of vertices in the graph</param>
        /// <param name="k">the k-regular value</param>
        /// <returns>a uniformly random <c>k</c>-regular graph on <c>V</c> vertices.</returns>
        ///
        public static Graph Regular(int V, int k)
        {
            if (V * k % 2 != 0)
            {
                throw new ArgumentException("Number of vertices * k must be even");
            }
            Graph G = new Graph(V);

            // create k copies of each vertex
            int[] vertices = new int[V * k];
            for (int v = 0; v < V; v++)
            {
                for (int j = 0; j < k; j++)
                {
                    vertices[v + V * j] = v;
                }
            }

            // pick a random perfect matching
            StdRandom.Shuffle(vertices);
            for (int i = 0; i < V * k / 2; i++)
            {
                G.AddEdge(vertices[2 * i], vertices[2 * i + 1]);
            }
            return(G);
        }
コード例 #3
0
        public static void MainTest(string[] args)
        {
            int V = int.Parse(args[0]);
            int E = int.Parse(args[1]);

            // Eulerian cycle
            Graph G1 = GraphGenerator.EulerianCycle(V, E);

            EulerianCycle.UnitTest(G1, "Eulerian cycle");

            // Eulerian path
            Graph G2 = GraphGenerator.EulerianPath(V, E);

            EulerianCycle.UnitTest(G2, "Eulerian path");

            // empty graph
            Graph G3 = new Graph(V);

            EulerianCycle.UnitTest(G3, "empty graph");

            // self loop
            Graph G4 = new Graph(V);
            int   v4 = StdRandom.Uniform(V);

            G4.AddEdge(v4, v4);
            EulerianCycle.UnitTest(G4, "single self loop");

            // union of two disjoint cycles
            Graph H1 = GraphGenerator.EulerianCycle(V / 2, E / 2);
            Graph H2 = GraphGenerator.EulerianCycle(V - V / 2, E - E / 2);

            int[] perm = new int[V];
            for (int i = 0; i < V; i++)
            {
                perm[i] = i;
            }
            StdRandom.Shuffle(perm);
            Graph G5 = new Graph(V);

            for (int v = 0; v < H1.V; v++)
            {
                foreach (int w in H1.Adj(v))
                {
                    G5.AddEdge(perm[v], perm[w]);
                }
            }
            for (int v = 0; v < H2.V; v++)
            {
                foreach (int w in H2.Adj(v))
                {
                    G5.AddEdge(perm[V / 2 + v], perm[V / 2 + w]);
                }
            }
            EulerianCycle.UnitTest(G5, "Union of two disjoint cycles");

            // random digraph
            Graph G6 = GraphGenerator.Simple(V, E);

            EulerianCycle.UnitTest(G6, "simple graph");
        }
コード例 #4
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a wheel graph on <c>V</c> vertices.</summary>
        /// <param name="V">the number of vertices in the wheel</param>
        /// <returns>a wheel graph on <c>V</c> vertices: a single vertex connected to
        ///    every vertex in a cycle on <c>V-1</c> vertices</returns>
        ///
        public static Graph Wheel(int V)
        {
            if (V <= 1)
            {
                throw new ArgumentException("Number of vertices must be at least 2");
            }
            Graph G = new Graph(V);

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);

            // simple cycle on V-1 vertices
            for (int i = 1; i < V - 1; i++)
            {
                G.AddEdge(vertices[i], vertices[i + 1]);
            }
            G.AddEdge(vertices[V - 1], vertices[1]);

            // connect vertices[0] to every vertex on cycle
            for (int i = 1; i < V; i++)
            {
                G.AddEdge(vertices[0], vertices[i]);
            }

            return(G);
        }
コード例 #5
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices,
        /// containing each possible edge with probability <c>p</c>.</summary>
        /// <param name="V1">the number of vertices in one partition</param>
        /// <param name="V2">the number of vertices in the other partition</param>
        /// <param name="p">the probability that the graph contains an edge with one endpoint in either side</param>
        /// <returns>a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices,
        ///   containing each possible edge with probability <c>p</c></returns>
        /// <exception cref="ArgumentException">if probability is not between 0 and 1</exception>
        ///
        public static Graph Bipartite(int V1, int V2, double p)
        {
            if (p < 0.0 || p > 1.0)
            {
                throw new ArgumentException("Probability must be between 0 and 1");
            }
            int[] vertices = new int[V1 + V2];
            for (int i = 0; i < V1 + V2; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            Graph G = new Graph(V1 + V2);

            for (int i = 0; i < V1; i++)
            {
                for (int j = 0; j < V2; j++)
                {
                    if (StdRandom.Bernoulli(p))
                    {
                        G.AddEdge(vertices[i], vertices[V1 + j]);
                    }
                }
            }
            return(G);
        }
コード例 #6
0
        public static void MainTest(string[] args)
        {
            int N = int.Parse(args[0]);

            Complex[] x = new Complex[N];

            // original data
            Random rnd = new Random();

            for (int i = 0; i < N; i++)
            {
                x[i] = new Complex(i, 0);
                x[i] = new Complex(-2 * StdRandom.Uniform() + 1, 0);
            }
            FFT.Show(x, "x");

            // FFT of original data
            Complex[] y = FFT.Fft(x);
            FFT.Show(y, "y = fft(x)");

            // take inverse FFT
            Complex[] z = FFT.Ifft(y);
            FFT.Show(z, "z = ifft(y)");

            // circular convolution of x with itself
            Complex[] c = FFT.Cconvolve(x, x);
            FFT.Show(c, "c = cconvolve(x, x)");

            // linear convolution of x with itself
            Complex[] d = FFT.Convolve(x, x);
            FFT.Show(d, "d = convolve(x, x)");
        }
コード例 #7
0
ファイル: DigraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a random simple DAG containing <c>V</c> vertices and <c>E</c> edges.
        /// Note: it is not uniformly selected at random among all such DAGs.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of vertices</param>
        /// <returns>a random simple DAG on <c>V</c> vertices, containing a total
        ///    of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple DAG exists</exception>
        ///
        public static Digraph Dag(int V, int E)
        {
            if (E > (long)V * (V - 1) / 2)
            {
                throw new ArgumentException("Too many edges");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges");
            }
            Digraph    G   = new Digraph(V);
            SET <Edge> set = new SET <Edge>();

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(v, w);
                if ((v < w) && !set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(vertices[v], vertices[w]);
                }
            }
            return(G);
        }
コード例 #8
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices
        /// with <c>E</c> edges.</summary>
        /// <param name="V1">the number of vertices in one partition</param>
        /// <param name="V2">the number of vertices in the other partition</param>
        /// <param name="E">the number of edges</param>
        /// <returns>a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices,
        ///   containing a total of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple bipartite graph exists</exception>
        ///
        public static Graph Bipartite(int V1, int V2, int E)
        {
            if (E > (long)V1 * V2)
            {
                throw new ArgumentException("Too many edges for bipartite graphs");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges for bipartite graphs");
            }
            Graph G = new Graph(V1 + V2);

            int[] vertices = new int[V1 + V2];
            for (int i = 0; i < V1 + V2; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);

            SET <Edge> set = new SET <Edge>();

            while (G.E < E)
            {
                int  i = StdRandom.Uniform(V1);
                int  j = V1 + StdRandom.Uniform(V2);
                Edge e = new Edge(vertices[i], vertices[j]);
                if (!set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(vertices[i], vertices[j]);
                }
            }
            return(G);
        }
コード例 #9
0
        public static void MainTest(string[] args)
        {
            // command-line arguments
            int N = int.Parse(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();
                    Console.WriteLine(x);
                }
            }
            else if (args.Length == 3)
            {
                double lo = double.Parse(args[1]);
                double hi = double.Parse(args[2]);

                // generate and print N numbers between lo and hi
                for (int i = 0; i < N; i++)
                {
                    double x = StdRandom.Uniform(lo, hi);
                    Console.Write("{0:F2}\n", x);
                }
            }

            else
            {
                throw new ArgumentException("Invalid number of arguments");
            }
        }
コード例 #10
0
ファイル: DirectedCycleX.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            // create random DAG with V vertices and E edges; then add F random edges
            int     V = int.Parse(args[0]);
            int     E = int.Parse(args[1]);
            int     F = int.Parse(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);
            }

            Console.WriteLine(G);

            DirectedCycleX finder = new DirectedCycleX(G);

            if (finder.HasCycle)
            {
                Console.Write("Directed cycle: ");
                foreach (int v in finder.GetCycle())
                {
                    Console.Write(v + " ");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No directed cycle");
            }
            Console.WriteLine();
        }
コード例 #11
0
ファイル: DigraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>Returns a random simple digraph containing <c>V</c> vertices and <c>E</c> edges.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of vertices</param>
        /// <returns>a random simple digraph on <c>V</c> vertices, containing a total
        ///    of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple digraph exists</exception>
        ///
        public static Digraph Simple(int V, int E)
        {
            if (E > (long)V * (V - 1))
            {
                throw new ArgumentException("Too many edges");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges");
            }
            Digraph    G   = new Digraph(V);
            SET <Edge> set = new SET <Edge>();

            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(v, w);
                if ((v != w) && !set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(v, w);
                }
            }
            return(G);
        }
コード例 #12
0
        public static void MainTest(string[] args)
        {
            int V1 = int.Parse(args[0]);
            int V2 = int.Parse(args[1]);
            int E  = int.Parse(args[2]);
            int F  = int.Parse(args[3]);

            Bipartite b;
            // 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);

            Console.WriteLine("Graph is {0}\n", G);

            b = new Bipartite(G);
            BipartiteReport(b);

            for (int i = 0; i < F; i++)
            {
                int v = StdRandom.Uniform(V1 + V2);
                int w = StdRandom.Uniform(V1 + V2);
                G.AddEdge(v, w);
            }
            Console.WriteLine("After adding {0} random edges", F);
            Console.WriteLine("Graph is {0}\n", G);

            b = new Bipartite(G);
            BipartiteReport(b);
        }
コード例 #13
0
ファイル: Quick.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Rearranges the array so that a[k] contains the kth smallest key;
        /// a[0] through a[k-1] are OrderHelper.Less than (or equal to) a[k]; and
        /// a[k+1] through a[N-1] are greater than (or equal to) a[k].</summary>
        /// <param name="a">a the array</param>
        /// <param name="k">k find the kth smallest</param>
        /// <returns>the rearranged array</returns>
        /// <exception cref="IndexOutOfRangeException"> if k is out of range</exception>
        ///
        public static IComparable Select(IComparable[] a, int k)
        {
            if (k < 0 || k >= a.Length)
            {
                throw new IndexOutOfRangeException("Selected element out of bounds");
            }
            StdRandom.Shuffle(a);
            int lo = 0, hi = a.Length - 1;

            while (hi > lo)
            {
                int i = partition(a, lo, hi);
                if (i > k)
                {
                    hi = i - 1;
                }
                else if (i < k)
                {
                    lo = i + 1;
                }
                else
                {
                    return(a[i]);
                }
            }
            return(a[lo]);
        }
コード例 #14
0
ファイル: TwoPersonZeroSumGame.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TwoPersonZeroSumGame.test1();
            TwoPersonZeroSumGame.test2();
            TwoPersonZeroSumGame.test3();
            TwoPersonZeroSumGame.test4();
            TwoPersonZeroSumGame.test5();

            int M = 5, N = 5;

            if (args.Length == 3)
            {
                M = int.Parse(args[1]);
                N = int.Parse(args[2]);
            }
            double[,] payoff = new double[M, N];
            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    payoff[i, j] = StdRandom.Uniform(-0.5, 0.5);
                }
            }
            TwoPersonZeroSumGame.test("random " + M + "-by-" + N, payoff);
        }
コード例 #15
0
        public static void MainTest(string[] args)
        {
            int n = int.Parse(args[0]);

            if (args.Length == 2)
            {
                StdRandom.Seed = int.Parse(args[1]);
            }
            double[] probabilities = { 0.5, 0.3, 0.1, 0.1 };
            int[]    frequencies   = { 5, 3, 1, 1 };
            string[] a             = "A B C D E F G".Split(' ');

            Console.WriteLine("seed = " + StdRandom.Seed);
            for (int i = 0; i < n; i++)
            {
                Console.Write("{0} ", StdRandom.Uniform(100));
                Console.Write("{0:F5} ", StdRandom.Uniform(10.0, 99.0));
                Console.Write("{0} ", StdRandom.Bernoulli(0.5));
                Console.Write("{0:F5} ", StdRandom.Gaussian(9.0, 0.2));
                Console.Write("{0} ", StdRandom.Discrete(probabilities));
                Console.Write("{0} ", StdRandom.Discrete(frequencies));
                StdRandom.Shuffle(a);

                foreach (string s in a)
                {
                    Console.Write(s);
                }
                Console.WriteLine();
            }
        }
コード例 #16
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        // For a complete description, see
        // http://www.proofwiki.org/wiki/Labeled_Tree_from_Prüfer_Sequence
        // http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.36.6484&rep=rep1&type=pdf
        /// <summary>
        /// Returns a uniformly random tree on <c>V</c> vertices.
        /// This algorithm uses a Prufer sequence and takes time proportional to <c>V log V</c>.</summary>
        /// <param name="V">the number of vertices in the tree</param>
        /// <returns>a uniformly random tree on <c>V</c> vertices</returns>
        ///
        public static Graph Tree(int V)
        {
            Graph G = new Graph(V);

            // special case
            if (V == 1)
            {
                return(G);
            }

            // Cayley's theorem: there are V^(V-2) labeled trees on V vertices
            // Prufer sequence: sequence of V-2 values between 0 and V-1
            // Prufer's proof of Cayley's theorem: Prufer sequences are in 1-1
            // with labeled trees on V vertices
            int[] prufer = new int[V - 2];
            for (int i = 0; i < V - 2; i++)
            {
                prufer[i] = StdRandom.Uniform(V);
            }

            // degree of vertex v = 1 + number of times it appers in Prufer sequence
            int[] degree = new int[V];
            for (int v = 0; v < V; v++)
            {
                degree[v] = 1;
            }
            for (int i = 0; i < V - 2; i++)
            {
                degree[prufer[i]]++;
            }

            // pq contains all vertices of degree 1
            MinPQ <int> pq = new MinPQ <int>();

            for (int v = 0; v < V; v++)
            {
                if (degree[v] == 1)
                {
                    pq.Insert(v);
                }
            }

            // repeatedly delMin() degree 1 vertex that has the minimum index
            for (int i = 0; i < V - 2; i++)
            {
                int v = pq.DelMin();
                G.AddEdge(v, prufer[i]);
                degree[v]--;
                degree[prufer[i]]--;
                if (degree[prufer[i]] == 1)
                {
                    pq.Insert(prufer[i]);
                }
            }
            G.AddEdge(pq.DelMin(), pq.DelMin());
            return(G);
        }
コード例 #17
0
ファイル: LinearProgramming.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            Console.WriteLine("----- test 1 --------------------");
            LinearProgramming.test1();
            Console.WriteLine("----- test 2 --------------------");
            LinearProgramming.test2();
            Console.WriteLine("----- test 3 --------------------");
            try
            {
                LinearProgramming.test3();
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            Console.WriteLine("----- test 4 --------------------");
            LinearProgramming.test4();


            Console.WriteLine("----- test random ---------------");
            int M = 5, N = 5;

            if (args.Length == 3)
            {
                M = int.Parse(args[1]);
                N = int.Parse(args[2]);
            }
            double[] c = new double[N];
            double[] b = new double[M];
            double[,] A = new double[M, N];
            for (int j = 0; j < N; j++)
            {
                c[j] = StdRandom.Uniform(1000);
            }
            for (int i = 0; i < M; i++)
            {
                b[i] = StdRandom.Uniform(1000);
            }
            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    A[i, j] = StdRandom.Uniform(100);
                }
            }
            try
            {
                LinearProgramming lp = new LinearProgramming(A, b, c);
                Console.WriteLine(lp.Value);
            }
            catch (ArithmeticException)
            {
                Console.WriteLine("The randomlly generated linear program is unbounded. Try again.");
            }
        }
コード例 #18
0
        public static void MainTest(string[] args)
        {
            // create random DAG with V vertices and E edges; then add F random edges
            int V = int.Parse(args[0]);
            int E = int.Parse(args[1]);
            int F = int.Parse(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));
            }

            Console.WriteLine(G);

            // find a directed cycle
            EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);

            if (finder.HasCycle)
            {
                Console.Write("Cycle: ");
                foreach (DirectedEdge e in finder.GetCycle())
                {
                    Console.Write(e + " ");
                }
                Console.WriteLine();
            }

            // or give topologial sort
            else
            {
                Console.WriteLine("No directed cycle");
            }
        }
コード例 #19
0
 /// <summary>
 /// Initializes a particle with a random position and velocity.
 /// The position is uniform in the unit box; the velocity in
 /// either direciton is chosen uniformly at random. Member initializers
 /// will replace these random values.</summary>
 /// <param name="target">the drawing target</param>
 public Particle(DrawingWindow target) : base(target)
 {
     X      = StdRandom.Uniform(0.0, 1.0);
     Y      = StdRandom.Uniform(0.0, 1.0);
     Vx     = StdRandom.Uniform(-.005, 0.005);
     Vy     = StdRandom.Uniform(-.005, 0.005);
     Radius = 0.01;
     Mass   = 0.5;
     color  = Colors.Black;
 }
コード例 #20
0
        public static void MainTest(string[] args)
        {
            GaussJordanElimination.test1();
            GaussJordanElimination.test2();
            GaussJordanElimination.test3();
            GaussJordanElimination.test4();
            GaussJordanElimination.test5();
            GaussJordanElimination.test6();

            // N-by-N random system (likely full rank)
            int N = int.Parse(args[0]);

            double[][] A = new double[N][];
            for (int i = 0; i < N; i++)
            {
                A[i] = new double[N];
                for (int j = 0; j < N; j++)
                {
                    A[i][j] = StdRandom.Uniform(1000);
                }
            }
            double[] b = new double[N];
            for (int i = 0; i < N; i++)
            {
                b[i] = StdRandom.Uniform(1000);
            }
            GaussJordanElimination.test("random " + N + "-by-" + N + " (likely full rank)", A, b);

            // N-by-N random system (likely infeasible)
            A = new double[N][];
            for (int i = 0; i < N - 1; i++)
            {
                A[i] = new double[N];
                for (int j = 0; j < N; j++)
                {
                    A[i][j] = StdRandom.Uniform(1000);
                }
            }
            A[N - 1] = new double[N];
            for (int i = 0; i < N - 1; i++)
            {
                double alpha = StdRandom.Uniform(11) - 5.0;
                for (int j = 0; j < N; j++)
                {
                    A[N - 1][j] += alpha * A[i][j];
                }
            }
            b = new double[N];
            for (int i = 0; i < N; i++)
            {
                b[i] = StdRandom.Uniform(1000);
            }
            GaussJordanElimination.test("random " + N + "-by-" + N + " (likely infeasible)", A, b);
        }
コード例 #21
0
        /// <summary>Returns the amount of time to call <c>ThreeSum.count()</c>
        /// with <c>N</c> random 6-digit integers.</summary>
        /// <param name="N">N the number of integers</param>
        /// <returns>amount of time (in seconds) to call <c>ThreeSum.count()</c>
        ///  with <c>N</c> random 6-digit integers</returns>
        ///
        public static double TimeTrial(int N)
        {
            int[] a = new int[N];
            for (int i = 0; i < N; i++)
            {
                a[i] = StdRandom.Uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
            }
            Stopwatch timer = new Stopwatch();

            ThreeSum.Count(a);
            return(timer.ElapsedTime());
        }
コード例 #22
0
 /// <summary>
 /// Initializes a random flow network with <c>V</c> vertices and <c>E</c> edges.
 /// The capacities are integers between 0 and 99 and the flow values are zero.</summary>
 /// <param name="V">the number of vertices</param>
 /// <param name="E">the number of edges</param>
 /// <exception cref="ArgumentException">if <c>V</c> &lt; 0</exception>
 /// <exception cref="ArgumentException">if <c>E</c> &lt; 0</exception>
 ///
 public FlowNetwork(int V, int E) : this(V)
 {
     if (E < 0)
     {
         throw new ArgumentException("Number of edges must be nonnegative");
     }
     for (int i = 0; i < E; i++)
     {
         int    v        = StdRandom.Uniform(V);
         int    w        = StdRandom.Uniform(V);
         double capacity = StdRandom.Uniform(100);
         AddEdge(new FlowEdge(v, w, capacity));
     }
 }
コード例 #23
0
ファイル: EdgeWeightedDigraph.cs プロジェクト: zzhi/Algs4Net
 /// <summary>
 /// Initializes a random edge-weighted digraph with <c>V</c> vertices and <c>E</c> edges.</summary>
 /// <param name="V">the number of vertices</param>
 /// <param name="E">the number of edges</param>
 /// <exception cref="ArgumentException">if <c>V</c> &lt; 0</exception>
 /// <exception cref="ArgumentException">if <c>E</c> &lt; 0</exception>
 ///
 public EdgeWeightedDigraph(int V, int E) : this(V)
 {
     if (E < 0)
     {
         throw new ArgumentException("Number of edges in a Digraph must be nonnegative");
     }
     for (int i = 0; i < E; i++)
     {
         int          v      = StdRandom.Uniform(V);
         int          w      = StdRandom.Uniform(V);
         double       weight = .01 * StdRandom.Uniform(100);
         DirectedEdge e      = new DirectedEdge(v, w, weight);
         AddEdge(e);
     }
 }
コード例 #24
0
ファイル: EdgeWeightedGraph.cs プロジェクト: zzhi/Algs4Net
 /// <summary>
 /// Initializes a random edge-weighted graph with <c>V</c> vertices and <c>E</c> edges.</summary>
 /// <param name="V"> V the number of vertices</param>
 /// <param name="E"> E the number of edges</param>
 /// <exception cref="ArgumentException">if <c>V</c> &lt; 0</exception>
 /// <exception cref="ArgumentException">if <c>E</c> &lt; 0</exception>
 ///
 public EdgeWeightedGraph(int V, int E) : this(V)
 {
     if (E < 0)
     {
         throw new ArgumentException("Number of edges must be nonnegative");
     }
     for (int i = 0; i < E; i++)
     {
         int    v      = StdRandom.Uniform(V);
         int    w      = StdRandom.Uniform(V);
         double weight = Math.Round(100 * StdRandom.Uniform()) / 100.0;
         Edge   e      = new Edge(v, w, weight);
         AddEdge(e);
     }
 }
コード例 #25
0
ファイル: DirectedEulerianPath.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            int V = int.Parse(args[0]);
            int E = int.Parse(args[1]);

            // Eulerian cycle
            Digraph G1 = DigraphGenerator.EulerianCycle(V, E);

            DirectedEulerianPath.UnitTest(G1, "Eulerian cycle");

            // Eulerian path
            Digraph G2 = DigraphGenerator.EulerianPath(V, E);

            DirectedEulerianPath.UnitTest(G2, "Eulerian path");

            // add one random edge
            Digraph G3 = new Digraph(G2);

            G3.AddEdge(StdRandom.Uniform(V), StdRandom.Uniform(V));
            DirectedEulerianPath.UnitTest(G3, "one random edge added to Eulerian path");

            // self loop
            Digraph G4 = new Digraph(V);
            int     v4 = StdRandom.Uniform(V);

            G4.AddEdge(v4, v4);
            DirectedEulerianPath.UnitTest(G4, "single self loop");

            // single edge
            Digraph G5 = new Digraph(V);

            G5.AddEdge(StdRandom.Uniform(V), StdRandom.Uniform(V));
            DirectedEulerianPath.UnitTest(G5, "single edge");

            // empty digraph
            Digraph G6 = new Digraph(V);

            DirectedEulerianPath.UnitTest(G6, "empty digraph");

            // random digraph
            Digraph G7 = DigraphGenerator.Simple(V, E);

            DirectedEulerianPath.UnitTest(G7, "simple digraph");

            // 4-vertex digraph
            //Digraph G8 = new Digraph(new TextInput("eulerianD.txt"));
            //DirectedEulerianPath.UnitTest(G8, "4-vertex Eulerian digraph");
        }
コード例 #26
0
ファイル: DigraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a complete binary tree digraph on <c>V</c> vertices.</summary>
        /// <param name="V">the number of vertices in the binary tree</param>
        /// <returns>a digraph that is a complete binary tree on <c>V</c> vertices</returns>
        ///
        public static Digraph BinaryTree(int V)
        {
            Digraph G = new Digraph(V);

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            for (int i = 1; i < V; i++)
            {
                G.AddEdge(vertices[i], vertices[(i - 1) / 2]);
            }
            return(G);
        }
コード例 #27
0
ファイル: DigraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a path digraph on <c>V</c> vertices.</summary>
        /// <param name="V">the number of vertices in the path</param>
        /// <returns>a digraph that is a directed path on <c>V</c> vertices</returns>
        ///
        public static Digraph Path(int V)
        {
            Digraph G = new Digraph(V);

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            for (int i = 0; i < V - 1; i++)
            {
                G.AddEdge(vertices[i], vertices[i + 1]);
            }
            return(G);
        }
コード例 #28
0
ファイル: GraphGenerator.cs プロジェクト: zzhi/Algs4Net
        /// <summary>
        /// Returns a cycle graph on <c>V</c> vertices.</summary>
        /// <param name="V">the number of vertices in the cycle</param>
        /// <returns>a cycle graph on <c>V</c> vertices</returns>
        ///
        public static Graph Cycle(int V)
        {
            Graph G = new Graph(V);

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            for (int i = 0; i < V - 1; i++)
            {
                G.AddEdge(vertices[i], vertices[i + 1]);
            }
            G.AddEdge(vertices[V - 1], vertices[0]);
            return(G);
        }
コード例 #29
0
ファイル: Quick.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            string[] a = StdIn.ReadAllStrings();
            OrderHelper.Show(a);

            // shuffle
            StdRandom.Shuffle(a);

            // display results again using select
            Console.WriteLine();
            for (int i = 0; i < a.Length; i++)
            {
                string ith = (string)Quick.Select(a, i);
                Console.WriteLine(ith);
            }
        }
コード例 #30
0
        public static void MainTest(string[] args)
        {
            int V = int.Parse(args[0]);
            int E = int.Parse(args[1]);

            // Eulerian cycle
            Graph G1 = GraphGenerator.EulerianCycle(V, E);

            EulerianPath.UnitTest(G1, "Eulerian cycle");

            // Eulerian path
            Graph G2 = GraphGenerator.EulerianPath(V, E);

            EulerianPath.UnitTest(G2, "Eulerian path");

            // add one random edge
            Graph G3 = new Graph(G2);

            G3.AddEdge(StdRandom.Uniform(V), StdRandom.Uniform(V));
            EulerianPath.UnitTest(G3, "one random edge added to Eulerian path");

            // self loop
            Graph G4 = new Graph(V);
            int   v4 = StdRandom.Uniform(V);

            G4.AddEdge(v4, v4);
            EulerianPath.UnitTest(G4, "single self loop");

            // single edge
            Graph G5 = new Graph(V);

            G5.AddEdge(StdRandom.Uniform(V), StdRandom.Uniform(V));
            EulerianPath.UnitTest(G5, "single edge");

            // empty graph
            Graph G6 = new Graph(V);

            EulerianPath.UnitTest(G6, "empty graph");

            // random graph
            Graph G7 = GraphGenerator.Simple(V, E);

            EulerianPath.UnitTest(G7, "simple graph");
        }