本文整理汇总了Python中networkx.all_pairs_shortest_path_length方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.all_pairs_shortest_path_length方法的具体用法?Python networkx.all_pairs_shortest_path_length怎么用?Python networkx.all_pairs_shortest_path_length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.all_pairs_shortest_path_length方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _compute_distance_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def _compute_distance_matrix(self):
"""Compute the full distance matrix on pairs of nodes.
The distance map self._dist_matrix is computed from the graph using
all_pairs_shortest_path_length.
"""
if not self.is_connected():
raise CouplingError("coupling graph not connected")
lengths = nx.all_pairs_shortest_path_length(self.graph.to_undirected(as_view=True))
lengths = dict(lengths)
size = len(lengths)
cmap = np.zeros((size, size))
for idx in range(size):
cmap[idx, np.fromiter(lengths[idx].keys(), dtype=int)] = np.fromiter(
lengths[idx].values(), dtype=int)
self._dist_matrix = cmap
示例2: shortest_path_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def shortest_path_matrix(G):
"""
Return a matrix of pairwise shortest path lengths between nodes.
Parameters
----------
G (nx.Graph): the graph in question
Returns
-------
pmat (np.ndarray): a matrix of shortest paths between nodes in G
"""
N = G.number_of_nodes()
pmat = np.zeros((N, N)) + N
paths = nx.all_pairs_shortest_path_length(G)
for node_i, node_ij in paths:
for node_j, length_ij in node_ij.items():
pmat[node_i, node_j] = length_ij
pmat[pmat == np.inf] = N
return pmat
示例3: precompute_dist_data
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def precompute_dist_data(edge_index, num_nodes, approximate=0):
'''
Here dist is 1/real_dist, higher actually means closer, 0 means disconnected
:return:
'''
graph = nx.Graph()
edge_list = edge_index.transpose(1,0).tolist()
graph.add_edges_from(edge_list)
n = num_nodes
dists_array = np.zeros((n, n))
# dists_dict = nx.all_pairs_shortest_path_length(graph,cutoff=approximate if approximate>0 else None)
# dists_dict = {c[0]: c[1] for c in dists_dict}
dists_dict = all_pairs_shortest_path_length_parallel(graph,cutoff=approximate if approximate>0 else None)
for i, node_i in enumerate(graph.nodes()):
shortest_dist = dists_dict[node_i]
for j, node_j in enumerate(graph.nodes()):
dist = shortest_dist.get(node_j, -1)
if dist!=-1:
# dists_array[i, j] = 1 / (dist + 1)
dists_array[node_i, node_j] = 1 / (dist + 1)
return dists_array
示例4: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
ans = dict(nx.shortest_path_length(self.cycle))
assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.all_pairs_shortest_path_length(self.cycle)))
ans = dict(nx.shortest_path_length(self.grid))
assert_equal(ans[1][16], 6)
# now with weights
ans = dict(nx.shortest_path_length(self.cycle, weight='weight'))
assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle)))
ans = dict(nx.shortest_path_length(self.grid, weight='weight'))
assert_equal(ans[1][16], 6)
# weights and method specified
ans = dict(nx.shortest_path_length(self.cycle, weight='weight',
method='dijkstra'))
assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle)))
ans = dict(nx.shortest_path_length(self.cycle, weight='weight',
method='bellman-ford'))
assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(ans,
dict(nx.all_pairs_bellman_ford_path_length(self.cycle)))
示例5: calc_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def calc_shortest_path(protein_graph, prefix, generate_plots=True, xmgrace=False):
num_nodes = len(protein_graph.nodes())
nodes_axis = range(1, num_nodes + 1)
path_dict = nx.all_pairs_shortest_path_length(protein_graph)
dj_path_matrix = np.zeros((num_nodes, num_nodes))
for i in range(num_nodes):
for j in range(num_nodes):
try:
dj_path_matrix[i,j] = path_dict[i][j]
except KeyError as ke:
raise nx.exception.NetworkXNoPath("\nERROR::type=orphan_node:message=No link between %d and %d:exception=%s\n" % (i, j, str(ke)))
np.savetxt("%s_L.dat" % prefix, dj_path_matrix)
avg_L_per_node = np.sum(dj_path_matrix, axis=0)/(num_nodes - 1)
if generate_plots:
plt.plot(nodes_axis, avg_L_per_node)
plt.title("%s L" % prefix, fontsize=18)
plt.xlabel('Node Indices', fontsize=16)
plt.ylabel('L', fontsize=16)
plt.savefig("%s_L.png" % prefix, dpi=300, bbox_inches='tight')
plt.close()
avg_L_per_node = avg_L_per_node.reshape(1, num_nodes)
np.savetxt("%s_avg_L.dat" % prefix, avg_L_per_node)
if xmgrace:
dat2xmgrace(avg_L_per_node, prefix, "L", traj=traj)
return dj_path_matrix
示例6: path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def path_length(self, from_node, to_node):
if self._path_lengths is None:
self._path_lengths = dict(nx.all_pairs_shortest_path_length(self, cutoff=None))
if from_node not in self._path_lengths or to_node not in self._path_lengths[from_node]:
return math.inf
else:
return self._path_lengths[from_node][to_node]
示例7: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
l=nx.shortest_path_length(self.cycle)
assert_equal(l[0],{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l,nx.all_pairs_shortest_path_length(self.cycle))
l=nx.shortest_path_length(self.grid)
assert_equal(l[1][16],6)
# now with weights
l=nx.shortest_path_length(self.cycle,weight='weight')
assert_equal(l[0],{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l,nx.all_pairs_dijkstra_path_length(self.cycle))
l=nx.shortest_path_length(self.grid,weight='weight')
assert_equal(l[1][16],6)
示例8: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
l=nx.all_pairs_shortest_path_length(self.cycle)
assert_equal(l[0],{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
l=nx.all_pairs_shortest_path_length(self.grid)
assert_equal(l[1][16],6)
示例9: all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def all_pairs_shortest_path_length(G, cutoff=None):
"""Computes the shortest path lengths between all nodes in ``G``.
Parameters
----------
G : NetworkX graph
cutoff : integer, optional
Depth at which to stop the search. Only paths of length at most
``cutoff`` are returned.
Returns
-------
lengths : dictionary
Dictionary of shortest path lengths keyed by source and target.
Notes
-----
The dictionary returned only has keys for reachable node pairs.
Examples
--------
>>> G = nx.path_graph(5)
>>> length = nx.all_pairs_shortest_path_length(G)
>>> print(length[1][4])
3
>>> length[1]
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}
"""
length = single_source_shortest_path_length
# TODO This can be trivially parallelized.
return {n: length(G, n, cutoff=cutoff) for n in G}
示例10: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
l = dict(nx.all_pairs_shortest_path_length(self.cycle))
assert_equal(l[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
l = dict(nx.all_pairs_shortest_path_length(self.grid))
assert_equal(l[1][16], 6)
示例11: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
l=dict(nx.shortest_path_length(self.cycle))
assert_equal(l[0],{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
assert_equal(l, dict(nx.all_pairs_shortest_path_length(self.cycle)))
l=dict(nx.shortest_path_length(self.grid))
assert_equal(l[1][16],6)
# now with weights
l = dict(nx.shortest_path_length(self.cycle, weight='weight'))
assert_equal(l[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
assert_equal(l, dict(nx.all_pairs_dijkstra_path_length(self.cycle)))
l = dict(nx.shortest_path_length(self.grid, weight='weight'))
assert_equal(l[1][16], 6)
示例12: test_all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def test_all_pairs_shortest_path_length(self):
l = dict(nx.all_pairs_shortest_path_length(self.cycle))
assert_equal(l[0],{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
l = dict(nx.all_pairs_shortest_path_length(self.grid))
assert_equal(l[1][16],6)
示例13: all_pairs_shortest_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import all_pairs_shortest_path_length [as 别名]
def all_pairs_shortest_path_length(G, cutoff=None):
"""Computes the shortest path lengths between all nodes in `G`.
Parameters
----------
G : NetworkX graph
cutoff : integer, optional
Depth at which to stop the search. Only paths of length at most
`cutoff` are returned.
Returns
-------
lengths : iterator
(source, dictionary) iterator with dictionary keyed by target and
shortest path length as the key value.
Notes
-----
The iterator returned only has reachable node pairs.
Examples
--------
>>> G = nx.path_graph(5)
>>> length = dict(nx.all_pairs_shortest_path_length(G))
>>> for node in [0, 1, 2, 3, 4]:
... print('1 - {}: {}'.format(node, length[1][node]))
1 - 0: 1
1 - 1: 0
1 - 2: 1
1 - 3: 2
1 - 4: 3
>>> length[3][2]
1
>>> length[2][2]
0
"""
length = single_source_shortest_path_length
# TODO This can be trivially parallelized.
for n in G:
yield (n, length(G, n, cutoff=cutoff))