Skip to content

Main #34

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 13 commits into
base: main
Choose a base branch
from
Open

Main #34

Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
int main() { return 0; }
// Дан граф в формате списка исходящих соседей. Данная топологическая
// сортировка, построенная на алгоритме DFS, распологает вершины в списке таким
// образом, чтобы родитель был записан раньше своего ребенка. То есть все ребра,
// соединяющие вершины списка, были направлены слева направо. Это невозможно
// только при наличие в графе цикла, что также учтено и отслеживается программой

int main() { return 0; }
110 changes: 108 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,111 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include <algorithm>
#include <vector>
// честно пыталась вынести это в отдельный файл, но FATAL ошибки, core dumped и
// я не знаю как это фиксить
void RecursiveDFS(std::vector<std::vector<int>> &data, int root,
std::vector<int> &status, std::vector<int> &top_order) {
status[root] = 1;
for (int i = 0; i < data[root].size(); ++i) {
if (status[data[root][i]] == 0) {
RecursiveDFS(data, data[root][i], status, top_order);
} else if (status[data[root][i]] == 1) {
throw std::runtime_error("cycle found");
}
}
status[root] = 2;
top_order.push_back(root);
}

std::vector<int> TopologicalSort(std::vector<std::vector<int>> &data) {
int lenth = data.size();
std::vector<int> status(lenth, 0);
std::vector<int> top_order;

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

 {
                 ^

Choose a reason for hiding this comment

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

warning: variable 'lenth' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> top_order;
{const

int flag = 1;
while (flag) {
flag = 0;
for (int i = 0; i < lenth; ++i) {
if (status[i] != 2) {
flag = 1;
RecursiveDFS(data, i, status, top_order);
break;
}
}
}
std::reverse(top_order.begin(), top_order.end());
return top_order;
}

bool CheckTest(std::vector<std::vector<int>> &data,
std::vector<int> &top_order) {
for (int i = 0; i < top_order.size(); ++i) {
for (int j = i + 1; j < top_order.size(); ++j) {
auto result = std::find(data[top_order[j]].begin(),
data[top_order[j]].end(), top_order[i]);
if (result != data[top_order[j]].end()) {
return false;
}
}
}
return true;
}

TEST(TopologySort, Tree) {
std::vector<std::vector<int>> data(
{{}, {}, {0, 1}, {5, 7}, {9, 2, 3, 8}, {}, {}, {}, {6}, {}});
std::vector<int> top_order = TopologicalSort(data);
ASSERT_EQ(CheckTest(data, top_order), true); // Stack []
}

TEST(TopologySort, TwoRoot) {
std::vector<std::vector<int>> data(
{{}, {4, 6}, {7}, {7, 4}, {5}, {}, {}, {0, 6, 5}});
std::vector<int> top_order = TopologicalSort(data);
ASSERT_EQ(CheckTest(data, top_order), true); // Stack []
}

TEST(TopologySort, ThreeRoot) {
std::vector<std::vector<int>> data(
{{1, 4, 6}, {4}, {3}, {}, {2, 5}, {2}, {4}, {6, 4, 2, 3}, {1, 5}});
std::vector<int> top_order = TopologicalSort(data);
ASSERT_EQ(CheckTest(data, top_order), true); // Stack []
}

TEST(TopologySort, OneRootOneLeaf) {
std::vector<std::vector<int>> data({{2, 3, 4},
{0, 2},
{3, 6},
{6},
{3, 6},
{0, 4},
{},
{1, 9},
{7, 9, 5},
{1, 0, 5}});
std::vector<int> top_order = TopologicalSort(data);
ASSERT_EQ(CheckTest(data, top_order), true); // Stack []
}

TEST(TopologySort, Cycle) {
bool error = false;
std::vector<std::vector<int>> data({{1}, {2, 3}, {3, 0}, {}});
try {
std::vector<int> top_order = TopologicalSort(data);
} 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: variable 'top_order' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
} catch (std::runtime_error) {
);const

error = true;
}
ASSERT_EQ(error, true); // Stack []

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]

Sort(data);
                       ^

}

TEST(TopologySort, Cycle2) {
bool error = false;
std::vector<std::vector<int>> data(
{{}, {4, 6}, {7}, {7, 4}, {5}, {}, {2}, {0, 6, 5}});
try {
std::vector<int> top_order = TopologicalSort(data);
} 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: variable 'top_order' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
} catch (std::runtime_error) {
);const

error = true;
}
ASSERT_EQ(error, true); // Stack []

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]

Sort(data);
                       ^

}
9 changes: 9 additions & 0 deletions task_02/src/bridge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <vector>

void WeakEdge(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
std::vector<int> &time_in, std::vector<int> &min_time_in,
std::vector<std::pair<int, int>> &bridges, int time, int v,
int p = -1);

std::vector<std::pair<int, int>> CuttingEdge(
std::vector<std::vector<int>> &data);
38 changes: 38 additions & 0 deletions task_02/src/brige.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "bridge.hpp"

void WeakEdge(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
std::vector<int> &time_in, std::vector<int> &min_time_in,
std::vector<std::pair<int, int>> &bridges, int time, int v,
int p) {
visited[v] = true;
time_in[v] = min_time_in[v] = time++;
for (size_t i = 0; i < data[v].size(); ++i) {
int const to = data[v][i];
if (to == p) {
continue;
}
if (visited[to]) {
min_time_in[v] = std::min(min_time_in[v], time_in[to]);
} else {
WeakEdge(data, visited, time_in, min_time_in, bridges, time, to, v);
min_time_in[v] = std::min(min_time_in[v], min_time_in[to]);
if (min_time_in[to] > time_in[v]) {
bridges.push_back(std::pair<int, int>({v, to}));
}
}
}
}

std::vector<std::pair<int, int>> CuttingEdge(
std::vector<std::vector<int>> &data) {
int const time = 0;
std::vector<int> time_in(data.size(), -1);
std::vector<int> min_time_in(data.size(), -1);
std::vector<std::pair<int, int>> bridges;
std::vector<bool> visited(data.size(), false);
for (int i = 0; i < data.size(); ++i)
if (!visited[i]) {
WeakEdge(data, visited, time_in, min_time_in, bridges, time, i);
}
return bridges;
}
9 changes: 7 additions & 2 deletions task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <iostream>

int main() { return 0; }

// Неориентированный граф задан в виде списка соседей каждой вершины. Два
// основных вопроса, на которые отвечает программа: удаление какой вершины/ребра
// сделает граф несвязным. Программа рассматривает случаи удаления каждого ребра
// и возможность дойти из одной вершины этого ребра до второй другим путем. В
// случае не нахождения такого пути, ребро считается слабым местом сети.
// Подобным образом проводится и проверка каждой вершины
48 changes: 48 additions & 0 deletions task_02/src/router.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <algorithm>
#include <set>
#include <vector>

void WeakVertex(std::vector<std::vector<int>> &data, std::vector<bool> &visited,

Choose a reason for hiding this comment

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

warning: function 'WeakVertex' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]

void WeakVertex(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
     ^
Additional context

task_02/src/router.hpp:6: make as 'inline'

void WeakVertex(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
     ^

Choose a reason for hiding this comment

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

warning: function 'WeakVertex' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]

void WeakVertex(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
     ^
Additional context

task_02/src/router.hpp:6: make as 'inline'

void WeakVertex(std::vector<std::vector<int>> &data, std::vector<bool> &visited,
     ^

std::vector<int> &time_in, std::vector<int> &min_time_in,
std::vector<int> &cut_verts, int time, int v, int p = -1) {
visited[v] = true;
time_in[v] = min_time_in[v] = time++;
int count = 0;
for (int i = 0; i < data[v].size(); ++i) {
if (data[v][i] == p) {
continue;
}
if (visited[data[v][i]]) {
min_time_in[v] = std::min(min_time_in[v], time_in[data[v][i]]);
} else {
WeakVertex(data, visited, time_in, min_time_in, cut_verts, time,
data[v][i], v);
count++;
min_time_in[v] = std::min(min_time_in[v], min_time_in[data[v][i]]);
if (min_time_in[data[v][i]] >= time_in[v] && p != -1) {
cut_verts.push_back(v);
}
}
}

if (p == -1 && count >= 2) {
cut_verts.push_back(v);
}
}

std::vector<int> CuttingVertex(std::vector<std::vector<int>> &data) {

Choose a reason for hiding this comment

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

warning: function 'CuttingVertex' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]

std::vector<int> CuttingVertex(std::vector<std::vector<int>> &data) {
                 ^
Additional context

task_02/src/router.hpp:34: make as 'inline'

std::vector<int> CuttingVertex(std::vector<std::vector<int>> &data) {
                 ^

Choose a reason for hiding this comment

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

warning: function 'CuttingVertex' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]

std::vector<int> CuttingVertex(std::vector<std::vector<int>> &data) {
                 ^
Additional context

task_02/src/router.hpp:34: make as 'inline'

std::vector<int> CuttingVertex(std::vector<std::vector<int>> &data) {
                 ^

int const time = 0;
std::vector<int> time_in(data.size(), -1);
std::vector<int> min_time_in(data.size(), -1);
std::vector<int> cut_verts;
std::vector<bool> visited(data.size(), false);
for (int i = 1; i < data.size(); ++i)
if (!visited[i]) {
WeakVertex(data, visited, time_in, min_time_in, cut_verts, time, i);
}
std::set<int> unique_elements(cut_verts.begin(), cut_verts.end());

Choose a reason for hiding this comment

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

warning: variable 'unique_elements' of type 'std::set' can be declared 'const' [misc-const-correctness]

Suggested change
std::set<int> unique_elements(cut_verts.begin(), cut_verts.end());
std::set<int> const unique_elements(cut_verts.begin(), cut_verts.end());

Choose a reason for hiding this comment

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

warning: variable 'unique_elements' of type 'std::set' can be declared 'const' [misc-const-correctness]

Suggested change
std::set<int> unique_elements(cut_verts.begin(), cut_verts.end());
std::set<int> const unique_elements(cut_verts.begin(), cut_verts.end());

cut_verts.assign(unique_elements.begin(), unique_elements.end());
return cut_verts;
}
124 changes: 87 additions & 37 deletions task_02/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,92 @@

#include <gtest/gtest.h>

#include <stack>

#include "stack.hpp"

TEST(StackTest, Simple) {
Stack stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
#include <iostream>

#include "bridge.hpp"
#include "router.hpp"

bool SameEdges(std::vector<std::pair<int, int>> &a,
std::vector<std::pair<int, int>> &b) {
for (auto elem : a) {
if ((std::find(b.begin(), b.end(), elem) == b.end()) &&
(std::find(b.begin(), b.end(),
std::pair<int, int>({elem.second, elem.first})) ==
b.end())) {
return false;
}
}
return true;
}

TEST(LocalNetwork, Line) {
std::vector<std::vector<int>> data(
{{1}, {0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 7}, {6, 8}, {7}});
std::vector<std::pair<int, int>> weak_edges = CuttingEdge(data);
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<std::pair<int, int>> ans_edge = {{2, 1}, {2, 3}, {3, 4}, {4, 5},

Choose a reason for hiding this comment

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

warning: variable 'weak_vertexes' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<int> const weak_vertexes = CuttingVertex(data);

Choose a reason for hiding this comment

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

warning: variable 'weak_vertexes' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<int> const weak_vertexes = CuttingVertex(data);

{5, 6}, {6, 7}, {7, 8}};
std::vector<int> ans_vert = {1, 2, 3, 4, 5, 6, 7};
ASSERT_EQ(SameEdges(ans_edge, weak_edges), true);

Choose a reason for hiding this comment

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

warning: variable 'ans_vert' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> ans_vert = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> const ans_vert = {1, 2, 3, 4, 5, 6, 7};

Choose a reason for hiding this comment

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

warning: variable 'ans_vert' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> ans_vert = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> const ans_vert = {1, 2, 3, 4, 5, 6, 7};

ASSERT_EQ(weak_vertexes, ans_vert);
}

TEST(LocalNetwork, Tree) {
std::vector<std::vector<int>> data({{2},
{2},
{0, 1, 4},
{5, 7, 4},
{9, 2, 3, 8},
{3},
{8},
{3},
{6, 4},
{4}});
std::vector<std::pair<int, int>> weak_edges = CuttingEdge(data);
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<std::pair<int, int>> ans_edge = {

Choose a reason for hiding this comment

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

warning: variable 'weak_vertexes' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<int> const weak_vertexes = CuttingVertex(data);

{2, 1}, {4, 9}, {3, 5}, {3, 7}, {4, 3}, {8, 6}, {4, 8}, {2, 4}, {0, 2}};
std::vector<int> ans_vert = {2, 3, 4, 8};
ASSERT_EQ(SameEdges(ans_edge, weak_edges), true);

Choose a reason for hiding this comment

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

warning: variable 'ans_vert' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> ans_vert = {2, 3, 4, 8};
std::vector<int> const ans_vert = {2, 3, 4, 8};

ASSERT_EQ(weak_vertexes, ans_vert);
}

TEST(LocalNetwork, Circle) {
std::vector<std::vector<int>> data(
{{1, 8}, {0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 7}, {6, 8}, {7, 0}});
std::vector<std::pair<int, int>> weak_edges = CuttingEdge(data);
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<std::pair<int, int>> ans_edge = {};

Choose a reason for hiding this comment

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

warning: variable 'weak_vertexes' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<int> const weak_vertexes = CuttingVertex(data);

std::vector<int> ans_vert = {};
ASSERT_EQ(SameEdges(ans_edge, weak_edges), true);

Choose a reason for hiding this comment

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

warning: variable 'ans_vert' of type 'std::vector' can be declared 'const' [misc-const-correctness]

Suggested change
std::vector<int> ans_vert = {};
std::vector<int> const ans_vert = {};

ASSERT_EQ(weak_vertexes, ans_vert);
}

TEST(LocalNetwork, Two_Circles) {
std::vector<std::vector<int>> data(
{{1, 3}, {0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 7}, {6, 8}, {4, 7}});
std::vector<std::pair<int, int>> weak_edges = CuttingEdge(data);
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<std::pair<int, int>> ans_edge = {{3, 4}};
std::vector<int> ans_vert = {3, 4};
std::cout << std::endl;
ASSERT_EQ(SameEdges(ans_edge, weak_edges), true);
ASSERT_EQ(weak_vertexes, ans_vert);
}

TEST(MinStackTest, Simple) {
MinStack stack;
stack.Push(1); // Stack [1]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
stack.Push(1); // Stack [1]
stack.Push(2); // Stack [1, 2]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 2); // Stack [1]
stack.Push(3); // Stack [1, 3]
ASSERT_EQ(stack.GetMin(), 1);
ASSERT_EQ(stack.Pop(), 3); // Stack [1]
ASSERT_EQ(stack.Pop(), 1); // Stack []
TEST(LocalNetwork, Three_Circles) {
std::vector<std::vector<int>> data({{1, 2},
{0, 2},
{1, 0, 3},
{5, 4, 6},
{3, 5},
{4, 3},
{3, 8, 7},
{6, 8},
{6, 7}});
std::vector<std::pair<int, int>> weak_edges = CuttingEdge(data);
std::vector<int> weak_vertexes = CuttingVertex(data);
std::vector<std::pair<int, int>> ans_edge = {{3, 2}, {3, 6}};
std::vector<int> ans_vert = {2, 3, 6};
ASSERT_EQ(SameEdges(ans_edge, weak_edges), true);
ASSERT_EQ(weak_vertexes, ans_vert);
}
27 changes: 27 additions & 0 deletions task_04/src/dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "dijkstra.hpp"

std::vector<double> Dijkstra(std::vector<std::map<int, double>> data,
int source) {
std::vector<double> ans(data.size(), std::numeric_limits<double>::infinity());
std::vector<bool> counted(data.size(), false);
ans[source] = 0;
counted[source] = true;
int pending = source;
double min_lenght;
for (int i = 0; i < data.size() - 1; ++i) {
for (auto neighbour : data[pending]) {
if (ans[neighbour.first] > neighbour.second + ans[pending]) {
ans[neighbour.first] = neighbour.second + ans[pending];
}
}
min_lenght = std::numeric_limits<double>::infinity();
for (int j = 0; j < data.size(); j++) {
if ((ans[j] < min_lenght) && !counted[j]) {
min_lenght = ans[j];
pending = j;
}
}
counted[pending] = true;
}
return ans;
}
Loading
Loading