Пример #1
0
        static void Kruskal(Graph graph)
        {
            int weight = 0;

            List <Edge> sortedEdges = graph.edges.OrderBy(o => o.Weight).ToList();

            DisjointSets ds = new DisjointSets(graph.vertices);

            foreach (Edge edge in sortedEdges)
            {
                string u = edge.Source;
                string v = edge.Destination;

                string set_u = ds.findParent(u);
                string set_v = ds.findParent(v);

                if (set_u != set_v)
                {
                    weight += edge.Weight;
                    ds.mergeByRank(set_u, set_v);
                }
            }

            Console.WriteLine("Weight of minimum spanning tree is " + weight);
        }