Ejemplo n.º 1
0
        private static IEnumerable<Edge> Kruskal(Vertex[] nodes, Edge[] edges)
        {
            var workingEdges = new Queue<Edge>(edges.OrderBy(e => e.Cost).ToArray());
            var forest = MakeSet(nodes).ToArray();
            Console.WriteLine("Forest: {0}", string.Join<Tree>("|", forest));

            while (1 < forest.Count())
            {
                var edge = workingEdges.Dequeue();
                var vtrees = TreesWith(edge.Ends[0], forest).ToArray();
                var utrees = TreesWith(edge.Ends[1], forest).ToArray();
                if (0 < vtrees.Length && 0 < utrees.Length)
                {
                    forest = MakeUnions(vtrees, utrees, edge, forest).ToArray();
                    Console.WriteLine("Forest: {0}", string.Join<Tree>("|", forest));
                }
            }

            return forest.First().Edges;
        }