Exemplo n.º 1
0
        private void TestMstAlgorithm(IWeightedGraph <int, double> graph, double expectedMstWeight, double precision, ALGORITHM algorithm)
        {
            // Build minimum spanning tree with algorithm to test
            IWeightedGraph <int, double> msp;

            switch (algorithm)
            {
            case ALGORITHM.KRUSKAL:
                msp = Kruskal.FindMinimumSpanningTree(graph);
                break;

            case ALGORITHM.PRIM:
                msp = Prim.FindMinimumSpanningTree(graph, 0, double.MaxValue);
                break;

            default:
                throw new NotImplementedException($"No minimum spanning tree test implemented for algorithm {algorithm.ToString()}.");
            }

            // Check that all verteces are included
            Assert.AreEqual(graph.VertexCount, msp.VertexCount);

            // Count edges and compute total weight
            IEnumerable <IWeightedEdge <int, double> > edges = msp.GetAllEdges();
            int    edgeCount = edges.Count();
            double mspWeight = edges.Sum(e => e.Weight);

            // Check edge count
            Assert.AreEqual(graph.VertexCount - 1, edgeCount);

            // Check that the total weight is as expected
            AssertDoublesNearlyEqual(expectedMstWeight, mspWeight, precision);
        }