Skip to content

Homework #27

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 83 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
fbf0799
delete useless files
MrWh1teF0x Nov 3, 2024
544f349
add class "Vertex"
MrWh1teF0x Nov 3, 2024
961a70d
Merge remote-tracking branch 'upstream/main' into homework
MrWh1teF0x Nov 3, 2024
fd3d3fa
fix mistake
MrWh1teF0x Nov 3, 2024
bd8a8d1
add template to class "Vertex"
MrWh1teF0x Nov 3, 2024
92d70ec
fix mistake with Cmake
MrWh1teF0x Nov 3, 2024
eacc703
add tests for "Vertex"
MrWh1teF0x Nov 3, 2024
a2bad74
fix method "PrintAdjacentVertices"
MrWh1teF0x Nov 3, 2024
46ac9c6
add test for class "Vertex"
MrWh1teF0x Nov 3, 2024
e043f10
add methods and tests for class "Vertex"
MrWh1teF0x Nov 3, 2024
3d1d9cb
remove useless method and beautify
MrWh1teF0x Nov 3, 2024
5a0bc2c
add class "Graph"
MrWh1teF0x Nov 3, 2024
119c30c
beautify
MrWh1teF0x Nov 3, 2024
2598c13
add methods for class "Graph"
MrWh1teF0x Nov 3, 2024
82993f3
add methods for classes "Vertex" and "Graph" and beautify
MrWh1teF0x Nov 5, 2024
5c42843
add check for dublicate adjacent vertex
MrWh1teF0x Nov 5, 2024
c98d7cd
fix mistake in delete method
MrWh1teF0x Nov 5, 2024
bef8daa
add field "is_oriented" and add comments
MrWh1teF0x Nov 5, 2024
097d78c
add tests for not-oriented graph
MrWh1teF0x Nov 5, 2024
79e2826
add tests for oriented graph and fix mistake
MrWh1teF0x Nov 5, 2024
5584041
add #pragma once
MrWh1teF0x Nov 5, 2024
66fe642
add method "GetEdgesCount"
MrWh1teF0x Nov 5, 2024
84f5c33
remove throw from add methods
MrWh1teF0x Nov 5, 2024
72f11b1
add class "WeightedGraph"
MrWh1teF0x Nov 5, 2024
cfe9111
change method "GetEdgesCount" for not-oriented graph and add tests
MrWh1teF0x Nov 8, 2024
2410b6f
fix mistakes
MrWh1teF0x Nov 8, 2024
d42d2d0
remove useless constructor
MrWh1teF0x Nov 8, 2024
ac225fd
add method "DeleteEdge" and add tests
MrWh1teF0x Nov 8, 2024
6fdf355
add method "DeleteWeightedEdge"
MrWh1teF0x Nov 8, 2024
8296652
beautify
MrWh1teF0x Nov 8, 2024
134d7e2
add method "GetEdgeWeight"
MrWh1teF0x Nov 8, 2024
07703c7
rename
MrWh1teF0x Nov 8, 2024
9acefa2
add tests for class "WeightedGraph"
MrWh1teF0x Nov 8, 2024
4e7f88c
add methods "PrintAdjList" and "DeleteVertex"
MrWh1teF0x Nov 8, 2024
926e156
fix mistake
MrWh1teF0x Nov 8, 2024
149f12f
add tests for class "WeightedGraph"
MrWh1teF0x Nov 9, 2024
e65de98
add methods for graph classes
MrWh1teF0x Nov 12, 2024
d1eac68
add tests for task_01
MrWh1teF0x Nov 12, 2024
bf7fd6d
add tests for task_01
MrWh1teF0x Nov 12, 2024
e47c1c4
rename
MrWh1teF0x Nov 12, 2024
db82719
add method and change constructor for class "Graph"
MrWh1teF0x Nov 17, 2024
1fe989d
beautify
MrWh1teF0x Nov 17, 2024
306139a
delete IsVisited
MrWh1teF0x Nov 17, 2024
965b901
add finding bridges in graph for task_02
MrWh1teF0x Nov 17, 2024
ce4e117
add finding joint vertices in graph for task_02
MrWh1teF0x Nov 17, 2024
8705935
add check for graph without vertices
MrWh1teF0x Nov 17, 2024
e05d9a3
add tests for "FindBridges" function
MrWh1teF0x Nov 17, 2024
9a2b8eb
rename
MrWh1teF0x Nov 17, 2024
f9d02f1
fix mistake
MrWh1teF0x Nov 17, 2024
eddb3cf
add tests for "FindJointVertices" function (same graphs)
MrWh1teF0x Nov 17, 2024
00d4530
add more tests
MrWh1teF0x Nov 17, 2024
b0ff365
add not oriented WeightedGraph, fix mistake and add tests
MrWh1teF0x Nov 18, 2024
fb96fd3
add method IsOriented for class "WeightedGraph"
MrWh1teF0x Nov 18, 2024
60c419c
add solution for task_04
MrWh1teF0x Nov 18, 2024
abf1492
add constructors for "MinPath", add comparison operator overloading, …
MrWh1teF0x Nov 18, 2024
0deed2d
beautify
MrWh1teF0x Nov 18, 2024
a00b561
add simple tests for task_04
MrWh1teF0x Nov 18, 2024
8409cfe
add more tests
MrWh1teF0x Nov 18, 2024
bd2d6d7
include dijkstra from task_04 to task_03
MrWh1teF0x Nov 22, 2024
ecb1bc1
add Bellman-Ford algorithm for task_03
MrWh1teF0x Nov 22, 2024
4783389
beautify
MrWh1teF0x Nov 22, 2024
4a3eb0b
change BellmanFord algorithm
MrWh1teF0x Nov 22, 2024
c70f536
add tests for BellmanFord algorithm
MrWh1teF0x Nov 22, 2024
9810bb7
delete useless files
MrWh1teF0x Nov 22, 2024
e8f06d8
fix and add test for BellmanFord
MrWh1teF0x Nov 29, 2024
45388d7
add more tests for task_01
MrWh1teF0x Nov 29, 2024
b2277b1
beautify
MrWh1teF0x Nov 29, 2024
087e507
add Johnson algorithm for task_03
MrWh1teF0x Nov 29, 2024
c569a16
fix mistake
MrWh1teF0x Nov 29, 2024
f38b8fb
fix mistake in Dijkstra, when typename is std::string
MrWh1teF0x Nov 29, 2024
859cab7
add tests for Johnson algorithm for task_03
MrWh1teF0x Nov 29, 2024
fd46e61
fix tests filenames and add main.cpp
MrWh1teF0x Nov 29, 2024
edb2ed9
beautify
MrWh1teF0x Nov 29, 2024
7d5e765
complete task_05
MrWh1teF0x Jan 14, 2025
631fd16
add tests for task_05
MrWh1teF0x Jan 14, 2025
9a0d918
update Cmake files (to include RMQ in LCA)
MrWh1teF0x Jan 14, 2025
6c24693
add default constructors, add method
MrWh1teF0x Jan 15, 2025
5a0d9b9
update 1 test
MrWh1teF0x Jan 15, 2025
b4b3393
complete task_06
MrWh1teF0x Jan 15, 2025
ca6aee8
update CmakeLists
MrWh1teF0x Jan 15, 2025
bef8458
add default constructor, add method and check for acyclic graph
MrWh1teF0x Jan 15, 2025
b89e8e3
add tests for task_06
MrWh1teF0x Jan 15, 2025
abe696a
beautify
MrWh1teF0x Jan 15, 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
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ file(GLOB_RECURSE lib_source_list "src/*.cpp" "src/*.hpp")

add_library(${PROJECT_NAME} ${lib_source_list})

set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
193 changes: 193 additions & 0 deletions lib/src/graph/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#pragma once

#include <map>

#include "vertex.hpp"

template <typename T>
class Graph {
public:
Graph(bool is_oriented = true) : is_oriented(is_oriented) {}

Graph(const std::vector<Vertex<T>>& vertices, bool is_oriented = true)
: vertices(vertices), is_oriented(is_oriented) {}

Graph(const std::vector<std::pair<T, T>>& edges, bool is_oriented = true)
: is_oriented(is_oriented) {
std::map<T, std::vector<T>> vertices_to_add;

for (auto edge : edges) {
vertices_to_add[edge.first].push_back(edge.second);
if (!is_oriented) vertices_to_add[edge.second].push_back(edge.first);
}

for (auto vertex : vertices_to_add) {
vertices.push_back(Vertex(vertex.first, vertex.second));
}
}

void PrintAdjList() const {
for (auto vertex : vertices) {
vertex.PrintAdjVertices();
}
}

bool IsOriented() const { return is_oriented; }

std::vector<Vertex<T>> GetVertices() const { return vertices; }

std::vector<T> GetVerticesIds() const {
std::vector<T> vertices_ids;
for (int i = 0; i < vertices.size(); i++)
vertices_ids.push_back(vertices[i].GetVertexId());
return vertices_ids;
}

std::vector<T> GetAdjVertices(const T& vertex) const {
if (!ContainsVertex(vertex))
throw std::invalid_argument("Vertex not found!");

std::vector<T> adj_vertices;
for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].GetVertexId() == vertex) {
adj_vertices = vertices[i].GetAdjVertices();
break;
}
}
return adj_vertices;
}

size_t GetVerticesCount() const { return vertices.size(); }

size_t GetEdgesCount() const {
size_t count = 0;
for (int i = 0; i < vertices.size(); i++)
count += vertices[i].GetAdjVerticesCount();
// У неориентированного графа в два раза меньше ребер, чем у такого же
// ориентированного
if (!is_oriented) count /= 2;

return count;
}

bool ContainsVertex(const Vertex<T>& vertex) const {
for (auto vert : vertices) {
if (vert.GetVertexId() == vertex.GetVertexId() &&
vert.GetAdjVerticesCount() == vertex.GetAdjVerticesCount()) {
// Проверяем смежности
auto adj_verts = vertex.GetAdjVertices();
for (int i = 0; i < adj_verts.size(); i++) {
if (!vertex.ContainsAdjVertex(adj_verts[i])) return false;
}
return true;
}
}
return false;
}

bool ContainsVertex(const T& vertex) const {
for (auto vert : vertices) {
if (vert.GetVertexId() == vertex) return true;
}
return false;
}

bool ContainsEdge(const T& vert_1, const T& vert_2) const {
for (int i = 0; i < vertices.size(); i++) {
if (vert_1 == vertices[i].GetVertexId() &&
vertices[i].ContainsAdjVertex(vert_2))
return true;
}
return false;
}

void AddVertex(const Vertex<T>& vertex) {
if (is_oriented) {
vertices.push_back(vertex);
return;
}

// Добавление вершины для неориентированного графа
for (auto adj_vertex : vertex.GetAdjVertices()) {
if (!ContainsVertex(adj_vertex)) AddVertex(adj_vertex);

for (int i = 0; i < vertices.size(); i++) {
if (adj_vertex == vertices[i].GetVertexId()) {
vertices[i].AddAdjVertex(vertex.GetVertexId());
break;
}
}
}
vertices.push_back(vertex);
}

void AddVertex(const T& vertex) { vertices.push_back(Vertex<T>(vertex)); }

void AddEdge(const T& vert_1, const T& vert_2) {
if (!ContainsVertex(vert_1)) AddVertex(vert_1);
if (!ContainsVertex(vert_2)) AddVertex(vert_2);

for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].GetVertexId() == vert_1) {
vertices[i].AddAdjVertex(vert_2);
if (is_oriented) return;
break;
}
}

// Если граф неориентированный, то мы добавляем ребро vert_2->vert_1
for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].GetVertexId() == vert_2) {
vertices[i].AddAdjVertex(vert_1);
return;
}
}
}

void DeleteVertex(const T& vertex) {
if (!ContainsVertex(vertex))
throw std::invalid_argument("Vertex not found!");

for (int i = 0; i < vertices.size(); i++) {
if (vertex == vertices[i].GetVertexId()) {
vertices.erase(vertices.begin() + i);

// Удаляем смежности с удаленной вершиной
for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].ContainsAdjVertex(vertex))
vertices[i].DeleteAdjVertex(vertex);
}
return;
}
}
}

void DeleteEdge(const T& vert_1, const T& vert_2) {
if (!ContainsVertex(vert_1)) throw std::invalid_argument("Edge not found!");

for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].GetVertexId() == vert_1) {
vertices[i].DeleteAdjVertex(vert_2);
if (is_oriented)
return;
else
break;
}
}

// Для неориентированного графа нужно удалить и у vert_2 смежную вершину
// vert_1
if (!ContainsVertex(vert_2)) throw std::invalid_argument("Edge not found!");

for (int i = 0; i < vertices.size(); i++) {
if (vertices[i].GetVertexId() == vert_2) {
vertices[i].DeleteAdjVertex(vert_1);
return;
}
}
}

private:
std::vector<Vertex<T>> vertices;
bool is_oriented;
};
53 changes: 53 additions & 0 deletions lib/src/graph/vertex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <iostream>
#include <stdexcept>
#include <vector>

template <typename T>
class Vertex {
public:
Vertex(const T& vertex_id, const std::vector<T>& adj_vertices = {})
: vertex_id(vertex_id), adjacent_vertices(adj_vertices) {}

void AddAdjVertex(const T& adj_vertex) {
if (ContainsAdjVertex(adj_vertex)) return;
adjacent_vertices.push_back(adj_vertex);
}

void DeleteAdjVertex(const T& adj_vertex) {
for (int i = 0; i < adjacent_vertices.size(); i++) {
if (adjacent_vertices[i] == adj_vertex) {
adjacent_vertices.erase(adjacent_vertices.begin() + i);
return;
}
}
throw std::invalid_argument("Adjacent vertex not found!");
}

T GetVertexId() const { return vertex_id; }

std::vector<T> GetAdjVertices() const { return adjacent_vertices; }

size_t GetAdjVerticesCount() const { return adjacent_vertices.size(); }

void PrintAdjVertices() const {
std::cout << "Adjacent vertices for " << vertex_id << ": {";
for (int i = 0; i < adjacent_vertices.size(); i++) {
std::cout << adjacent_vertices[i];
if (i != adjacent_vertices.size() - 1) std::cout << ", ";
}
std::cout << "}\n";
}

bool ContainsAdjVertex(const T& adj_vert) const {
for (auto vertex : adjacent_vertices) {
if (adj_vert == vertex) return true;
}
return false;
}

private:
T vertex_id;
std::vector<T> adjacent_vertices;
};
Loading
Loading