本文整理汇总了Python中networkx.all_pairs_shortest_path_length函数的典型用法代码示例。如果您正苦于以下问题:Python all_pairs_shortest_path_length函数的具体用法?Python all_pairs_shortest_path_length怎么用?Python all_pairs_shortest_path_length使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了all_pairs_shortest_path_length函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_nx
def run_nx(n, niter):
pb = progressbar.ProgressBar(maxval=niter).start()
g = nx.barabasi_albert_graph(n, 2)
start = time.time()
for i in range(niter):
nx.all_pairs_shortest_path_length(g)
pb.update(i)
pb.finish()
end = time.time()
return (start, end)
示例2: closenessCentrality
def closenessCentrality(A):
H = nx.from_numpy_matrix(A);
length = list(nx.all_pairs_shortest_path_length(H));
print(length)
distanceMatrix = [];
rows = len(length);
for i in range(0, rows):
x = length[i];
y = x[1];
for j in range(0, rows):
distanceMatrix.append(y[j]);
a = np.array(distanceMatrix);
a = a.reshape(rows, rows);
sum = 0;
result1 = [];
rows = a.shape[0];
cols = a.shape[1];
for r in range(0, rows):
sum = 0;
for c in range(0, cols):
if(r != c):
sum += a[r][c];
result1.append((rows - 1) / sum);
return result1
示例3: create_hc
def create_hc(G, t=1.0):
"""
Creates hierarchical cluster of graph G from distance matrix
Maksim Tsvetovat ->> Generalized HC pre- and post-processing to work on labelled graphs and return labelled clusters
The threshold value is now parameterized; useful range should be determined experimentally with each dataset
"""
"""Modified from code by Drew Conway"""
## Create a shortest-path distance matrix, while preserving node labels
labels=G.nodes()
path_length=nx.all_pairs_shortest_path_length(G)
distances=numpy.zeros((len(G),len(G)))
i=0
for u,p in path_length.items():
j=0
for v,d in p.items():
distances[i][j]=d
distances[j][i]=d
if i==j: distances[i][j]=0
j+=1
i+=1
# Create hierarchical cluster
Y=distance.squareform(distances)
Z=hierarchy.complete(Y) # Creates HC using farthest point linkage
# This partition selection is arbitrary, for illustrive purposes
membership=list(hierarchy.fcluster(Z,t=t))
# Create collection of lists for blockmodel
partition=defaultdict(list)
for n,p in zip(list(range(len(G))),membership):
partition[p].append(labels[n])
return list(partition.values())
示例4: path_lengths
def path_lengths(G):
"""Compute array of all shortest path lengths for the given graph.
The length of the output array is the number of unique pairs of nodes that
have a connecting path, so in general it is not known in advance.
This assumes the graph is undirected, as for any pair of reachable nodes,
once we've seen the pair we do not keep the path length value for the
inverse path.
Parameters
----------
G : an undirected graph object.
"""
assert_no_selfloops(G)
length = nx.all_pairs_shortest_path_length(G)
paths = []
seen = set()
for src,targets in length.iteritems():
seen.add(src)
neigh = set(targets.keys()) - seen
paths.extend(targets[targ] for targ in neigh)
return np.array(paths)
示例5: calc_distance_matrix
def calc_distance_matrix(G, max_distance=None):
"""Returns a matrix containing the shortest distance
between all nodes in a network
Parameters
----------
G : graph
A NetworkX graph
max_distance : float or None, optional (default='None')
The maximum possible distance value in the network.
If None, max_distance is the longest shortest path between
two nodes of the network (the graph eccentricity)
Returns
-------
dist_matrix : NumPy array
An NxN numpy array.
Notes
-----
Along the diagonal, the values are all 0.
Unconnected nodes have a distance of max_distance to other nodes.
"""
# Network (collaborator) Distance
dist_matrix = nx.all_pairs_shortest_path_length(G)
dist_matrix = DataFrame(dist_matrix, index=G.nodes(), columns=G.nodes())
if max_distance is None:
max_distance = float(dist_matrix.max().max())
dist_matrix = dist_matrix.fillna(max_distance)
# The unconnected ones are infinitely far from the rest
diag_idx = np.diag_indices(len(dist_matrix), ndim=2)
dist_matrix.values[diag_idx] = 0
return dist_matrix
示例6: create_hr
def create_hr(G):
"""
Create heirarchical cluster of a graph G from distance matrix
"""
# create shortest path matrix
labels=G.nodes()
path_length = nx.all_pairs_shortest_path_length(G)
示例7: od_pairs_from_topology
def od_pairs_from_topology(topology):
"""
Calculate all possible origin-destination pairs of the topology.
This function does not simply calculate all possible pairs of the topology
nodes. Instead, it only returns pairs of nodes connected by at least
a path.
Parameters
----------
topology : Topology or DirectedTopology
The topology whose OD pairs are calculated
Returns
-------
od_pair : list
List containing all origin destination tuples.
Examples
--------
>>> import fnss
>>> topology = fnss.ring_topology(3)
>>> fnss.od_pairs_from_topology(topology)
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
"""
if topology.is_directed():
routes = nx.all_pairs_shortest_path_length(topology)
return [(o, d) for o in routes for d in routes[o] if o != d]
else:
conn_comp = nx.connected_components(topology)
return [(o, d) for G in conn_comp for o in G for d in G if o != d]
示例8: get_distance_dict
def get_distance_dict(filename):
g = nx.read_edgelist(filename)
print "Read in edgelist file ", filename
print nx.info(g)
path_length = nx.all_pairs_shortest_path_length(g)
print len(path_length.keys())
print path_length
示例9: create_shortest_path_matrix
def create_shortest_path_matrix(weighted=False, discount_highways=False):
G = nx.DiGraph()
logging.info("Loading graph to NetworkX from database...")
c = connection.cursor()
if discount_highways:
c.execute("SELECT l.beg_node_id, l.end_node_id, (CASE WHEN l.link_type='1' THEN 0.5 WHEN l.link_type='2' THEN 0.5 ELSE 1.0 END) FROM microsim_link l")
else:
c.execute("SELECT l.beg_node_id, l.end_node_id, l.length/l.lane_count AS resistance FROM microsim_link l")
G.add_weighted_edges_from(c.fetchall())
logging.debug("Road network is strongly connected: %s" % repr(nx.is_strongly_connected(G)))
logging.info("Computing shortest paths...")
if weighted:
sp = nx.all_pairs_dijkstra_path_length(G)
else:
sp = nx.all_pairs_shortest_path_length(G)
logging.info("Converting shortest paths into matrix...")
c.execute("SELECT ROW_NUMBER() OVER (ORDER BY id), beg_node_id, end_node_id FROM microsim_link")
links = c.fetchall()
N_LINKS = len(links)
shortest_paths = np.zeros((N_LINKS, N_LINKS))
for col_idx, _, col_end_node in links:
for row_idx, _, row_end_node in links:
if col_idx == row_idx:
continue
nodes = sp[col_end_node]
if row_end_node not in nodes:
shortest_paths[row_idx - 1, col_idx - 1] = float(N_LINKS)
else:
shortest_paths[row_idx - 1, col_idx - 1] = nodes[row_end_node]
logging.info("Shortest path matrix complete.")
return shortest_paths
示例10: CheckAllHostConnectivity
def CheckAllHostConnectivity (pairs, g):
matrix = nx.all_pairs_shortest_path_length(g)
connected = 0
for (a, b) in pairs:
if b in matrix[a]:
connected += 1
return connected
示例11: nodal_matrix
def nodal_matrix(self):
"""
Returns a matrix containing the nodal 'distance' between all labelled nodes.
EXAMPLES::
>>> network = PhyloNetwork(eNewick="(((1,2), 3), 4);")
>>> network.nodal_matrix()
... array([[0, 1, 2, 3],
... [1, 0, 2, 3],
... [1, 1, 0, 2],
... [1, 1, 1, 0])
"""
n = len(self.taxa())
matrix = numpy.zeros((n, n), int)
dicdist = all_pairs_shortest_path_length(self)
for i in range(n):
ti = self.taxa()[i]
for j in range(i, n):
tj = self.taxa()[j]
lcsa = self.LCSA(ti, tj)
matrix[i, j] = dicdist[lcsa][self.node_by_taxa(ti)]
matrix[j, i] = dicdist[lcsa][self.node_by_taxa(tj)]
return matrix
示例12: first_return_times
def first_return_times( self, k ):
"""Computes:
length = shortest path lengths <= k
length[i][j] = length of shortest path i->j, if <= k, using
NX.all_pairs_shortest_path_length
length[i] is a dict keyed by neighbors of node i, with values
length of path to j
Returns dictionary of return times <= k, length dictionary
described above.
"""
return_times = dict()
# length = shortest path lengths <= k
# length[i][j] = length of shortest path i->j, if <= k
# length[i] a dict keyed by neighbors of node i, with values
# length of path to j
length = nx.all_pairs_shortest_path_length( self.graph, k )
for i in G.nodes_iter():
# nodes = list of successors j which return to i
nodes = filter(lambda j: length[j].has_key(i),G.successors(i))
# distances for each successor j
distances = [length[j][i]+1 for j in nodes]
if distances:
return_times[i] = min(distances)
return return_times, length
示例13: inter_node_distances
def inter_node_distances(graph):
"""Compute the shortest path lengths between all nodes in graph.
This performs the same operation as NetworkX's
all_pairs_shortest_path_lengths with two exceptions: Here, self
paths are excluded from the dictionary returned, and the distance
between disconnected nodes is set to infinity. The latter
difference is consistent with the Brain Connectivity Toolbox for
Matlab.
Parameters
----------
graph: networkx Graph
An undirected graph.
Returns
-------
lengths: dictionary
Dictionary of shortest path lengths keyed by source and target.
"""
lengths = nx.all_pairs_shortest_path_length(graph)
node_labels = sorted(lengths)
for src in node_labels:
lengths[src].pop(src)
for targ in node_labels:
if src != targ:
try:
lengths[src][targ]
except KeyError:
lengths[src][targ] = np.inf
return lengths
示例14: local_efficiency
def local_efficiency(G):
"""Compute array of local efficiency for the given graph.
Local efficiency: returns a list of paths that represent the nodal
efficiencies across all nodes with their direct neighbors"""
assert_no_selfloops(G)
nodepaths = []
length = nx.all_pairs_shortest_path_length(G)
for n in G:
nneighb = set(nx.neighbors(G,n))
paths = []
for nei in nneighb:
other_neighbors = nneighb - set([nei])
nei_len = length[nei]
paths.extend( [nei_len[o] for o in other_neighbors] )
if paths:
p = 1.0 / np.array(paths,float)
nodepaths.append(p.mean())
else:
nodepaths.append(0.0)
return np.array(nodepaths)
示例15: get_distance_matrix_from_graph
def get_distance_matrix_from_graph(network, filename = None, floyd = True):
""" Returns and optionally stores the distance matrix for a given network.
By default the networkX BFS implementation is used.
Parameters
----------
network : a NetworkX graph (ATTENTION: nodes need to be sequentially numbered starting at 1!)
filename : destination for storing the matrix (optional)
floyd : set to true to use floyd warshall instead of BFS
Returns
-------
A Numpy matrix storing all pairs shortest paths for the given network (or the nodes in the given nodelist).
"""
n = nx.number_of_nodes(network)
if floyd:
D = nx.floyd_warshall_numpy(network)
else:
D_dict = nx.all_pairs_shortest_path_length(network)
D = numpy.zeros((n,n))
for row, col_dict in D_dict.iteritems():
for col in col_dict:
D[row-1,col-1] = col_dict[col]
if filename:
numpy.savetxt(filename, D, fmt='%s', delimiter=",", newline="\n")
return D