Skip to content

Commit 75bd9a3

Browse files
Add namespace topological_sort
1 parent 457c03c commit 75bd9a3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

graph/topological_sort.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
*/
2626
namespace graph {
2727

28+
/**
29+
* @namespace topological_sort
30+
* @brief Topological Sort Algorithm
31+
*/
32+
namespace topological_sort {
2833
/**
2934
* @class Graph
3035
* @brief Class that represents a directed graph and provides methods for
@@ -111,6 +116,7 @@ std::vector<int> topologicalSort(const Graph& g) {
111116
}
112117
return ans;
113118
}
119+
} // namespace topological_sort
114120
} // namespace graph
115121

116122
/**
@@ -121,14 +127,14 @@ static void test() {
121127
// Test 1
122128
std::cout << "Testing for graph 1\n";
123129
int n_1 = 6;
124-
graph::Graph graph1(n_1);
130+
graph::topological_sort::Graph graph1(n_1);
125131
graph1.addEdge(4, 0);
126132
graph1.addEdge(5, 0);
127133
graph1.addEdge(5, 2);
128134
graph1.addEdge(2, 3);
129135
graph1.addEdge(3, 1);
130136
graph1.addEdge(4, 1);
131-
std::vector<int> ans_1 = graph::topologicalSort(graph1);
137+
std::vector<int> ans_1 = graph::topological_sort::topologicalSort(graph1);
132138
std::vector<int> expected_1 = {5, 4, 2, 3, 1, 0};
133139
std::cout << "Topological Sorting Order: ";
134140
for (int i : ans_1) {
@@ -141,14 +147,14 @@ static void test() {
141147
// Test 2
142148
std::cout << "Testing for graph 2\n";
143149
int n_2 = 5;
144-
graph::Graph graph2(n_2);
150+
graph::topological_sort::Graph graph2(n_2);
145151
graph2.addEdge(0, 1);
146152
graph2.addEdge(0, 2);
147153
graph2.addEdge(1, 2);
148154
graph2.addEdge(2, 3);
149155
graph2.addEdge(1, 3);
150156
graph2.addEdge(2, 4);
151-
std::vector<int> ans_2 = graph::topologicalSort(graph2);
157+
std::vector<int> ans_2 = graph::topological_sort::topologicalSort(graph2);
152158
std::vector<int> expected_2 = {0, 1, 2, 4, 3};
153159
std::cout << "Topological Sorting Order: ";
154160
for (int i : ans_2) {
@@ -161,12 +167,12 @@ static void test() {
161167
// Test 3 - Graph with cycle
162168
std::cout << "Testing for graph 3\n";
163169
int n_3 = 3;
164-
graph::Graph graph3(n_3);
170+
graph::topological_sort::Graph graph3(n_3);
165171
graph3.addEdge(0, 1);
166172
graph3.addEdge(1, 2);
167173
graph3.addEdge(2, 0);
168174
try {
169-
graph::topologicalSort(graph3);
175+
graph::topological_sort::topologicalSort(graph3);
170176
} catch (std::invalid_argument& err) {
171177
assert(std::string(err.what()) == "cycle detected in graph");
172178
}
@@ -178,6 +184,6 @@ static void test() {
178184
* @returns 0 on exit
179185
*/
180186
int main() {
181-
test(); // run self test implementations
187+
test(); // run self test implementations
182188
return 0;
183189
}

0 commit comments

Comments
 (0)