Skip to content

Graph class ~/lib/src #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4540813
Graph class ~/lib/src
LightAboveFighter Sep 11, 2024
f9b6ed9
simple Graph
LightAboveFighter Jan 13, 2025
f28131b
task_01
LightAboveFighter Jan 13, 2025
177a3a4
task_04
LightAboveFighter Jan 13, 2025
0630f42
format
LightAboveFighter Jan 15, 2025
72c8f79
Merge branch 'main' into main
LightAboveFighter Jan 15, 2025
6a5fd0e
test base task_01
LightAboveFighter Jan 15, 2025
1466e47
Merge branch 'main' of https://github.com/LightAboveFighter/autumn_ho…
LightAboveFighter Jan 15, 2025
5cf054a
format
LightAboveFighter Jan 15, 2025
de3cf01
fix includes
LightAboveFighter Jan 15, 2025
87cbbc5
clang tidy advices
LightAboveFighter Jan 15, 2025
03ee99e
tests task_01
LightAboveFighter Jan 15, 2025
c3a46c5
fix tests
LightAboveFighter Jan 15, 2025
7de22a7
task_02
LightAboveFighter Jan 16, 2025
c7b68c0
format
LightAboveFighter Jan 16, 2025
9a84bec
split task_01 to .hpp .cpp files
LightAboveFighter Jan 16, 2025
e3bf6a2
task_04
LightAboveFighter Jan 16, 2025
e49a06a
convenient graph node constructor
LightAboveFighter Jan 16, 2025
4364e13
clang tidy
LightAboveFighter Jan 16, 2025
b022189
format
LightAboveFighter Jan 16, 2025
c42ad03
format
LightAboveFighter Jan 16, 2025
b367fa7
fix include deleted files
LightAboveFighter Jan 16, 2025
f89348b
task_05
LightAboveFighter Jan 16, 2025
dd2a6e6
clang tidy + delete dublicates
LightAboveFighter Jan 16, 2025
be0cfeb
graph const method
LightAboveFighter Jan 16, 2025
0a3102e
task_05 turn into classes
LightAboveFighter Jan 16, 2025
4d9a06b
format
LightAboveFighter Jan 16, 2025
724bdcf
task_06
LightAboveFighter Jan 16, 2025
be184a5
task_03
LightAboveFighter Jan 16, 2025
a16ce56
fix swap
LightAboveFighter Jan 16, 2025
0d5b061
remove extra lines
LightAboveFighter Jan 16, 2025
e802244
task_06_tests
LightAboveFighter Jan 16, 2025
e5381d7
formatter's advices
LightAboveFighter Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/src/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <vector>

class Graph {
struct vert_neigh {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хотяб VertNeighbour

int name;
double lenght;

vert_neigh(int name, double lenght) : name{name}, lenght{lenght} {}
vert_neigh(std::pair<int, double> p) : name{p.first}, lenght{p.second} {}
};

public:
// { {0, 1}, {1, 0}, {2, 3}, {2, 4}, {2, 5} }
Graph(int verts_num, std::initializer_list<std::pair<int, int>> links) {
adjacents = std::vector<std::vector<vert_neigh>>(verts_num,
std::vector<vert_neigh>());
for (auto pair : links) {
adjacents[pair.first].push_back(vert_neigh(pair.second, 1.));
}
}

// { {{0, 1}, lenght1}, {{1, 0}, lenght2}, ... }
Graph(int verts_num,
std::initializer_list<std::pair<std::pair<int, int>, double>> links) {
adjacents = std::vector<std::vector<vert_neigh>>(verts_num,
std::vector<vert_neigh>());
for (auto pair : links) {
adjacents[pair.first.first].push_back(
vert_neigh(pair.first.second, pair.second));
}
}

int size() const { return adjacents.size(); }

void add_vert() { adjacents.push_back({}); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

имена методов не по кодстайлу


friend std::ostream& operator<<(std::ostream& os, const Graph& g) {
os << " { ";
for (int i = 0; i < g.adjacents.size(); ++i) {
for (auto p : g.adjacents[i]) {
os << "{ {" << i << ", " << p.name << "}, " << p.lenght;
os << "}, ";
}
}
os << "}";
return os;
}

void see_vertical() {
int vert_name = -1;
for (auto vert : adjacents) {
vert_name++;
if (vert.size() == 0) {
std::cout << vert_name << std::endl;
continue;
}
for (auto link_to : vert) {
std::cout << vert_name << " -> " << link_to.name
<< ", lenght:" << link_to.lenght << std::endl;
}
}
}
std::vector<std::vector<vert_neigh>> adjacents;
};

#endif
241 changes: 239 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,242 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include <stdexcept>

#include "topology_sort.hpp"

bool is_sorted(Graph& g, std::vector<int>& order) {
for (int i = 0; i < order.size(); ++i) {
for (int j = i + 1; j < order.size(); ++j) {
for (auto child : g.adjacents[order[j]]) {
if (child.name == order[i]) {
return false;
}
}
}
}
return true;
}

TEST(non_recursive_topological_sort, Test_1) {
Graph g(8, {
{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_non_rec(g)), true);
}

TEST(non_recursive_topological_sort, Test_2) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{2, 3}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_non_rec(g)), true);
}

TEST(non_recursive_topological_sort, Test_3) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{2, 3}, 1},
{{1, 2}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_non_rec(g)), true);
}

TEST(non_recursive_topological_sort, Test_4) {
Graph g(9, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{8, 0}, 1},
{{0, 5}, 1},
{{5, 6}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_non_rec(g)), true);
}

TEST(non_recursive_topological_sort, Test_cycles_1) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{5, 2}, 1}});

bool error_occured = false;

try {
topological_sort_dfs_non_rec(g);
} catch (std::runtime_error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

error_occured = true;
}

ASSERT_EQ(error_occured, true);
}

TEST(non_recursive_topological_sort, Test_cycles_2) {
Graph g(9, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{6, 8}, 1},
{{4, 8}, 1},
{{8, 3}, 1}});

bool error_occured = false;

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

есть ASSERT_THROW и другие похожие макросы

topological_sort_dfs_non_rec(g);
} catch (std::runtime_error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

error_occured = true;
}

ASSERT_EQ(error_occured, true);
}

TEST(recursive_topological_sort, Test_1) {
Graph g(8, {
{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_rec(g)), true);
}

TEST(recursive_topological_sort, Test_2) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{2, 3}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_rec(g)), true);
}

TEST(recursive_topological_sort, Test_3) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{2, 3}, 1},
{{1, 2}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_rec(g)), true);
}

TEST(recursive_topological_sort, Test_4) {
Graph g(9, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{8, 0}, 1},
{{0, 5}, 1},
{{5, 6}, 1}});

ASSERT_EQ(is_sorted(g, *topological_sort_dfs_rec(g)), true);
}

TEST(recursive_topological_sort, Test_cycles_1) {
Graph g(8, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{5, 2}, 1}});

bool error_occured = false;

try {
topological_sort_dfs_rec(g);
} catch (std::runtime_error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут анализатор прав

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кроме макросов THROW если сделать тут не референс а конкретную ошибку - код уже не исполняется. Думаю тут немного другое имелось в виду.. Или проясните? Когда будет удобно, конечно же

error_occured = true;
}

ASSERT_EQ(error_occured, true);
}

TEST(recursive_topological_sort, Test_cycles_2) {
Graph g(9, {{{1, 4}, 4},
{{1, 6}, 5},
{{2, 7}, 9},
{{3, 4}, 1},
{{3, 7}, 5.5},
{{4, 5}, 3.14},
{{7, 0}, 0.2},
{{7, 5}, 1.3},
{{7, 6}, 2.6},
{{6, 8}, 1},
{{4, 8}, 1},
{{8, 3}, 1}});

bool error_occured = false;

try {
topological_sort_dfs_rec(g);
} catch (std::runtime_error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: catch handler catches by value; should catch by reference instead [misc-throw-by-value-catch-by-reference]

  } catch (std::runtime_error) {
           ^

error_occured = true;
}

ASSERT_EQ(error_occured, true);
}
Loading
Loading