On Wed, 2025-04-09 at 08:28 -0700, Julian Claus wrote:
> I assumed that the iteration follows the order of the edges. Here is
> an example:
>
Thanks.
The important thing to understand here is that "order of the edges" is
not a concept in a graph (unless you make it one — by default, we
don't). A graph is an unordered pair of sets, E and V. Because of this
definition there is no intrinsic ordering of edges.
If the graph is represented as an edge list (each element of V has a
set out edges in E), then it is possible to get the behaviour that you
want, but the set of out edges held by each v must be an ordered set.
We specifically do not do this for two reasons; the first is that in
large graphs this would impact on performance since we would need to do
linear search for edges, and possible more importantly, in nearly all
graph algorithms order of edge iteration should not matter, but if it
is not randomised can cause subtle incorrect success. Randomisation
prevents a lucky success. If ordering is important, you will need to
encode that in the graph.
Please read
https://www.gonum.org/post/word_ladder/ for an explanation
of the design principles around the graph packages use.
> This returns the array I posted first.
Sometimes it does, sometimes it does not. If you run it a number of
times, you'll see that it does not print a consistently ordered output,
but all the orders are correct for the traversal.