本文整理汇总了Python中networkx.average_shortest_path_length方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.average_shortest_path_length方法的具体用法?Python networkx.average_shortest_path_length怎么用?Python networkx.average_shortest_path_length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.average_shortest_path_length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def decode_graph(adj, prefix):
adj = np.asmatrix(adj)
G = nx.from_numpy_matrix(adj)
# G.remove_nodes_from(nx.isolates(G))
print('num of nodes: {}'.format(G.number_of_nodes()))
print('num of edges: {}'.format(G.number_of_edges()))
G_deg = nx.degree_histogram(G)
G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
if nx.is_connected(G):
print('average path length: {}'.format(nx.average_shortest_path_length(G)))
print('average diameter: {}'.format(nx.diameter(G)))
G_cluster = sorted(list(nx.clustering(G).values()))
print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
cycle_len = []
cycle_all = nx.cycle_basis(G, 0)
for item in cycle_all:
cycle_len.append(len(item))
print('cycles', cycle_len)
print('cycle count', len(cycle_len))
draw_graph(G, prefix=prefix)
示例2: avg_distance
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def avg_distance(graph, communities, **kwargs):
"""Average distance.
The average distance of a community is defined average path length across all possible pair of nodes composing it.
:param graph: a networkx/igraph object
:param communities: NodeClustering object
:param summary: boolean. If **True** it is returned an aggregated score for the partition is returned, otherwise individual-community ones. Default **True**.
:return: If **summary==True** a FitnessResult object, otherwise a list of floats.
Example:
>>> from cdlib.algorithms import louvain
>>> from cdlib import evaluation
>>> g = nx.karate_club_graph()
>>> communities = louvain(g)
>>> scd = evaluation.avg_distance(g,communities)
"""
return __quality_indexes(graph, communities,
lambda graph, coms: nx.average_shortest_path_length(nx.subgraph(graph, coms)), **kwargs)
示例3: test_specified_methods
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [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)
示例4: average_path_length
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def average_path_length(self, *args):
"""Returns the average path length of the network."""
if args:
try:
return nx.average_shortest_path_length(args[0])
except ZeroDivisionError:
return 0
else:
return nx.average_shortest_path_length(self.G.graph)
示例5: test_average_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_average_shortest_path(self):
l=nx.average_shortest_path_length(self.cycle)
assert_almost_equal(l,2)
l=nx.average_shortest_path_length(nx.path_graph(5))
assert_almost_equal(l,2)
示例6: test_weighted_average_shortest_path
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_weighted_average_shortest_path(self):
G=nx.Graph()
G.add_cycle(range(7),weight=2)
l=nx.average_shortest_path_length(G,weight='weight')
assert_almost_equal(l,4)
G=nx.Graph()
G.add_path(range(5),weight=2)
l=nx.average_shortest_path_length(G,weight='weight')
assert_almost_equal(l,4)
示例7: test_average_shortest_disconnected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_average_shortest_disconnected(self):
g = nx.Graph()
g.add_nodes_from(range(3))
g.add_edge(0, 1)
assert_raises(nx.NetworkXError,nx.average_shortest_path_length,g)
g = g.to_directed()
assert_raises(nx.NetworkXError,nx.average_shortest_path_length,g)
示例8: test_cycle_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_cycle_graph(self):
ans = nx.average_shortest_path_length(nx.cycle_graph(7))
assert_almost_equal(ans, 2)
示例9: test_path_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_path_graph(self):
ans = nx.average_shortest_path_length(nx.path_graph(5))
assert_almost_equal(ans, 2)
示例10: test_weighted
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_weighted(self):
G = nx.Graph()
nx.add_cycle(G, range(7), weight=2)
ans = nx.average_shortest_path_length(G, weight='weight')
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')
assert_almost_equal(ans, 4)
示例11: test_disconnected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_disconnected(self):
g = nx.Graph()
g.add_nodes_from(range(3))
g.add_edge(0, 1)
assert_raises(nx.NetworkXError, nx.average_shortest_path_length, g)
g = g.to_directed()
assert_raises(nx.NetworkXError, nx.average_shortest_path_length, g)
示例12: test_null_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_null_graph(self):
nx.average_shortest_path_length(nx.null_graph())
示例13: test_bad_method
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_bad_method(self):
G = nx.path_graph(2)
nx.average_shortest_path_length(G, weight='weight', method='SPAM')
示例14: test_lattice_reference
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_lattice_reference():
G = nx.connected_watts_strogatz_graph(50, 6, 1, seed=rng)
Gl = lattice_reference(G, niter=1, seed=rng)
L = nx.average_shortest_path_length(G)
Ll = nx.average_shortest_path_length(Gl)
assert_true(Ll > L)
assert_raises(nx.NetworkXError, lattice_reference, nx.Graph())
assert_raises(nx.NetworkXNotImplemented, lattice_reference, nx.DiGraph())
H = nx.Graph(((0, 1), (2, 3)))
Hl = lattice_reference(H, niter=1)
示例15: test_cycle_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import average_shortest_path_length [as 别名]
def test_cycle_graph(self):
l = nx.average_shortest_path_length(nx.cycle_graph(7))
assert_almost_equal(l, 2)