Skip to content

edge_subgraph() (migrated from Trac #333) #330

Closed

Description

@networkx-trac

Original ticket https://networkx.lanl.gov/trac/ticket/333
Reported 2010-04-12 by @chebee7i, assigned to @dschult.

I'm interested in adding methods for returning edge-induced subgraphs. For my use, I definitely do not need copies at all...I need these to be views, because all I want to do is analyze the properties of these subgraphs. I'll wait to hear on #314 before writing the code. Does this sound like something of general interest?

Activity

  1. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2010-04-14

    I'd like to discuss the idea of views of graphs further.
    You can experiment in http://networkx.lanl.gov/svn/networkx/branches/networkx-view/
    with that.

    If you want to make copies (which I realize you don't) then what
    would the API look like? Just like subgraph but with edges specified?

  2. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @chebee7i, 2010-04-14

    That's correct. The API will follow subgraph (punting on views until later) except it will require a list of (u,v) values for graphs or (u,v,k) for multigraphs.

  3. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @dschult, 2010-04-14

    Hmmmm...... interesting to have subgraphs identified with edges.

    I guess we would want to check if each edge is in the original graph.
    If edgelist is the list of (u,v) values then could we use:
    '''
    H=nx.Graph()
    H.add_edges_from( (u,v) for (u,v) in edgelist if u in G and v in G[u])
    '''
    Is this the kind of thing you had in mind?

  4. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @chebee7i, 2010-04-15

    Exactly, but with data as well...and perhaps optimized for speed (once we figure out how to do that with node-induced subgraphs).

  5. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-02-06

    How about we add edge_subgraph(self,edges) to the base classes?

  6. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-09-16

    Here is some (almost) working code:
    '''
    def edge_subgraph(G,edges):
    # create new graph and copy subgraph into it
    H = G.class()
    for u,v in edges:
    H.add_edge(u,v,*G[u][v])
    for n in H:
    H.node[n]=G.node[n].copy()
    H.graph=G.g
    '''

    Shall we add this method? (I finally needed it in #588).

  7. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Attachment in Trac by @hagberg, 2011-09-28: edge_subgraph.patch

  8. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-09-28

    Added a patch that includes this functionality in the base classes.
    Opinions?

  9. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-04

  10. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-04

  11. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @dschult, 2011-10-09

    The patch uses '''H.node[n] = self.node[n].copy()'''

    and ''' H.add_edge(u,v,attr_dict=self.adj[u][v])'''

    Both create shallow copies of the attributes. Is that what we want? It says in the docstring that attributes won't be copied. The H.graph dict is not a shallow copy, so we should make it consistent with whatever we decide for H.node and H.adj.

  12. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-09

    Hmm.. I think I meant it to be consistent with Graph.subgraph. So whatever we ended up doing there we should do if we implement edge_subgraph.

  13. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Attachment in Trac by @dschult, 2011-10-09: edge_subgraph2.patch

  14. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-09

    That looks good. Is this something we want to include in the base classes?

  15. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @dschult, 2011-10-09

    Arg.... My edge_subgraph2 patch fails a bunch of tests because I had to build add_edge into edge_subgraph and add_edge is different for DiGraph, MultiGraph, etc. So to keep it "view-like" we have to put a version into each graph class.

    Maybe it would be better to have a simple _add_edge helper function in each class (and _add_node for that matter) that doesn't go through all the checking and creating data attributes etc. Or maybe an EdgeData class a la #583 would help too. Actually, just defining self.pred and self.succ for Graph would reduce some of the codebase because e.g. adding an edge in DiGraph and in Graph are really the same if you have pred and succ point to adj in Graph.

    But that is a bigger issue worthy of its own ticket and probably 2.0 status. I'll try to get a get a patch for edge_subgraph that works for all classes.

    Should it be included in base classes? Probably---but I don't know how much it gets used.

  16. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-09

    Would it help if add_edge() handled attr_dict differently? Instead of creating a new dictionary for edge data and updating it with attr_dict we could directly assign the data to attr_dict.
    But that must break many things... and probably is not intuitive (the dictionary you pass as attr_dict could get modified as a side effect).

  17. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-09

    Replying to [comment:13 dschult]:

    Arg.... My edge_subgraph2 patch fails a bunch of tests because I had to build add_edge into edge_subgraph and add_edge is different for DiGraph, MultiGraph, etc. So to keep it "view-like" we have to put a version into each graph class.

    Maybe it would be better to have a simple _add_edge helper function in each class (and _add_node for that matter) that doesn't go through all the checking and creating data attributes etc. Or maybe an EdgeData class a la #583 would help too. Actually, just defining self.pred and self.succ for Graph would reduce some of the codebase because e.g. adding an edge in DiGraph and in Graph are really the same if you have pred and succ point to adj in Graph.

    Adding a helper might be good. I'd have to see how that looks. If assigning pred and succ helps that makes sense too. I wonder if that breaks anything?

  18. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2011-10-10

  19. networkx-trac commented on Jul 20, 2012

    @networkx-trac
    Author

    Comment in Trac by @hagberg, 2012-04-08

  20. ghost assigned on Dec 29, 2012
  21. jfinkels commented on May 5, 2015

    @jfinkels
    Contributor

    There seems to be an edge_subgraph implementation in networkx.algorithms.trees.branchings, for some reason. (It is not used anywhere, and definitely doesn't belong in its current module.)

  22. dschult commented on Feb 7, 2016

    @dschult
    Member

    Closed by #1740. But it isn't a view. It is a shallow copy however.
    When we get a view version we can update it.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions