Skip to content
Open

task #25

Show file tree
Hide file tree
Changes from 2 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
File renamed without changes.
95 changes: 95 additions & 0 deletions lib/src/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once

#include <iostream>
#include <vector>

// UUG - undirected unweighted graph
// UWG - undirected weighted graph
// DUG - directed unweighted graph
// DWG - directed weighted graph

class UUGraph {
std::vector<std::vector<int>> edgeList; // невзвешенный

std::vector<int> visited; // массив для обхода

void UUGAddEdge(int u, int v) {
if (0 < u && u <= edgeList.size() && 0 < v && v <= edgeList.size()) {
edgeList[u].push_back(v);
edgeList[v].push_back(u);
} else {
std::cerr << "Uncorrected values";
}
}
};

class UWGraph {
std::vector<std::vector<std::pair<int, double>>> edgeList; // взвешенный

void UWGAddEdge(int u, int v, double weight) {
if (0 < u && u <= edgeList.size() && 0 < v && v <= edgeList.size()) {
edgeList[u].push_back({v, weight});
edgeList[v].push_back({u, weight});
} else {
std::cerr << "Uncorrected values";
}
}
};

class DUGraph {
public:
std::vector<std::vector<int>> graph; // невзвешенный
std::vector<int> used; // массив для обхода

std::vector<int> topSort; // массив для топологической сортировки
std::vector<int> color; // массив для цветов вершин

void DUGAddEdge(int u, int v) {
if (0 < u && u <= graph.size() && 0 < v && v <= graph.size()) {
graph[u].push_back(v);
} else {
std::cerr << "Uncorrected values";
}
}

void DUGReadGraph(int &vertices, int &edges,
std::vector<std::pair<int, int>> &vecEdges) {
graph.resize(vertices + 1);
used.resize(vertices + 1, 0);
color.resize(vertices + 1, 0);
for (int i = 0; i < edges; i++) {
DUGAddEdge(vecEdges[i].first, vecEdges[i].second);
}
}

bool DUGDfsCycle(int v) {
used[v] = 1;
color[v] = 1;
for (int edge : graph[v]) {
int to = edge;
if (!used[to]) {
if (DUGDfsCycle(to)) {
return true;
}
} else if (color[to] == 1) {
return true;
}
}
color[v] = 2;
topSort.push_back(v);
return false;
}
};

class DWGraph {
Copy link
Contributor

Choose a reason for hiding this comment

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

нигде не используется

std::vector<std::vector<std::pair<int, double>>> edgeListW; // взвешенный
std::vector<int> visited;

void DWGAddEdge(int u, int v, double weight) {
if (0 < u && u <= edgeListW.size() && 0 < v && v <= edgeListW.size()) {
edgeListW[u].push_back({v, weight});
} else {
std::cerr << "Uncorrected values";
}
}
};
1 change: 0 additions & 1 deletion lib/src/util.cpp

This file was deleted.

34 changes: 31 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
#include <gtest/gtest.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
#include "top_sort.hpp"

TEST(SimpleGraph, Simple) {
int vertices = 3;
int edges = 2;
std::vector<std::pair<int, int>> edge = {{1, 2}, {2, 3}};

std::vector<int> result = TopologySort(vertices, edges, edge);
std::vector<int> expected = {1, 2, 3};

ASSERT_EQ(result, expected);
}

TEST(DisconnectedGraph, Simple) {
int vertices = 5;
int edges = 3;
std::vector<std::pair<int, int>> edge = {{1, 2}, {3, 4}, {3, 5}};

std::vector<int> result = TopologySort(vertices, edges, edge);
std::vector<int> expected = {3, 5, 4, 1, 2};

ASSERT_EQ(result, expected);
}

TEST(GraphWithCycle, Simple) {
int vertices = 3;
int edges = 3;
std::vector<std::pair<int, int>> edge = {{1, 3}, {3, 2}, {2, 1}};

EXPECT_THROW(TopologySort(vertices, edges, edge);, CycleDetectedException);
}
21 changes: 21 additions & 0 deletions task_01/src/top_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "top_sort.hpp"

#include <algorithm>

std::vector<int> TopologySort(int vertices, int edges,
std::vector<std::pair<int, int>> &vecEdges) {
DUGraph graph;
graph.DUGReadGraph(vertices, edges, vecEdges);
bool is_cycle = false;
for (int i = 1; i < vertices; i++) {
if (!graph.used[i]) {
is_cycle = graph.DUGDfsCycle(i);
}
if (is_cycle) {
throw CycleDetectedException(
"Topological sorting is not possible with cycle graph");
}
}
std::reverse(graph.topSort.begin(), graph.topSort.end());
return graph.topSort;
}
9 changes: 9 additions & 0 deletions task_01/src/top_sort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <graph.hpp>

class CycleDetectedException : public std::logic_error {
using std::logic_error::logic_error;
};

std::vector<int> TopologySort(int vertices, int edges,
std::vector<std::pair<int, int>> &vecEdges);