On Tue, 21 Oct 2014 11:11:24 +0000
Vincent Delecroix <[email protected]> wrote:

> > G=Graph()
> > for i in range(0,10): G.add_vertex(randint(1,1000))
> > for x in G.vertex_iterator(): print x
> > G.vertices()
> >
> > gives them in about random order.
> 
> Nope. The order is not random. It is the one you get by doing list(set(X)).

That, however, can depend on the order of X. For instance given

class C:
    def __init__(self, v):
        self.v = v
    def __repr__(self):
        return "C(%r)" % (self.v,)
    def __hash__(self):
        return int(0)
zero = C(0)
one = C(1)

when I do

print list(set([zero,one]))
print list(set([one,zero]))

I obtain

[C(0), C(1)]
[C(1), C(0)]

..

>     sage: list(set(G.vertices())) == list(G.vertex_iterator())
>     True
> 
> You can have a look at how is coded vertex_iterator in the backend
> (i.e. G._backend.iterator_verts).

That can vary by backend, can it not?

> 
> > I started thinking this when doing http://trac.sagemath.org/ticket/17173 .
> > Nathann suggested using directly (sub)graphs instead of (sub)poset. It is
> > OK with .vertices() but not with .vertex_iterator(); try
> >
> > G=Graph()
> > for i in range(2,50): G.add_vertex(i)
> > for i in range(2,50):
> >      if not is_prime(i):
> >          G.delete_vertex(i)
> > for x in G.vertex_iterator(): print x
> >
> > This seems to be quite open door for nasty bugs. .vertex_iterator might
> > give vertices in order when graph is small or has not been modified many
> > times.
> 
> The code is not very clean, but nevertheless the order of
> .vertex_iterator does not depend on the way you built your graph. Do
> you have an example where it is not the case ?

Yes:

G = Graph()
G.add_vertex(zero)
G.add_vertex(one)
H = Graph()
H.add_vertex(one)
H.add_vertex(zero)
print G == H
print list(G.vertex_iterator()) == list(H.vertex_iterator())
print G.vertices() == H.vertices()

gives me

True
False
True

..


Cheers,

Erik Massop

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to