def min_connected_k_forcing_set(G, k): """Return a smallest connected k-forcing set in *G*. The method used 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 connected k-forcing set in *G*. """ # 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 start search if graph is connected if not is_connected(G): return None for i in range(1, G.order() + 1): for S in combinations(G.nodes(), i): if is_connected_k_forcing_set(G, S, k): return list(S)
def is_connected_k_forcing_set(G, nodes, k): """Return whether or not the nodes in `nodes` comprise a connected k-forcing set in *G*. A *connected k-forcing set* in a graph *G* is a k-forcing set that induces a connected subgraph. Parameters ---------- G : NetworkX graph An undirected graph. nodes : list, set An iterable container of nodes in G. k : int A positive integer. Returns ------- boolean True if the nodes in nbunch comprise a connected k-forcing set in *G*. False otherwise. """ # 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.") S = set(n for n in nodes if n in G) H = G.subgraph(S) return is_connected(H) and is_k_forcing_set(G, S, k)
def is_connected_k_dominating_set(G, nodes, k): """Return whether or not *nodes* is a connected *k*-dominating set of *G*. A set *D* is a *connected k-dominating set* of *G* is *D* is a *k*-dominating set in *G* and the subgraph of *G* induced by *D* is a connected graph. Parameters ---------- G : NetworkX graph An undirected graph. nodes : list, set An iterable container of nodes in G. k : int A positive integer Returns ------- boolean True if *nbunch* is a connected *k*-dominating set in *G*, and false otherwise. """ # 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.") S = set(n for n in nodes if n in G) H = G.subgraph(S) return is_connected(H) and is_k_dominating_set(G, S, k)
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)
def chromatic_number_ram_rama(G): """Return the chromatic number of G. The *chromatic number* of a graph G is the size of a mininum coloring of the nodes in G such that no two adjacent nodes have the same color. The method for computing the chromatic number is an implementation of the algorithm discovered by Ram and Rama. Parameters ---------- G : NetworkX graph An undirected graph. Returns ------- int The chromatic number of G. References ---------- A.M. Ram, R. Rama, An alternate method to find the chromatic number of a finite, connected graph, *arXiv preprint arXiv:1309.3642*, (2013) """ if not is_connected(G): raise TypeError("Invalid graph: not connected") if is_complete_graph(G): return G.order() # get list of pairs of non neighbors in G N = [ list(p) for p in pairs_of_nodes(G) if not are_neighbors(G, p[0], p[1]) ] # get a pair of non neighbors who have the most common neighbors num_common_neighbors = list(map(lambda p: len(common_neighbors(G, p)), N)) P = N[np.argmax(num_common_neighbors)] # Collapse the nodes in P and repeat the above process H = G.copy() contract_nodes(H, P) return chromatic_number(H)