/** * 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; } } }