Skip to content

Tasks #32

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 7 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions task_01/src/graph.tpp
Copy link
Contributor

Choose a reason for hiding this comment

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

расширение не то(

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef AUTUMN_HOMEWORK_TASK_01_GRAPH_TPP_
#define AUTUMN_HOMEWORK_TASK_01_GRAPH_TPP_

#include <list>
#include <vector>

template <class T>
class Graph {
private:
size_t v_num_;
std::vector<std::vector<T>> adj_;

public:
Graph(size_t v_num) : v_num_{v_num}, adj_{v_num_} {}

void AddEdge(size_t first_vertex, size_t second_vertex) {
adj_[first_vertex].push_back(second_vertex);
}

void TopologicalSortVertex(int i, std::vector<bool>& visited,
std::list<T>& list) {
for (auto neighbor : adj_[i]) {
if (!visited[neighbor]) {
TopologicalSortVertex(neighbor, visited, list);
}
}
list.push_front(i);
visited[i] = true;
}

std::list<T> TopologicalSort() {
std::list<T> list;
std::vector<bool> visited(v_num_ + 1, false);
for (size_t i = 0; i < v_num_; i++) {
if (visited[i] == false) {
TopologicalSortVertex(i, visited, list);
}
}
return list;
}
};

#endif
7 changes: 6 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
int main() { return 0; }
#include <iostream>

int main() {
std::cout << "Lol" << std::endl;
return 0;

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << "Lol" << std::endl;
std::cout << "Lol" << '\n';

}
55 changes: 53 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
#include "graph.tpp"

TEST(TopologicalSort, test1) {
Graph<int> a(1);

EXPECT_EQ(a.TopologicalSort(), std::list<int>({0}));
}

TEST(TopologicalSort, test2) {
Graph<int> a(2);

a.AddEdge(0, 1);

EXPECT_EQ(a.TopologicalSort(), std::list<int>({0, 1}));
}

TEST(TopologicalSort, test3) {
Graph<int> a(3);

a.AddEdge(0, 1);
a.AddEdge(1, 2);

EXPECT_EQ(a.TopologicalSort(), std::list<int>({0, 1, 2}));
}

TEST(TopologicalSort, test4) {
Graph<int> a(6);

a.AddEdge(0, 1);
a.AddEdge(0, 3);
a.AddEdge(1, 2);
a.AddEdge(3, 1);
a.AddEdge(3, 5);
a.AddEdge(3, 4);
a.AddEdge(4, 5);

EXPECT_EQ(a.TopologicalSort(), std::list<int>({0, 3, 4, 5, 1, 2}));
}

TEST(TopologicalSort, test5) {
Graph<int> a(7);

a.AddEdge(0, 1);
a.AddEdge(0, 2);
a.AddEdge(1, 2);
a.AddEdge(1, 5);
a.AddEdge(2, 3);
a.AddEdge(5, 3);
a.AddEdge(5, 4);
a.AddEdge(6, 1);
a.AddEdge(6, 5);

EXPECT_EQ(a.TopologicalSort(), std::list<int>({6, 0, 1, 5, 4, 2, 3}));
}
69 changes: 69 additions & 0 deletions task_02/src/graph.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef AUTUMN_HOMEWORK_TASK_02_GRAPH_TPP_
#define AUTUMN_HOMEWORK_TASK_02_GRAPH_TPP_

#include <cmath>
#include <vector>

template <typename T>
class Graph {
private:
size_t v_num_;
std::vector<std::vector<T>> adj_;
std::vector<int> disc_,
Copy link
Contributor

Choose a reason for hiding this comment

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

1 объявление - 1 строчка, не через запятую

least_; // discovery time and least_[i] is node with least disc_ that can
// be reached from i node
std::vector<std::pair<int, int>> br_; // bridges
std::vector<int> a_p_; // articulation points

void dfs(int u, int p, int& timer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Dfs

disc_[u] = least_[u] = timer++;
int subgraph_count = 0;

for (int v : adj_[u]) {
if (v == p) continue;
if (disc_[v]) {
least_[u] = std::min(least_[u], disc_[v]);
} else {
++subgraph_count;
dfs(v, u, timer);
least_[u] = std::min(least_[u], least_[v]);

if (disc_[u] <= least_[v] && p != -1) {
if (a_p_.empty() || a_p_.back() != u) {
a_p_.push_back(u);
}
}
if (disc_[u] < least_[v]) br_.push_back({u, v});
}
}

if (subgraph_count > 1 && p == -1) a_p_.push_back(u);
}

public:
Graph(size_t number) {
v_num_ = number;
adj_ = std::vector<std::vector<T>>(v_num_);
}

void AddEdge(size_t first_verticle, size_t second_verticle) {
adj_[first_verticle].push_back(second_verticle);
adj_[second_verticle].push_back(first_verticle);
}

void UpdateBrAp() {
int timer = 1;
disc_.assign(v_num_, 0);
least_.assign(v_num_, 0);
br_.clear();
a_p_.clear();

for (size_t i = 0; i < v_num_; ++i) {
if (!disc_[i]) dfs(i, -1, timer);
}
}

std::vector<std::pair<int, int>> GetBr() { return br_; }
Copy link
Contributor

Choose a reason for hiding this comment

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

так себе название функции, как и следующей

std::vector<int> GetAP() { return a_p_; }
};
#endif
5 changes: 4 additions & 1 deletion task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <iostream>

int main() { return 0; }
int main() {
std::cout << "Lol" << std::endl;

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << "Lol" << std::endl;
std::cout << "Lol" << '\n';

return 0;
}
21 changes: 0 additions & 21 deletions task_02/src/stack.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions task_02/src/stack.hpp

This file was deleted.

Loading
Loading