-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Topological Sorting #93
Merged
Merged
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
3fe9e21
- added DFS cycle detection for directed and undirected graph
Hromz a629352
- corrected code according to comments
Hromz 99f77cd
Apply suggestions from code review
bobluppes 956eac3
Merge branch 'bobluppes:main' into main
Hromz 3ec396d
Merge branch 'bobluppes:main' into main
Hromz 3ba348d
Merge branch 'bobluppes:main' into main
Hromz 432b6b4
- First example of potential use of the library
Hromz 97ccea7
- small fixes
Hromz 3e55e23
Update transport-example.md
Hromz 286e9ce
Merge remote-tracking branch 'upstream/main'
bobluppes 419b663
clang format fix
bobluppes 2408835
Merge remote-tracking branch 'upstream/main'
bobluppes 7e459bf
reflect breaking changes on main
bobluppes 6fc3af0
clang format fix
bobluppes 767a532
- corrected according to git comments
Hromz 8ebb5f4
small fixes
Hromz ffe4553
small fixes
Hromz bb28f8c
Merge remote-tracking branch 'upstream/main'
bobluppes ce5df25
fix issues after merge
bobluppes 1c82f04
fix syntax highlighting code blocks
bobluppes 1c644a0
clang format
bobluppes a0fde77
Merge branch 'bobluppes:main' into main
Hromz 2ae7d97
Merge branch 'bobluppes:main' into main
Hromz a3a3b31
Merge branch 'bobluppes:main' into main
Hromz c05cd27
- Added MST algorithm
Hromz b5db653
clang style correction
Hromz 057ee77
Apply suggestions from code review
bobluppes 9e92f3f
Merge remote-tracking branch 'upstream/main'
bobluppes 665a484
Merge branch 'bobluppes:main' into main
Hromz 5dce08b
Added topological sort algorithm DFS-based
Hromz 03200f7
Merge branch 'main' into main
Hromz f8cd4fb
small fixes
Hromz 21cde40
Merge branch 'main' of https://github.com/Hromz/graaf
Hromz 14f0b8d
clang format fix
Hromz 7efd134
added test
Hromz 0bcad82
more tests
Hromz f96849a
Minor fixes
Hromz 57d3c47
Clang format
Hromz c5ddba1
Update topological-sort.md
Hromz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,21 @@ | ||
--- | ||
sidebar_position: 1 | ||
--- | ||
|
||
# Topological sort algorithm | ||
Topological sort algorithm processing DAG(directed acyclic graph) using DFS traversal. | ||
Each vertex is visited only after all its dependencies are visited. | ||
The runtime of the algorithm is `O(|V|+|E|)` and the memory consumption is `O(|V|)`. | ||
|
||
[wikipedia](https://en.wikipedia.org/wiki/Topological_sorting) | ||
|
||
## Syntax | ||
|
||
```cpp | ||
template <typename V, typename E> | ||
[[nodiscard]] std::optional<std::vector<vertex_id_t>> topological_sort( | ||
const graph<V, E, graph_type::DIRECTED>& graph); | ||
``` | ||
|
||
- **graph** The directed graph to traverse. | ||
- **return** Vector of vertices sorted in topological order. If the graph contains cycles, it returns an empty vector. | ||
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,22 @@ | ||
#pragma once | ||
|
||
#include <graaflib/graph.h> | ||
|
||
#include <optional> | ||
|
||
namespace graaf::algorithm { | ||
/** | ||
* @brief Calculates order of vertices in topological order | ||
* using DFS traversal | ||
* | ||
* @tparam V The vertex type of the graph. | ||
* @tparam E The edge type of the graph. | ||
* @param graph The input graph. | ||
* @return Vector of vertices sorted in topological order | ||
*/ | ||
template <typename V, typename E> | ||
[[nodiscard]] std::optional<std::vector<vertex_id_t>> topological_sort( | ||
const graph<V, E, graph_type::DIRECTED>& graph); | ||
|
||
} // namespace graaf::algorithm | ||
#include "topological_sorting.tpp" |
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,58 @@ | ||
#pragma once | ||
|
||
#include <graaflib/algorithm/cycle_detection.h> | ||
#include <graaflib/algorithm/topological_sorting.h> | ||
|
||
#include <vector> | ||
#include <optional> | ||
#include <unordered_set> | ||
|
||
namespace graaf::algorithm { | ||
|
||
namespace detail { | ||
|
||
// DFS topological sort | ||
template <typename V, typename E> | ||
void do_dfs_topological_sort( | ||
const graph<V, E, graph_type::DIRECTED>& graph, | ||
vertex_id_t start_vertex, | ||
std::unordered_set<vertex_id_t>& processed_vertices, | ||
std::vector<vertex_id_t>& sorted_vertices) { | ||
|
||
processed_vertices.insert(start_vertex); | ||
for (const auto& next_vertex : graph.get_neighbors(start_vertex)) { | ||
if (!processed_vertices.contains(next_vertex)) { | ||
do_dfs_topological_sort(graph, next_vertex, processed_vertices, | ||
sorted_vertices); | ||
} | ||
} | ||
|
||
sorted_vertices.push_back(start_vertex); | ||
} | ||
|
||
}; // namespace detail | ||
|
||
template <typename V, typename E> | ||
std::optional<std::vector<vertex_id_t>> topological_sort( | ||
const graph<V, E, graph_type::DIRECTED>& graph) { | ||
|
||
// Graph should be acyclic | ||
if (dfs_cycle_detection(graph)) { | ||
return std::nullopt; | ||
} | ||
|
||
std::vector<vertex_id_t> sorted_vertices{}; | ||
sorted_vertices.reserve(graph.vertex_count()); | ||
std::unordered_set<vertex_id_t> processed_vertices{}; | ||
|
||
for (const auto& [vertex_id, _] : graph.get_vertices()) { | ||
if (!processed_vertices.contains(vertex_id)) { | ||
detail::do_dfs_topological_sort(graph, vertex_id, processed_vertices, sorted_vertices); | ||
} | ||
} | ||
|
||
std::reverse(sorted_vertices.begin(), sorted_vertices.end()); | ||
return sorted_vertices; | ||
} | ||
|
||
}; // namespace graaf::algorithm |
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,234 @@ | ||
#include <fmt/core.h> | ||
#include <graaflib/algorithm/topological_sorting.h> | ||
#include <graaflib/graph.h> | ||
#include <graaflib/types.h> | ||
#include <gtest/gtest.h> | ||
|
||
namespace graaf::algorithm { | ||
namespace { | ||
template <typename T> | ||
struct TypedTopologicalSort : public testing::Test { | ||
using graph_t = T; | ||
}; | ||
|
||
using graph_types = testing::Types<directed_graph<int, int>>; | ||
TYPED_TEST_SUITE(TypedTopologicalSort, graph_types); | ||
|
||
}; // namespace | ||
|
||
TYPED_TEST(TypedTopologicalSort, ShortGraph) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
|
||
graph.add_edge(vertex_1, vertex_2, 25); | ||
graph.add_edge(vertex_2, vertex_3, 35); | ||
graph.add_edge(vertex_3, vertex_4, 45); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
std::vector<vertex_id_t> expected_vertices{vertex_1, vertex_2, vertex_3, | ||
vertex_4}; | ||
|
||
// THEN | ||
ASSERT_EQ(expected_vertices, sorted_vertices); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, RhombusShapeGraph) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
|
||
graph.add_edge(vertex_1, vertex_2, 25); | ||
graph.add_edge(vertex_1, vertex_3, 35); | ||
graph.add_edge(vertex_3, vertex_4, 45); | ||
graph.add_edge(vertex_2, vertex_4, 55); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
std::vector<vertex_id_t> expected_vertices_1{vertex_1, vertex_2, vertex_3, | ||
vertex_4}; | ||
std::vector<vertex_id_t> expected_vertices_2{vertex_1, vertex_3, vertex_2, | ||
vertex_4}; | ||
|
||
// THEN | ||
ASSERT_TRUE(expected_vertices_1 == sorted_vertices || | ||
expected_vertices_2 == sorted_vertices); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, CycleGraph) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
|
||
graph.add_edge(vertex_1, vertex_2, 25); | ||
graph.add_edge(vertex_2, vertex_3, 35); | ||
graph.add_edge(vertex_3, vertex_4, 45); | ||
graph.add_edge(vertex_4, vertex_1, 55); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
const auto expected_vertices = std::nullopt; | ||
|
||
// THEN | ||
ASSERT_EQ(expected_vertices, sorted_vertices); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, GraphWithParallelEdge) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
|
||
graph.add_edge(vertex_1, vertex_2, 25); | ||
graph.add_edge(vertex_3, vertex_4, 35); | ||
graph.add_edge(vertex_4, vertex_1, 45); | ||
graph.add_edge(vertex_1, vertex_4, 55); | ||
|
||
// WHEN | ||
auto sorted_vertices = topological_sort(graph); | ||
const auto expected_vertices = std::nullopt; | ||
|
||
// THEN | ||
ASSERT_EQ(expected_vertices, sorted_vertices); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, SelfLoop) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
|
||
graph.add_edge(vertex_1, vertex_1, -1); | ||
graph.add_edge(vertex_2, vertex_2, -1); | ||
graph.add_edge(vertex_1, vertex_2, 15); | ||
graph.add_edge(vertex_3, vertex_4, 25); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
const auto expected_vertices = std::nullopt; | ||
|
||
// THEN | ||
ASSERT_EQ(expected_vertices, sorted_vertices); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, SimpleGraph) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
const auto vertex_5{graph.add_vertex(50)}; | ||
const auto vertex_6{graph.add_vertex(60)}; | ||
const auto vertex_7{graph.add_vertex(70)}; | ||
|
||
graph.add_edge(vertex_1, vertex_5, 1); | ||
graph.add_edge(vertex_5, vertex_3, 2); | ||
graph.add_edge(vertex_3, vertex_7, 3); | ||
graph.add_edge(vertex_1, vertex_4, 4); | ||
graph.add_edge(vertex_1, vertex_2, 5); | ||
graph.add_edge(vertex_4, vertex_2, 6); | ||
graph.add_edge(vertex_2, vertex_6, 7); | ||
graph.add_edge(vertex_6, vertex_3, 8); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
std::vector<vertex_id_t> expected_vertices_1{ | ||
vertex_1, vertex_4, vertex_2, vertex_6, vertex_5, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_2{ | ||
vertex_1, vertex_2, vertex_4, vertex_6, vertex_5, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_3{ | ||
vertex_1, vertex_2, vertex_4, vertex_5, vertex_6, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_4{ | ||
vertex_1, vertex_5, vertex_4, vertex_2, vertex_6, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_5{ | ||
vertex_1, vertex_5, vertex_2, vertex_4, vertex_6, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_6{ | ||
vertex_1, vertex_2, vertex_5, vertex_4, vertex_6, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_7{ | ||
vertex_1, vertex_4, vertex_2, vertex_5, vertex_6, vertex_3, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_8{ | ||
vertex_1, vertex_4, vertex_5, vertex_2, vertex_6, vertex_3, vertex_7}; | ||
|
||
// THEN | ||
ASSERT_TRUE((expected_vertices_1 == sorted_vertices) || | ||
(expected_vertices_2 == sorted_vertices) || | ||
(expected_vertices_3 == sorted_vertices) || | ||
(expected_vertices_4 == sorted_vertices) || | ||
(expected_vertices_5 == sorted_vertices) || | ||
(expected_vertices_6 == sorted_vertices) || | ||
(expected_vertices_7 == sorted_vertices) || | ||
(expected_vertices_8 == sorted_vertices)); | ||
} | ||
|
||
TYPED_TEST(TypedTopologicalSort, SixSortResults) { | ||
// GIVEN | ||
using graph_t = typename TestFixture::graph_t; | ||
graph_t graph{}; | ||
|
||
const auto vertex_1{graph.add_vertex(10)}; | ||
const auto vertex_2{graph.add_vertex(20)}; | ||
const auto vertex_3{graph.add_vertex(30)}; | ||
const auto vertex_4{graph.add_vertex(40)}; | ||
const auto vertex_5{graph.add_vertex(50)}; | ||
const auto vertex_6{graph.add_vertex(60)}; | ||
const auto vertex_7{graph.add_vertex(70)}; | ||
|
||
graph.add_edge(vertex_1, vertex_2, 1); | ||
graph.add_edge(vertex_2, vertex_3, 2); | ||
graph.add_edge(vertex_1, vertex_4, 3); | ||
graph.add_edge(vertex_4, vertex_5, 4); | ||
graph.add_edge(vertex_1, vertex_6, 5); | ||
graph.add_edge(vertex_6, vertex_7, 6); | ||
|
||
// WHEN; | ||
auto sorted_vertices = topological_sort(graph); | ||
std::vector<vertex_id_t> expected_vertices_1{ | ||
vertex_1, vertex_2, vertex_3, vertex_4, vertex_5, vertex_6, vertex_7}; | ||
std::vector<vertex_id_t> expected_vertices_2{ | ||
vertex_1, vertex_2, vertex_3, vertex_6, vertex_7, vertex_4, vertex_5}; | ||
std::vector<vertex_id_t> expected_vertices_3{ | ||
vertex_1, vertex_6, vertex_7, vertex_2, vertex_3, vertex_4, vertex_5}; | ||
std::vector<vertex_id_t> expected_vertices_4{ | ||
vertex_1, vertex_6, vertex_7, vertex_4, vertex_5, vertex_2, vertex_3}; | ||
std::vector<vertex_id_t> expected_vertices_5{ | ||
vertex_1, vertex_4, vertex_5, vertex_6, vertex_7, vertex_2, vertex_3}; | ||
std::vector<vertex_id_t> expected_vertices_6{ | ||
vertex_1, vertex_4, vertex_5, vertex_2, vertex_3, vertex_6, vertex_7}; | ||
|
||
// THEN | ||
ASSERT_TRUE((expected_vertices_1 == sorted_vertices) || | ||
(expected_vertices_2 == sorted_vertices) || | ||
(expected_vertices_3 == sorted_vertices) || | ||
(expected_vertices_4 == sorted_vertices) || | ||
(expected_vertices_4 == sorted_vertices) || | ||
(expected_vertices_6 == sorted_vertices)); | ||
} | ||
|
||
}; // namespace graaf::algorithm |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.