Skip to content
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

semester_tasks #13

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 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
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/clang-tidy-review@v0.19.0
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
211 changes: 211 additions & 0 deletions lib/src/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <memory>
#include <set>
#include <type_traits>
#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;
};

template <typename T, typename VT>
concept IsVertex = std::is_base_of<Vertex<VT>, T>::value;

Choose a reason for hiding this comment

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

warning: unknown type name 'concept' [clang-diagnostic-error]

concept IsVertex = std::is_base_of<Vertex<VT>, T>::value;
^


/// @brief Basic graph
/// @tparam VertexType
/// @tparam T
template <typename VertexType, typename T>
requires IsVertex<VertexType, T>

Choose a reason for hiding this comment

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

warning: expected ';' at end of declaration [clang-diagnostic-error]

Suggested change
requires IsVertex<VertexType, T>
requires IsVertex<VertexType, T>;

Choose a reason for hiding this comment

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

warning: unknown type name 'requires' [clang-diagnostic-error]

  requires IsVertex<VertexType, T>
  ^

Choose a reason for hiding this comment

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

warning: variable template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list [clang-diagnostic-error]

Suggested change
requires IsVertex<VertexType, T>
requires IsVertex

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:208:

-   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:26: make it public and virtual

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 {
      ^

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 {
      ^

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 {
      ^

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 {
      ^

Copy link

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_{};

Copy link

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 {
      ^

Copy link

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_{};

Copy link

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 {
      ^

Copy link

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_{};

Copy link

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
*/
virtual void AddVertex(const T &data) {

Choose a reason for hiding this comment

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

warning: unknown type name 'T' [clang-diagnostic-error]

  virtual void AddVertex(const T &data) {
                               ^

vertices_.push_back(std::make_shared<VertexType>(data));

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

    vertices_.push_back(std::make_shared<VertexType>(data));
                                         ^

};

std::shared_ptr<VertexType> operator[](size_t index) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  std::shared_ptr<VertexType> operator[](size_t index) {
                  ^

return vertices_[index];
}

const std::shared_ptr<VertexType> operator[](size_t index) const {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  const std::shared_ptr<VertexType> operator[](size_t index) const {
                        ^

return vertices_[index];
}

/**
* @brief
* Find vertexes index
* @param vertex
* @return size_t
*/
size_t Find(const T &vertex) const {

Choose a reason for hiding this comment

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

warning: unknown type name 'T' [clang-diagnostic-error]

  size_t Find(const T &vertex) const {
                    ^

size_t index;
for (index = 0; index < vertices_.size(); ++index)
if (vertices_[index]->data == vertex) return index;

return index;
}

/**
* @brief
* Remove a vertex from the graph via index
* @param vertex_id
*/
virtual void RemoveVertex(size_t vertex_id) {
if (vertex_id >= Size()) {
// Vertex not found
return;
}

// Remove edges pointing to the vertex
for (auto &v : vertices_[vertex_id]->adjacent) {
RemoveDirEdge(v, vertices_[vertex_id]);
}

// Remove the vertex from the graph
vertices_.erase(vertices_.begin() + vertex_id);
}

/**
* @brief
* Remove a vertex from the graph
* @param vertex
*/
virtual void RemoveVertex(std::shared_ptr<VertexType> vertex) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  virtual void RemoveVertex(std::shared_ptr<VertexType> vertex) {
                                            ^

// Find the vertex in the graph
auto it = std::find(vertices_.begin(), vertices_.end(), vertex);
if (it == vertices_.end()) {
// Vertex not found
return;
}

RemoveVertex(it - vertices_.begin());
}

/**
* @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
*/
virtual 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
*/
virtual void AddDirEdge(std::shared_ptr<VertexType> source,

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  virtual void AddDirEdge(std::shared_ptr<VertexType> source,
                                          ^

std::shared_ptr<VertexType> target) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

                          std::shared_ptr<VertexType> target) {
                                          ^

source->adjacent.insert(target);
}

/**
* @brief
* Remove a directed edge between two vertices via indices
* @param source_id
* @param target_id
*/
virtual 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
*/
virtual void RemoveDirEdge(std::shared_ptr<VertexType> source,

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  virtual void RemoveDirEdge(std::shared_ptr<VertexType> source,
                                             ^

std::shared_ptr<VertexType> target) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

                             std::shared_ptr<VertexType> 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
*/
virtual 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
*/
virtual void AddEdge(std::shared_ptr<VertexType> vertex_1,

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  virtual void AddEdge(std::shared_ptr<VertexType> vertex_1,
                                       ^

std::shared_ptr<VertexType> vertex_2) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

                       std::shared_ptr<VertexType> 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
*/
virtual 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
*/
virtual void RemoveEdge(std::shared_ptr<VertexType> vertex_1,

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  virtual void RemoveEdge(std::shared_ptr<VertexType> vertex_1,
                                          ^

std::shared_ptr<VertexType> vertex_2) {

Choose a reason for hiding this comment

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

warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

                          std::shared_ptr<VertexType> vertex_2) {
                                          ^

RemoveDirEdge(vertex_1, vertex_2);
RemoveDirEdge(vertex_2, vertex_1);
}

/**
* @brief
* Print the adjacency list of the graph
*/
virtual 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;

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Copy link

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

Choose a reason for hiding this comment

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

warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]

Suggested change
std::cout << std::endl;
std::cout << '\n';

}
}

protected:
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: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  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: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  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: use of undeclared identifier 'VertexType' [clang-diagnostic-error]

  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: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  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: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  std::vector<std::shared_ptr<VertexType>> 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<VertexType>> 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<VertexType>> 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<VertexType>> 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.
136 changes: 136 additions & 0 deletions lib/src/weighted_graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#pragma once

#include <limits>

#include "graph.hpp"

/// @brief Basic weighted graph
/// @tparam VertexType
/// @tparam T
template <typename VertexType, typename T>
class WeightedGraph : public Graph<VertexType, T> {
public:
WeightedGraph() : Graph<VertexType, T>() {
weights.resize(Graph<VertexType, T>::vertices_.size(),
std::vector<int>(Graph<VertexType, T>::vertices_.size(),
std::numeric_limits<int>::max()));
}

/**
* @brief
* Override AddVertex to handle weights matrix resizing
* @param data
*/
void AddVertex(const T& data) override {
Graph<VertexType, T>::AddVertex(data);
weights.resize(Graph<VertexType, T>::vertices_.size(),
std::vector<int>(Graph<VertexType, T>::vertices_.size(),
std::numeric_limits<int>::max()));
for (auto& row : weights)
row.resize(Graph<VertexType, T>::vertices_.size(),
std::numeric_limits<int>::max());
}

/**
* @brief
* Override RemoveVertex via index to handle weights matrix resizing
* @param vertex_id
*/
void RemoveVertex(size_t vertex_id) override {
// Remove the vertex from the base class
Graph<VertexType, T>::RemoveVertex(vertex_id);

// Remove the corresponding row and column from the weights matrix
weights.erase(weights.begin() + vertex_id); // Remove the row

for (auto& row : weights)
row.erase(row.begin() + vertex_id); // Remove the column
}

/**
* @brief
* Add a weighted directed edge
* @param source_id
* @param target_id
* @param weight
*/
void AddDirEdge(size_t source_id, size_t target_id, int weight) {
Graph<VertexType, T>::AddDirEdge(source_id, target_id);
weights[source_id][target_id] = weight;
}

/**
* @brief
* Add a weighted undirected edge
* @param source_id
* @param target_id
* @param weight
*/
void AddEdge(size_t source_id, size_t target_id, int weight) {
AddDirEdge(source_id, target_id, weight);
AddDirEdge(target_id, source_id, weight);
}

/**
* @brief
* Remove a weighted directed edge
* @param source_id
* @param target_id
*/
void RemoveDirEdge(size_t source_id, size_t target_id) override {
Graph<VertexType, T>::RemoveDirEdge(source_id, target_id);
weights[source_id][target_id] = std::numeric_limits<int>::max();
}

/**
* @brief
* Remove a weighted undirected edge
* @param source_id
* @param target_id
*/
void RemoveEdge(size_t source_id, size_t target_id) override {
RemoveDirEdge(source_id, target_id);
RemoveDirEdge(target_id, source_id);
}

/**
* @brief
* Get the weight of an edge
* @param source_id
* @param target_id
* @return int
*/
int GetWeight(size_t source_id, size_t target_id) const {
return weights[source_id][target_id];
}

/**
* @brief
* Reweight an edge
* @param source_id
* @param target_id
* @param new_weight
*/
void Reweight(size_t source_id, size_t target_id, int new_weight) {
weights[source_id][target_id] = new_weight;
}

/**
* @brief
* Print the weighted graph
*/
void PrintGraph() const override {
for (size_t i = 0; i < Graph<VertexType, T>::vertices_.size(); ++i) {
std::cout << Graph<VertexType, T>::vertices_[i]->data << " -> ";
for (const auto& neighbor :
Graph<VertexType, T>::vertices_[i]->adjacent) {
size_t j = Graph<VertexType, T>::Find(neighbor->data);
std::cout << "(" << neighbor->data << ", " << weights[i][j] << ") ";
}
std::cout << std::endl;
}
}

private:
std::vector<std::vector<int>> weights;
};
26 changes: 25 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
int main() { return 0; }
#include "packman.hpp"

int main() {
DependencyGraph dg;

std::vector<std::string> packages = {"Lib_1", "Lib_2", "Lib_3", "Lib_4",
"Lib_5"};
for (const auto& package : packages) dg.AddVertex(package);

dg.AddDirEdge(0, 1); // Lib_1 depends on Lib_2
dg.AddDirEdge(0, 2); // Lib_1 depends on Lib_3
dg.AddDirEdge(2, 3); // Lib_3 depends on Lib_4
dg.AddDirEdge(3, 1); // Lib_4 depends on Lib_2
dg.AddDirEdge(0, 3); // Lib_1 depends on Lib_4

PackageManager packman(dg);

Choose a reason for hiding this comment

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

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

Suggested change
PackageManager packman(dg);
PackageManager const packman(dg);

Choose a reason for hiding this comment

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

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

Suggested change
PackageManager packman(dg);
PackageManager const packman(dg);

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' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman(dg);
PackageManager const packman(dg);

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' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman(dg);
PackageManager const packman(dg);

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' of type 'PackageManager' can be declared 'const' [misc-const-correctness]

Suggested change
PackageManager packman(dg);
PackageManager const packman(dg);

packman.FindDownloadingOrder("Lib_1");
packman.FindDownloadingOrder("Lib_2");
packman.FindDownloadingOrder("Lib_3");
packman.FindDownloadingOrder("Lib_4");
packman.FindDownloadingOrder("Lib_5");

return 0;
}
Loading
Loading