-
Notifications
You must be signed in to change notification settings - Fork 23
Graph class ~/lib/src #5
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?
Changes from 10 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
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,74 @@ | ||||||
| #ifndef __GRAPH_H__ | ||||||
| #define __GRAPH_H__ | ||||||
LightAboveFighter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
LightAboveFighter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| #define __GRAPH_H__ | |
| #define GRAPH_H_ |
LightAboveFighter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.
хотяб VertNeighbour
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1 +1,127 @@ | ||||||
| #include <stack> | ||||||
| #include <stdexcept> | ||||||
| #include <vector> | ||||||
|
|
||||||
| #include "../../lib/src/graph.h" | ||||||
|
|
||||||
| void dfs_mark_non_rec(Graph& g, std::vector<int>& marks, | ||||||
| std::vector<int>& visited, int start_name) { | ||||||
| std::stack<int> st; | ||||||
| st.push(start_name); | ||||||
|
|
||||||
| while (!st.empty()) { | ||||||
| int v = st.top(); | ||||||
|
||||||
| int v = st.top(); | |
| int const v = st.top(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,51 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| TEST(Test, Simple) { | ||
| ASSERT_EQ(1, 1); // Stack [] | ||
| #include "../../lib/src/graph.h" | ||
| #include "main.cpp" | ||
LightAboveFighter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| bool is_sorted(Graph& g, std::vector<int>& order) { | ||
| bool found_parent; | ||
| for (int i = 0; i < order.size(); ++i) { | ||
| for (int j = i + 1; j < order.size(); ++j) { | ||
| found_parent = false; | ||
|
||
| for (auto child : g.adjacents[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(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); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,39 @@ | ||
| #include <iostream> | ||
| #include <limits> | ||
| #include <map> | ||
| #include <vector> | ||
|
|
||
| #include "../../lib/src/graph.h" | ||
|
|
||
| std::map<int, double> dijkstra(Graph& g, int start_vert) { | ||
| std::map<int, double> dists; | ||
| std::vector<bool> finished(g.size(), false); | ||
|
|
||
| for (int i = 0; i < g.size(); ++i) { | ||
| dists[i] = std::numeric_limits<double>::infinity(); | ||
| } | ||
| dists[start_vert] = 0; | ||
| finished[start_vert] = true; | ||
|
|
||
| int active_vert = start_vert; | ||
| double min_lenght = std::numeric_limits<double>::infinity(); | ||
| for (int i = 0; i < g.size() - 1; ++i) { | ||
| for (auto child : g.adjacents[active_vert]) { | ||
| if (dists[child.name] > child.lenght + dists[active_vert]) { | ||
| dists[child.name] = child.lenght + dists[active_vert]; | ||
| } | ||
| } | ||
|
|
||
| min_lenght = std::numeric_limits<double>::infinity(); | ||
| for (int j = 0; j < g.size(); j++) { | ||
| if (!finished[j] && (dists[j] < min_lenght)) { | ||
| min_lenght = dists[j]; | ||
| active_vert = j; | ||
| } | ||
| } | ||
| finished[active_vert] = true; | ||
| } | ||
|
|
||
| return dists; | ||
| } | ||
|
|
||
| int main() { return 0; } |
Uh oh!
There was an error while loading. Please reload this page.