本文整理汇总了Python中networkx.classes.graph.Graph.add_nodes_from方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_nodes_from方法的具体用法?Python Graph.add_nodes_from怎么用?Python Graph.add_nodes_from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.graph.Graph
的用法示例。
在下文中一共展示了Graph.add_nodes_from方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self):
"""Return an undirected representation of the digraph.
Returns
-------
G : Graph
An undirected graph with the same name and nodes and
with edge (u,v,data) if either (u,v,data) or (v,u,data)
is in the digraph. If both edges exist in digraph and
their edge data is different, only one edge is created
with an arbitrary choice of which edge data to use.
You must check and correct for this manually if desired.
Notes
-----
If edges in both directions (u,v) and (v,u) exist in the
graph, attributes for the new undirected edge will be a combination of
the attributes of the directed edges. The edge data is updated
in the (arbitrary) order that the edges are encountered. For
more customized control of the edge attributes use add_edge().
This is similar to Graph(self) which returns a shallow copy.
self.to_undirected() returns a deepcopy of edge, node and
graph attributes.
"""
H=Graph()
H.name=self.name
H.add_nodes_from(self)
H.add_edges_from( (u,v,deepcopy(d))
for u,nbrs in self.adjacency_iter()
for v,d in nbrs.iteritems() )
H.graph=deepcopy(self.graph)
H.node=deepcopy(self.node)
return H
示例2: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self, reciprocal=False):
"""Return an undirected representation of the digraph.
Parameters
----------
reciprocal : bool (optional)
If True only keep edges that appear in both directions
in the original digraph.
Returns
-------
G : Graph
An undirected graph with the same name and nodes and
with edge (u,v,data) if either (u,v,data) or (v,u,data)
is in the digraph. If both edges exist in digraph and
their edge data is different, only one edge is created
with an arbitrary choice of which edge data to use.
You must check and correct for this manually if desired.
Notes
-----
If edges in both directions (u,v) and (v,u) exist in the
graph, attributes for the new undirected edge will be a combination of
the attributes of the directed edges. The edge data is updated
in the (arbitrary) order that the edges are encountered. For
more customized control of the edge attributes use add_edge().
This returns a "deepcopy" of the edge, node, and
graph attributes which attempts to completely copy
all of the data and references.
This is in contrast to the similar G=DiGraph(D) which returns a
shallow copy of the data.
See the Python copy module for more information on shallow
and deep copies, http://docs.python.org/library/copy.html.
Warning
-------
If you have subclassed DiGraph to use dict-like objects in the
data structure, those changes do not transfer to the Graph
created by this method.
"""
H=Graph()
H.name=self.name
H.add_nodes_from(self)
if reciprocal is True:
H.add_edges_from( (u,v,deepcopy(d))
for u,nbrs in self.adjacency_iter()
for v,d in nbrs.items()
if v in self.pred[u])
else:
H.add_edges_from( (u,v,deepcopy(d))
for u,nbrs in self.adjacency_iter()
for v,d in nbrs.items() )
H.graph=deepcopy(self.graph)
H.node=deepcopy(self.node)
return H
示例3: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self):
H = Graph()
H.name = self.name
H.add_nodes_from(self)
H.add_edges_from((u, v, deepcopy(d)) for u, nbrs in self.adjacency_iter() for v, d in nbrs.iteritems())
H.graph = deepcopy(self.graph)
H.node = deepcopy(self.node)
return H
示例4: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self):
"""Return an undirected representation of the digraph.
A new graph is returned with the same name and nodes and
with edge (u,v,data) if either (u,v,data) or (v,u,data)
is in the digraph. If both edges exist in digraph and
their edge data is different, only one edge is created
with an arbitrary choice of which edge data to use.
You must check and correct for this manually if desired.
"""
H=Graph()
H.name=self.name
H.add_nodes_from(self)
H.add_edges_from([(v,u,d) for (u,v,d) in self.edges_iter(data=True)])
return H
示例5: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self):
"""Return an undirected representation of the digraph.
Returns
-------
G : Graph
An undirected graph with the same name and nodes and
with edge (u,v,data) if either (u,v,data) or (v,u,data)
is in the digraph. If both edges exist in digraph and
their edge data is different, only one edge is created
with an arbitrary choice of which edge data to use.
You must check and correct for this manually if desired.
Notes
-----
If edges in both directions (u,v) and (v,u) exist in the
graph, attributes for the new undirected edge will be a combination of
the attributes of the directed edges. The edge data is updated
in the (arbitrary) order that the edges are encountered. For
more customized control of the edge attributes use add_edge().
This returns a "deepcopy" of the edge, node, and
graph attributes which attempts to completely copy
all of the data and references.
This is in contrast to the similar G=DiGraph(D) which returns a
shallow copy of the data.
See the Python copy module for more information on shallow
and deep copies, http://docs.python.org/library/copy.html.
"""
H=Graph()
H.name=self.name
H.add_nodes_from(self)
H.add_edges_from( (u,v,deepcopy(d))
for u,nbrs in self.adjacency_iter()
for v,d in nbrs.iteritems() )
H.graph=deepcopy(self.graph)
H.node=deepcopy(self.node)
return H
示例6: to_undirected
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_nodes_from [as 别名]
def to_undirected(self, reciprocal=False, as_view=False):
"""Returns an undirected representation of the digraph.
Parameters
----------
reciprocal : bool (optional)
If True only keep edges that appear in both directions
in the original digraph.
as_view : bool (optional, default=False)
If True return an undirected view of the original directed graph.
Returns
-------
G : Graph
An undirected graph with the same name and nodes and
with edge (u, v, data) if either (u, v, data) or (v, u, data)
is in the digraph. If both edges exist in digraph and
their edge data is different, only one edge is created
with an arbitrary choice of which edge data to use.
You must check and correct for this manually if desired.
See Also
--------
Graph, copy, add_edge, add_edges_from
Notes
-----
If edges in both directions (u, v) and (v, u) exist in the
graph, attributes for the new undirected edge will be a combination of
the attributes of the directed edges. The edge data is updated
in the (arbitrary) order that the edges are encountered. For
more customized control of the edge attributes use add_edge().
This returns a "deepcopy" of the edge, node, and
graph attributes which attempts to completely copy
all of the data and references.
This is in contrast to the similar G=DiGraph(D) which returns a
shallow copy of the data.
See the Python copy module for more information on shallow
and deep copies, https://docs.python.org/2/library/copy.html.
Warning: If you have subclassed DiGraph to use dict-like objects
in the data structure, those changes do not transfer to the
Graph created by this method.
Examples
--------
>>> G = nx.path_graph(2) # or MultiGraph, etc
>>> H = G.to_directed()
>>> list(H.edges)
[(0, 1), (1, 0)]
>>> G2 = H.to_undirected()
>>> list(G2.edges)
[(0, 1)]
"""
graph_class = self.to_undirected_class()
if as_view is True:
return nx.graphviews.generic_graph_view(self, Graph)
# deepcopy when not a view
G = Graph()
G.graph.update(deepcopy(self.graph))
G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
if reciprocal is True:
G.add_edges_from((u, v, deepcopy(d))
for u, nbrs in self._adj.items()
for v, d in nbrs.items()
if v in self._pred[u])
else:
G.add_edges_from((u, v, deepcopy(d))
for u, nbrs in self._adj.items()
for v, d in nbrs.items())
return G