-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Graph.successors(): add support for ordering the nodes #2650
Comments
I agree, that seems like a really expensive workaround. :( A somewhat less expensive workaround is below, assuming that your tree is fixed (not being mutated). It costs you an extra HashMap and a copy of successors(), but for nodes with a small number of successors that shouldn't be a big deal. If your tree is being mutated, I don't have any great suggestions for you; you'd essentially have to listen to the mutations and update In general, at the moment, if you want successors() to be ordered, you need to do the sorting yourself. // build this index once at tree creation time; this establishes the ordering
// as returned by nodes()' Iterator
Map<N, Integer> nodeIndex = new HashMap<>();
int i = 0;
for (N node : graph.nodes()) {
nodeIndex.put(node, i++);
}
Comparator<N> nodeComparator = new Comparator<N>() {
int compare(N n1, N n2) {
return nodeIndex.get(n1) - nodeIndex.get(n2);
}
}
...
fun <N> Graph<N>.children(node: N): Set<N> {
return Collections.sort(new ArrayList<N>(graph.successors(node)), nodeComparator));
} |
Adding to my observations from above: if you want the successors of a node in a specified order, then your major options are: I don't expect Guava to provide an implementation that provides the in the near future; if you're reading this and you'd like us to reconsider, please leave a comment (and ideally explain why (2) is not practical in your case). |
Hi @jrtom |
@ejemba The reasons that we don't (and probably won't) provide support for sorting the Note that you don't need a separate scan of your nodes in order to sort them; you can create indices for them as you add them to the graph, which should be fast: // build the graph
MutableGraph<N> graph = GraphBuilder.directed().build();
// import the nodes
Map<N, Integer> node_index = new HashMap<>();
int index = 0;
for (N node : inputNodes) {
graph.addNode(node);
node_index.put(node, index++);
}
// add edges, etc. Of course, if your node type is Hope that helps. |
@jrtom Apologies for the thread-necromancy - but as a corollary to this, since the traverser classes call |
Hi, I'm a also a maintainer of this library and just added the possibility of returning successors in insertion order. This should be included in the next Guava release for Example of how to enable this:
This gives the following guarantees:
|
@nymanjens - Any idea when this might get released? Thanks! |
The master branch has this implemented for Looks like the latest release doesn't yet contain this, but it should be in the next one. I don't know when that will be though. |
This was released yesterday: https://github.com/google/guava/releases/tag/v29.0 |
Hi @nymanjens , thank you and I have been waiting for this. However, it does not work for me though I tried as suggested |
Your snippet should build a MutableGraph that guarantees a stable edge order. Can you explain what expectation you exactly had about the built graph? |
Yes I was building MutableGraph as well, I did a deeper dive. It looks like my I would test properly again and provide you my findings. |
@nymanjens, do you still have plans to add this feature to |
This option was foreseen for Do you have an example motivating use case? |
Our use case is that we rely on Currently, we're sorting each |
Fair enough, that sounds like a use case that would indeed benefit from a similar feature on Network |
Graph.successors()
returns nodes in an undetermined order which makes it difficult to useGraph
as a rooted tree where the ordering of the children is important (e.g. something similar to XML) with a schema that orders some elements).I have the following workaround but it seems expensive (although for small trees I suppose it doesn't really matter):
Can supported be added to specify the order of nodes returned by
successors()
?The text was updated successfully, but these errors were encountered: