コード例 #1
0
        /// <summary>
        /// Running Time = O(n + m) linear time
        /// </summary>
        public static void DepthFirstSearch(List<Vertex> vertices, Vertex startVertex, Vertex leader, ref int counter)
        {
            startVertex.IsExplored = true;
            startVertex.Leader = leader;

            foreach (var edge in startVertex.Edges)
            {
                var newVertex = vertices.Find((x) => x.Number == edge);
                if (!newVertex.IsExplored)
                {
                    DepthFirstSearch(vertices, newVertex, leader, ref counter);
                }
            }
            counter++;
            startVertex.Label = counter.ToString();
        }
コード例 #2
0
        private static void MergeTwoVertices(List<Vertex> vertices, Vertex v1, Vertex v2)
        {
            for (int i = 0; i < vertices.Count; i++)
            {
                if (vertices[i].Number != v2.Number)
                {
                    for (int j = 0; j < vertices[i].Edges.Count; j++)
                    {
                        if (vertices[i].Edges[j] == v2.Number)
                            vertices[i].Edges[j] = v1.Number;
                    }
                }
            }

            for (int i = 0; i < v2.Edges.Count; i++)
            {
                v1.Edges.Add(v2.Edges[i]);
            }

            vertices.Remove(v2);
        }