Return the subgraph induced on nodes in nbunch. nbunch: can be a singleton node, a string (which is treated as a singleton node), or any iterable container of of nodes. (It can be an iterable or an iterator, e.g. a list, set, graph, file, numeric array, etc.) Setting inplace=True will return induced subgraph in original graph by deleting nodes not in nbunch. It overrides any setting of create_using. WARNING: specifying inplace=True makes it easy to destroy the graph. Unless otherwise specified, return a new graph of the same type as self. Use (optional) create_using=R to return the resulting subgraph in R. R can be an existing graphlike object (to be emptied) or R can be a call to a graph object, e.g. create_using=DiGraph(). See documentation for empty_graph() Note: use subgraph(G) rather than G.subgraph() to access the more general subgraph() function from the operators module. Reimplemented from networkx::base::DiGraph. Definition at line 1474 of file xbase.py. 01474 : """Return the subgraph induced on nodes in nbunch. nbunch: can be a singleton node, a string (which is treated as a singleton node), or any iterable container of of nodes. (It can be an iterable or an iterator, e.g. a list, set, graph, file, numeric array, etc.) Setting inplace=True will return induced subgraph in original graph by deleting nodes not in nbunch. It overrides any setting of create_using. WARNING: specifying inplace=True makes it easy to destroy the graph. Unless otherwise specified, return a new graph of the same type as self. Use (optional) create_using=R to return the resulting subgraph in R. R can be an existing graphlike object (to be emptied) or R can be a call to a graph object, e.g. create_using=DiGraph(). See documentation for empty_graph() Note: use subgraph(G) rather than G.subgraph() to access the more general subgraph() function from the operators module. """ bunch=self.prepare_nbunch(nbunch) # WARNING: setting inplace=True destroys the graph. if inplace: # demolish all nodes (and attached edges) not in nbunch # override any setting of create_using bunch=dict.fromkeys(bunch) # make a dict self.delete_nodes_from([n for n in self if n not in bunch]) self.name="Subgraph of (%s)"%(self.name) return self # create new graph if create_using is None: # return a Graph object of the same type as current graph # subgraph inherits multiedges and selfloops settings H=self.__class__(multiedges=self.multiedges, selfloops=self.selfloops) else: # Recreate subgraph with create_using. # Currently create_using must be an XGraph type object # or a multiedge list will be copied as a single edge H=create_using H.clear() H.name="Subgraph of (%s)"%(self.name) H.add_nodes_from(bunch) # add edges H_succ=H.succ # store in local variables H_pred=H.pred self_succ=self.succ self_pred=self.pred if self.multiedges: for n in H: # create dicts with copies of edge data list from self H_succ[n]=dict([(u,d[:]) for u,d in self_succ[n].iteritems() if u in H_succ]) H_pred[n]=dict([(u,d[:]) for u,d in self_pred[n].iteritems() if u in H_pred]) else: # no multiedges for n in H: # create dicts with edge data from self H_succ[n]=dict([(u,d) for u,d in self_succ[n].iteritems() if u in H_succ]) H_pred[n]=dict([(u,d) for u,d in self_pred[n].iteritems() if u in H_pred]) return H def copy(self):
