Exemplo n.º 1
0
def neighborhood_degree_list(G, nbunch):
    """Return a list of the unique degrees of all neighbors of nodes in
    `nbunch`.

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

    nbunch :
        A single node or iterable container of nodes.

    Returns
    -------
    list
        A list of the degrees of all nodes in the neighborhood of the nodes
        in `nbunch`.

    See Also
    --------
    closed_neighborhood_degree_list, neighborhood

    Examples
    --------
    >>> import grinpy as gp
    >>> G = gp.path_graph(3) # Path on 3 nodes
    >>> gp.neighborhood_degree_list(G, 1)
    [1, 2]
    """
    if isinstance(nodes, collections.abc.Iterable):
        return list(set(degree(G, u) for u in set_neighborhood(G, nbunch)))
    else:
        return list(set(degree(G, u) for u in neighborhood(G, nbunch)))
Exemplo n.º 2
0
def is_complete_graph(G):
    """Returns True if *G* is a complete graph, and False otherwise.

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

    Returns
    -------
    boolean
        True if G is a complete graph, False otherwise.
    """
    V = set(nodes(G))
    for v in V:
        if not set(neighborhood(G, v)) == V.difference({v}):
            return False
    return True
Exemplo n.º 3
0
def contract_nodes(G, nbunch, new_node=None):
    """ Contract the nodes in nbunch.

    Contracting a set S of nodes in a graph G first removes the nodes in S from
    G then creates a new node *v* with new edges *(v, u)* for all nodes *u* that
    are neighbors of a nodes in S.

    Parameters
    ----------

    G : NetworkX graph
        An undirected graph.

    nbunch :
        A single node or iterable container of nodes in G.

    new_node :
        The node to be added. If no node is given, it defaults to the minimum
        node in nbunch according to the natural ordering of nodes.

    Notes
    -----
    This method does not return a value. It alters the graph in place.
    """
    # check if nbunch is an iterable; if not, convert to a list
    try:
        _ = (v for v in nbunch)
    except:
        nbunch = [nbunch]
    nbunch = [n for n in nbunch if n in G]
    # get all neighbors of nodes in nbunch that are not also in nbunch
    N = set().union(*[neighborhood(G, n) for n in nbunch]).difference(nbunch)
    # remove the nodes in nbunch from G, then add a new node with approriate edges
    G.remove_nodes_from(nbunch)
    new_node = new_node or min(nbunch)
    G.add_node(new_node)
    G.add_edges_from(map(lambda n: (new_node, n), N))
Exemplo n.º 4
0
def neighborhood_degree_list(G, nbunch):
    # TODO: Add documentation
    return [degree(G, u) for u in neighborhood(G, nbunch)]