-
Notifications
You must be signed in to change notification settings - Fork 23
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
base: main
Are you sure you want to change the base?
Readme #37
Changes from all commits
4540813
f9b6ed9
f28131b
177a3a4
0630f42
72c8f79
6a5fd0e
1466e47
5cf054a
de3cf01
87cbbc5
03ee99e
c3a46c5
7de22a7
c7b68c0
9a84bec
e3bf6a2
e49a06a
4364e13
b022189
c42ad03
b367fa7
f89348b
dd2a6e6
be0cfeb
0a3102e
4d9a06b
724bdcf
be184a5
a16ce56
0d5b061
e802244
e5381d7
74fa6a9
23d696a
30e868a
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,70 @@ | ||
#ifndef GRAPH_H_ | ||
#define GRAPH_H_ | ||
|
||
#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 |
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) { | ||
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: 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) { | ||
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: 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) { | ||
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: 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) { | ||
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: 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); | ||
} |
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.
warning: 'iostream' file not found [clang-diagnostic-error]