当前位置: 首页>>代码示例>>Python>>正文


Python networkx.average_shortest_path_length方法代码示例

本文整理汇总了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) 
开发者ID:JiaxuanYou,项目名称:graph-generation,代码行数:23,代码来源:utils.py

示例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) 
开发者ID:GiulioRossetti,项目名称:cdlib,代码行数:23,代码来源:fitness.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:23,代码来源:test_generic.py

示例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) 
开发者ID:NREL,项目名称:ditto,代码行数:11,代码来源:network_analysis.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:7,代码来源:test_generic.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:11,代码来源:test_generic.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:9,代码来源:test_generic.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:5,代码来源:test_generic.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:5,代码来源:test_generic.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:11,代码来源:test_generic.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:9,代码来源:test_generic.py

示例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()) 
开发者ID:holzschu,项目名称:Carnets,代码行数:4,代码来源:test_generic.py

示例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') 
开发者ID:holzschu,项目名称:Carnets,代码行数:5,代码来源:test_generic.py

示例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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:14,代码来源:test_smallworld.py

示例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) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:5,代码来源:test_generic.py


注:本文中的networkx.average_shortest_path_length方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。