Skip to content
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 39 commits into from
Sep 12, 2023
Merged

Topological Sorting #93

merged 39 commits into from
Sep 12, 2023

Conversation

Hromz
Copy link
Contributor

@Hromz Hromz commented Sep 5, 2023

Added TS DFS-based.

  • Algorithm
  • Tests
  • Documentation
  • Comments

@codecov
Copy link

codecov bot commented Sep 5, 2023

Codecov Report

Patch coverage is 100.00% of modified lines.

Files Changed Coverage
include/graaflib/algorithm/topological_sorting.tpp 100.00%
...st/graaflib/algorithm/topological_sorting_test.cpp 100.00%

📢 Thoughts on this report? Let us know!.

Copy link
Collaborator

@joweich joweich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Hromz, I did an initial round of review that primarily concerns codestyle improvements. Let me know what you think!

@Hromz
Copy link
Contributor Author

Hromz commented Sep 7, 2023

Hey @joweich, thanks for your comments. I'll fix it. :)

Copy link
Owner

@bobluppes bobluppes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good already! Just some minor comments apart from @joweich's points :)

Comment on lines 23 to 31
processed_vertices[start_vertex] = vertex_status::VISITED;
for (const auto& next_vertex : graph.get_neighbors(start_vertex)) {
if (processed_vertices[next_vertex] == vertex_status::UNVISITED) {
do_dfs_topological_sort(graph, next_vertex, processed_vertices,
sorted_vertices);
}
}

sorted_vertices.push_back(start_vertex);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add the start_vertex to the sorted_vertices before the for-loop, do you already have the vertices recorded in the correct order? Maybe it would be possible to get rid of the std::reverse then.

Copy link
Contributor Author

@Hromz Hromz Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that. In this case, vertices will be stored as usual in DFS traversal, ignoring all other dependencies.
Consider this directed graph
0
/ \
1 2
\ /
3
If we do preorder, the vertices will be 0–1–3–2, which is incorrect for TS.
But if we do post order 0-1-2-3, Or maybe I missed something, correct me if I'm getting something wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quickly checked @bobluppes's proposal and it indeed fails the tests. I would suggest going with the solution in this PR and think about ways getting rid of the std::reverse in a follow up.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad indeed. One thing we could investigate is to use a std::deque rather than a vector and call push_front. However, let's not optimize prematurely and let's tackle this in a follow up.

@Hromz Hromz requested review from bobluppes and joweich September 10, 2023 14:28
Copy link
Collaborator

@joweich joweich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hromz LGTM, all points in the reviews have been addressed properly 🚀
Only have one quick fix concerning the docs.

```

- **graph** The directed graph to traverse.
- **return** Vector of vertices sorted in topological order. If the graph contains cycles, it returns an empty vector.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **return** Vector of vertices sorted in topological order. If the graph contains cycles, it returns an empty vector.
- **return** Vector of vertices sorted in topological order. If the graph contains cycles, it returns std::nullopt.

Comment on lines 23 to 31
processed_vertices[start_vertex] = vertex_status::VISITED;
for (const auto& next_vertex : graph.get_neighbors(start_vertex)) {
if (processed_vertices[next_vertex] == vertex_status::UNVISITED) {
do_dfs_topological_sort(graph, next_vertex, processed_vertices,
sorted_vertices);
}
}

sorted_vertices.push_back(start_vertex);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quickly checked @bobluppes's proposal and it indeed fails the tests. I would suggest going with the solution in this PR and think about ways getting rid of the std::reverse in a follow up.

Corrected doc file
Copy link
Owner

@bobluppes bobluppes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, LGTM!
Also thanks to @joweich for the great feedback!

@bobluppes bobluppes added the enhancement New feature label Sep 12, 2023
@bobluppes bobluppes merged commit e083faa into bobluppes:main Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants