[graph-tool] Some clarifications regarding directness

Tiago de Paula Peixoto tiago at skewed.de
Thu Feb 23 12:45:51 CET 2017


On 23.02.2017 03:35, Steve wrote:
> Should not set_directed() redefine the definition of out_neighbours() and
> in_neighbours()?

It does.

> When I created a simple Erdős–Rényi graph, then change it to a directed
> graph, out_neighbours() and in_neighbours() do not return the expected
> edges:
> 
> import graph_tool.all as gt, numpy as np, numpy.random as rand
> N = 12
> p = 1.5 * np.log(N)/N
> g = gt.random_graph(N, deg_sampler=lambda: rand.poisson((N-1)*p),
>     directed=False, model='erdos')
> vroot = g.vertex(0)
> 
> g.set_directed(False)
> print([g.edge(vroot,v) for v in vroot.out_neighbours()])
> print([g.edge(v,vroot) for v in vroot.in_neighbours()])
> g.set_directed(True)
> print([g.edge(vroot,v) for v in vroot.out_neighbours()])
> print([g.edge(v,vroot) for v in vroot.in_neighbours()])
> 
> Result:
> 
> [<Edge object with source '0' and target '3' at 0x1245b9050>, <Edge object
> with source '0' and target '10' at 0x1245bbf28>, <Edge object with source
> '0' and target '6' at 0x1245b90e8>]
> []
> [None, None, <Edge object with source '0' and target '6' at 0x1245bf050>]
> []
> 
> You can see that out_neighbours() returns *all* edges, even ones that are
> inward-pointing, and in_neighbours() fails to return any edges.

What is happening here is that the vertex descriptor is still referring to
the original graph. When you do g.set_directed(True), the  vroot vertex
descriptor still refers to the old graph. To get the result you were
expecting you have to do:

    vroot = g.vertex(0)

after you do g.set_directed().

This is done for efficiency reasons, but is somewhat counter-intuitive. I'll
add a clear note in the documentation.

Best,
Tiago

-- 
Tiago de Paula Peixoto <tiago at skewed.de>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://lists.skewed.de/pipermail/graph-tool/attachments/20170223/02d88649/attachment.asc>


More information about the graph-tool mailing list