Skip to content

Tikhonov_Timofey #33

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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file added task_01/src/main
Copy link
Contributor

Choose a reason for hiding this comment

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

лишние бинарники в ПРе

Binary file not shown.
40 changes: 39 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
int main() { return 0; }
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

void topologicalSortUtil(vector<vector<int>> &g, int v, vector<bool> &visited,
stack<int> &Stack) {
visited[v] = true;

vector<int>::iterator i;
for (i = g[v].begin(); i != g[v].end(); ++i)
if (!visited[*i]) topologicalSortUtil(g, *i, visited, Stack);

Stack.push(v);
}

void topologicalSort(vector<vector<int>> &g) {
stack<int> Stack;
int V = g.size();
vector<bool> visited(V, false);

for (int i = 0; i < V; i++)
if (visited[i] == false) topologicalSortUtil(g, i, visited, Stack);

while (Stack.empty() == false) {
cout << Stack.top() << " ";
Stack.pop();
}
}

int main() {
vector<vector<int>> g = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0}, {1, 0, 1, 0, 0, 0}};
int N = g.size();
topologicalSort(g);

return 0;
}
Binary file added task_02/src/main
Binary file not shown.
93 changes: 92 additions & 1 deletion task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,94 @@
#include <iostream>
#include <vector>

int main() { return 0; }
using namespace std;

void dfs(vector<vector<int>> &gr, int v, vector<bool> &b) {
b[v] = 1;
for (int i = 0; i < gr.size(); ++i) {
if (gr[v][i] != 0)
if (!b[i]) dfs(gr, i, b);
}
}

bool link_graph(vector<vector<int>> &gr, vector<bool> &s) {
int V = gr.size();
if (s[0] == 1)
dfs(gr, 1, s);
else
dfs(gr, 0, s);
for (int i = 0; i < V; i++) {
if (!s[i]) return 0;
}

return 1;
}

int main() {
vector<vector<int>> graph = {{0, 1, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 1, 0}};

vector<vector<int>> new_graph(0);

int V = graph.size();
vector<bool> s(V);
vector<bool> krit_v(V);
vector<vector<int>> krit_most(0);

for (int i = 0; i < V; ++i) {
krit_v[i] = 0;
}

for (int i = 0; i < V; i++) {
new_graph = graph;
for (int h = 0; h < V; ++h) s[h] = 0;

s[i] = 1;

for (int k = 0; k < V; k++) {
for (int l = 0; l < V; l++) {
if (k == i) {
new_graph[k][l] = 0;
}
if (l == i) {
new_graph[k][l] = 0;
}
}
}

if (!link_graph(new_graph, s)) krit_v[i] = 1;
}

for (int i = 0; i < V; i++) {
if (krit_v[i] == 1) {
new_graph = graph;
for (int h = 0; h < V; ++h) s[h] = 0;

for (int k = 0; k < V; k++) {
if (new_graph[i][k] != 0) {
new_graph[i][k] = 0;
new_graph[k][i] = 0;

if (!link_graph(new_graph, s)) {
krit_most.push_back({i, k});
}
}
}
}
}

cout << "Krit ver: ";
for (int i = 0; i < V; i++) {
if (krit_v[i] == 1) cout << i << " ";
}
cout << endl;

cout << "Krit most:";
for (int i = 0; i < krit_most.size(); i++) {
cout << "[" << krit_most[i][0] << ", " << krit_most[i][1] << "], ";
}
cout << endl;

return 0;
}
Binary file added task_03/src/main
Binary file not shown.
55 changes: 54 additions & 1 deletion task_03/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
#include <iostream>
#include <limits>
#include <vector>

int main() { return 0; }
#define INF numeric_limits<int>::max()
using namespace std;

int min_dist(const vector<int> &dist, const vector<bool> &visited) {
int min = INF, min_index;
for (int v = 0; v < dist.size(); ++v) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}

void Dijkstra_Algorithm(const vector<vector<int>> &graph, int source) {
int V = graph.size();
vector<int> dist(V, INF);
vector<bool> visited(V, false);
dist[source] = 0;

for (int count = 0; count < V - 1; ++count) {
int u = min_dist(dist, visited);
visited[u] = true;

for (int v = 0; v < V; ++v) {
if (!visited[v] && graph[u][v] != 0 && dist[u] != INF &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}

cout << "Shortest distance from a vertex " << source << ":\n";
for (int i = 0; i < V; ++i) {
cout << "Vertex " << i << ": "
<< (dist[i] == INF ? "INF" : to_string(dist[i])) << endl;
}
}

int main() {
vector<vector<int>> graph = {{0, 7, 9, 0, 0, 14}, {7, 0, 10, 15, 0, 0},
{9, 10, 0, 11, 0, 2}, {0, 15, 11, 0, 6, 0},
{0, 0, 0, 6, 0, 9}, {14, 0, 2, 0, 9, 0}};

int V = graph.size();

for (int source = 0; source < V; ++source) {
Dijkstra_Algorithm(graph, source);
}

return 0;
}
Binary file added task_04/src/main
Binary file not shown.
109 changes: 108 additions & 1 deletion task_04/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,110 @@

#include <iostream>
#include <limits>
#include <vector>
using namespace std;

#define INF numeric_limits<int>::max()

int min_dist(const vector<int> &dist, const vector<bool> &visited) {
int min = INF, min_index;
for (int v = 0; v < dist.size(); ++v) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}

void Dijkstra_Algorithm_wAlter(const vector<vector<int>> &graph,
const vector<vector<int>> &altered_graph,
int source) {
int V = graph.size();
vector<int> dist(V, INF);
vector<bool> visited(V, false);
dist[source] = 0;

for (int count = 0; count < V - 1; ++count) {
int u = min_dist(dist, visited);
visited[u] = true;

for (int v = 0; v < V; ++v) {
if (!visited[v] && graph[u][v] != 0 && dist[u] != INF &&
dist[u] + altered_graph[u][v] < dist[v]) {
dist[v] = dist[u] + altered_graph[u][v];
}
}
}

cout << "Shortest Distance from a vertex " << source << ":\n";
for (int i = 0; i < V; ++i) {
cout << "Vertex " << i << ": "
<< (dist[i] == INF ? "INF" : to_string(dist[i])) << endl;
}
}

vector<int> BelmanFord_Algorithm(const vector<vector<int>> &edges, int V) {
vector<int> dist(V + 1, INF);
dist[V] = 0;

vector<vector<int>> edges_with_extra(edges);
for (int i = 0; i < V; ++i) {
edges_with_extra.push_back({V, i, 0});
}

for (int i = 0; i < V; ++i) {
for (const auto &edge : edges_with_extra) {
if (dist[edge[0]] != INF && dist[edge[0]] + edge[2] < dist[edge[1]]) {
dist[edge[1]] = dist[edge[0]] + edge[2];
}
}
}
return vector<int>(dist.begin(), dist.begin() + V);
}

void Johnson_Algorithm(const vector<vector<int>> &graph) {
int V = graph.size();
vector<vector<int>> edges;

for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if (graph[i][j] != 0) {
edges.push_back({i, j, graph[i][j]});
}
}
}

vector<int> altered_weights = BelmanFord_Algorithm(edges, V);
vector<vector<int>> altered_graph(V, vector<int>(V, 0));

for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if (graph[i][j] != 0) {
altered_graph[i][j] =
graph[i][j] + altered_weights[i] - altered_weights[j];
}
}
}

cout << "Alternative Graph:\n";
for (const auto &row : altered_graph) {
for (int weight : row) {
cout << weight << ' ';
}
cout << endl;
}

for (int source = 0; source < V; ++source) {
Dijkstra_Algorithm_wAlter(graph, altered_graph, source);
}
}

int main() {
vector<vector<int>> graph = {{0, 7, 9, 0, 0, 14}, {7, 0, 10, 15, 0, 0},
{9, 10, 0, 11, 0, 2}, {0, 15, 11, 0, 6, 0},
{0, 0, 0, 6, 0, 9}, {14, 0, 2, 0, 9, 0}};

int main() { return 0; }
Johnson_Algorithm(graph);
return 0;
}
Binary file added task_05/src/main
Binary file not shown.
39 changes: 38 additions & 1 deletion task_05/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() { return 0; }
int main() {
int n;
n = 17; // cin >> n;
vector<int> a(n);
// for (int i = 0; i < n; i++) cin >> a[i];
a = {1, 2, 3, 4, 5, 6, 0, 8, 10, 11, 2, 3, 4, 5, 7, 8, 6};
int mx = (int)log2(n) + 1;
vector<vector<int>> st(mx, vector<int>(n, 0)); // sparse_table
st[0] = a;
for (int lvl = 1; lvl < mx; lvl++) {
int prevlvl = lvl - 1;
for (int i = 0; i <= n - (1 << lvl); i++) {
st[lvl][i] = min(st[prevlvl][i], st[prevlvl][i + (1 << prevlvl)]);
}
}
int q;
q = 1; // cin >> q;
int l, r;
int n2;
vector<int> log2_(n + 1);
log2_[1] = 0;
for (int i = 2; i <= n; i++) {
log2_[i] = log2_[i >> 1] + 1;
}

for (; q; q--) {
// cin >> l >> r; // для 1-индексации нужно делать l-- r--
l = 8;
r = 16;
n2 = log2_[r - l + 1];
cout << min(st[n2][l], st[n2][r - (1 << n2) + 1]) << endl;
}

return 0;
}
Binary file added task_06/src/main
Binary file not shown.
Loading
Loading