-
Notifications
You must be signed in to change notification settings - Fork 23
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
base: main
Are you sure you want to change the base?
Tasks #32
Changes from all commits
a0bfb22
3c4ec25
ce24e9a
bbffc45
e7ca580
b425ad7
4753707
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
} |
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})); | ||
} |
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_, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. так себе название функции, как и следующей |
||
std::vector<int> GetAP() { return a_p_; } | ||
}; | ||
#endif |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
return 0; | ||||||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
расширение не то(