Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4540813
Graph class ~/lib/src
LightAboveFighter Sep 11, 2024
f9b6ed9
simple Graph
LightAboveFighter Jan 13, 2025
f28131b
task_01
LightAboveFighter Jan 13, 2025
177a3a4
task_04
LightAboveFighter Jan 13, 2025
0630f42
format
LightAboveFighter Jan 15, 2025
72c8f79
Merge branch 'main' into main
LightAboveFighter Jan 15, 2025
6a5fd0e
test base task_01
LightAboveFighter Jan 15, 2025
1466e47
Merge branch 'main' of https://github.com/LightAboveFighter/autumn_ho…
LightAboveFighter Jan 15, 2025
5cf054a
format
LightAboveFighter Jan 15, 2025
de3cf01
fix includes
LightAboveFighter Jan 15, 2025
87cbbc5
clang tidy advices
LightAboveFighter Jan 15, 2025
03ee99e
tests task_01
LightAboveFighter Jan 15, 2025
c3a46c5
fix tests
LightAboveFighter Jan 15, 2025
7de22a7
task_02
LightAboveFighter Jan 16, 2025
c7b68c0
format
LightAboveFighter Jan 16, 2025
9a84bec
split task_01 to .hpp .cpp files
LightAboveFighter Jan 16, 2025
e3bf6a2
task_04
LightAboveFighter Jan 16, 2025
e49a06a
convenient graph node constructor
LightAboveFighter Jan 16, 2025
4364e13
clang tidy
LightAboveFighter Jan 16, 2025
b022189
format
LightAboveFighter Jan 16, 2025
c42ad03
format
LightAboveFighter Jan 16, 2025
b367fa7
fix include deleted files
LightAboveFighter Jan 16, 2025
f89348b
task_05
LightAboveFighter Jan 16, 2025
dd2a6e6
clang tidy + delete dublicates
LightAboveFighter Jan 16, 2025
be0cfeb
graph const method
LightAboveFighter Jan 16, 2025
0a3102e
task_05 turn into classes
LightAboveFighter Jan 16, 2025
4d9a06b
format
LightAboveFighter Jan 16, 2025
724bdcf
task_06
LightAboveFighter Jan 16, 2025
be184a5
task_03
LightAboveFighter Jan 16, 2025
a16ce56
fix swap
LightAboveFighter Jan 16, 2025
0d5b061
remove extra lines
LightAboveFighter Jan 16, 2025
e802244
task_06_tests
LightAboveFighter Jan 16, 2025
e5381d7
formatter's advices
LightAboveFighter Jan 16, 2025
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
74 changes: 74 additions & 0 deletions lib/src/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef __GRAPH_H__
#define __GRAPH_H__

Choose a reason for hiding this comment

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

warning: declaration uses identifier 'GRAPH_H', which is a reserved identifier [bugprone-reserved-identifier]

Suggested change
#define __GRAPH_H__
#define GRAPH_H_


#include <initializer_list>
#include <iostream>
#include <vector>

class Graph {
struct vert_neigh {
Copy link
Contributor

Choose a reason for hiding this comment

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

хотяб VertNeighbour

int name;
double lenght;

vert_neigh(int name, double lenght) : name{name}, lenght{lenght} {}
};

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() {
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
126 changes: 126 additions & 0 deletions task_01/src/main.cpp
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();

Choose a reason for hiding this comment

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

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

Suggested change
int v = st.top();
int const v = st.top();


if (visited[v] == 2) {
st.pop();
for (int i = 0; i < marks.size(); ++i) {
if (marks[marks.size() - 1 - i] == -1) {
marks[marks.size() - 1 - i] = v;
break;
}
}
visited[v] = 3;
continue;
} else if (visited[v] == 3) {
st.pop();
continue;
}

visited[v] = 1;

int finished_children = 0;
for (auto child : g.adjacents[v]) {
if (visited[child.name] == 1) {
throw std::runtime_error("Graph contains cycle!");
} else if (visited[child.name] == 0) {
st.push(child.name);
} else {
finished_children++;
}
}

if (finished_children == g.adjacents[v].size()) {
visited[v] = 2;
}
}
}

std::vector<int>* topological_sort_dfs_non_rec(Graph& g) {
if (g.size() == 0) {
return new std::vector<int>();
}

std::vector<int> visited(
g.size(),
0); // 0 not started verticle , 1 started verticle, 2 - finished
// verticle, 3 - added and finished verticle
std::vector<int>* sorted = new std::vector<int>(g.size(), -1);

bool calculated;
for (int i = 0; i < visited.size(); ++i) {
calculated = true;
for (int j = 0; j < visited.size(); ++j) {
if (visited[j] == 0) {
calculated = false;
dfs_mark_non_rec(g, *sorted, visited, j);
break;
}
}
if (calculated) {
break;
};
}

return sorted;
}

void dfs_mark_rec(Graph& g, std::vector<int>& marks, std::vector<int>& visited,
int vert) {
visited[vert] = 1;

for (auto child : g.adjacents[vert]) {
if (visited[child.name] == 1) {
throw std::runtime_error("Graph contains cycle!");
} else if (visited[child.name] == 0) {
dfs_mark_rec(g, marks, visited, child.name);
}
}

visited[vert] = 2;
for (int i = 0; i < marks.size(); ++i) {
if (marks[marks.size() - 1 - i] == -1) {
marks[marks.size() - 1 - i] = vert;
break;
}
}
}

std::vector<int>* topological_sort_dfs_rec(Graph& g) {
if (g.size() == 0) {
return new std::vector<int>();
}

std::vector<int> visited(g.size(),
0); // 0 not started verticle , 1 started verticle ,
// 2 - finished verticle
std::vector<int>* sorted = new std::vector<int>(g.size(), -1);

bool calculated;
for (int i = 0; i < visited.size(); ++i) {
calculated = true;
for (int j = 0; j < visited.size(); ++j) {
if (visited[j] == 0) {
calculated = false;
dfs_mark_rec(g, *sorted, visited, j);
break;
}
}
if (calculated) {
break;
};
}

return sorted;
}

int main() { return 0; }
50 changes: 48 additions & 2 deletions task_01/src/test.cpp
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"

Choose a reason for hiding this comment

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

warning: suspicious #include of file with '.cpp' extension [bugprone-suspicious-include]

#include "main.cpp"
          ^

Choose a reason for hiding this comment

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

warning: suspicious #include of file with '.cpp' extension [bugprone-suspicious-include]

#include "main.cpp"
          ^


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;

Choose a reason for hiding this comment

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

warning: Value stored to 'found_parent' is never read [clang-analyzer-deadcode.DeadStores]

      found_parent = false;
      ^
Additional context

task_01/src/test.cpp:9: Value stored to 'found_parent' is never read

      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);
}
38 changes: 37 additions & 1 deletion task_04/src/main.cpp
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; }
Loading