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


Python networkx.all_node_cuts函数代码示例

本文整理汇总了Python中networkx.all_node_cuts函数的典型用法代码示例。如果您正苦于以下问题:Python all_node_cuts函数的具体用法?Python all_node_cuts怎么用?Python all_node_cuts使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: test_cycle_graph

def test_cycle_graph():
    G = nx.cycle_graph(5)
    solution = [{0, 2}, {0, 3}, {1, 3}, {1, 4}, {2, 4}]
    cuts = list(nx.all_node_cuts(G))
    assert_true(len(solution) == len(cuts))
    for cut in cuts:
        assert_true(cut in solution)
开发者ID:aparamon,项目名称:networkx,代码行数:7,代码来源:test_kcutsets.py

示例2: test_articulation_points

def test_articulation_points():
    Ggen = _generate_no_biconnected()
    for i in range(1):  # change 1 to 3 or more for more realizations.
        G = next(Ggen)
        articulation_points = list({a} for a in nx.articulation_points(G))
        for cut in nx.all_node_cuts(G):
            assert_true(cut in articulation_points)
开发者ID:aparamon,项目名称:networkx,代码行数:7,代码来源:test_kcutsets.py

示例3: test_articulation_points

def test_articulation_points():
    Ggen = _generate_no_biconnected()
    for i in range(2):
        G = next(Ggen)
        articulation_points = list({a} for a in nx.articulation_points(G))
        for cut in nx.all_node_cuts(G):
            assert_true(cut in articulation_points)
开发者ID:CaptainAL,项目名称:Spyder,代码行数:7,代码来源:test_kcutsets.py

示例4: _check_separating_sets

def _check_separating_sets(G):
    for Gc in nx.connected_component_subgraphs(G):
        if len(Gc) < 3:
            continue
        for cut in nx.all_node_cuts(Gc):
            assert_equal(nx.node_connectivity(Gc), len(cut))
            H = Gc.copy()
            H.remove_nodes_from(cut)
            assert_false(nx.is_connected(H))
开发者ID:CaptainAL,项目名称:Spyder,代码行数:9,代码来源:test_kcutsets.py

示例5: test_alternative_flow_functions

def test_alternative_flow_functions():
    graph_funcs = [graph_example_1, nx.davis_southern_women_graph]
    for graph_func in graph_funcs:
        G = graph_func()
        for flow_func in flow_funcs:
            for cut in nx.all_node_cuts(G, flow_func=flow_func):
                assert_equal(nx.node_connectivity(G), len(cut))
                H = G.copy()
                H.remove_nodes_from(cut)
                assert_false(nx.is_connected(H))
开发者ID:AmesianX,项目名称:networkx,代码行数:10,代码来源:test_kcutsets.py

示例6: test_alternative_flow_functions

def test_alternative_flow_functions():
    graphs = [nx.grid_2d_graph(4, 4),
              nx.cycle_graph(5)]
    for G in graphs:
        node_conn = nx.node_connectivity(G)
        for flow_func in flow_funcs:
            all_cuts = nx.all_node_cuts(G, flow_func=flow_func)
            # Only test a limited number of cut sets to reduce test time.
            for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
                assert_equal(node_conn, len(cut))
                assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
开发者ID:jianantian,项目名称:networkx,代码行数:11,代码来源:test_kcutsets.py

示例7: _check_separating_sets

def _check_separating_sets(G):
    for cc in nx.connected_components(G):
        if len(cc) < 3:
            continue
        Gc = G.subgraph(cc)
        node_conn = nx.node_connectivity(Gc)
        all_cuts = nx.all_node_cuts(Gc)
        # Only test a limited number of cut sets to reduce test time.
        for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
            assert_equal(node_conn, len(cut))
            assert_false(nx.is_connected(nx.restricted_view(G, cut, [])))
开发者ID:jianantian,项目名称:networkx,代码行数:11,代码来源:test_kcutsets.py

示例8: test_complete_graph

def test_complete_graph():
    G = nx.complete_graph(5)
    solution = [
        {0, 1, 2, 3},
        {0, 1, 2, 4},
        {0, 1, 3, 4},
        {0, 2, 3, 4},
        {1, 2, 3, 4},
    ]
    cuts = list(nx.all_node_cuts(G))
    assert_true(len(solution) == len(cuts))
    for cut in cuts:
        assert_true(cut in solution)
开发者ID:aparamon,项目名称:networkx,代码行数:13,代码来源:test_kcutsets.py

示例9: test_grid_2d_graph

def test_grid_2d_graph():
    # All minimum node cuts of a 2d grid
    # are the four pairs of nodes that are
    # neighbors of the four corner nodes.
    G = nx.grid_2d_graph(5, 5)
    solution = [
        set([(0, 1), (1, 0)]),
        set([(3, 0), (4, 1)]),
        set([(3, 4), (4, 3)]),
        set([(0, 3), (1, 4)]),
    ]
    for cut in nx.all_node_cuts(G):
        assert_true(cut in solution)
开发者ID:aparamon,项目名称:networkx,代码行数:13,代码来源:test_kcutsets.py

示例10: test_non_repeated_cuts

def test_non_repeated_cuts():
    # The algorithm was repeating the cut {0, 1} for the giant biconnected
    # component of the Karate club graph.
    K = nx.karate_club_graph()
    G = max(list(nx.biconnected_component_subgraphs(K)), key=len)
    solution = [{32, 33}, {2, 33}, {0, 3}, {0, 1}, {29, 33}]
    cuts = list(nx.all_node_cuts(G))
    if len(solution) != len(cuts):
        print(nx.info(G))
        print("Solution: {}".format(solution))
        print("Result: {}".format(cuts))
    assert_true(len(solution) == len(cuts))
    for cut in cuts:
        assert_true(cut in solution)
开发者ID:aparamon,项目名称:networkx,代码行数:14,代码来源:test_kcutsets.py

示例11: test_disconnected_graph

def test_disconnected_graph():
    G = nx.fast_gnp_random_graph(100, 0.01)
    cuts = nx.all_node_cuts(G)
    assert_raises(nx.NetworkXError, next, cuts)
开发者ID:aparamon,项目名称:networkx,代码行数:4,代码来源:test_kcutsets.py

示例12: k_components

def k_components(G, flow_func=None):
    r"""Returns the k-component structure of a graph G.

    A `k`-component is a maximal subgraph of a graph G that has, at least,
    node connectivity `k`: we need to remove at least `k` nodes to break it
    into more components. `k`-components have an inherent hierarchical
    structure because they are nested in terms of connectivity: a connected
    graph can contain several 2-components, each of which can contain
    one or more 3-components, and so forth.

    Parameters
    ----------
    G : NetworkX graph

    flow_func : function
        Function to perform the underlying flow computations. Default value
        :meth:`edmonds_karp`. This function performs better in sparse graphs with
        right tailed degree distributions. :meth:`shortest_augmenting_path` will
        perform better in denser graphs.

    Returns
    -------
    k_components : dict
        Dictionary with all connectivity levels `k` in the input Graph as keys
        and a list of sets of nodes that form a k-component of level `k` as
        values.

    Raises
    ------
    NetworkXNotImplemented:
        If the input graph is directed.

    Examples
    --------
    >>> # Petersen graph has 10 nodes and it is triconnected, thus all
    >>> # nodes are in a single component on all three connectivity levels
    >>> G = nx.petersen_graph()
    >>> k_components = nx.k_components(G)

    Notes
    -----
    Moody and White [1]_ (appendix A) provide an algorithm for identifying
    k-components in a graph, which is based on Kanevsky's algorithm [2]_
    for finding all minimum-size node cut-sets of a graph (implemented in
    :meth:`all_node_cuts` function):

        1. Compute node connectivity, k, of the input graph G.

        2. Identify all k-cutsets at the current level of connectivity using
           Kanevsky's algorithm.

        3. Generate new graph components based on the removal of
           these cutsets. Nodes in a cutset belong to both sides
           of the induced cut.

        4. If the graph is neither complete nor trivial, return to 1;
           else end.

    This implementation also uses some heuristics (see [3]_ for details)
    to speed up the computation.

    See also
    --------
    node_connectivity
    all_node_cuts
    biconnected_components : special case of this function when k=2
    k_edge_components : similar to this function, but uses edge-connectivity
        instead of node-connectivity

    References
    ----------
    .. [1]  Moody, J. and D. White (2003). Social cohesion and embeddedness:
            A hierarchical conception of social groups.
            American Sociological Review 68(1), 103--28.
            http://www2.asanet.org/journals/ASRFeb03MoodyWhite.pdf

    .. [2]  Kanevsky, A. (1993). Finding all minimum-size separating vertex
            sets in a graph. Networks 23(6), 533--541.
            http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract

    .. [3]  Torrents, J. and F. Ferraro (2015). Structural Cohesion:
            Visualization and Heuristics for Fast Computation.
            https://arxiv.org/pdf/1503.04476v1

    """
    # Dictionary with connectivity level (k) as keys and a list of
    # sets of nodes that form a k-component as values. Note that
    # k-compoents can overlap (but only k - 1 nodes).
    k_components = defaultdict(list)
    # Define default flow function
    if flow_func is None:
        flow_func = default_flow_func
    # Bicomponents as a base to check for higher order k-components
    for component in nx.connected_components(G):
        # isolated nodes have connectivity 0
        comp = set(component)
        if len(comp) > 1:
            k_components[1].append(comp)
    bicomponents = list(nx.biconnected_component_subgraphs(G))
    for bicomponent in bicomponents:
#.........这里部分代码省略.........
开发者ID:ProgVal,项目名称:networkx,代码行数:101,代码来源:kcomponents.py

示例13: compute_3_connected_k_dominating_set

def compute_3_connected_k_dominating_set(G, D):

    graphCDS = G.subgraph(D)

    if nx.node_connectivity(graphCDS) < 2:
        print "Input is not 2-connected.Exiting"
        return D

    separators = list(nx.all_node_cuts(graphCDS))

    # print "separators"
    # print separators

    while separators and nx.node_connectivity(graphCDS) < 3:

        broken = False

        discD = list(set(D) - separators[0])

        # print "discD"
        # print discD

        temp_graph_D = G.subgraph(discD)

        genD = nx.connected_components(temp_graph_D)

        components = list(genD)
        # print "Components"
        # print components

        for v in components[0]:

            tempD = list(D)
            tempD.remove(v)

            for u in components[1]:

                tempD.remove(u)

                newG = G.copy()
                newG.remove_nodes_from(tempD)

                if nx.has_path(newG, v, u):
                    Hpath = nx.shortest_path(newG, v, u)

                    # print "Hpath is",
                    # print Hpath

                    broken = True
                    break

            if broken:
                break

        for node in Hpath:
            if node not in D:
                D.append(node)

        graphCDS = G.subgraph(D)
        separators = list(nx.all_node_cuts(graphCDS))

        # print "separators"
        # print separators

    return D
开发者ID:georgSquared,项目名称:3-connected-m-dominating-set,代码行数:65,代码来源:RobustCDS.py


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