Skip to content

Kulikov Maksimilian #35

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 16 commits into
base: main
Choose a base branch
from
44 changes: 44 additions & 0 deletions lib/src/util.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
#include "util.hpp"

EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

Choose a reason for hiding this comment

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

warning: the parameter 'edges' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

lib/src/util.hpp:12:

-     EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
+     EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges);
Suggested change
EdgeOrientedGraph::EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges) {
EdgeOrientedGraph::EdgeOrientedGraph(int V, const std::vector<std::vector<int>>& edges) {

_ways = std::vector<std::vector<int>>(V);
for (auto x : edges)
_ways[x[0]].push_back(x[1]);
}

EdgeOrientedGraph::EdgeOrientedGraph(EdgeOrientedGraph const& graph) : _ways(graph._ways) {}

int EdgeOrientedGraph::size() { return _ways.size(); }

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int EdgeOrientedGraph::size() { return _ways.size(); }
                                       ^


std::vector<int>& EdgeOrientedGraph::operator[] (int key) { return _ways[key]; }


OrientedWeightedGraph::OrientedWeightedGraph(int V) {
_weights = std::vector<std::vector<double>>(V,
std::vector<double>(V, std::numeric_limits<double>::infinity()));
}

OrientedWeightedGraph::OrientedWeightedGraph(std::vector<std::vector<double>> const& weights) : _weights(weights) {
int V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

    int V = weights.size();
            ^

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 = weights.size();
int const V = weights.size();

for (auto& x : weights)
if (V != x.size())
throw std::exception();
}

OrientedWeightedGraph::OrientedWeightedGraph(OrientedWeightedGraph const& graph) : _weights(graph._weights) {}

void OrientedWeightedGraph::_zero_vertex() {
for (auto& x : _weights)
x.push_back(std::numeric_limits<double>::infinity());
_weights.push_back(std::vector<double>(this->size()+1));
_weights[this->size()-1][this->size()-1] = std::numeric_limits<double>::infinity();
}

void OrientedWeightedGraph::_delete_vertex() {
_weights.pop_back();
for (auto& x : _weights)
x.pop_back();
}

int OrientedWeightedGraph::size() { return _weights.size(); }

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^

Choose a reason for hiding this comment

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

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

int OrientedWeightedGraph::size() { return _weights.size(); }
                                           ^


std::vector<double>& OrientedWeightedGraph::operator[](int first) { return _weights[first]; }
48 changes: 48 additions & 0 deletions lib/src/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <vector>
#include <limits>

class EdgeOrientedGraph {
// Contains directions for each corresponding vertex key;
// Key is the order in which vector<int> are stored.
// Keys start from zero.
std::vector<std::vector<int>> _ways;

public:
// int V : the amount of vertices;
// Vector<vector<int>> : edges of the graph {..., (start_i, end_i), ...}.
EdgeOrientedGraph(int V, std::vector<std::vector<int>> edges);
// Transfers `_ways`
EdgeOrientedGraph(EdgeOrientedGraph const& graph);

// Returns the amount of vertices in the graph.
int size();
// Returns all available directions from vertex with same key value.
std::vector<int>& operator[](int key);
};


class OrientedWeightedGraph {
// Matrix of weights.
// First index is a key of the beginning vertex;
// Second index is a key of the end vertex.
std::vector<std::vector<double>> _weights;
public:
// Creates weight matrix with all infinite weights.
// int V : amount of vertices.
OrientedWeightedGraph(int V);
// Creates weight matrix with the same values as the `weights` matrix.
OrientedWeightedGraph(std::vector<std::vector<double>> const& weights);
// Transfers matrix
OrientedWeightedGraph(OrientedWeightedGraph const& graph);

// Add zero weight vertex needed for Johnson's algorithm.
void _zero_vertex();
// Deletes the last vertex from the weight matrix.
void _delete_vertex();
// Returns the amount of vertices in the graph.
int size();
// Returns the weight of an edge.
// First index is a key of the beginning vertex;
// Second index is a key of the end vertex.
std::vector<double>& operator[](int first);
};
9 changes: 7 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
TEST(Topa, Simple) {
// std::vector<int> expected = {0, 3, 2, 1};

// std::vector<std::vector<int>> edges = {{0, 3}, {3, 1}, {3, 2}, {2, 1}};
// OrientedGraph gr(4, edges);
// std::vector<int> result = topo_sort(gr);
ASSERT_EQ(1, 1); // Stack []
}
}
33 changes: 33 additions & 0 deletions task_01/src/topo.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

не экономь на именах

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "topo.hpp"

void topo_recr(EdgeOrientedGraph& graph, std::stack<int>& vstack, std::vector<int>& vcolor, int current) {
Copy link
Contributor

Choose a reason for hiding this comment

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

не по код стайлу(

vcolor[current] = 1;
for (auto n : graph[current]) {
if (vcolor[n] == 2)
continue;
if (vcolor[n] == 1) {
vstack = {};
throw std::exception(); // if graph is cyclic
}
topo_recr(graph, vstack, vcolor, n);
}
vcolor[current] = 2;
vstack.push(current);
}

std::vector<int> topo_sort(EdgeOrientedGraph graph, int from_key) {
std::vector<int> fin_seq;
std::stack<int> vert_stack;
// 0 -- white
// 1 -- grey
// 2 -- black
std::vector<int> vert_color(graph.size());
vert_color[from_key] = 1;
topo_recr(graph, vert_stack, vert_color, from_key);

while (!vert_stack.empty()) {
fin_seq.push_back(vert_stack.top());
vert_stack.pop();
}
return fin_seq;
}
7 changes: 7 additions & 0 deletions task_01/src/topo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <vector>
#include <stack>
#include "util.hpp"

Choose a reason for hiding this comment

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

warning: 'util.hpp' file not found [clang-diagnostic-error]

#include "util.hpp"
         ^


void topo_recr(EdgeOrientedGraph& graph, std::stack<int>& vstack, std::vector<int>& vcolor, int current);

std::vector<int> topo_sort(EdgeOrientedGraph graph, int from_key);
83 changes: 83 additions & 0 deletions task_03/src/bell_john.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "bell_john.hpp"

bool FirstGreater::operator()(std::pair<double, int> const& lhs,
std::pair<double, int> const& rhs) const
{ return lhs.first > rhs.first; }


std::vector<double> dijkstra(OrientedWeightedGraph graph, int from_key)
{
double infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'infinity' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
double infinity = std::numeric_limits<double>::infinity();
double const infinity = std::numeric_limits<double>::infinity();

// 0 -- haven't visited this vertex yet
// 1 -- have already visited this vertex
std::vector<double> visited(graph.size(), 0);
std::vector<double> distances(graph.size(), infinity);
std::priority_queue<std::pair<double, int>,
std::vector<std::pair<double, int>>,
FirstGreater> queue;
int N = graph.size(), current;
distances[from_key] = 0;
queue.push(std::make_pair(distances[from_key], from_key));

while (!queue.empty()) {
current = queue.top().second;
queue = {};
for(int i=0; i<N; ++i) {
if (visited[i] == 1 || i == current)
continue;
if (distances[current] + graph[current][i] < distances[i])
distances[i] = distances[current] + graph[current][i];
}
visited[current] = 1;
for(int i=0; i<N; ++i)
if (!visited[i])
queue.push(std::make_pair(distances[i], i));
}

return distances;
}

std::vector<double> bellman(OrientedWeightedGraph graph, int from_key, int iterations) {
if (iterations == std::numeric_limits<int>::infinity())
iterations = graph.size();
std::vector<double> distances(graph.size(), std::numeric_limits<double>::infinity()),
infinite_row = distances;
std::vector<std::vector<double>> paths = {};
int V = graph.size();
distances[from_key] = 0;
paths.push_back(distances);

for(int step=1; step<iterations; ++step) {
paths.push_back(infinite_row);

for(int from=0; from<V; ++from) {
double current_weight = paths[step-1][from];
if (current_weight == std::numeric_limits<double>::infinity())
continue;

for(int to=0; to<graph[from].size(); ++to) {
if (current_weight + graph[from][to] < paths[step][to])
paths[step][to] = current_weight + graph[from][to];
if (current_weight + graph[from][to] < distances[to])
distances[to] = current_weight + graph[from][to];
}
}
}
return distances;
}

std::vector<double> johnson(OrientedWeightedGraph graph, int from_key) {
graph._zero_vertex();
std::vector<double> bel_dist = bellman(graph, graph.size()-1);
graph._delete_vertex();
int V = graph.size();

for(int i=0; i<V; ++i)
for(int j=0; j<V; ++j)
graph[i][j] += bel_dist[i] - bel_dist[j];

std::vector<double> res_dist = dijkstra(graph, from_key);
for(int i=0; i<V; ++i)
res_dist[i] -= bel_dist[from_key] - bel_dist[i];
return res_dist;
}
16 changes: 16 additions & 0 deletions task_03/src/bell_john.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <limits>
#include <vector>
#include <queue>
#include <map>
#include "util.hpp"

Choose a reason for hiding this comment

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

warning: 'util.hpp' file not found [clang-diagnostic-error]

#include "util.hpp"
         ^


struct FirstGreater {
bool operator()(std::pair<double, int> const& lhs,
std::pair<double, int> const& rhs) const;
};

std::vector<double> dijkstra(OrientedWeightedGraph graph, int from_key);

std::vector<double> bellman(OrientedWeightedGraph graph, int from_key, int iterations = std::numeric_limits<int>::infinity());

std::vector<double> johnson(OrientedWeightedGraph graph, int from_key);
16 changes: 13 additions & 3 deletions task_03/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"

TEST(TopologySort, Simple) {
TEST(Johnson, Simple) {
// std::vector<double> expected = {-4., -2., 3., 0., -6.};

// double inf = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'inf' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
// double inf = std::numeric_limits<double>::infinity();
double const inf = std::numeric_limits<double>::infinity();

// std::vector<std::vector<double>> weights = {
// {inf, 6, 7, inf, inf},
// {inf, inf, 8, 5, -4 },
// {inf, inf, inf, -3, 9 },
// {inf, -2, inf, inf, inf},
// {2, inf, inf, 7, inf}
// };
// OrientedWeightedGraph graph(weights);
// std::vector<double> result = johnson(graph, from);
ASSERT_EQ(1, 1); // Stack []
}
12 changes: 12 additions & 0 deletions task_04/src/dijkstra.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <vector>
#include <queue>
#include <map>
#include "util.hpp"

Choose a reason for hiding this comment

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

warning: 'util.hpp' file not found [clang-diagnostic-error]

#include "util.hpp"
         ^


struct FirstGreater {
bool operator()(std::pair<double, int> const& lhs,
std::pair<double, int> const& rhs) const;
};


std::vector<double> dijkstra(OrientedWeightedGraph graph, int from_key);
38 changes: 38 additions & 0 deletions task_04/src/dijsktra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "dijkstra.hpp"

bool FirstGreater::operator()(std::pair<double, int> const& lhs,
std::pair<double, int> const& rhs) const
{ return lhs.first > rhs.first; }


std::vector<double> dijkstra(OrientedWeightedGraph graph, int from_key)
{
double infinity = std::numeric_limits<double>::infinity();
// 0 -- haven't visited this vertex yet
// 1 -- have already visited this vertex
std::vector<double> visited(graph.size(), 0);
std::vector<double> distances(graph.size(), infinity);
std::priority_queue<std::pair<double, int>,
std::vector<std::pair<double, int>>,
FirstGreater> queue;
int N = graph.size(), current;
distances[from_key] = 0;
queue.push(std::make_pair(distances[from_key], from_key));

while (!queue.empty()) {
current = queue.top().second;
queue = {};
for(int i=0; i<N; ++i) {
if (visited[i] == 1 || i == current)
continue;
if (distances[current] + graph[current][i] < distances[i])
distances[i] = distances[current] + graph[current][i];
}
visited[current] = 1;
for(int i=0; i<N; ++i)
if (!visited[i])
queue.push(std::make_pair(distances[i], i));
}

return distances;
}
15 changes: 14 additions & 1 deletion task_04/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@

#include <gtest/gtest.h>

TEST(TopologySort, Simple) {
TEST(Dijkstra, Simple) {
// std::vector<double> expected = {0., 7., 9., 20., 20., 11.};

// double inf = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'inf' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
// double inf = std::numeric_limits<double>::infinity();
double const inf = std::numeric_limits<double>::infinity();

Choose a reason for hiding this comment

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

warning: variable 'inf' of type 'double' can be declared 'const' [misc-const-correctness]

Suggested change
// double inf = std::numeric_limits<double>::infinity();
double const inf = std::numeric_limits<double>::infinity();

// std::vector<std::vector<double>> weights = {
// {inf, 7, 9, inf, inf, 14},
// {7, inf, 10, inf, inf, inf},
// {9, 10, inf, 11, inf, 2},
// {inf, inf, 11, inf, 6, inf},
// {inf, inf, inf, 6, inf, 9},
// {14, inf, 2, inf, 9, inf}
// };
// OrientedWeightedGraph graph(weights);
// std::vector<double> result = dijkstra_alg(graph, 0);
ASSERT_EQ(1, 1); // Stack []
}
32 changes: 32 additions & 0 deletions task_05/src/rmq.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <vector>
#include <cmath>

template<class Comparator>
std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

Choose a reason for hiding this comment

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

warning: the parameter 'vals' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

std::vector<std::vector<double>> sparse_table(std::vector<double> vals, Comparator comp) {
                                                                  ^

int K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(vals.size()))+1;
int const K = static_cast<int>(std::log2(vals.size()))+1;

std::vector<std::vector<double>> sparse_table;
sparse_table.push_back(vals);
int power = 1;

for(int i=1; i<K; ++i) {
std::vector<double> vali = {};
for(int j = 0; j < sparse_table[i-1].size()-power; ++j) {
(comp(sparse_table[i-1][j], sparse_table[i-1][j+power])) ?
vali.push_back(sparse_table[i-1][j]) : vali.push_back(sparse_table[i-1][j+power]);
}
sparse_table.push_back(vali);
power*=2;
}
return sparse_table;
}

template<class Comparator>
double rmq(int start, int finish, std::vector<std::vector<double>> sparse_table, Comparator comp) {
int K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

Choose a reason for hiding this comment

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

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

Suggested change
int K = static_cast<int>(std::log2(finish - start + 1));
int const K = static_cast<int>(std::log2(finish - start + 1));

int power = 1;
for(int p=0; p<K; ++p)
power *= 2;
double result = (comp(sparse_table[K][start], sparse_table[K][finish-power+1])) ?
sparse_table[K][start] : sparse_table[K][finish-power+1];
return result;
}
Loading
Loading