예제 #1
0
        public PrimMST(IGraph <TWeight> graph)
        {
            G        = graph;
            ipq      = new IndexMinHeap <TWeight>(G.V);
            marked   = new bool[G.V];
            MSTEdges = new List <Edge <TWeight> >();
            EdgeTo   = new Edge <TWeight> [G.V];

            // Prim
            Visit(0);
            while (!ipq.IsEmpty())
            {
                var v = ipq.ExtractMinIndex();
                if (EdgeTo[v] == null)
                {
                    continue;
                }
                MSTEdges.Add(EdgeTo[v]);
                Visit(v);
            }

            var value     = 0.0;
            var converter = TypeDescriptor.GetConverter(typeof(TWeight));

            foreach (var edge in MSTEdges)
            {
                value += (double)converter.ConvertTo(edge.Weight, typeof(double));
            }
            MSTWeight = (TWeight)converter.ConvertTo(value, typeof(TWeight));
        }