diff --git a/LibraryCPPTemplate/Tests/graph.cpp b/LibraryCPPTemplate/Tests/graph.cpp index 1b77819a50..3a2571bd73 100644 --- a/LibraryCPPTemplate/Tests/graph.cpp +++ b/LibraryCPPTemplate/Tests/graph.cpp @@ -141,7 +141,7 @@ int main() { } //////////Vertex removal check////////////////////////////////////////// - /*graph.add_Edge(2, 3, 5); + graph.add_Edge(2, 3, 5); graph.add_Edge(0, 4, 1); int index1 = 3; @@ -153,7 +153,7 @@ int main() { } else { std::cout << "valid vertex removal" << std::endl; - }*/ + } return 0; diff --git a/LibraryCPPTemplate/graph.h b/LibraryCPPTemplate/graph.h index 7779872bdd..21b40c2073 100644 --- a/LibraryCPPTemplate/graph.h +++ b/LibraryCPPTemplate/graph.h @@ -176,15 +176,18 @@ class Graph { return index; } - void remove_Vertex(size_t index) { - size_t qty_vertex = get_VertexAmount(); + size_t qty_vertex = get_VertexAmount(); if (index >= qty_vertex) { return; } + for (size_t i = index; i < qty_vertex - 1; i++) { + vertices.set(i, vertices.get(i + 1)); + } + vertices.resize(qty_vertex - 1); - for (size_t i = 0; i < qty_vertex; ++i) { + for (size_t i = 0; i < qty_vertex; i++) { Edge* edge = edgeMatrix.get(index * qty_vertex + i); if (edge) { delete edge; @@ -195,29 +198,20 @@ class Graph { } } - - Vector TimeMatrix; - TimeMatrix.resize((qty_vertex - 1) * (qty_vertex - 1)); - - size_t newIndex = 0; - for (size_t i = 0; i < qty_vertex; ++i) { - if (i == index) continue; - - size_t newInnerIndex = 0; - for (size_t j = 0; j < qty_vertex; ++j) { - if (j == index) continue; - - Edge* edge = edgeMatrix.get(i * qty_vertex + j); - TimeMatrix.set(newIndex * (qty_vertex - 1) + newInnerIndex, edge ? new Edge(*edge) : nullptr); - newInnerIndex++; + Vector TimeEdgeMatrix; + size_t qty_vertex1 = get_VertexAmount(); + + TimeEdgeMatrix.resize(qty_vertex1 * qty_vertex1); + for (size_t i = 0; i < qty_vertex1; i++) { + for (size_t j = 0; j < qty_vertex1; j++) { + Edge* edge = edgeMatrix.get(((i + (i >= index)) * qty_vertex) + (j + (j >= index))); + TimeEdgeMatrix.set((i * qty_vertex1) + j, edge); } - newIndex++; } - edgeMatrix.swap_data(TimeMatrix); - vertices.resize(qty_vertex - 1); - } + edgeMatrix.swap_data(TimeEdgeMatrix); + } std::vector get_AllVertexData() const { std::vector allData;