Skip to content

Behavior Neighborhood in directed graphs (migrated from Trac #195) #196

Closed

Description

@networkx-trac

Original ticket https://networkx.lanl.gov/trac/ticket/195
Reported 2008-11-28 by trac user diederik, assigned to @hagberg.

Hi,

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.

best,
Diederik

Activity

  1. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    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?

  2. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    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'.

  3. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    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.

  4. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by trac user diederik, 2008-12-10

    Replying to [comment:3 aric]:

    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

  5. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2008-12-15

    This ticket is now reflected in #205.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions