예제 #1
0
        /// <summary>
        /// Computes the minimum spanning tree using Kruskal's algorithm.
        /// </summary>
        /// <typeparam name="TVertex">type of the vertices</typeparam>
        /// <typeparam name="TEdge">type of the edges</typeparam>
        /// <param name="visitedGraph"></param>
        /// <param name="weights"></param>
        /// <returns></returns>
        public static IEnumerable <TEdge> MinimumSpanningTreeKruskal <TVertex, TEdge>(
            IUndirectedGraph <TVertex, TEdge> visitedGraph,
            Func <TEdge, double> weights)
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(weights != null);

            if (visitedGraph.VertexCount == 0)
            {
                return(new TEdge[0]);
            }

            var kruskal      = new KruskalMinimumSpanningTreeAlgorithm <TVertex, TEdge>(visitedGraph, weights);
            var edgeRecorder = new EdgeRecorderObserver <TVertex, TEdge>();

            using (edgeRecorder.Attach(kruskal))
                kruskal.Compute();

            return(edgeRecorder.Edges);
        }