-
Notifications
You must be signed in to change notification settings - Fork 3k
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
digraph preorder and postorder traversals return wrong results #9040
Comments
I looked into the code and documentation. I think the documentation describes what the two functions do accurately, but without reading the documentation, what the two functions do are counter-intuitive for people who know graphs. The functions should be fixed to give what users need. For both functions, we run a DFS on the whole graph, and output all vertices in the order or reversed order of them being visited. It is not guaranteed to start from the root node of a tree. The start node can vary if vertices/edges are added in a different order, or with relabeling of the graph. If you are like me, who understand preorder for a tree as "visit the root, then recursively visit the left child, then recursively visit the right child", then the result from With the current If you want a topologically sorted list of vertices, maybe The current code doesn't try to find a root node in any of the 3 functions. Since the starting node is randomly chosen, we will not break backwards compatibility if It might be useful if I add two functions |
I see your point. Let me just add a few more comments, or better, opinions, strongly biased in the user perspective:
What really got me stuck was the fact that one cannot use Now, I understand not breaking backward compatibility in general. In this case, it sounds like nobody has noticed this issue before, or it is just me being too pedantic... I would break it, as the API does not tell the truth at least; and any user is either turning around it with tricks, or is in some "special" situation, I would guess. Being a core functionality in the OTP stdlib is different and somewhat more painful than being in "all the rest of the world outside the OTP". But let's stick anyhow with the current implementation and function naming for a while, which I imagine is your preferred option; still I feel like the stdlib is missing the "classical" Adding the In any case, I am definitely favourable to be more descriptive in the documentation. Thanks for your time. |
Hi! I have a fix that makes the traversal always start and restart at roots. I'd like the PR to gather feedback for a bit, to ensure the functionality is what's really needed. Then I'll make documentation changes. |
Describe the bug
digraph_utils:postorder/1
seems to return a wrong result, but it is consistent under a re-labelling of the vertices.digraph_utils:preorder/1
behaves differently on equivalent graphs, and results are wrong.Very simple examples are given below.
To Reproduce
Two examples follows. The idea is:
H
digraph_utils:preorder/1
anddigraph_utils:postorder/1
Then the test is repeated for an identical tree
G
, the only difference being a different naming for the vertices.We should expect the same sequence of vertices, just renamed for both the tree traversal algorithms.
Part 1
Let's define the tree
H
below, and evaluatedigraph_utils:postorder/1
anddigraph_utils:preorder/1
for it:Both calls here seem to return the in-order traversal path (or even a breadth-first search), but not what they are named after.
Part 2
Let's define the tree
G
below, and evaluatedigraph_utils:postorder/1
anddigraph_utils:preorder/1
for it.Please note that
G
is the same asH
, with vertices re-labelled.Expected behavior
A call to
digraph_utils:postorder/1
should:A call to
digraph_utils:preorder/1
should:H
andG
Affected versions
Erlang/OTP 27.1
Didn't check previous versions.
Additional context
The suggested "correct results" in the examples above may be different accordingly to the precedence of vertex visiting.
It may be useful to address the visiting precedence rule in the documentation, too.
The issue just points out the inconsistency of
digraph_utils:preorder/1
anddigraph_utils:postorder/1
.The text was updated successfully, but these errors were encountered: