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); }
public void TestRemovingEdges() { IWeightedGraph <int> graph = new UndirectedWeightedGraph <int>(3); int[] vertices = { 88, 1, -3, 7, 9, -23, 3, 84, 20, 45 }; byte[,] edges = { { 0, 1, 0, 1, 1, 0, 1, 1, 0, 0 }, { 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 }, { 0, 1, 0, 1, 1, 0, 0, 0, 1, 1 }, { 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 }, { 1, 1, 1, 1, 0, 0, 0, 1, 0, 1 }, { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0 }, { 1, 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 1, 1, 0, 1, 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 } }; Tuple <int, int>[] edges_to_remove = { new Tuple <int, int>(0, 9), new Tuple <int, int>(4, 0), new Tuple <int, int>(7, 4), new Tuple <int, int>(8, 9), new Tuple <int, int>(6, 5), new Tuple <int, int>(2, 3), new Tuple <int, int>(8, 3), new Tuple <int, int>(3, 7), new Tuple <int, int>(6, 7), new Tuple <int, int>(1, 9) }; // Sanity check Assert.AreEqual(vertices.Length, edges.GetLength(0)); Assert.AreEqual(vertices.Length, edges.GetLength(1)); // Add vertices foreach (var vertex in vertices) { graph.AddVertex(vertex); } // Add edges. Iterate over the upper triangular matrix only // as the lower triangular matrix (below the diagonal) must // be its mirror. for (int row = 0; row < vertices.Length; ++row) { for (int col = row + 1; col < vertices.Length; ++col) { // Sanity check Assert.AreEqual(edges[row, col], edges[col, row]); if (Convert.ToBoolean(edges[row, col])) { graph.AddEdge(row, col, 1.0f); } } } // Remove the edges foreach (var edge_to_remove in edges_to_remove) { // Sanity check Assert.GreaterOrEqual(edge_to_remove.Item1, 0); Assert.GreaterOrEqual(edge_to_remove.Item2, 0); Assert.Greater(vertices.Length, edge_to_remove.Item1); Assert.Greater(vertices.Length, edge_to_remove.Item2); graph.RemoveEdge(edge_to_remove.Item1, edge_to_remove.Item2); // Also remove the edge from the 'edges' array used later for verification edges[edge_to_remove.Item1, edge_to_remove.Item2] = edges[edge_to_remove.Item2, edge_to_remove.Item1] = 0; } // Verify that the edges have been removed from the graph for (int row = 0; row < vertices.Length; ++row) { for (int col = 0; col < vertices.Length; ++col) { Assert.AreEqual(Convert.ToBoolean(edges[row, col]), graph.EdgeExists(row, col)); } } }