From 944013f0dcb168f046772d58a12e96be25f475ac Mon Sep 17 00:00:00 2001 From: Aman Mehara Date: Sat, 23 Mar 2024 21:43:18 +0530 Subject: [PATCH] [C++] Graph Algorithms: Topological Sort (Depth-First Search) --- C++/CMakeLists.txt | 1 + C++/README.md | 3 ++ C++/graph_algorithms/topological_sort.cpp | 57 +++++++++++++++++++++++ C++/graph_algorithms/topological_sort.h | 28 +++++++++++ 4 files changed, 89 insertions(+) create mode 100644 C++/graph_algorithms/topological_sort.cpp create mode 100644 C++/graph_algorithms/topological_sort.h diff --git a/C++/CMakeLists.txt b/C++/CMakeLists.txt index 36c4c21..e859a9e 100644 --- a/C++/CMakeLists.txt +++ b/C++/CMakeLists.txt @@ -18,6 +18,7 @@ set_target_properties(programming PROPERTIES LINKER_LANGUAGE CXX) enable_testing() add_subdirectory(data_structures) +add_subdirectory(graph_algorithms) add_subdirectory(mathematics) add_subdirectory(randomized_algorithms) add_subdirectory(search) diff --git a/C++/README.md b/C++/README.md index a52eb0a..53d3d45 100644 --- a/C++/README.md +++ b/C++/README.md @@ -5,6 +5,9 @@ * Queue * Segment Tree +# Graph Algorithms +* Topological Sort _(Depth-First Search)_ + ## Mathematics ### Linear Algebra * Compute Determinant _(Gaussian Elimination)_ diff --git a/C++/graph_algorithms/topological_sort.cpp b/C++/graph_algorithms/topological_sort.cpp new file mode 100644 index 0000000..acd0d3c --- /dev/null +++ b/C++/graph_algorithms/topological_sort.cpp @@ -0,0 +1,57 @@ +// Copyright 2024 Aman Mehara +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "topological_sort.h" + +#include +#include +#include +#include + +namespace mehara::graph { + +using namespace std; + +namespace { + +void dfs(int vertex, const vector>& adjacency_list, + vector& colors, deque& order) +{ + if (colors[vertex] == 2) { + return; + } + if (colors[vertex] == 1) { + throw logic_error("Graph has at least one cycle."); + } + colors[vertex] = 1; + for (const auto& neighbour : adjacency_list[vertex]) { + dfs(neighbour, adjacency_list, colors, order); + } + colors[vertex] = 2; + order.push_front(vertex); +} + +} // namespace + +deque dfs_topological_sort(const vector>& adjacency_list) +{ + vector colors(adjacency_list.size()); + deque order; + for (int i = 0; i < adjacency_list.size(); i++) { + dfs(i, adjacency_list, colors, order); + } + return order; +} + +} // namespace mehara::graph diff --git a/C++/graph_algorithms/topological_sort.h b/C++/graph_algorithms/topological_sort.h new file mode 100644 index 0000000..b9998d8 --- /dev/null +++ b/C++/graph_algorithms/topological_sort.h @@ -0,0 +1,28 @@ +// Copyright 2024 Aman Mehara +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef MEHARA_TOPOLOGICAL_SORT_H_ +#define MEHARA_TOPOLOGICAL_SORT_H_ + +#include +#include + +namespace mehara::graph { + +std::deque +dfs_topological_sort(const std::vector>& adjacency_list); + +} + +#endif // MEHARA_TOPOLOGICAL_SORT_H_