本文整理汇总了Python中networkx.add_path方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.add_path方法的具体用法?Python networkx.add_path怎么用?Python networkx.add_path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.add_path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testload
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def testload(self):
def compare_graph_paths_names(g, paths, names):
expected = nx.DiGraph()
for p in paths:
nx.add_path(expected, p)
assert_equal(sorted(expected.nodes), sorted(g.nodes))
assert_equal(sorted(expected.edges()), sorted(g.edges()))
g_names = [g.get_edge_data(s, e)['Name'] for s, e in g.edges()]
assert_equal(names, sorted(g_names))
# simplified
G = nx.read_shp(self.shppath)
compare_graph_paths_names(G, self.simplified_paths,
self.simplified_names)
# unsimplified
G = nx.read_shp(self.shppath, simplify=False)
compare_graph_paths_names(G, self.paths, self.names)
# multiline unsimplified
G = nx.read_shp(self.multi_shppath, simplify=False)
compare_graph_paths_names(G, self.paths, self.multi_names)
示例2: test_default_attribute
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_default_attribute(self):
G = nx.Graph()
G.add_node(1, label='1', color='green')
nx.add_path(G, [0, 1, 2, 3])
G.add_edge(1, 2, foo=3)
G.graph['node_default'] = {'color': 'yellow'}
G.graph['edge_default'] = {'foo': 7}
fh = io.BytesIO()
nx.write_gexf(G, fh)
fh.seek(0)
H = nx.read_gexf(fh, node_type=int)
assert_equal(sorted(G.nodes()), sorted(H.nodes()))
assert_equal(
sorted(sorted(e) for e in G.edges()),
sorted(sorted(e) for e in H.edges()))
# Reading a gexf graph always sets mode attribute to either
# 'static' or 'dynamic'. Remove the mode attribute from the
# read graph for the sake of comparing remaining attributes.
del H.graph['mode']
assert_equal(G.graph, H.graph)
示例3: test_info_digraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_info_digraph(self):
G = nx.DiGraph(name='path_graph(5)')
nx.add_path(G, [0, 1, 2, 3, 4])
info = nx.info(G)
expected_graph_info = '\n'.join(['Name: path_graph(5)',
'Type: DiGraph',
'Number of nodes: 5',
'Number of edges: 4',
'Average in degree: 0.8000',
'Average out degree: 0.8000'])
assert_equal(info, expected_graph_info)
info = nx.info(G, n=1)
expected_node_info = '\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 2'])
assert_equal(info, expected_node_info)
assert_raises(nx.NetworkXError, nx.info, G, n=-1)
示例4: setup
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def setup(self):
# Create a quadruply-linked path graph on five nodes.
G = nx.MultiDiGraph()
nx.add_path(G, range(5))
nx.add_path(G, range(5))
nx.add_path(G, reversed(range(5)))
nx.add_path(G, reversed(range(5)))
# Add some node, edge, and graph attributes.
for i in range(5):
G.nodes[i]['name'] = 'node{}'.format(i)
G.adj[0][1][0]['name'] = 'edge010'
G.adj[0][1][1]['name'] = 'edge011'
G.adj[3][4][0]['name'] = 'edge340'
G.adj[3][4][1]['name'] = 'edge341'
G.graph['name'] = 'graph'
# Get the subgraph induced by one of the first edges and one of
# the last edges.
self.G = G
self.H = G.edge_subgraph([(0, 1, 0), (3, 4, 1)])
示例5: setup
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def setup(self):
# Create a doubly-linked path graph on five nodes.
G = nx.MultiGraph()
nx.add_path(G, range(5))
nx.add_path(G, range(5))
# Add some node, edge, and graph attributes.
for i in range(5):
G.nodes[i]['name'] = 'node{}'.format(i)
G.adj[0][1][0]['name'] = 'edge010'
G.adj[0][1][1]['name'] = 'edge011'
G.adj[3][4][0]['name'] = 'edge340'
G.adj[3][4][1]['name'] = 'edge341'
G.graph['name'] = 'graph'
# Get the subgraph induced by one of the first edges and one of
# the last edges.
self.G = G
self.H = G.edge_subgraph([(0, 1, 0), (3, 4, 1)])
示例6: test_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_cycle(self):
path = list(range(10))
G = nx.Graph()
nx.add_path(G, path, weight=5)
G.add_edge(path[-1], path[0], weight=1)
A = nx.laplacian_matrix(G).todense()
for normalized in (False, True):
for method in methods:
try:
order = nx.spectral_ordering(G, normalized=normalized,
method=method)
except nx.NetworkXError as e:
if e.args not in (('Cholesky solver unavailable.',),
('LU solver unavailable.',)):
raise
else:
if not normalized:
ok_(order in [[1, 2, 0, 3, 4, 5, 6, 9, 7, 8],
[8, 7, 9, 6, 5, 4, 3, 0, 2, 1]])
else:
ok_(order in [[1, 2, 3, 0, 4, 5, 9, 6, 7, 8],
[8, 7, 6, 9, 5, 4, 0, 3, 2, 1]])
示例7: test_generic_weighted_projected_graph_simple
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_generic_weighted_projected_graph_simple(self):
def shared(G, u, v):
return len(set(G[u]) & set(G[v]))
B = nx.path_graph(5)
G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4], weight_function=shared)
assert_nodes_equal(list(G), [0, 2, 4])
assert_edges_equal(list(list(G.edges(data=True))),
[(0, 2, {'weight': 1}), (2, 4, {'weight': 1})])
G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4])
assert_nodes_equal(list(G), [0, 2, 4])
assert_edges_equal(list(list(G.edges(data=True))),
[(0, 2, {'weight': 1}), (2, 4, {'weight': 1})])
B = nx.DiGraph()
nx.add_path(B, range(5))
G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4])
assert_nodes_equal(list(G), [0, 2, 4])
assert_edges_equal(list(G.edges(data=True)),
[(0, 2, {'weight': 1}), (2, 4, {'weight': 1})])
示例8: test_all_shortest_paths
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_all_shortest_paths(self):
G = nx.Graph()
nx.add_path(G, [0, 1, 2, 3])
nx.add_path(G, [0, 10, 20, 3])
assert_equal([[0, 1, 2, 3], [0, 10, 20, 3]],
sorted(nx.all_shortest_paths(G, 0, 3)))
# with weights
G = nx.Graph()
nx.add_path(G, [0, 1, 2, 3])
nx.add_path(G, [0, 10, 20, 3])
assert_equal([[0, 1, 2, 3], [0, 10, 20, 3]],
sorted(nx.all_shortest_paths(G, 0, 3, weight='weight')))
# weights and method specified
G = nx.Graph()
nx.add_path(G, [0, 1, 2, 3])
nx.add_path(G, [0, 10, 20, 3])
assert_equal([[0, 1, 2, 3], [0, 10, 20, 3]],
sorted(nx.all_shortest_paths(G, 0, 3, weight='weight',
method='dijkstra')))
G = nx.Graph()
nx.add_path(G, [0, 1, 2, 3])
nx.add_path(G, [0, 10, 20, 3])
assert_equal([[0, 1, 2, 3], [0, 10, 20, 3]],
sorted(nx.all_shortest_paths(G, 0, 3, weight='weight',
method='bellman-ford')))
示例9: test_specified_methods
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_specified_methods(self):
G = nx.Graph()
nx.add_cycle(G, range(7), weight=2)
ans = nx.average_shortest_path_length(G,
weight='weight',
method='dijkstra')
assert_almost_equal(ans, 4)
ans = nx.average_shortest_path_length(G,
weight='weight',
method='bellman-ford')
assert_almost_equal(ans, 4)
G = nx.Graph()
nx.add_path(G, range(5), weight=2)
ans = nx.average_shortest_path_length(G,
weight='weight',
method='dijkstra')
assert_almost_equal(ans, 4)
ans = nx.average_shortest_path_length(G,
weight='weight',
method='bellman-ford')
assert_almost_equal(ans, 4)
示例10: test_bidirectional_dijkstra_ignore
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_bidirectional_dijkstra_ignore():
G = nx.Graph()
nx.add_path(G, [1, 2, 10])
nx.add_path(G, [1, 3, 10])
assert_raises(
nx.NetworkXNoPath,
_bidirectional_dijkstra,
G,
1, 2,
ignore_nodes=[1],
)
assert_raises(
nx.NetworkXNoPath,
_bidirectional_dijkstra,
G,
1, 2,
ignore_nodes=[2],
)
assert_raises(
nx.NetworkXNoPath,
_bidirectional_dijkstra,
G,
1, 2,
ignore_nodes=[1, 2],
)
示例11: test_disconnected_path_endpoints
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_disconnected_path_endpoints(self):
"""Betweenness centrality: disconnected path endpoints"""
G = nx.Graph()
nx.add_path(G, [0, 1, 2])
nx.add_path(G, [3, 4, 5, 6])
b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3}
b = nx.betweenness_centrality(G,
weight=None,
normalized=False,
endpoints=True)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n])
# normalized = True case
b = nx.betweenness_centrality(G,
weight=None,
normalized=True,
endpoints=True)
for n in sorted(G):
assert_almost_equal(b[n], b_answer[n] / 21)
示例12: make_demo_infr
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def make_demo_infr(ccs, edges=[], nodes=[], infer=True):
"""
Depricate in favor of demodata_infr
"""
import ibeis
import networkx as nx
if nx.__version__.startswith('1'):
nx.add_path = nx.Graph.add_path
G = ibeis.AnnotInference._graph_cls()
G.add_nodes_from(nodes)
for cc in ccs:
if len(cc) == 1:
G.add_nodes_from(cc)
nx.add_path(G, cc, evidence_decision=POSTV, meta_decision=NULL)
# for edge in edges:
# u, v, d = edge if len(edge) == 3 else tuple(edge) + ({},)
G.add_edges_from(edges)
infr = ibeis.AnnotInference.from_netx(G, infer=infer)
infr.verbose = 3
infr.relabel_using_reviews(rectify=False)
infr.graph.graph['dark_background'] = False
infr.graph.graph['ignore_labels'] = True
infr.set_node_attrs('width', 40)
infr.set_node_attrs('height', 40)
# infr.set_node_attrs('fontsize', fontsize)
# infr.set_node_attrs('fontname', fontname)
infr.set_node_attrs('fixed_size', True)
return infr
示例13: test_two_cliques
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_two_cliques(self):
# This graph has two major cliques [0,1,2,3,4] and [11,12,13,14]
# but the first one is bigger so that's the maximum_clique.
G = nx.complete_graph(5)
nx.add_path(G, [4, 5, 6, 7, 8])
nx.add_path(G, [2, 9, 10])
nx.add_path(G, [9, 11])
nx.add_path(G, [11, 12, 13, 14, 11])
nx.add_path(G, [12, 14])
nx.add_path(G, [13, 11])
clique = dnx.maximum_clique(G, dimod.ExactSolver())
self.assertEqual(clique, [0, 1, 2, 3, 4])
示例14: test_two_cliques_num
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_two_cliques_num(self):
# This graph has two major cliques [0,1,2,3,4] and [11,12,13,14]
# but the first one is bigger so that's the maximum_clique.
G = nx.complete_graph(5)
nx.add_path(G, [4, 5, 6, 7, 8])
nx.add_path(G, [2, 9, 10])
nx.add_path(G, [9, 11])
nx.add_path(G, [11, 12, 13, 14, 11])
nx.add_path(G, [12, 14])
nx.add_path(G, [13, 11])
clique_number = dnx.clique_number(G, dimod.ExactSolver())
self.assertEqual(clique_number, 5)
示例15: test_default_attribute
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import add_path [as 别名]
def test_default_attribute(self):
G = nx.Graph(name="Fred")
G.add_node(1, label=1, color='green')
nx.add_path(G, [0, 1, 2, 3])
G.add_edge(1, 2, weight=3)
G.graph['node_default'] = {'color': 'yellow'}
G.graph['edge_default'] = {'weight': 7}
fh = io.BytesIO()
self.writer(G, fh)
fh.seek(0)
H = nx.read_graphml(fh, node_type=int)
assert_nodes_equal(G.nodes(), H.nodes())
assert_edges_equal(G.edges(), H.edges())
assert_equal(G.graph, H.graph)