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


Python networkx.single_source_shortest_path_length方法代码示例

本文整理汇总了Python中networkx.single_source_shortest_path_length方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.single_source_shortest_path_length方法的具体用法?Python networkx.single_source_shortest_path_length怎么用?Python networkx.single_source_shortest_path_length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.single_source_shortest_path_length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_shortest_path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_shortest_path(self):
        deg=[3,2,2,1]
        G=nx.generators.havel_hakimi_graph(deg)
        cs1=nxt.creation_sequence(deg, with_labels=True)
        for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3),
                     (3, 1), (1, 2), (2, 3)]:
            assert_equal(nxt.shortest_path(cs1,n,m),
                         nx.shortest_path(G, n, m))

        spl=nxt.shortest_path_length(cs1,3)
        spl2=nxt.shortest_path_length([ t for v,t in cs1],2)
        assert_equal(spl, spl2)

        spld={}
        for j,pl in enumerate(spl):
            n=cs1[j][0]
            spld[n]=pl
        assert_equal(spld, nx.single_source_shortest_path_length(G, 3)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:20,代码来源:test_threshold.py

示例2: test_shortest_path

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_shortest_path(self):
        deg = [3, 2, 2, 1]
        G = nx.generators.havel_hakimi_graph(deg)
        cs1 = nxt.creation_sequence(deg, with_labels=True)
        for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3),
                     (3, 1), (1, 2), (2, 3)]:
            assert_equal(nxt.shortest_path(cs1, n, m),
                         nx.shortest_path(G, n, m))

        spl = nxt.shortest_path_length(cs1, 3)
        spl2 = nxt.shortest_path_length([t for v, t in cs1], 2)
        assert_equal(spl, spl2)

        spld = {}
        for j, pl in enumerate(spl):
            n = cs1[j][0]
            spld[n] = pl
        assert_equal(spld, nx.single_source_shortest_path_length(G, 3))

        assert_equal(nxt.shortest_path(['d', 'd', 'd', 'i', 'd', 'd'], 1, 2), [1, 2])
        assert_equal(nxt.shortest_path([3, 1, 2], 1, 2), [1, 2])
        assert_raises(TypeError, nxt.shortest_path, [3., 1., 2.], 1, 2)
        assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 'a', 2)
        assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 1, 'b')
        assert_equal(nxt.shortest_path([3, 1, 2], 1, 1), [1]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:27,代码来源:test_threshold.py

示例3: _distances_from_function_start

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def _distances_from_function_start(function):
        """
        :param function:    A normalized Function object.
        :returns:           A dictionary of basic block addresses and their distance to the start of the function.
        """
        return networkx.single_source_shortest_path_length(function.graph,
                                                           function.startpoint) 
开发者ID:angr,项目名称:angr,代码行数:9,代码来源:bindiff.py

示例4: _distances_from_function_exit

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def _distances_from_function_exit(function):
        """
        :param function:    A normalized Function object.
        :returns:           A dictionary of basic block addresses and their distance to the exit of the function.
        """
        reverse_graph = function.graph.reverse()
        # we aren't guaranteed to have an exit from the function so explicitly add the node
        reverse_graph.add_node("start")
        found_exits = False
        for n in function.graph.nodes():
            if len(list(function.graph.successors(n))) == 0:
                reverse_graph.add_edge("start", n)
                found_exits = True

        # if there were no exits (a function with a while 1) let's consider the block with the highest address to
        # be the exit. This isn't the most scientific way, but since this case is pretty rare it should be okay
        if not found_exits:
            last = max(function.graph.nodes(), key=lambda x:x.addr)
            reverse_graph.add_edge("start", last)

        dists = networkx.single_source_shortest_path_length(reverse_graph, "start")

        # remove temp node
        del dists["start"]

        # correct for the added node
        for n in dists:
            dists[n] -= 1

        return dists 
开发者ID:angr,项目名称:angr,代码行数:32,代码来源:bindiff.py

示例5: get_nodes_n_hops_away

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def get_nodes_n_hops_away(self, node_pub_key, n):
        """
        Returns all nodes, which are n hops away from a given
        node_pub_key node.

        :param node_pub_key: string
        :param n: int
        :return: dict with nodes and distance as value
        """

        return nx.single_source_shortest_path_length(
            self.node.network.graph, node_pub_key, cutoff=n) 
开发者ID:bitromortac,项目名称:lndmanage,代码行数:14,代码来源:network_info.py

示例6: weiner_index

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def weiner_index(G, weight=None):
    # compute sum of distances between all node pairs
    # (with optional weights)
    weiner=0.0
    if weight is None:
        for n in G:
            path_length=nx.single_source_shortest_path_length(G,n)
            weiner+=sum(path_length.values())
    else:
        for n in G:
            path_length=nx.single_source_dijkstra_path_length(G,
                    n,weight=weight)
            weiner+=sum(path_length.values())
    return weiner 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:16,代码来源:vitality.py

示例7: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        l=nx.shortest_path_length(self.cycle,0)
        assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
        assert_equal(l,nx.single_source_shortest_path_length(self.cycle,0))
        l=nx.shortest_path_length(self.grid,1)
        assert_equal(l[16],6)
        # now with weights
        l=nx.shortest_path_length(self.cycle,0,weight='weight')
        assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
        assert_equal(l,nx.single_source_dijkstra_path_length(self.cycle,0))
        l=nx.shortest_path_length(self.grid,1,weight='weight')
        assert_equal(l[16],6) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:14,代码来源:test_generic.py

示例8: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        assert_equal(nx.single_source_shortest_path_length(self.cycle,0),
                     {0:0,1:1,2:2,3:3,4:3,5:2,6:1}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:5,代码来源:test_unweighted.py

示例9: all_pairs_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_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} 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:35,代码来源:unweighted.py

示例10: single_source_shortest_path_length_range

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def single_source_shortest_path_length_range(graph, node_range, cutoff):
    dists_dict = {}
    for node in node_range:
        dists_dict[node] = nx.single_source_shortest_path_length(graph, node, cutoff)
    return dists_dict 
开发者ID:JiaxuanYou,项目名称:P-GNN,代码行数:7,代码来源:utils.py

示例11: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        ans = dict(nx.shortest_path_length(self.cycle, 0))
        assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
        assert_equal(ans,
                     dict(nx.single_source_shortest_path_length(self.cycle,
                                                                0)))
        ans = dict(nx.shortest_path_length(self.grid, 1))
        assert_equal(ans[16], 6)
        # now with weights
        ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight'))
        assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
        assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
            self.cycle, 0)))
        ans = dict(nx.shortest_path_length(self.grid, 1, weight='weight'))
        assert_equal(ans[16], 6)
        # weights and method specified
        ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
                                           method='dijkstra'))
        assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
        assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
            self.cycle, 0)))
        ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
                                           method='bellman-ford'))
        assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
        assert_equal(ans, dict(nx.single_source_bellman_ford_path_length(
            self.cycle, 0))) 
开发者ID:holzschu,项目名称:Carnets,代码行数:28,代码来源:test_generic.py

示例12: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        pl = nx.single_source_shortest_path_length
        lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}
        assert_equal(dict(pl(self.cycle, 0)), lengths)
        lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
        assert_equal(dict(pl(self.directed_cycle, 0)), lengths) 
开发者ID:holzschu,项目名称:Carnets,代码行数:8,代码来源:test_unweighted.py

示例13: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        l = dict(nx.shortest_path_length(self.cycle,0))
        assert_equal(l,{0:0,1:1,2:2,3:3,4:3,5:2,6:1})
        assert_equal(l, dict(nx.single_source_shortest_path_length(self.cycle,0)))
        l = dict(nx.shortest_path_length(self.grid,1))
        assert_equal(l[16],6)
        # now with weights
        l = dict(nx.shortest_path_length(self.cycle, 0, weight='weight'))
        assert_equal(l, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
        assert_equal(l, dict(nx.single_source_dijkstra_path_length(
            self.cycle, 0)))
        l = dict(nx.shortest_path_length(self.grid, 1, weight='weight'))
        assert_equal(l[16], 6) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:15,代码来源:test_generic.py

示例14: test_single_source_shortest_path_length

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def test_single_source_shortest_path_length(self):
        pl = nx.single_source_shortest_path_length
        lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}
        assert_equal(dict(pl(self.cycle,0)), lengths)
        lengths = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
        assert_equal(dict(pl(self.directed_cycle,0)), lengths) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:8,代码来源:test_unweighted.py

示例15: query

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import single_source_shortest_path_length [as 别名]
def query(self, topic, max_depth=4, config=None, dont_follow=['enrichment', 'classification']):
        """
            :param topic: a  graph to return the context of.  At least one node ID in topic \
             must be in full graph g to return any context.
            :param max_depth: The maximum distance from the topic to search
            :param config: The titanDB configuration to use if not using the one configured with the plugin
            :param dont_follow: A list of attribute types to not follow
            :return: subgraph in networkx format
        """
        distances = dict()

        if config is None:
            config = self.context_graph

        # Conver topic from a graph into a set of nodes
        topic_nodes = set()
        for n, d in topic.nodes(data=True):
            topic_nodes.add("class={0}&key={1}&value={2}".format(d['class'], d['key'], d['value']))

        nodes = topic_nodes.copy()

        for t in topic:
            # get all nodes within max_depth distance from each topic and add them to the set
            new_distances = nx.single_source_shortest_path_length(self.context_graph.to_undirected(), t, cutoff=max_depth)
            nodes = nodes.union(set(new_distances.keys()))

            # Update shortest distances from topic to node
            for n in new_distances.keys():
                if n in distances:
                    if new_distances[n] < distances[n]:
                        distances[n] = new_distances[n]
                else:
                    distances[n] = new_distances[n]

        # remove dont_follow nodes:
        nodes_to_remove = set()
        for n in nodes:
            if self.context_graph.node[n]['key'] in dont_follow:
                nodes_to_remove.add(n)
        nodes = nodes.difference(nodes_to_remove)

        # Get the subgraph represented by the nodes:
        g = nx.MultiDiGraph(self.context_graph.subgraph(nodes))

        # Prune out non-relevant components by removing those that contain no topic nodes.
        #  This gets ride of nodes that were found by following dont_follow nodes
        for component in nx.connected_components(g.to_undirected()):
            if len(topic_nodes.intersection(set(component))) <= 0:  # if there's no overlap betweent the component and topic
                g.remove_nodes_from(component)  # remove the component

        # add the topic distances to the subgraph
        for n in g.nodes():
            g.node[n]['topic_distance'] = distances[n]

        return g 
开发者ID:vz-risk,项目名称:Verum,代码行数:57,代码来源:networkx.py


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