Ejemplo n.º 1
0
        /*********************************************************************************/
        /*                   S  U  P  P  O  R  T     M  E  T  H  O  D  S                 */
        /*********************************************************************************/
        private Edge CreateGraph(int howManyChildren, string nameRoot, out Edge randomInnerEdge)
        {
            randomInnerEdge = null;
            m_edges         = new ArrayList();
            for (int i = 0; i < howManyChildren; i++)
            {
                Edge edge = new Edge(nameRoot + i);
                if (i == ((int)howManyChildren / 2))
                {
                    randomInnerEdge = edge;
                }
                //Debug.WriteLine("Creating edge " + edge.Name);
                m_edges.Add(edge);
            }

            while (true)
            {
                // Select 2 edges, and connect them.
                Edge edgeA = (Edge)((Edge)m_edges[m_random.Next(m_edges.Count)]);
                Edge edgeB = (Edge)((Edge)m_edges[m_random.Next(m_edges.Count)]);

                if (edgeA == edgeB)
                {
                    continue;
                }

                //Debug.WriteLine(String.Format("Considering a connection between {0} and {1}.",edgeA.Name,edgeB.Name));

                int forward  = Graphs.Analysis.PathLength.ShortestPathLength(edgeA, edgeB);
                int backward = Graphs.Analysis.PathLength.ShortestPathLength(edgeB, edgeA);

                //Debug.WriteLine(String.Format("Forward path length is {0}, and reverse path length is {1}.",forward,backward));

                if ((forward == int.MaxValue) && (backward == int.MaxValue))
                {
                    edgeA.AddSuccessor(edgeB);
                    //Debug.WriteLine(String.Format("{0} will follow {1}.",edgeB.Name,edgeA.Name));
                }
                else if ((forward != int.MaxValue) && (backward == int.MaxValue))
                {
                    edgeA.AddSuccessor(edgeB);
                    //Debug.WriteLine(String.Format("{0} will follow {1}.",edgeB.Name,edgeA.Name));
                }
                else if ((forward == int.MaxValue) && (backward != int.MaxValue))
                {
                    edgeB.AddSuccessor(edgeA);
                    //Debug.WriteLine(String.Format("{1} will follow {0}.",edgeB.Name,edgeA.Name));
                }
                else
                {
                    throw new ApplicationException("Cycle exists between " + edgeA.Name + " and " + edgeB.Name + ".");
                }

                // Once all edges are connected to something, we're done constructing the test.
                bool allEdgesAreConnected = true;
                foreach (Edge edge in m_edges)
                {
                    if ((edge.PredecessorEdges.Count == 0) && (edge.SuccessorEdges.Count == 0))
                    {
                        allEdgesAreConnected = false;
                        break;
                    }
                }
                if (allEdgesAreConnected)
                {
                    break;
                }
            }

            Edge root = new Edge("Root");

            foreach (Edge edge in m_edges)
            {
                if (edge.PreVertex.PredecessorEdges.Count == 0)
                {
                    root.AddChildEdge(edge);
                }
                if (edge.PostVertex.SuccessorEdges.Count == 0)
                {
                    root.AddChildEdge(edge);
                }
            }

            return(root);
        }