[graph] [python]

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[graph] [python]

David E. Konerding
Hi folks,

The bgl-python documentation says that the _graph.Graph.add_vertex()
function returns a Vertex object.

 From my testing, the Vertex object that gets returned doesn't actually
seem to be a member of the graph.
Is this intentional?  I was hoping that the returned Vertex object was
valid, since I need access to it immediately.

Here's a test program.  I woudl expect the values for v1 and v2 to be in
the graph, but they are not:

===================
import boost.graph as bgl

graph = bgl.Graph()
v1 = graph.add_vertex()
print "new v1=", v1
v2 = graph.add_vertex()
print "new v2=", v2
e = graph.add_edge(v1, v2)
print "new edge=", e
for vertex in graph.vertices:
    print "vertex: ", vertex
for edge in graph.edges:
    print "edge: ", edge

print "edge source:", graph.source(e)
=========================

Here's what I see:
new v1= <boost.graph._graph.Vertex object at 0x40168a04>
new v2= <boost.graph._graph.Vertex object at 0x40168aac>
new edge= <boost.graph._graph.Edge object at 0x40164dec>
vertex:  <boost.graph._graph.Vertex object at 0x40168ae4>
vertex:  <boost.graph._graph.Vertex object at 0x40168b54>
edge:  <boost.graph._graph.Edge object at 0x40164d6c>
edge source: <boost.graph._graph.Vertex object at 0x40168ae4>

As you can see, v1 and v2 return and have different addresses than the
Vertex objects that are listed by the vertices
iterator.  My guess here is that the Vertex objects do point to the same
underlying vertex in the C++ implementation
of the graph.



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [graph] [python]

Doug Gregor-2

On Feb 27, 2006, at 2:33 PM, David E. Konerding wrote:

> Here's what I see:
> new v1= <boost.graph._graph.Vertex object at 0x40168a04>
> new v2= <boost.graph._graph.Vertex object at 0x40168aac>
> new edge= <boost.graph._graph.Edge object at 0x40164dec>
> vertex:  <boost.graph._graph.Vertex object at 0x40168ae4>
> vertex:  <boost.graph._graph.Vertex object at 0x40168b54>
> edge:  <boost.graph._graph.Edge object at 0x40164d6c>
> edge source: <boost.graph._graph.Vertex object at 0x40168ae4>
>
> As you can see, v1 and v2 return and have different addresses than the
> Vertex objects that are listed by the vertices
> iterator.  My guess here is that the Vertex objects do point to the
> same
> underlying vertex in the C++ implementation
> of the graph.

Your guess is correct. We don't cache the Python objects corresponding
to vertex and edge descriptors.

        Doug

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [graph] [python]

David E. Konerding
Doug Gregor wrote:

>
>> As you can see, v1 and v2 return and have different addresses than the
>> Vertex objects that are listed by the vertices
>> iterator.  My guess here is that the Vertex objects do point to the
>> same
>> underlying vertex in the C++ implementation
>> of the graph.
>>    
>
> Your guess is correct. We don't cache the Python objects corresponding
> to vertex and edge descriptors.
>  

Ah.  OK.  Well, I guess that leads me to my next question.  I am writing
a model-view graph editor using bgl-python (which the model
implemented as a boost graph).  Originally my idea was to keep the
mapping between on-screen widgets and the graph vertices they corresponded
to in a dictionary:

self._widgetmap[vertex] = widget

However, since Vertex objects aren't cached, placing the vertex object I
get back from add_vertex obviously isn't going to work.

I got to thinking, and it seems to me like the 'right way' to do this
would be to invert the problem (somewhat violating the model/view
abstraction):
add a vertex property map to the graph with mappings from the graph
vertex object to the widget.  I can't see another way to do it
if Vertex objects aren't cached.


Dave
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [graph] [python]

David Abrahams
In reply to this post by Doug Gregor-2
Doug Gregor <[hidden email]> writes:

> On Feb 27, 2006, at 2:33 PM, David E. Konerding wrote:
>> Here's what I see:
>> new v1= <boost.graph._graph.Vertex object at 0x40168a04>
>> new v2= <boost.graph._graph.Vertex object at 0x40168aac>
>> new edge= <boost.graph._graph.Edge object at 0x40164dec>
>> vertex:  <boost.graph._graph.Vertex object at 0x40168ae4>
>> vertex:  <boost.graph._graph.Vertex object at 0x40168b54>
>> edge:  <boost.graph._graph.Edge object at 0x40164d6c>
>> edge source: <boost.graph._graph.Vertex object at 0x40168ae4>
>>
>> As you can see, v1 and v2 return and have different addresses than the
>> Vertex objects that are listed by the vertices
>> iterator.  My guess here is that the Vertex objects do point to the
>> same
>> underlying vertex in the C++ implementation
>> of the graph.
>
> Your guess is correct. We don't cache the Python objects corresponding
> to vertex and edge descriptors.

You probably don't want to pay this cost, but you might be able to
make the behavior more predictable if you used shared_ptrs inside the
C++ graph representation, since they will automatically retain the
identity of the Python object wrappers.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [graph] [python]

Doug Gregor
In reply to this post by David E. Konerding

On Feb 27, 2006, at 5:09 PM, David E. Konerding wrote:
> I got to thinking, and it seems to me like the 'right way' to do this
> would be to invert the problem (somewhat violating the model/view
> abstraction):
> add a vertex property map to the graph with mappings from the graph
> vertex object to the widget.  I can't see another way to do it
> if Vertex objects aren't cached.

Yes, I think this is your best bet. Future revisions of the BGL-
Python bindings will fix this problem, but it'll happen faster if  
someone goes ahead and implements it for me :)

        Doug
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users