Skip to content
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
f0bd3c8
Add graph class in utils
Stargazer2005 Sep 21, 2024
1d846ff
Undirected edge adding func
Stargazer2005 Sep 21, 2024
d3602f6
Rename files
Stargazer2005 Sep 21, 2024
22ffad7
Graph class improvement
Stargazer2005 Sep 22, 2024
3709d12
Return utils files
Stargazer2005 Sep 22, 2024
e3e497c
Move methods definitions to the header
Stargazer2005 Sep 22, 2024
75d57fa
Improve naming
Stargazer2005 Sep 22, 2024
0c36f3e
Task_01 classes
Stargazer2005 Sep 22, 2024
3fe4cd4
Improve methods naming
Stargazer2005 Sep 22, 2024
22d90b8
Add Size() method
Stargazer2005 Sep 22, 2024
e1ce917
Disacard changes
Stargazer2005 Sep 22, 2024
310e284
task_01 packman alogirthm
Stargazer2005 Sep 23, 2024
4eb21cb
Add indices methods
Stargazer2005 Sep 23, 2024
c84a76b
Output impovment
Stargazer2005 Sep 23, 2024
e2f2f28
Fomat
Stargazer2005 Sep 23, 2024
7a6280a
Topology sort algorithm + tests
Stargazer2005 Sep 24, 2024
4f1a34d
resolve conflict
Stargazer2005 Oct 4, 2024
4246f4b
Merge branch 'main' into main
Stargazer2005 Oct 4, 2024
8c0bdf5
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Stargazer2005 Oct 7, 2024
faa6e67
Merge branch 'AlgorithmsDafeMipt2024:main' into main
Stargazer2005 Oct 30, 2024
d545794
codestyle improvement
Stargazer2005 Oct 31, 2024
d4966ea
apply some clang-tidy suggestions
Stargazer2005 Oct 31, 2024
6aa324c
task_02
Stargazer2005 Oct 31, 2024
474f6a6
made function return beridges and cut vertices, fixed tests due to this
Stargazer2005 Oct 31, 2024
158b669
add missing '#pragma once'
Stargazer2005 Oct 31, 2024
e202e3d
added weighted graph class
Stargazer2005 Nov 1, 2024
b5c3636
task_04
Stargazer2005 Nov 1, 2024
8221b80
fix graph methods
Stargazer2005 Nov 15, 2024
d861367
add corresponding changes in tasks
Stargazer2005 Nov 15, 2024
1d2d204
add djikstra library
Stargazer2005 Nov 15, 2024
85afe6b
fix naming
Stargazer2005 Nov 15, 2024
ad12812
add target include directories
Stargazer2005 Nov 15, 2024
0d34e44
final fix naming(djikstra->dijkstra)
Stargazer2005 Nov 15, 2024
b7cce0e
add reweight() method
Stargazer2005 Nov 15, 2024
3bf6e15
task_03
Stargazer2005 Nov 15, 2024
e417c49
make concept usage in lib
Stargazer2005 Nov 18, 2024
a1bdeff
rewrite tasks due to lib changes
Stargazer2005 Nov 18, 2024
1375f5d
format changes
Stargazer2005 Nov 18, 2024
c3f6de7
weaken graph concept
Stargazer2005 Nov 18, 2024
1ea4514
task_01 adapted to package manager simulating
Stargazer2005 Nov 29, 2024
89105a3
add check if graph is cyclic
Stargazer2005 Nov 29, 2024
238a5a3
add task description
Stargazer2005 Dec 1, 2024
a91e0b3
task solution
Stargazer2005 Dec 1, 2024
5deaffe
revert changes
Stargazer2005 Dec 1, 2024
8bd78f8
revert changes
Stargazer2005 Dec 1, 2024
cdb6dd5
bring missing clang-tidy configs back
Stargazer2005 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
2 changes: 0 additions & 2 deletions .github/workflows/clang-tidy-review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:

- uses: ZedThree/[email protected]
with:
apt_packages: libgtest-dev
clang_tidy_version: 18
clang_tidy_checks: '-*,performance-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-avoid-magic-numbers,-clang-diagnostic-error,-misc-no-recursion,-cppcoreguidelines-owning-memory,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,-misc-non-private-member-variables-in-classes,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-special-member-functions,-cppcoreguidelines-init-variables,-misc-include-cleaner'
split_workflow: true

Expand Down
187 changes: 187 additions & 0 deletions lib/src/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <memory>
#include <set>
#include <vector>

/// @brief Graphs vertex
/// @tparam T
template <typename T>
struct Vertex {
Vertex(const T &d) : data(d) {}

T data;
std::set<std::shared_ptr<Vertex<T>>> adjacent;
};

/// @brief Basic graph
/// @tparam T
template <typename T>
class Graph {

Choose a reason for hiding this comment

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

warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init]

lib/src/graph.hpp:209:

-   std::vector<std::shared_ptr<VertexType>> vertices_;
+   std::vector<std::shared_ptr<VertexType>> vertices_{};

Choose a reason for hiding this comment

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

warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor]

class Graph {
      ^
Additional context

lib/src/graph.hpp:27: make it public and virtual

class Graph {
      ^

public:
/**
* @brief
* Add a new vertex to the graph
* @param data
*/
void AddVertex(const T &data) {
vertices_.push_back(std::make_shared<Vertex<T>>(data));
};

std::shared_ptr<Vertex<T>> operator[](size_t index) {
return vertices_[index];
}

const std::shared_ptr<Vertex<T>> operator[](size_t index) const {
return vertices_[index];
}

/**
* @brief
* Find vertexes index
* @param vertex
* @return size_t
*/
size_t Find(const std::shared_ptr<Vertex<T>> &vertex) const {
return std::find(vertices_.begin(), vertices_.end(), vertex) -
vertices_.begin();
};

/**
* @brief
* Remove a vertex from the graph
* @param vertex
*/
void RemoveVertex(std::shared_ptr<Vertex<T>> vertex) {
// Find the vertex in the graph
auto it = std::find(vertices_.begin(), vertices_.end(), vertex);
if (it == vertices_.end()) {
// Vertex not found
return;
}

// Remove edges pointing to the vertex
for (auto &v : vertices_[it - vertices_.begin()]->adjacent) {
RemoveEdge(v, vertices_[it - vertices_.begin()]);
}
// Remove the vertex from the graph
vertices_.erase(it);
}

/**
* @brief
* Returns the number of vertices in the graph
* @return size_t
*/
size_t Size() const { return vertices_.size(); }

/**
* @brief
* Add a directed edge between two vertices via indices
* @param source_id
* @param target_id
*/
void AddDirEdge(size_t source_id, size_t target_id) {
operator[](source_id)->adjacent.insert(operator[](target_id));
}

/**
* @brief
* Add a directed edge between two vertices
* @param source
* @param target
*/
void AddDirEdge(std::shared_ptr<Vertex<T>> source,
std::shared_ptr<Vertex<T>> target) {
source->adjacent.insert(target);
}

/**
* @brief
* Remove a directed edge between two vertices via indices
* @param source_id
* @param target_id
*/
void RemoveDirEdge(size_t source_id, size_t target_id) {
operator[](source_id)->adjacent.erase(*std::find(
operator[](source_id)->adjacent.begin(),
operator[](source_id)->adjacent.end(), operator[](target_id)));
}

/**
* @brief
* Remove a directed edge between two vertices
* @param source
* @param target
*/
void RemoveDirEdge(std::shared_ptr<Vertex<T>> source,
std::shared_ptr<Vertex<T>> target) {
source->adjacent.erase(
*std::find(source->adjacent.begin(), source->adjacent.end(), target));
}

/**
* @brief
* Add a non-directed edge between two vertices via indices
* @param first_id
* @param second_id
*/
void AddEdge(size_t first_id, size_t second_id) {
AddDirEdge(first_id, second_id);
AddDirEdge(second_id, first_id);
}

/**
* @brief
* Add a non-directed edge between two vertices
* @param vertex_1
* @param vertex_2
*/
void AddEdge(std::shared_ptr<Vertex<T>> vertex_1,
std::shared_ptr<Vertex<T>> vertex_2) {
AddDirEdge(vertex_1, vertex_2);
AddDirEdge(vertex_2, vertex_1);
}

/**
* @brief
* Remove a non-directed edge between two vertices via indices
* @param first_id
* @param second_id
*/
void RemoveEdge(size_t first_id, size_t second_id) {
RemoveDirEdge(first_id, second_id);
RemoveDirEdge(second_id, first_id);
}

/**
* @brief
* Remove a non-directed edge between two vertices
* @param vertex_1
* @param vertex_2
*/
void RemoveEdge(std::shared_ptr<Vertex<T>> vertex_1,
std::shared_ptr<Vertex<T>> vertex_2) {
RemoveDirEdge(vertex_1, vertex_2);
RemoveDirEdge(vertex_2, vertex_1);
}

/**
* @brief
* Print the adjacency list of the graph
*/
void PrintGraph() const {
for (const auto &vertex : vertices_) {
std::cout << vertex->data << " -> ";
for (const auto &neighbor : vertex->adjacent) {
std::cout << neighbor->data << " ";
}
std::cout << std::endl;
}
}

protected:
std::vector<std::shared_ptr<Vertex<T>>> vertices_;

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Copy link

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

Choose a reason for hiding this comment

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

warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<Vertex<T>>> vertices_;
                                          ^

};
1 change: 0 additions & 1 deletion lib/src/util.cpp

This file was deleted.

1 change: 1 addition & 0 deletions lib/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "utils.hpp"
File renamed without changes.
24 changes: 23 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
int main() { return 0; }
#include "packman.hpp"

int main() {
DependencyGraph dg_1;

dg_1.AddVertex("Lib_1");
dg_1.AddVertex("Lib_2");
dg_1.AddVertex("Lib_3");
dg_1.AddVertex("Lib_4");
dg_1.AddVertex("Lib_5");

dg_1.AddDirEdge(0, 1);
dg_1.AddDirEdge(0, 2);
dg_1.AddDirEdge(2, 3);
dg_1.AddDirEdge(3, 1);
dg_1.AddDirEdge(0, 3);

PackageManager packman_1(dg_1);
Copy link

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);

Choose a reason for hiding this comment

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

warning: variable 'packman_1' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman_1(dg_1);
PackageManager const packman_1(dg_1);


packman_1.FindDownloadingOrder();

return 0;
}
35 changes: 35 additions & 0 deletions task_01/src/packman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "packman.hpp"

void PackageManager::FindingOrderStep(
std::shared_ptr<Vertex<std::string>> target) {
is_visited_[dependencies_.Find(target)] = true;

for (auto& neighbor : target->adjacent) {
if (!is_visited_[dependencies_.Find(neighbor)]) {
FindingOrderStep(neighbor);
}
}

reverse_order_.push(target->data);
}

std::vector<std::string> PackageManager::FindDownloadingOrder(/*
std::shared_ptr<Vertex<std::string>> target*/) {
is_visited_.resize(dependencies_.Size());
for (size_t i = 0; i < is_visited_.size(); ++i) is_visited_[i] = false;

for (size_t i = 0; i < dependencies_.Size(); ++i)
if (!is_visited_[i]) FindingOrderStep(dependencies_[i]);

std::vector<std::string> order;

std::cout << "Downloading Order for "
<< /*target->data <<*/ " is:" << std::endl;
while (reverse_order_.size() > 0) {
order.push_back(reverse_order_.top());
std::cout << reverse_order_.top() << " ";
reverse_order_.pop();
}
std::cout << std::endl;
return order;
}
31 changes: 31 additions & 0 deletions task_01/src/packman.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stack>
#include <string>

#include "graph.hpp"

/// @brief Graph of dependencies between libraries
/// The "parent" libraries should be installed before thier children
class DependencyGraph : public Graph<std::string> {};

/// @brief Packman basic algorithm
class PackageManager {
public:
PackageManager() = delete;
PackageManager(DependencyGraph& dep_graph) : dependencies_{dep_graph} {}

/**
* @brief
* Finds the right order to install libraries in
* with respect to dependencies
* @param target Needed library
* @return std::vector<std::string>
*/
std::vector<std::string> FindDownloadingOrder(/*
std::shared_ptr<Vertex<std::string>> target*/);

private:
void FindingOrderStep(std::shared_ptr<Vertex<std::string>> target);
DependencyGraph& dependencies_;
std::vector<bool> is_visited_;
std::stack<std::string> reverse_order_;
};
Loading
Loading