-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopological_sort_demo.cpp
50 lines (37 loc) · 1.16 KB
/
topological_sort_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include "../src/graphs.h"
#include <vector>
using namespace std;
void linear_graph() {
edgeList edgeList;
edgeList.add_edge({0, 1});
edgeList.add_edge({1, 2});
edgeList.add_edge({2, 3});
edgeList.add_edge({3, 4});
vector<int> expected_result = {4, 3, 2, 1, 0};
vector<int> actual_result;
topological_sort(&edgeList, actual_result);
for (const int node: actual_result) cout << node << "\t";
cout << endl;
std::equal(expected_result.begin(), expected_result.end(), actual_result.begin());
}
void random_graph() {
graph *graph = new edgeList;
bool a = graph->add_edge({1, 0});
bool b = graph->add_edge({2, 0});
bool c = graph->add_edge({3, 1});
bool d = graph->add_edge({3, 2});
auto edges = graph->get_edges();
vector<int> expected_result = {0, 2, 1, 3};
vector<int> actual_result;
topological_sort(graph, actual_result);
for (const int node: actual_result) cout << node << "\t";
cout << endl;
std::equal(expected_result.begin(), expected_result.end(), actual_result.begin());
delete graph;
}
int main() {
linear_graph();
random_graph();
return 0;
}