Skip to content

Readme #37

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 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 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
74fa6a9
orfography
LightAboveFighter Jan 16, 2025
23d696a
add main instructions
LightAboveFighter Jan 16, 2025
30e868a
fix task's files advice
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
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,49 @@
### Можно получить дополнительные баллы, если добавить теорию в папку doc. Делается в отдельном ПР, полчуть дополнительные баллы можно только если пулл реквест замержен в основную ветку.

### Код должен быть отформатирован clang-format'ом со стилем Google


## Карта проекта

* [doc](https://github.com/AlgorithmsDafeMipt2024/autumn_homework/tree/main/doc) - теория, которую добавляет преподаватель или студенты
* lib - папка для ваших структур, которые вы используете во множестве задач
* [additional tasks](https://github.com/AlgorithmsDafeMipt2024/autumn_homework/tree/main/additional_tasks) - сюда обычно оформляются особые задания от преподавателя (как дополнительная теория, или личные задания)


Каждое задание выглядит так:

* task_N
* src
* main.cpp - нужен для вашего личного тестирования результата. Для проверки он не используется
* test.cpp - здесь в должны написать тестирование вашего алгоритма с помощью **<gtest/gtest.h>**
* CMakeLists.txt
* README.md - тут описано задание и требования к используемому алгоритму


## Инструкция для работы с репозиторием

1. Создайте [fork](https://github.com/AlgorithmsDafeMipt2024/autumn_homework/fork) этого репозитория
2. Перейдите в него, и найдите зеленую кнопку <span style="color:green"> Code
3. В открывающемся списке выберите левую вкладку **Codespaces**
4. Создайте новое окружение (метка "+") или откройте старое

Готово!

### Советы при работе с codespaces

* При работе с заданиями желательно создавать файлы .hpp и .cpp с заданием, причем .hpp - именно заголовочный файл, который будет импортироваться вами в test.cpp для тестирования
* Если вы работаете через VS Code, то чтобы поменять цель сборки, нажмите Ctrl+p, и введите **>CMake: Set Launch/Debug Target**
* Если при запуске тестов консоль остается пустой, проверьте что int main() внутри main.cpp соответствующего задания пуст
* Если вся среда неожиданно зависла и очень долго пытается соединиться с сервером - попробуйте выключить интернет на 10 секунд и включить снова. Если не помогает - ищите более надежную сеть(
* При ошибке **./googletest/src/gtest-internal-inl.h:685:: Condition !original_working_dir_.IsEmpty() failed** проверьте что структура задания не нарушена: main.cpp, test.cpp

Шаблон теста:

```
#include <gtest/gtest.h>

Test(test_name, test_number) {
/* code */
ASSERT_EQ(expected_value, real_value); // лучше сравнивать что то простое, например bool и int
}
```
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>

Choose a reason for hiding this comment

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

warning: 'iostream' file not found [clang-diagnostic-error]

#include <iostream>
         ^

#include <vector>

class Graph {
struct vert_neigh {
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({}); }

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 {
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) {
           ^

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) {
           ^

error_occured = true;
}

ASSERT_EQ(error_occured, true);
}
Loading
Loading