Построение минимального остового дерева. Изучение основных алгоритмов построение минимальных остовых деревьев, страница 2

                    if ((visited.Contains(sortedEdg[i].StartV) && !visited.Contains(sortedEdg[i].EndV)) || (!visited.Contains(sortedEdg[i].StartV) && visited.Contains(sortedEdg[i].EndV)))

                    {

                        res += sortedEdg[i].StartV + "->" + sortedEdg[i].EndV + "\n";

                        length += sortedEdg[i].Weight;

                        if (visited.Contains(sortedEdg[i].EndV))

                            visited.Add(sortedEdg[i].StartV);

                        else

                            visited.Add(sortedEdg[i].EndV);

                        break;

                    }

                }

            }

            res += "Вес минимального остового дерева равен " + length;

            return res;

        }

        /// <summary>

        ///Алгоритм Краскала

        /// </summary>

        /// <returns></returns>

        public string Kraskal()

        {

            string res="";

            List<string> visited = new List<string>();

            int length = 0;

            //Создадим список смежности

            List<list> vList = new List<list>();

            for (int i = 0; vList.Count<vCount; ++i)

            {

                if (IndexOf(vList, edges[i].StartV) == -1)

                {

                    list tmp = new list(edges[i].StartV);

                    vList.Add(tmp);

                } if (IndexOf(vList, edges[i].EndV) == -1)

                {

                    list tmp = new list(edges[i].EndV);

                    vList.Add(tmp);

                }

            }

            while (/*visited.Count<vCount*/res.Split('\n').Length!=vCount)

            {

                int min = Int32.MaxValue;

                int iMin = -1;

                for (int i = 0; i < edges.Count; ++i)

                {

                    int index = IndexOf(vList, edges[i].StartV);

                    if (!vList[index].VList.Contains(edges[i].EndV))

                    {

                        if (edges[i].Weight < min)

                        {

                            min = edges[i].Weight;

                            iMin = i;

                        }

                    }                   

                }

                    vList = ChangeList(vList, edges[iMin].StartV, edges[iMin].EndV);

                    if (!visited.Contains(edges[iMin].EndV))

                        visited.Add(edges[iMin].EndV);

                    if (!visited.Contains(edges[iMin].StartV))

                        visited.Add(edges[iMin].StartV);

                    res += edges[iMin].StartV + " -> " + edges[iMin].EndV + "\n";

                    length += edges[iMin].Weight;

            }

            res += "Вес минимального остового дерева равен " + length;

            return res;

        }

        /// <summary>

        /// Алгоритм Дейкстры-ПРима

        /// </summary>

        /// <param name="startV"></param>

        /// <returns></returns>

        public string DeikstraPrim(string startV)

        {

            string res = "";

            int length = 0;

            List<string> visited = new List<string>();//Список посещенных вершин

            visited.Add(startV);

            List<Edge> visitEdges = new List<Edge>();//список ребер, куда можно пойти

            while (visited.Count < vCount)

            {

                //Составляем список ребер, куда можно пойти

          //   

                    for (int i = 0; i < edges.Count; ++i)

                        if (visited.Contains(edges[i].StartV) && !visited.Contains(edges[i].EndV)&&!visitEdges.Contains(edges[i]))