Ejemplo n.º 1
0
def min_connected_k_dominating_set(G, k):
    """Return a smallest connected k-dominating set in the graph.

    The method to compute the set is brute force.

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

    k : int
        A positive integer.

    Returns
    -------
    list
        A list of nodes in a smallest k-dominating set in the graph.

    """
    # check that k is a positive integer
    if not float(k).is_integer():
        raise TypeError("Expected k to be an integer.")
    k = int(k)
    if k < 1:
        raise ValueError("Expected k to be a positive integer.")
    # Only proceed with search if graph is connected
    if not is_connected(G):
        return []
    for i in range(1, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_connected_k_dominating_set(G, S, k):
                return list(S)
Ejemplo n.º 2
0
def max_k_independent_set(G, k):
    """Return a largest k-independent set of nodes in *G*.
    The method used is brute force, except when *k*=1. In this case,
    the search starts with subsets of *G* with cardinality equal to the
    annihilation number of *G*, which was shown by Pepper to be an upper
    bound for the independence number of a graph, and then continues
    checking smaller subsets until a maximum independent set is found.
    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.
    k : int
        A positive integer.
    Returns
    -------
    list
        A list of nodes comprising a largest k-independent set in *G*.
    See Also
    --------
    max_independent_set
    """
    # set the maximum for the loop range
    rangeMax = number_of_nodes(G) + 1
    if k == 1:
        rangeMax = annihilation_number(G) + 1

    # 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 set(S)
Ejemplo n.º 3
0
def irregularity(G):
    """Return the irregularity measure of the graph.

    The *irregularity* of an *n*-vertex graph is defined as:

    .. math::
        \\frac{2}{n(n+1)}\sum_{i=0}^{n-i}(n-i)d_i

    where *d_i* is the i-th element in the closed disparity sequence, ordered
    in weakly decreasing order.

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

    Returns
    -------
    float
        The irregularity of the graph.

    See Also
    --------
    k_disparity
    """
    return closed_k_disparity(G, number_of_nodes(G))
Ejemplo n.º 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
Ejemplo n.º 5
0
def min_power_dominating_set(G):
    # TODO: Add documentation
    for i in range(1, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_power_dominating_set(G, S):
                return list(S)
    # if above loop completes, return None (should not occur)
    return None
Ejemplo n.º 6
0
def min_total_dominating_set(G):
    # TODO: Add documentation
    # use naive lower bound for domination to compute a starting point for the search range
    rangeMin = sub_total_domination_number(G)
    # loop through subsets of nodes of G in increasing order of size until a total dominating set is found
    for i in range(rangeMin, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_total_dominating_set(G, S):
                return list(S)
    # return None if no total dominating set is found (should not occur)
    return None
Ejemplo n.º 7
0
def min_k_forcing_set(G, k):
    # TODO: Add documentation
    # use naive lower bound to compute a starting point for the search range
    rangeMin = min_degree(G) if k == 1 else 1
    # loop through subsets of nodes of G in increasing order of size until a zero forcing set is found
    for i in range(rangeMin, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_k_forcing_set(G, S, k):
                return list(S)
    # if the above loop completes, return None (should not occur)
    return None
def graph_property_check(G, property):
    if property == "is_planar":
        return gp.check_planarity(G)[0]
    elif property == "is_regular":
        return gp.min_degree(G) == gp.max_degree(G)
    elif property == "is_cubic":
        return gp.min_degree(G) == 3 and gp.max_degree(G) == 3
    elif property == "is_not_K_n":
        return gp.is_isomorphic(G, gp.complete_graph(gp.number_of_nodes(G))) == False
    elif property == "is_triangle_free":
        return set(gp.triangles(G).values()) == {0}
    else:
        return getattr(gp, property)(G)
Ejemplo n.º 9
0
def min_k_dominating_set(G, k):
    """Return a smallest k-dominating set in the graph.

    The method to compute the set is brute force except that the subsets
    searched begin with those whose cardinality is equal to the
    sub-k-domination number of the graph, which was defined by Amos et
    al. and shown to be a tractable lower bound for the k-domination
    number.

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

    k : int
        A positive integer.

    Returns
    -------
    list
        A list of nodes in a smallest k-dominating set in the graph.

    References
    ----------
    D. Amos, J. Asplund, and R. Davila, The sub-k-domination number of a
    graph with applications to k-domination, *arXiv preprint
    arXiv:1611.02379*, (2016)

    """
    # check that k is a positive integer
    if not float(k).is_integer():
        raise TypeError("Expected k to be an integer.")
    k = int(k)
    if k < 1:
        raise ValueError("Expected k to be a positive integer.")
    # use the sub-k-domination number to compute a starting point for the
    # search range
    rangeMin = sub_k_domination_number(G, k)
    # loop through subsets of nodes of G in increasing order of size until a
    # dominating set is found
    for i in range(rangeMin, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_k_dominating_set(G, S, k):
                return list(S)
Ejemplo n.º 10
0
def average_degree(G):
    """Return the average degree of G.

    The average degree of a graph is the average of the degrees of all nodes
    in the graph.

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

    Returns
    -------
    float
        The average degree of the graph.

    Examples
    --------
    >>> G = nx.star_graph(3) # Star on 4 nodes
    >>> nx.average_degree(G)
    1.5
    """
    return sum(degree_sequence(G)) / number_of_nodes(G)
Ejemplo n.º 11
0
def min_independent_k_dominating_set(G, k):
    """Return a smallest independent k-dominating set in the graph.

    The method to compute the set is brute force.

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

    Returns
    -------
    list
        A list of nodes in a smallest independent k-dominating set in
        the graph.

    """
    # loop through subsets of nodes of G in increasing order of size until a
    # total dominating set is found
    for i in range(1, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_independent_k_dominating_set(G, S, k):
                return list(S)
Ejemplo n.º 12
0
def min_total_dominating_set_bf(G):
    """Return a smallest total dominating set in the graph.

    The method to compute the set is brute force except that the subsets
    searched begin with those whose cardinality is equal to the
    sub-total-domination number of the graph, which was defined by
    Davila and shown to be a tractable lower bound for the k-domination
    number.

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

    Returns
    -------
    list
        A list of nodes in a smallest total dominating set in the graph.

    References
    ----------
    R. Davila, A note on sub-total domination in graphs. *arXiv preprint
    arXiv: 1701.07811*, (2017)

    """
    # use naive lower bound for domination to compute a starting point
    # for the search range
    rangeMin = sub_total_domination_number(G)

    if number_of_nodes_of_degree_k(G, 0) > 0:
        return set()

    for i in range(rangeMin, number_of_nodes(G) + 1):
        for S in combinations(nodes(G), i):
            if is_total_dominating_set(G, S):
                return list(S)