コード例 #1
0
        ///<summary>
        /// Random graph generator for sparse graphs.
        ///</summary>
        private void GenerateSparseGraph(Graph G)
        {
            G.Clear();
            var index = new Dictionary <Node, int>();

            int n = NodeCount;

            int m = Math.Min(GetMaxEdges(), EdgeCount);

            Node[] V = new Node[n];

            for (int i = 0; i < n; i++)
            {
                V[i]        = G.CreateNode();
                index[V[i]] = i;
            }

            RandomSupport.Permutate(random, V);

            int count = m;

            while (count > 0)
            {
                int  vi = random.Next(n);
                Node v  = V[vi];
                Node w  = V[random.Next(n)];

                if (G.ContainsEdge(v, w) || (v == w && (!AllowSelfLoops || !AllowCycles)))
                {
                    continue;
                }
                G.CreateEdge(v, w);
                count--;
            }

            if (!AllowCycles)
            {
                foreach (Edge edge in G.Edges)
                {
                    if (index[edge.Source] > index[edge.Target])
                    {
                        G.ChangeEdge(edge, edge.Target, edge.Source);
                    }
                }
            }
        }
コード例 #2
0
        ///<summary>
        /// Random graph generator for dense graphs.
        ///</summary>
        private void GenerateDenseGraph(Graph g)
        {
            g.Clear();
            Node[] nodes = new Node[NodeCount];

            for (int i = 0; i < NodeCount; i++)
            {
                nodes[i] = g.CreateNode();
            }

            RandomSupport.Permutate(random, nodes);

            int m = Math.Min(GetMaxEdges(), EdgeCount);
            int n = NodeCount;

            int adder = (AllowSelfLoops && AllowCycles) ? 0 : 1;

            bool[] edgeWanted = RandomSupport.GetBoolArray(random, GetMaxEdges(), m);
            for (int i = 0, k = 0; i < n; i++)
            {
                for (int j = i + adder; j < n; j++, k++)
                {
                    if (edgeWanted[k])
                    {
                        if (AllowCycles && random.NextDouble() > 0.5f)
                        {
                            g.CreateEdge(nodes[j], nodes[i]);
                        }
                        else
                        {
                            g.CreateEdge(nodes[i], nodes[j]);
                        }
                    }
                }
            }
        }