public void Kruskal()
        {
            for (int i = 0; i < NumberOfComponents; i++)
            {
                DisjointSet set = new DisjointSet();
                foreach (var nodesKey in Nodes.Keys)
                {
                    if (Nodes[nodesKey].ComponentIndex == i)
                    {
                        set.MakeSet(Nodes[nodesKey]);
                    }
                }

                List <Edge <MultiGraphNode> > sortedEdges = Edges.OrderBy(x => x.Weight).ToList();

                foreach (var edge in sortedEdges)
                {
                    if (set.FindSet(edge.Node1) != set.FindSet(edge.Node2))
                    {
                        MSFWeight += edge.Weight;
                        set.Union(edge.Node1, edge.Node2);
                    }
                }
            }
        }
        public static void Kruskal(WeightedUndirectedGraph <string> graph)
        {
            DisjointSet set = new DisjointSet();

            foreach (var nodesKey in graph.Nodes.Keys)
            {
                set.MakeSet(nodesKey);
            }

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

            foreach (var edge in sortedEdges)
            {
                if (set.FindSet(edge.Node1) != set.FindSet(edge.Node2))
                {
                    edge.PrintEdge();
                    set.Union(edge.Node1, edge.Node2);
                }
            }
        }
Ejemplo n.º 3
0
        public static void Kruskal(ref WeightedUndirectedGraph<T> graph) 
        {
            DisjointSet<T>set = new DisjointSet<T>();
            foreach (var nodesKey in graph.Nodes.Keys)
            {
                set.MakeSet(nodesKey);
            }

            List<Edge<T>> sortedEdges = new List<Edge<T>>();
         
            sortedEdges = graph.Edges.OrderBy(if(x is WeightedUndirectedGraphNode<T, int> node) => node.Weight).ToList<T>();

            foreach (var edge in sortedEdges)
            {
                if (set.FindSet(edge.Node1) != set.FindSet(edge.Node2))
                {
                    edge.PrintEdge();
                    set.Union(edge.Node1, edge.Node2);
                }
            }
        }