예제 #1
0
        public void TestPrimsAlgorithm()
        {
            Graph <int> g = new Graph <int>(false);

            List <Vertex <int> > vertices = new List <Vertex <int> >();

            for (int i = 1; i < 15; i++)
            {
                vertices.Add(g.AddVertex(i));
            }

            AddEdge(g, vertices, 1, 2, 5);
            AddEdge(g, vertices, 1, 5, 4);
            AddEdge(g, vertices, 2, 4, 10);
            AddEdge(g, vertices, 2, 3, 6);
            AddEdge(g, vertices, 3, 4, 2);
            AddEdge(g, vertices, 3, 5, 6);
            AddEdge(g, vertices, 3, 7, 4);
            AddEdge(g, vertices, 5, 6, 1);
            AddEdge(g, vertices, 6, 7, 1);
            AddEdge(g, vertices, 6, 8, 9);
            AddEdge(g, vertices, 6, 9, 5);
            AddEdge(g, vertices, 7, 9, 3);
            AddEdge(g, vertices, 7, 10, 4);
            AddEdge(g, vertices, 9, 10, 6);
            AddEdge(g, vertices, 9, 12, 2);
            AddEdge(g, vertices, 11, 12, 9);
            AddEdge(g, vertices, 11, 13, 8);
            AddEdge(g, vertices, 13, 14, 6);

            Graph <int> result = PrimsAlgorithm <int> .FindMinimalSpanningTree(g, vertices[0]);

            Assert.AreEqual(result.ContainsEdge(1, 2), true);
            Assert.AreEqual(result.ContainsEdge(1, 5), true);
            Assert.AreEqual(result.ContainsEdge(5, 6), true);
            Assert.AreEqual(result.ContainsEdge(6, 8), true);
            Assert.AreEqual(result.ContainsEdge(6, 7), true);
            Assert.AreEqual(result.ContainsEdge(7, 3), true);
            Assert.AreEqual(result.ContainsEdge(3, 4), true);
            Assert.AreEqual(result.ContainsEdge(7, 9), true);
            Assert.AreEqual(result.ContainsEdge(7, 10), true);
            Assert.AreEqual(result.ContainsEdge(9, 12), true);
            Assert.AreEqual(result.ContainsEdge(12, 11), true);
            Assert.AreEqual(result.ContainsEdge(11, 13), true);
            Assert.AreEqual(result.ContainsEdge(13, 14), true);

            Assert.AreEqual(result.EdgeCount, 13);

            double totalCost = 0;

            using (IEnumerator <Edge <int> > enumerator = result.Edges) {
                while (enumerator.MoveNext())
                {
                    totalCost += enumerator.Current.Weight;
                }
            }

            Assert.AreEqual(totalCost, 58);
        }
예제 #2
0
 public void TestNullVertex()
 {
     PrimsAlgorithm <int> .FindMinimalSpanningTree(new Graph <int>(true), null);
 }
예제 #3
0
 public void TestInvalidVertex()
 {
     PrimsAlgorithm <int> .FindMinimalSpanningTree(new Graph <int>(true), new Vertex <int>(5));
 }
예제 #4
0
 public void TestNullGraph()
 {
     PrimsAlgorithm <int> .FindMinimalSpanningTree(null, new Vertex <int>(5));
 }