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

                            visitEdges.Add(edges[i]);

                //Если до вершины можно дойти несколькими путями, выбираем кратчайший. Остальные варианты удаляем

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

                {

                    for (int j = 0; j < visitEdges.Count; ++j)

                    {

                        if (visitEdges[i].EndV == visitEdges[j].EndV && visitEdges[i].Weight < visitEdges[j].Weight)

                        {

                            visitEdges.RemoveAt(j);

                            j--;

                            if (i == visitEdges.Count)

                                i--;

                        }

                    }

                }

                int min = Int32.MaxValue;

                int iMin = -1;

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

                {

                    if (visitEdges[i].Weight < min)

                    {

                        min = visitEdges[i].Weight;

                        iMin = i;

                    }

                }

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

                length += visitEdges[iMin].Weight;

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

                visitEdges.RemoveAt(iMin);

            }

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

            return res;

        }

        private int IndexOf(List<list> vList, string vertex)

        {

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

            {

                if (vList[i].Vertex == vertex)

                    return i;

            }

            return -1;

        }

        private List<list> ChangeList(List<list> vList, string startV, string endV)

        {

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

            {

                if (vList[i].VList.Contains(startV))

                {

                    vList[i].VList.Add(endV);

                    vList[i].ClearDublicates();

                }              

            }

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

            {

                if (vList[i].VList.Contains(endV))

                {

                    vList[i].VList.Add(startV);

                    vList[i].ClearDublicates();

                }

            }

            int ind1 = IndexOf(vList, startV);

            int ind2 = IndexOf(vList, endV);

            vList[ind1].VList.AddRange(vList[ind2].VList);

            vList[ind2].VList.AddRange(vList[ind1].VList);

            vList[ind1].ClearDublicates();

            vList[ind2].ClearDublicates();

            return vList;

        }

    }

}

Класс Edge

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;

namespace lab4

{

    class Edge: IComparable

    {

        string startV;//Откуда

        string endV;//Куда

        int weight;//Вес ребра

        public Edge(string startV, string endV, int weight)

        {

            if (weight < 0)

                throw new Exception("Неверные данные!");

            this.startV = startV;

            this.endV = endV;

            this.weight = weight;

        }

        /// <summary>

        /// Откуда идет ребро

        /// </summary>

        public string StartV

        {

            get { return startV; }

            set { startV = value; }

        }

        /// <summary>

        /// Куда идет ребро

        /// </summary>

        public string EndV

        {

            get { return endV; }

            set { endV = value; }

        }