def closed_neighborhood_degree_list(G, nbunch): """Return a list of the unique degrees of all nodes in the closed neighborhood of the 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 closed neighborhood of the nodes in `nbunch`. See Also -------- closed_neighborhood, neighborhood_degree_list Examples -------- >>> import grinpy as gp >>> G = gp.path_graph(3) # Path on 3 nodes >>> gp.closed_neighborhood_degree_list(G, 1) [1, 2, 2] """ if isinstance(nodes, collections.abc.Iterable): return list( set(degree(G, u) for u in set_closed_neighborhood(G, nbunch))) else: return list(set(degree(G, u) for u in closed_neighborhood(G, nbunch)))
def is_k_regular(G, k): """ Return True if the graph is regular of degree k and False otherwise. A graph is *regular of degree k* if all nodes have degree equal to *k*. Parameters ---------- G : NetworkX graph An undirected graph k : int An integer Returns ------- boolean True if all nodes have degree equal to *k*, False otherwise. """ # check that k is an integer if not float(k).is_integer(): raise TypeError('Expected k to be an integer.') k = int(k) for v in nodes(G): if not degree(G, v) == k: return False return True
def number_of_nodes_of_degree_k(G, k): """Return the number of nodes of the graph with degree equal to k. Parameters ---------- G : NetworkX graph An undirected graph. k : int A positive integer. Returns ------- int The number of nodes in the graph with degree equal to k. See Also -------- number_of_leaves, number_of_min_degree_nodes, number_of_max_degree_nodes Examples -------- >>> G = nx.path_graph(3) # Path on 3 nodes >>> nx.number_of_nodes_of_degree_k(G, 1) 2 """ return sum(1 for v in nodes(G) if degree(G, v) == k)
def degree_sequence(G): """Return the degree sequence of G. The degree sequence of a graph is the sequence of degrees of the nodes in the graph. Parameters ---------- G : NetworkX graph An undirected graph. Returns ------- list The degree sequence of the graph. Examples -------- >>> G = nx.path_graph(3) # Path on 3 nodes >>> nx.degree_sequence(G) [1, 2, 1] """ return [degree(G, v) for v in nodes(G)]
def closed_neighborhood_degree_list(G, nbunch): # TODO: Add documentation return [degree(G, u) for u in closed_neighborhood(G, nbunch)]