-
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
Conversation
Corrected trailling slashes
- relative railway example - corrected png files
- Added test cases and docs
Codecov ReportPatch coverage is
📢 Thoughts on this report? Let us know!. |
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.
Hey @Hromz, I did an initial round of review that primarily concerns codestyle improvements. Let me know what you think!
Hey @joweich, thanks for your comments. I'll fix it. :) |
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.
Looks very good already! Just some minor comments apart from @joweich's points :)
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); |
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.
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.
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.
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.
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.
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.
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.
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.
Added one test
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.
@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. |
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.
- **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. |
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); |
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.
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
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.
Thanks for working on this, LGTM!
Also thanks to @joweich for the great feedback!
Added TS DFS-based.