本文整理匯總了Python中networkx.to_directed方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.to_directed方法的具體用法?Python networkx.to_directed怎麽用?Python networkx.to_directed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.to_directed方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setUp
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def setUp(self):
self.G = nx.path_graph(9)
self.DG = nx.path_graph(9, create_using=nx.DiGraph())
self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
self.Gv = nx.to_undirected(self.DG)
self.DGv = nx.to_directed(self.G)
self.MGv = nx.to_undirected(self.MDG)
self.MDGv = nx.to_directed(self.MG)
self.Rv = self.DG.reverse()
self.MRv = self.MDG.reverse()
self.graphs = [self.G, self.DG, self.MG, self.MDG,
self.Gv, self.DGv, self.MGv, self.MDGv,
self.Rv, self.MRv]
for G in self.graphs:
G.edges, G.nodes, G.degree
示例2: setup
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def setup(self):
self.G = nx.path_graph(9)
self.dv = nx.to_directed(self.G)
self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
self.Mdv = nx.to_directed(self.MG)
示例3: test_already_directed
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def test_already_directed(self):
dd = nx.to_directed(self.dv)
Mdd = nx.to_directed(self.Mdv)
assert_edges_equal(dd.edges, self.dv.edges)
assert_edges_equal(Mdd.edges, self.Mdv.edges)
示例4: test_subgraph_todirected
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def test_subgraph_todirected(self):
SG = nx.induced_subgraph(self.G, [4, 5, 6])
SSG = SG.to_directed()
assert_equal(sorted(SSG), [4, 5, 6])
assert_equal(sorted(SSG.edges), [(4, 5), (5, 4), (5, 6), (6, 5)])
示例5: save_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def save_graph(G, output_path, delimiter=',', write_stats=True, write_weights=False, write_dir=True):
r"""
Saves a graph to a file as an edgelist of weighted edgelist. If the stats parameter is set to True the file
will include several lines containing the same basic graph statistics as provided by the get_stats function.
For undirected graphs, the method stores both directions of every edge.
Parameters
----------
G : graph
A NetworkX graph
output_path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode.
delimiter : string, optional
The string used to separate values. Default is ','.
write_stats : bool, optional
Sets if graph statistics should be added to the edgelist or not. Default is True.
write_weights : bool, optional
If True data will be stored as weighted edgelist (e.g. triplets src, dst, weight) otherwise as normal edgelist.
If the graph edges have no weight attribute and this parameter is set to True,
a weight of 1 will be assigned to each edge. Default is False.
write_dir : bool, optional
This option is only relevant for undirected graphs. If False, the graph will be stored with a single
direction of the edges. If True, both directions of edges will be stored. Default is True.
"""
# Write the graph stats in the file if required
if write_stats:
get_stats(G, output_path)
# Open the file where data should be stored
f = open(output_path, 'a+b')
# Write the graph to a file and use both edge directions if graph is undirected
if G.is_directed():
# Store edgelist
if write_weights:
J = nx.DiGraph()
J.add_weighted_edges_from(G.edges.data('weight', 1))
nx.write_weighted_edgelist(J, f, delimiter=delimiter)
else:
nx.write_edgelist(G, f, delimiter=delimiter, data=False)
else:
if write_dir:
H = nx.to_directed(G)
J = nx.DiGraph()
else:
H = G
J = nx.DiGraph()
# Store edgelist
if write_weights:
J.add_weighted_edges_from(H.edges.data('weight', 1))
nx.write_weighted_edgelist(J, f, delimiter=delimiter)
else:
nx.write_edgelist(H, f, delimiter=delimiter, data=False)
示例6: caveman_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def caveman_graph(l, k):
"""Returns a caveman graph of ``l`` cliques of size ``k``.
Parameters
----------
l : int
Number of cliques
k : int
Size of cliques
Returns
-------
G : NetworkX Graph
caveman graph
Notes
-----
This returns an undirected graph, it can be converted to a directed
graph using :func:`nx.to_directed`, or a multigraph using
``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
described in [1]_ and it is unclear which of the directed
generalizations is most useful.
Examples
--------
>>> G = nx.caveman_graph(3, 3)
See also
--------
connected_caveman_graph
References
----------
.. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
Amer. J. Soc. 105, 493-527, 1999.
"""
# l disjoint cliques of size k
G = nx.empty_graph(l*k)
G.name = "caveman_graph(%s,%s)" % (l*k, k)
if k > 1:
for start in range(0, l*k, k):
edges = itertools.combinations(range(start, start+k), 2)
G.add_edges_from(edges)
return G
示例7: connected_caveman_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def connected_caveman_graph(l, k):
"""Returns a connected caveman graph of ``l`` cliques of size ``k``.
The connected caveman graph is formed by creating ``n`` cliques of size
``k``, then a single edge in each clique is rewired to a node in an
adjacent clique.
Parameters
----------
l : int
number of cliques
k : int
size of cliques
Returns
-------
G : NetworkX Graph
connected caveman graph
Notes
-----
This returns an undirected graph, it can be converted to a directed
graph using :func:`nx.to_directed`, or a multigraph using
``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
described in [1]_ and it is unclear which of the directed
generalizations is most useful.
Examples
--------
>>> G = nx.connected_caveman_graph(3, 3)
References
----------
.. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
Amer. J. Soc. 105, 493-527, 1999.
"""
G = nx.caveman_graph(l, k)
G.name = "connected_caveman_graph(%s,%s)" % (l, k)
for start in range(0, l*k, k):
G.remove_edge(start, start+1)
G.add_edge(start, (start-1) % (l*k))
return G
示例8: caveman_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import to_directed [as 別名]
def caveman_graph(l, k):
"""Returns a caveman graph of `l` cliques of size `k`.
Parameters
----------
l : int
Number of cliques
k : int
Size of cliques
Returns
-------
G : NetworkX Graph
caveman graph
Notes
-----
This returns an undirected graph, it can be converted to a directed
graph using :func:`nx.to_directed`, or a multigraph using
``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
described in [1]_ and it is unclear which of the directed
generalizations is most useful.
Examples
--------
>>> G = nx.caveman_graph(3, 3)
See also
--------
connected_caveman_graph
References
----------
.. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
Amer. J. Soc. 105, 493-527, 1999.
"""
# l disjoint cliques of size k
G = nx.empty_graph(l * k)
if k > 1:
for start in range(0, l * k, k):
edges = itertools.combinations(range(start, start + k), 2)
G.add_edges_from(edges)
return G