-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include <graph.h> |