You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Behavior Neighborhood in directed graphs (migrated from Trac #195) #196
I was just wondering, is it a deliberate design choice to have the neighborhood method default to only the successor edges in a directed graph? Or is in graph the neighborhood in a directed graph equal to its successors? If the neighborhood function would encompass both predecessors and successors in a directed graph then it would be easier to write algorithms that apply to both direct and undirected graphs.
It was a design choice. I'm not sure if there is an agreed definition
of "neighbors" (if there is I'd like to follow it).
With networkx-0.99 the best way to iterate over the adjacent
edges is with "for nbr in G[n]". For DiGraphs that gives the
successors.
For some algorithms this makes sense for Graphs and DiGraphs.
For example if you want to find a path between two nodes - in
a DiGraph you want to consider only the successors.
Do you have a DiGraph example algorithm where you want
to follow both in and out edges?
I also had a question about this. The recent API changes show that in_edges_iter is now gone and out_edges_iter is just edges_iter. Can you expand on the motivation for this change?
I can understand it from the point of view of API consistency with the Graph class (especially since !DiGraph inherits from Graph). On the other hand, graphs and digraphs are quite different in some respects. Since !NetworkX is already distinguishing between preceding and succeeding nodes, shouldn't it also distinguish between incoming and outgoing edges? This would also complement the in_degree and out_degree functions.
The workaround is indeed simple:
'''
G.reverse(copy=False)
for n in G:
for e in G.edges_iter(n):
pass
'''
but (and perhaps I am being picky) this seems cleaner:
'''
for n in G:
for e in G.in_edges_iter(n):
pass
'''
And I think it has the added advantage that if you are making any graph calls within the inner for loop which make use of G.adj, then you might get surprising results due to the G.reverse() call. Comparing this with a separate algorithm, iterating through G.pred, the 'gotcha' is gone.
There are some hidden Markov model algorithms which work with incoming transitions. Notably, the forward-backward algorithms. For example, what is the probability of seeing 'some_word' and ending up in state 'B'? If symbols are output on the edges, then you need to consider all edges incoming to 'B'.
I think we should have in_edges() and in_edges_iter() in DiGraph.
And probably then alias out_edges() to edges() and out_edges() iter
to edges_iter() for consistency.
I think we should have in_edges() and in_edges_iter() in DiGraph.
And probably then alias out_edges() to edges() and out_edges() iter
to edges_iter() for consistency.
See my structuralholes code for a workaround.
Diederik
Activity
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-11-29
It was a design choice. I'm not sure if there is an agreed definition
of "neighbors" (if there is I'd like to follow it).
With networkx-0.99 the best way to iterate over the adjacent
edges is with "for nbr in G[n]". For DiGraphs that gives the
successors.
For some algorithms this makes sense for Graphs and DiGraphs.
For example if you want to find a path between two nodes - in
a DiGraph you want to consider only the successors.
Do you have a DiGraph example algorithm where you want
to follow both in and out edges?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
I also had a question about this. The recent API changes show that in_edges_iter is now gone and out_edges_iter is just edges_iter. Can you expand on the motivation for this change?
I can understand it from the point of view of API consistency with the Graph class (especially since !DiGraph inherits from Graph). On the other hand, graphs and digraphs are quite different in some respects. Since !NetworkX is already distinguishing between preceding and succeeding nodes, shouldn't it also distinguish between incoming and outgoing edges? This would also complement the in_degree and out_degree functions.
The workaround is indeed simple:
'''
G.reverse(copy=False)
for n in G:
for e in G.edges_iter(n):
pass
'''
but (and perhaps I am being picky) this seems cleaner:
'''
for n in G:
for e in G.in_edges_iter(n):
pass
'''
And I think it has the added advantage that if you are making any graph calls within the inner for loop which make use of G.adj, then you might get surprising results due to the G.reverse() call. Comparing this with a separate algorithm, iterating through G.pred, the 'gotcha' is gone.
There are some hidden Markov model algorithms which work with incoming transitions. Notably, the forward-backward algorithms. For example, what is the probability of seeing 'some_word' and ending up in state 'B'? If symbols are output on the edges, then you need to consider all edges incoming to 'B'.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
I think we should have in_edges() and in_edges_iter() in DiGraph.
And probably then alias out_edges() to edges() and out_edges() iter
to edges_iter() for consistency.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by trac user diederik, 2008-12-10
Replying to [comment:3 aric]:
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-15
This ticket is now reflected in #205.