Skip to content

Commit

Permalink
task_01 done
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFueRr committed Sep 7, 2024
1 parent d2e9154 commit e68f2df
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
28 changes: 28 additions & 0 deletions lib/src/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <list>
#include <stdexcept>

class Graph {
private:
int vertices_number;
std::list<int>* adjacency_list;

public:
Graph(int number) {
if (number <= 0) throw std::logic_error("number must be positive!");
vertices_number = number;
adjacency_list = new std::list<int>[vertices_number + 1];
}
void AddEdge(int first_verticle, int second_verticle) {
if (first_verticle < 0 || first_verticle > vertices_number ||
second_verticle < 0 || second_verticle > vertices_number)
throw std::logic_error("such node does not exist");
if (first_verticle == second_verticle)
throw std::logic_error("graph must be acyclic!");
adjacency_list[first_verticle].push_back(second_verticle);
}
void TopologySortStep(int current_vertice, bool visited_vertices[],
std::list<int>& list);
std::list<int> TopologySort();
};
3 changes: 0 additions & 3 deletions task_01/src/main.cpp

This file was deleted.

46 changes: 44 additions & 2 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
#include <gtest/gtest.h>
#include <topology_sort.h>

TEST(Test, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(TopologySort, test1) {
Graph g(6);
EXPECT_THROW(g.AddEdge(5, 5), std::logic_error);
}

TEST(TopologySort, test2) {
EXPECT_THROW(Graph g(-5), std::logic_error);
EXPECT_THROW(Graph g(0), std::logic_error);
}

TEST(TopologySort, test3) {
Graph g(2);
EXPECT_THROW(g.AddEdge(5, 6), std::logic_error);
}

TEST(TopologySort, test4) {
Graph g(6);
g.AddEdge(5, 2);
g.AddEdge(5, 0);
g.AddEdge(4, 0);
g.AddEdge(4, 1);
g.AddEdge(2, 3);
g.AddEdge(3, 1);
ASSERT_EQ(g.TopologySort(), std::list<int>({5, 4, 2, 3, 1, 0}));
}

TEST(TopologySort, test5) {
Graph g(15);
g.AddEdge(10, 3);
g.AddEdge(3, 10);
g.AddEdge(10, 14);
g.AddEdge(14, 2);
g.AddEdge(5, 3);
g.AddEdge(3, 5);
g.AddEdge(1, 2);
g.AddEdge(2, 14);
g.AddEdge(14, 1);
g.AddEdge(1, 4);
g.AddEdge(11, 15);
g.AddEdge(15, 5);

ASSERT_EQ(g.TopologySort(), std::list<int>({13, 12, 11, 15, 9, 8, 7, 6, 3, 5,
10, 1, 4, 2, 14, 0}));
}
20 changes: 20 additions & 0 deletions task_01/src/topology_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "topology_sort.h"

void Graph::TopologySortStep(int current_vertice, bool visited_vertices[],
std::list<int>& list) {
visited_vertices[current_vertice] = true;
for (std::list<int>::iterator i = adjacency_list[current_vertice].begin();
i != adjacency_list[current_vertice].end(); i++) {
if (!visited_vertices[*i]) TopologySortStep(*i, visited_vertices, list);
}
list.push_front(current_vertice);
}

std::list<int> Graph::TopologySort() {
std::list<int> list;
bool* visited_vertices = new bool[vertices_number];
for (int i = 0; i < vertices_number; i++) {
if (!visited_vertices[i]) TopologySortStep(i, visited_vertices, list);
}
return list;
}
1 change: 1 addition & 0 deletions task_01/src/topology_sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <graph.h>

0 comments on commit e68f2df

Please sign in to comment.