예제 #1
0
        /// <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);
        }
예제 #2
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();
            }
        }
예제 #3
0
        // tournament
        /// <summary>
        /// Returns a random tournament digraph on <c>V</c> vertices. A tournament digraph
        /// is a DAG in which for every two vertices, there is one directed edge.
        /// A tournament is an oriented complete graph.</summary>
        /// <param name="V">the number of vertices</param>
        /// <returns>a random tournament digraph on <c>V</c> vertices</returns>
        ///
        public static Digraph Tournament(int V)
        {
            Digraph G = new Digraph(V);

            for (int v = 0; v < G.V; v++)
            {
                for (int w = v + 1; w < G.V; w++)
                {
                    if (StdRandom.Bernoulli(0.5))
                    {
                        G.AddEdge(v, w);
                    }
                    else
                    {
                        G.AddEdge(w, v);
                    }
                }
            }
            return(G);
        }
예제 #4
0
        /// <summary>
        /// Returns a random simple graph on <c>V</c> vertices, with an
        /// edge between any two vertices with probability <c>p</c>. This is sometimes
        /// referred to as the Erdos-Renyi random graph model.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="p">the probability of choosing an edge</param>
        /// <returns>a random simple graph on <c>V</c> vertices, with an edge between
        ///    any two vertices with probability <c>p</c></returns>
        /// <exception cref="ArgumentException">if probability is not between 0 and 1</exception>
        ///
        public static Graph Simple(int V, double p)
        {
            if (p < 0.0 || p > 1.0)
            {
                throw new ArgumentException("Probability must be between 0 and 1");
            }
            Graph G = new Graph(V);

            for (int v = 0; v < V; v++)
            {
                for (int w = v + 1; w < V; w++)
                {
                    if (StdRandom.Bernoulli(p))
                    {
                        G.AddEdge(v, w);
                    }
                }
            }
            return(G);
        }