示例#1
0
文件: dsi.py 项目: somacdivad/grinpy
def annihilation_number(G):
    r"""Return the annihilation number of the graph.

    The annihilation number of a graph G is defined as:

    .. math::
        a(G) = \max\{t : \sum_{i=0}^t d_i \leq m\}

    where

    .. math::
        {d_1 \leq d_2 \leq \cdots \leq d_n}

    is the degree sequence of the graph ordered in non-decreasing order and *m*
    is the number of edges in G.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    Returns
    -------
    int
        The annihilation number of the graph.
    """
    D = degree_sequence(G)
    D.sort()  # sort in non-decreasing order
    n = len(D)
    m = number_of_edges(G)
    # sum over degrees in the sequence until the sum is larger than the number of edges in the graph
    for i in reversed(range(n + 1)):
        if sum(D[:i]) <= m:
            return i
示例#2
0
def annihilation_number(G):
    # TODO: Add documentation
    D = degree_sequence(G)
    D.sort() # sort in non-decreasing order
    m = number_of_edges(G)
    # sum over degrees in the sequence until the sum is larger than the number of edges in the graph
    S = [D[0]]
    while(sum(S) <= m):
        S.append(D[len(S)])
    return len(S) - 1
def max_matching_bf(G):
    """Return a maximum matching in G.
    A *maximum matching* is a largest set of edges such that no two
    edges in the set have a common endpoint.
    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.
    Returns
    -------
    set
        A set of edges in a maximum matching.
    """
    if number_of_edges(G) == 0:
        return set()

    for i in reversed(range(1, number_of_edges(G) + 1)):
        for S in combinations(edges(G), i):
            if is_matching(G, set(S)):
                return set(S)
示例#4
0
def max_k_independent_set(G, k):
    # TODO: Add documentation
    # set the maximum for the loop range
    rangeMax = number_of_nodes(G) + 1
    if k == 1:
        rangeMax = annihilation_number(G) + 1
    elif number_of_edges(G) > 0:
        rangeMax = number_of_nodes(G)
    # TODO: can the above range be improved with some general upper bound for the k-independence number?
    # loop through subsets of nodes of G in decreasing order of size until a k-independent set is found
    for i in reversed(range(rangeMax)):
        for S in combinations(nodes(G), i):
            if is_k_independent_set(G, S, k):
                return list(S)
    # return None if no independent set is found (should not occur)
    return None
示例#5
0
def test_matching_number_of_path_is_ceil_of_half_of_edges():
    for i in range(2, 12):
        P = gp.path_graph(i)
        m = math.ceil(gp.number_of_edges(P) / 2)
        assert gp.matching_number(P, method="bf") == m
        assert gp.matching_number(P, method="ilp") == m