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
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?
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.
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?
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).
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.
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.
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).
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?
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.)
Activity
networkx-trac commentedon Jul 20, 2012
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?
networkx-trac commentedon Jul 20, 2012
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.
networkx-trac commentedon Jul 20, 2012
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?
networkx-trac commentedon Jul 20, 2012
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).
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-02-06
How about we add edge_subgraph(self,edges) to the base classes?
networkx-trac commentedon Jul 20, 2012
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).
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @hagberg, 2011-09-28: edge_subgraph.patch
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-09-28
Added a patch that includes this functionality in the base classes.
Opinions?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-10-04
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-10-04
networkx-trac commentedon Jul 20, 2012
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.
networkx-trac commentedon Jul 20, 2012
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.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @dschult, 2011-10-09: edge_subgraph2.patch
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-10-09
That looks good. Is this something we want to include in the base classes?
networkx-trac commentedon Jul 20, 2012
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.
networkx-trac commentedon Jul 20, 2012
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).
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-10-09
Replying to [comment:13 dschult]:
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?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-10-10
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2012-04-08
jfinkels commentedon May 5, 2015
There seems to be an
edge_subgraphimplementation innetworkx.algorithms.trees.branchings, for some reason. (It is not used anywhere, and definitely doesn't belong in its current module.)dschult commentedon Feb 7, 2016
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.