Exemplo n.º 1
0
        public void Test()
        {
            UndirectedWeightedGraph graph1 = new UndirectedWeightedGraph(false);

            #region Basic stuff
            Assert.AreEqual(false, graph1.directed);
            Assert.AreEqual(true, graph1.weighted);
            Assert.AreEqual(false, graph1.multigraph);
            #endregion

            #region Vertex stuff
            for (char i = 'a'; i <= 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(i));
            }
            graph1.AddVertex("a");

            Assert.AreEqual(8, graph1.verticesCount);

            graph1.RemoveVertex("h");
            graph1.RemoveVertex("z");

            Assert.AreEqual(7, graph1.verticesCount);
            Assert.AreEqual(true, graph1.ExistVertex("a"));
            Assert.AreEqual(false, graph1.ExistVertex("z"));
            #endregion

            #region Edge stuff
            graph1.AddEdge("a", "b", 10);
            graph1.AddEdge("a", "b", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("a", "a");
            graph1.AddEdge("a", "a", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("a", "z", 2);
            graph1.AddEdge("z", "a", 2);
            graph1.AddEdge("y", "z", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("b", "c", 1);
            graph1.AddEdge("c", "d", 1);
            graph1.AddEdge("d", "e", 1);
            graph1.AddEdge("e", "f", 1);
            graph1.AddEdge("f", "g", 1);

            Assert.AreEqual(6, graph1.edgesCount);

            graph1.RemoveEdge("a", "b");
            graph1.RemoveEdge("a", "c", 1);
            graph1.RemoveEdge("f", "g", 1);

            Assert.AreEqual(5, graph1.edgesCount);
            Assert.AreEqual(true, graph1.ExistEdge("a", "b"));
            Assert.AreEqual(true, graph1.ExistEdge("b", "a"));

            Assert.AreEqual(10, graph1.GetEdgeWeight("a", "b"));

            graph1.ChangeWeight("a", "b", 10, 20);

            Assert.AreEqual(20, graph1.GetEdgeWeight("a", "b"));

            graph1.RemoveVertex("d");
            Assert.AreEqual(3, graph1.edgesCount);
            #endregion

            graph1.Clear();

            Assert.AreEqual(0, graph1.verticesCount);
            Assert.AreEqual(0, graph1.edgesCount);


            UndirectedWeightedGraph graph2 = new UndirectedWeightedGraph(true);

            #region Basic stuff
            Assert.AreEqual(false, graph2.directed);
            Assert.AreEqual(true, graph2.weighted);
            Assert.AreEqual(true, graph2.multigraph);
            #endregion

            #region Vertex stuff
            for (char i = 'a'; i <= 'h'; i++)
            {
                graph2.AddVertex(Convert.ToString(i));
            }
            graph2.AddVertex("a");

            Assert.AreEqual(8, graph2.verticesCount);

            graph2.RemoveVertex("h");
            graph2.RemoveVertex("z");

            Assert.AreEqual(7, graph2.verticesCount);
            Assert.AreEqual(true, graph2.ExistVertex("a"));
            Assert.AreEqual(false, graph2.ExistVertex("z"));
            #endregion

            #region Edge stuff
            graph2.AddEdge("a", "b", 10);
            graph2.AddEdge("a", "b", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("a", "a");
            graph2.AddEdge("a", "a", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("a", "z", 2);
            graph2.AddEdge("z", "a", 2);
            graph2.AddEdge("y", "z", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("b", "c", 1);
            graph2.AddEdge("c", "d", 1);
            graph2.AddEdge("d", "e", 1);
            graph2.AddEdge("e", "f", 1);
            graph2.AddEdge("f", "g", 1);

            Assert.AreEqual(7, graph2.edgesCount);

            graph2.RemoveEdge("a", "b");
            graph1.RemoveEdge("a", "c", 1);
            graph2.RemoveEdge("f", "g", 1);

            Assert.AreEqual(6, graph2.edgesCount);
            Assert.AreEqual(true, graph2.ExistEdge("a", "b"));

            Assert.AreEqual(1, graph2.GetEdgeWeight("a", "b"));

            graph2.ChangeWeight("a", "b", 10, 20);

            Assert.AreEqual(1, graph2.GetEdgeWeight("a", "b"));

            graph2.RemoveVertex("d");
            Assert.AreEqual(4, graph2.edgesCount);
            #endregion

            graph2.Clear();

            Assert.AreEqual(0, graph2.verticesCount);
            Assert.AreEqual(0, graph2.edgesCount);
        }
        /**
         * Helper method that removes the specified vertex and checks
         * whether the vertex has been removed along with its associated
         * edges.
         */
        private void RemoveVertexHelper(int[] vertices, byte[,] edges, int index)
        {
            // Sanity check
            Assert.AreEqual(vertices.Length, edges.GetLength(0));
            Assert.AreEqual(vertices.Length, edges.GetLength(1));

            IWeightedGraph <int> graph = new UndirectedWeightedGraph <int>(2);

            // Add the vertices
            foreach (var vertex in vertices)
            {
                Assert.IsTrue(graph.AddVertex(vertex));
            }

            // Add the edges. Iterate over the lower triangular matrix
            // only as the upper triangular matrix (above the diagonal)
            // is the mirror of the lower triangular matrix.
            int row, col;

            for (row = 1; row < vertices.Length; ++row)
            {
                for (col = 0; col < row; ++col)
                {
                    // Sanity check
                    Assert.AreEqual(edges[row, col], edges[col, row]);

                    if (Convert.ToBoolean(edges[row, col]))
                    {
                        Assert.IsTrue(graph.AddEdge(row, col, 1.0f));
                    }
                }
            }

            // Remove the vertex
            Assert.AreEqual(vertices[index], graph.RemoveVertex(index));

            // Confirm that the graph size has been decremented
            Assert.AreEqual(vertices.Length - 1, graph.Size);

            // Confirm that vertex is no longer in the graph.
            Assert.AreEqual(-1, graph.GetIndexOf(vertices[index]));

            // Confirm that edges corresponding to the removed vertex have
            // also been removed from the graph
            row = 0;
            for (int i = 0; i < vertices.Length; ++i)
            {
                if (i != index)
                {
                    // We mustn't iterate over the row that belongs to the
                    // removed vertex
                    col = 0;
                    for (int j = 0; j < vertices.Length; ++j)
                    {
                        if (j != index)
                        {
                            // We must skip the column that correponds to the
                            // removed vertex
                            Assert.AreEqual(Convert.ToBoolean(edges[i, j]), graph.EdgeExists(row, col++));
                        }
                    }
                    ++row;
                }
            }
        }