Skip to content

Commit

Permalink
update graph class (code simplify and beautify)
Browse files Browse the repository at this point in the history
  • Loading branch information
UmbrellaLeaf5 committed Dec 9, 2024
1 parent efa50c7 commit 33130a9
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 236 deletions.
44 changes: 22 additions & 22 deletions additional_tasks/chem_experiments_chain/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ TEST(SCCSTA_Test, SimpleCycle) {
Graph<int, long> graph;
graph.AddVert(1);
graph.AddVert(2);
graph.AddEdge(1, 2);
graph.AddEdge(2, 1);
graph.AddEdge({1, 2});
graph.AddEdge({2, 1});
auto components = StronglyConnectedComponents(graph);
ASSERT_EQ(components.size(), 1);
ASSERT_EQ(components[0], std::vector<int>({1, 2}));
Expand All @@ -62,10 +62,10 @@ TEST(SCCSTA_Test, MultipleCycles) {
graph.AddVert(2);
graph.AddVert(3);
graph.AddVert(4);
graph.AddEdge(1, 2);
graph.AddEdge(2, 1);
graph.AddEdge(3, 4);
graph.AddEdge(4, 3);
graph.AddEdge({1, 2});
graph.AddEdge({2, 1});
graph.AddEdge({3, 4});
graph.AddEdge({4, 3});

auto components = StronglyConnectedComponents(graph);

Expand All @@ -86,13 +86,13 @@ TEST(SCCSTA_Test, ComplexGraph) {
graph.AddVert(4);
graph.AddVert(5);
graph.AddVert(6);
graph.AddEdge(1, 2);
graph.AddEdge(2, 3);
graph.AddEdge(3, 1);
graph.AddEdge(4, 5);
graph.AddEdge(5, 6);
graph.AddEdge(6, 4);
graph.AddEdge(2, 4);
graph.AddEdge({1, 2});
graph.AddEdge({2, 3});
graph.AddEdge({3, 1});
graph.AddEdge({4, 5});
graph.AddEdge({5, 6});
graph.AddEdge({6, 4});
graph.AddEdge({2, 4});

auto components = StronglyConnectedComponents(graph);

Expand All @@ -111,7 +111,7 @@ TEST(SCCSTA_Test, UndirectedGraph) {
Graph<int, long> graph;
graph.AddVert(1);
graph.AddVert(2);
graph.AddEdge(1, 2);
graph.AddEdge({1, 2});

graph.MakeUndirected();

Expand All @@ -121,7 +121,7 @@ TEST(SCCSTA_Test, UndirectedGraph) {
TEST(SCCSTA_Test, SelfLoop) {
Graph<int, long> graph;
graph.AddVert(1);
graph.AddEdge(1, 1);
graph.AddEdge({1, 1});

auto components = StronglyConnectedComponents(graph);

Expand All @@ -135,8 +135,8 @@ TEST(SCCSTA_Test, MultipleSelfLoops) {
Graph<int, long> graph;
graph.AddVert(1);
graph.AddVert(2);
graph.AddEdge(1, 1);
graph.AddEdge(2, 2);
graph.AddEdge({1, 1});
graph.AddEdge({2, 2});

auto components = StronglyConnectedComponents(graph);

Expand All @@ -152,11 +152,11 @@ TEST(SCCSTA_Test, ConnectedComponents) {
graph.AddVert(2);
graph.AddVert(3);
graph.AddVert(4);
graph.AddEdge(1, 2);
graph.AddEdge(2, 1);
graph.AddEdge(3, 4);
graph.AddEdge(4, 3);
graph.AddEdge(1, 3);
graph.AddEdge({1, 2});
graph.AddEdge({2, 1});
graph.AddEdge({3, 4});
graph.AddEdge({4, 3});
graph.AddEdge({1, 3});

auto components = StronglyConnectedComponents(graph);

Expand Down
61 changes: 24 additions & 37 deletions additional_tasks/graph/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ TEST(GraphTest_size_t, CreateGraphWithDuplicateEdges) {
// для скорости работы по умолчанию дубликаты допускаются
Graph<size_t, long> g = Graph<size_t, long>::GraphNonWeighted(edges);

ASSERT_EQ(g.VertsAmount(), 3);
ASSERT_EQ(g.EdgesAmount(), 5);
ASSERT_FALSE(g.IsWeighted());
ASSERT_TRUE(g.IsDirected());

g.RemoveDuplicates();

ASSERT_EQ(g.VertsAmount(), 3);
ASSERT_EQ(g.EdgesAmount(), 3);
ASSERT_FALSE(g.IsWeighted());
Expand Down Expand Up @@ -362,15 +355,15 @@ TEST(GraphTest_size_t, AddVert) {
TEST(GraphTest_size_t, AddEdgeUnweighted) {
Graph<size_t, long> g;

g.AddEdge(0, 1);
g.AddEdge({0, 1});
EXPECT_EQ(g.EdgesAmount(), 1);
EXPECT_TRUE(g.ContainsEdge({0, 1}));

g.AddEdge(0, 1);
EXPECT_EQ(g.EdgesAmount(), 2);
g.AddEdge({0, 1});
EXPECT_EQ(g.EdgesAmount(), 1); // теперь в графе только уникальные ребра
EXPECT_TRUE(g.ContainsEdge({0, 1}));

g.AddEdge(2, 3);
g.AddEdge({2, 3});
EXPECT_EQ(g.VertsAmount(), 4);
EXPECT_TRUE(Contains(g.Verts(), size_t(2)));
EXPECT_TRUE(Contains(g.Verts(), size_t(3)));
Expand All @@ -379,34 +372,28 @@ TEST(GraphTest_size_t, AddEdgeUnweighted) {
TEST(GraphTest_size_t, AddEdgeWeighted) {
Graph<size_t, long> g;

g.AddEdge(0, 1, 10);
g.AddEdge({0, 1, 10});
EXPECT_EQ(g.EdgesAmount(), 1);
EXPECT_TRUE(g.ContainsEdge({0, 1, 10}));
EXPECT_EQ(g.GetEdgeWeight({0, 1}), 10);

g.AddEdge(0, 1, 15);

// если пользователь решает добавить то же ребро,
// но с другим весом - он сам дурак

// (иначе добавление ребра будет работать за O(E))

g.RemoveDuplicates();
g.AddEdge({0, 1, 15});

EXPECT_EQ(g.EdgesAmount(), 2);
EXPECT_TRUE(g.ContainsEdge({0, 1, 10}));
EXPECT_EQ(g.GetEdgeWeight({0, 1}), 10);

EXPECT_THROW(g.AddEdge(2, 3), std::logic_error);
// Warning
EXPECT_NO_THROW(g.AddEdge({2, 3}));
}

TEST(GraphTest_size_t, RemoveVert) {
Graph<size_t, long> g;
g.AddVert(1);
g.AddVert(2);
g.AddVert(3);
g.AddEdge(1, 2, 5);
g.AddEdge(2, 3, 7);
g.AddEdge({1, 2, 5});
g.AddEdge({2, 3, 7});

ASSERT_NO_THROW(g.RemoveVert(2));
EXPECT_FALSE(Contains(g.Verts(), size_t(2)));
Expand All @@ -421,8 +408,8 @@ TEST(GraphTest_size_t, RemoveEdgeByPair) {
g.AddVert(1);
g.AddVert(2);
g.AddVert(3);
g.AddEdge(1, 2, 5);
g.AddEdge(2, 3, 7);
g.AddEdge({1, 2, 5});
g.AddEdge({2, 3, 7});

ASSERT_NO_THROW(g.RemoveEdge({1, 2}));
EXPECT_FALSE(g.ContainsEdge({1, 2}));
Expand All @@ -436,8 +423,8 @@ TEST(GraphTest_size_t, RemoveEdgeByTuple) {
g.AddVert(1);
g.AddVert(2);
g.AddVert(3);
g.AddEdge(1, 2, 5);
g.AddEdge(2, 3, 7);
g.AddEdge({1, 2, 5});
g.AddEdge({2, 3, 7});

ASSERT_NO_THROW(g.RemoveEdge({2, 3, 7}));
EXPECT_FALSE(g.ContainsEdge({2, 3, 7}));
Expand Down Expand Up @@ -471,8 +458,8 @@ TEST(GraphTest_size_t, AddAndRemoveEdges) {
g.AddVert(2);
g.AddVert(3);

g.AddEdge(1, 2, 5);
g.AddEdge(2, 3, 7);
g.AddEdge({1, 2, 5});
g.AddEdge({2, 3, 7});
EXPECT_EQ(g.EdgesAmount(), 2);
EXPECT_TRUE(g.ContainsEdge({1, 2, 5}));
EXPECT_TRUE(g.ContainsEdge({2, 3, 7}));
Expand Down Expand Up @@ -506,8 +493,8 @@ TEST(GraphTest_size_t, RemoveEdgeFromDirectedGraph) {
graph_directed.AddVert(0);
graph_directed.AddVert(1);
graph_directed.AddVert(2);
graph_directed.AddEdge(0, 1, 1);
graph_directed.AddEdge(1, 2, 2);
graph_directed.AddEdge({0, 1, 1});
graph_directed.AddEdge({1, 2, 2});

ASSERT_NO_THROW(graph_directed.RemoveEdge({0, 1}));
ASSERT_FALSE(graph_directed.ContainsEdge({0, 1}));
Expand All @@ -519,8 +506,8 @@ TEST(GraphTest_size_t, RemoveEdgeFromUndirectedGraph) {
graph_undirected.AddVert(0);
graph_undirected.AddVert(1);
graph_undirected.AddVert(2);
graph_undirected.AddEdge(0, 1, 1);
graph_undirected.AddEdge(1, 2, 2);
graph_undirected.AddEdge({0, 1, 1});
graph_undirected.AddEdge({1, 2, 2});

graph_undirected.MakeUndirected();

Expand All @@ -535,17 +522,17 @@ TEST(GraphTest_size_t, RemoveNonExistentEdgeThrowsException) {
graph_directed.AddVert(0);
graph_directed.AddVert(1);
graph_directed.AddVert(2);
graph_directed.AddEdge(0, 1, 1);
graph_directed.AddEdge(1, 2, 2);
graph_directed.AddEdge({0, 1, 1});
graph_directed.AddEdge({1, 2, 2});

ASSERT_THROW(graph_directed.RemoveEdge({0, 2}), std::invalid_argument);

Graph<size_t, long> graph_undirected{};
graph_undirected.AddVert(0);
graph_undirected.AddVert(1);
graph_undirected.AddVert(2);
graph_undirected.AddEdge(0, 1, 1);
graph_undirected.AddEdge(1, 2, 2);
graph_undirected.AddEdge({0, 1, 1});
graph_undirected.AddEdge({1, 2, 2});

graph_undirected.MakeUndirected();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inline void AddEdgesNonDirected(Graph<std::string, long>& graph) {

for (const auto& another_vert : {up_vert, down_vert, right_vert, left_vert})
if (!another_vert.empty() && !graph.ContainsEdge({another_vert, vert}))
graph.AddEdge(vert, another_vert);
graph.AddEdge({vert, another_vert});
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ inline void AddEdgesIterative(Graph<std::string, long>& graph,
for (const auto& another_vert : {up_vert, down_vert, right_vert, left_vert})
if (!another_vert.empty() &&
!graph.ContainsEdge({another_vert, current_vert})) {
graph.AddEdge(current_vert, another_vert);
graph.AddEdge({current_vert, another_vert});
if (another_vert[0] != 'E') s.push(another_vert);
}
}
Expand Down
Loading

0 comments on commit 33130a9

Please sign in to comment.