コード例 #1
0
        public void TestAddED_RemoveED_3()
        {
            SimpleGraph graph = new SimpleGraph(4);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(2, 3);
            Assert.IsFalse(graph.IsEdge(1, 3));
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(0, 2));
            Assert.IsTrue(graph.IsEdge(0, 3));
            Assert.IsTrue(graph.IsEdge(2, 3));

            graph.RemoveVertex(2);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsFalse(graph.IsEdge(0, 2));
            Assert.IsFalse(graph.IsEdge(2, 0));

            Assert.IsFalse(graph.IsEdge(3, 2));
        }
コード例 #2
0
        public void RemoveVertex_1v_in_5v_Graph()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(19);
            testGraph.AddVertex(18);
            testGraph.AddVertex(17);
            testGraph.AddVertex(16);
            testGraph.AddVertex(15);

            testGraph.AddEdge(0, 0); // добавление ребёр между вершинами
            testGraph.AddEdge(0, 1);
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(2, 1);
            testGraph.AddEdge(2, 3);

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine();

            testGraph.RemoveVertex(0); // удаление вершины

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            Assert.AreEqual(0, testGraph.m_adjacency[0, 0]); // проверка удаления ребра вершины на саму себя
            Assert.AreEqual(0, testGraph.m_adjacency[0, 1]); // проверка удаления всех рёбер
            Assert.AreEqual(0, testGraph.m_adjacency[1, 0]);
            Assert.AreEqual(0, testGraph.m_adjacency[0, 2]);
            Assert.AreEqual(0, testGraph.m_adjacency[2, 0]);
            Assert.AreEqual(0, testGraph.m_adjacency[0, 3]);
            Assert.AreEqual(0, testGraph.m_adjacency[3, 0]);
            Assert.AreEqual(1, testGraph.m_adjacency[1, 2]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 1]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 3]);
            Assert.AreEqual(1, testGraph.m_adjacency[3, 2]);

            Assert.IsNull(testGraph.vertex[0]); // вершина удалена
        }
コード例 #3
0
ファイル: TestsSimpleGraph.cs プロジェクト: ostart/C-sharp
 public void TestRemoveVertex()
 {
     Assert.IsNotNull(_graph.vertex[3]);
     for (int i = 0; i < _graph.max_vertex - 1; i++)
     {
         Assert.IsTrue(_graph.IsEdge(i, 3));
         Assert.IsTrue(_graph.IsEdge(3, i));
     }
     _graph.RemoveVertex(3);
     Assert.IsNull(_graph.vertex[3]);
     for (int i = 0; i < _graph.max_vertex; i++)
     {
         Assert.IsFalse(_graph.IsEdge(i, 3));
         Assert.IsFalse(_graph.IsEdge(3, i));
     }
 }
コード例 #4
0
        public void TestAddED_RemoveED_2()
        {
            SimpleGraph graph = new SimpleGraph(4);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(2, 3);
            Assert.IsFalse(graph.IsEdge(1, 3));
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(0, 2));
            Assert.IsTrue(graph.IsEdge(0, 3));
            Assert.IsTrue(graph.IsEdge(2, 3));

            graph.RemoveVertex(2);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsFalse(graph.IsEdge(0, 2));
            Assert.IsFalse(graph.IsEdge(2, 0));

            Assert.IsFalse(graph.IsEdge(2, 3));
            Assert.IsFalse(graph.IsEdge(3, 2));

            Assert.IsTrue(graph.vertex[0] != null);
            Assert.IsTrue(graph.vertex[1] != null);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsTrue(graph.vertex[3] != null);

            Assert.IsTrue(graph.m_adjacency[0, 0] == 0);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 2] == 0);
            Assert.IsTrue(graph.m_adjacency[0, 3] == 1);
            Assert.IsTrue(graph.m_adjacency[1, 0] == 1);
            Assert.IsTrue(graph.m_adjacency[1, 2] == 0);
            Assert.IsTrue(graph.m_adjacency[1, 3] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 0] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 1] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 3] == 0);
            Assert.IsTrue(graph.m_adjacency[3, 0] == 1);
            Assert.IsTrue(graph.m_adjacency[3, 1] == 0);
            Assert.IsTrue(graph.m_adjacency[3, 2] == 0);
        }
コード例 #5
0
        public void TestAddRemove_Edge_1()
        {
            SimpleGraph graph = new SimpleGraph(5);

            graph.AddVertex(2);
            graph.AddVertex(5);

            graph.AddEdge(0, 1);
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsFalse(graph.IsEdge(4, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(1, 0));

            graph.RemoveVertex(0);
            Assert.IsTrue(graph.vertex[0] == null);
            for (int i = 0; i < graph.vertex.Length; i++)
            {
                Assert.IsTrue(graph.m_adjacency[i, 0] == 0);
                Assert.IsTrue(graph.m_adjacency[0, i] == 0);
            }
        }
コード例 #6
0
        public void TestAddED_RemoveED_4()
        {
            SimpleGraph graph = new SimpleGraph(7);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);
            graph.AddVertex(5);
            graph.AddVertex(6);
            graph.AddVertex(7);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(0, 4);
            graph.AddEdge(0, 5);
            graph.AddEdge(0, 6);
            graph.AddEdge(3, 5);
            Assert.IsTrue(graph.m_adjacency[0, 3] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 5] == 1);
            Assert.IsTrue(graph.vertex[3] != null);
            Assert.IsTrue(graph.vertex[5] != null);

            graph.RemoveVertex(3);
            Assert.IsTrue(graph.m_adjacency[0, 3] == 0);
            Assert.IsTrue(graph.m_adjacency[0, 5] == 1);
            Assert.IsTrue(graph.vertex[3] == null);
            Assert.IsTrue(graph.vertex[5] != null);

            graph.RemoveEdge(0, 5);
            Assert.IsTrue(graph.m_adjacency[0, 5] == 0);
            Assert.IsTrue(graph.vertex[5] != null);

            graph.AddEdge(5, 0);
            Assert.IsTrue(graph.m_adjacency[0, 5] == 1);
            Assert.IsTrue(graph.vertex[5] != null);
        }
コード例 #7
0
        public void RemoveVertex()
        {
            var g = new SimpleGraph(10);

            g.AddVertex(1);
            g.AddVertex(2);
            g.AddVertex(3);
            g.AddVertex(4);

            Assert.True(!g.IsEdge(1, 1));
            Assert.True(!g.IsEdge(1, 2));
            Assert.True(!g.IsEdge(1, 3));
            Assert.True(!g.IsEdge(1, 4));

            Assert.True(!g.IsEdge(2, 1));
            Assert.True(!g.IsEdge(2, 2));
            Assert.True(!g.IsEdge(2, 3));
            Assert.True(!g.IsEdge(2, 4));

            Assert.True(!g.IsEdge(3, 1));
            Assert.True(!g.IsEdge(3, 2));
            Assert.True(!g.IsEdge(3, 3));
            Assert.True(!g.IsEdge(3, 4));

            Assert.True(!g.IsEdge(4, 1));
            Assert.True(!g.IsEdge(4, 2));
            Assert.True(!g.IsEdge(4, 3));
            Assert.True(!g.IsEdge(4, 4));

            g.AddEdge(1, 1);
            g.AddEdge(1, 2);
            g.AddEdge(1, 3);
            g.AddEdge(1, 4);

            g.AddEdge(2, 1);
            g.AddEdge(2, 2);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);

            g.AddEdge(3, 1);
            g.AddEdge(3, 2);
            g.AddEdge(3, 3);
            g.AddEdge(3, 4);

            g.AddEdge(4, 1);
            g.AddEdge(4, 2);
            g.AddEdge(4, 3);
            g.AddEdge(4, 4);

            Assert.True(g.IsEdge(1, 1));
            Assert.True(g.IsEdge(1, 2));
            Assert.True(g.IsEdge(1, 3));
            Assert.True(g.IsEdge(1, 4));

            Assert.True(g.IsEdge(2, 1));
            Assert.True(g.IsEdge(2, 2));
            Assert.True(g.IsEdge(2, 3));
            Assert.True(g.IsEdge(2, 4));

            Assert.True(g.IsEdge(3, 1));
            Assert.True(g.IsEdge(3, 2));
            Assert.True(g.IsEdge(3, 3));
            Assert.True(g.IsEdge(3, 4));

            Assert.True(g.IsEdge(4, 1));
            Assert.True(g.IsEdge(4, 2));
            Assert.True(g.IsEdge(4, 3));
            Assert.True(g.IsEdge(4, 4));

            g.RemoveVertex(3);

            Assert.True(g.IsEdge(1, 1));
            Assert.True(g.IsEdge(1, 2));
            Assert.True(!g.IsEdge(1, 3));
            Assert.True(g.IsEdge(1, 4));

            Assert.True(g.IsEdge(2, 1));
            Assert.True(g.IsEdge(2, 2));
            Assert.True(!g.IsEdge(2, 3));
            Assert.True(g.IsEdge(2, 4));

            Assert.True(!g.IsEdge(3, 1));
            Assert.True(!g.IsEdge(3, 2));
            Assert.True(!g.IsEdge(3, 3));
            Assert.True(!g.IsEdge(3, 4));

            Assert.True(g.IsEdge(4, 1));
            Assert.True(g.IsEdge(4, 2));
            Assert.True(!g.IsEdge(4, 3));
            Assert.True(g.IsEdge(4, 4));
        }
コード例 #8
0
        public void deleteverted()
        {
            int[] arr = { 10, 15, 21, 23 };

            foreach (int item in arr)
            {
                graph.AddVertex(item);
            }
            int[] j = { 0, 0, 1, 0, 2 };
            int[] k = { 1, 2, 2, 3, 3 };
            for (int i1 = 0; i1 < j.Length; i1++)
            {
                if (!graph.IsEdge(j[i1], k[i1]))
                {
                    graph.AddEdge(j[i1], k[i1]);
                    if (!graph.IsEdge(j[i1], k[i1]))
                    {
                        Assert.Fail();
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            if (!graph.IsEdge(0, 3))
            {
                Assert.Fail();
            }
            graph.RemoveEdge(0, 3);
            if (graph.IsEdge(0, 3))
            {
                Assert.Fail();
            }

            int edgecount = 0;

            for (int i2 = 0; i2 < graph.max_vertex; i2++)
            {
                if (!graph.IsEdge(1, i2))
                {
                    edgecount++;
                }
            }
            if (edgecount == graph.max_vertex)
            {
                Assert.Fail();
            }
            graph.RemoveVertex(1);
            edgecount = 0;
            for (int i2 = 0; i2 < graph.max_vertex; i2++)
            {
                if (!graph.IsEdge(1, i2))
                {
                    edgecount++;
                }
            }
            if (edgecount != graph.max_vertex)
            {
                Assert.Fail();
            }
            graph.output();
        }