public void TestAddED_RemoveED_5() { SimpleGraph graph = new SimpleGraph(3); graph.AddVertex(1); graph.AddVertex(2); graph.AddVertex(3); graph.AddEdge(0, 1); graph.AddEdge(0, 2); Assert.IsTrue(graph.m_adjacency[0, 1] == 1); Assert.IsTrue(graph.m_adjacency[0, 2] == 1); graph.RemoveEdge(0, 2); Assert.IsTrue(graph.m_adjacency[0, 1] == 1); Assert.IsTrue(graph.m_adjacency[0, 2] == 0); graph.AddEdge(2, 0); Assert.IsTrue(graph.m_adjacency[0, 1] == 1); Assert.IsTrue(graph.m_adjacency[0, 2] == 1); graph.RemoveEdge(2, 0); Assert.IsTrue(graph.m_adjacency[0, 1] == 1); Assert.IsTrue(graph.m_adjacency[2, 0] == 0); }
// The only way to cover a leaf's edge is by choosing the leaf, or by choosing // its parent. Choosing its parent is better since that'll cover other edges // as well. This solution chooses leaf parents, and removes from the tree any // edges the leaf parent covers. As this removal process happens, new leaves // get created and set aside to work on. Once there are no more leaves left, // we're done. We know this works because assume we missed part of the tree. The // part is also a tree, so it has a leaf. This leaf wasn't one of the initial leaves, // otherwise we would've seen it already. So to be a leaf now it must've had an // edge removed, so it must've been a neighbor of one of the parent leaves, // so we must've seen it, contradiction. public static int Solve(SimpleGraph tree) { var leaves = new HashSet <Vertex>(tree.Vertices.Where(v => v.Degree == 1)); int vertexCoverSize = 0; while (leaves.Any()) { var leaf = leaves.First(); var parentLeaf = leaf.Neighbors.First(); var parentLeafNeighbors = parentLeaf.Neighbors.ToArray(); foreach (var parentLeafNeighor in parentLeafNeighbors) { tree.RemoveEdge(parentLeaf, parentLeafNeighor); if (parentLeafNeighor.Degree == 1) { leaves.Add(parentLeafNeighor); } else if (parentLeafNeighor.Degree == 0) { leaves.Remove(parentLeafNeighor); } } // Parent may have been a leaf, and its degree is 0 now, so try removing. leaves.Remove(parentLeaf); ++vertexCoverSize; } return(vertexCoverSize); }
public void TestRemoveEdge() { Assert.IsTrue(_graph.IsEdge(0, 3)); Assert.IsTrue(_graph.IsEdge(3, 0)); _graph.RemoveEdge(0, 3); Assert.IsFalse(_graph.IsEdge(0, 3)); Assert.IsFalse(_graph.IsEdge(3, 0)); }
public void RemoveEdge() { var g = new SimpleGraph(4); g.AddVertex(1); g.AddVertex(2); g.AddEdge(1, 2); g.AddEdge(1, 1); Assert.True(g.IsEdge(1, 2)); Assert.True(g.IsEdge(1, 1)); g.RemoveEdge(1, 2); Assert.True(!g.IsEdge(1, 2)); Assert.True(g.IsEdge(1, 1)); g.RemoveEdge(1, 1); Assert.True(!g.IsEdge(1, 2)); Assert.True(!g.IsEdge(1, 1)); g.RemoveEdge(3, 3); }
public void removeEdge() { 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(); } }
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); }
public void RemoveEdge_5e_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); Assert.IsTrue(testGraph.vertex[0].Value == 19); Assert.IsTrue(testGraph.vertex[1].Value == 18); Assert.IsTrue(testGraph.vertex[2].Value == 17); Assert.IsTrue(testGraph.vertex[3].Value == 16); Assert.IsTrue(testGraph.vertex[4].Value == 15); Assert.AreEqual(size, testGraph.max_vertex); Assert.AreEqual(size, testGraph.vertex.Length); Assert.AreEqual(size, testGraph.m_adjacency.GetLength(0)); Assert.AreEqual(size, testGraph.m_adjacency.GetLength(1)); Assert.AreEqual(0, testGraph.m_adjacency[0, 2]); Assert.AreEqual(0, testGraph.m_adjacency[2, 0]); testGraph.AddEdge(0, 1); // добавление ребра между вершинами testGraph.AddEdge(0, 2); testGraph.AddEdge(0, 3); testGraph.AddEdge(2, 1); testGraph.AddEdge(2, 3); Assert.IsTrue(testGraph.IsEdge(0, 1)); // проверка добавления ребёр Assert.IsTrue(testGraph.IsEdge(0, 2)); Assert.IsTrue(testGraph.IsEdge(0, 3)); Assert.IsTrue(testGraph.IsEdge(2, 1)); Assert.IsTrue(testGraph.IsEdge(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(); } Assert.AreEqual(1, testGraph.m_adjacency[0, 1]); Assert.AreEqual(1, testGraph.m_adjacency[0, 2]); Assert.AreEqual(1, testGraph.m_adjacency[0, 3]); Assert.AreEqual(1, testGraph.m_adjacency[1, 0]); Assert.AreEqual(1, testGraph.m_adjacency[1, 2]); Assert.AreEqual(1, testGraph.m_adjacency[2, 0]); Assert.AreEqual(1, testGraph.m_adjacency[2, 1]); Assert.AreEqual(1, testGraph.m_adjacency[2, 3]); Assert.AreEqual(1, testGraph.m_adjacency[3, 0]); Assert.AreEqual(1, testGraph.m_adjacency[3, 2]); testGraph.RemoveEdge(0, 1); // удаление ребер между вершинами testGraph.RemoveEdge(0, 2); testGraph.RemoveEdge(0, 3); testGraph.RemoveEdge(2, 1); testGraph.RemoveEdge(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(); } }