def minimize_pairwise_connectivity(G, S0): tmp = S0.copy() nodes = tmp.copy() node = next(iter(tmp)) tmp.discard(node) subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in tmp) connectivity = connectivity_metrics.pairwise_connectivity(subgraph) vertices = [node] minimum = connectivity while tmp: node = next(iter(tmp)) tmp.discard(node) nodes.discard(node) subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in nodes) connectivity = connectivity_metrics.pairwise_connectivity(subgraph) if connectivity < minimum: vertices = [node] minimum = connectivity elif connectivity == minimum: vertices.append(node) nodes.add(node) return vertices
def maximize_disconnected_pairs(G, k): tmp = set(G) nodes = tmp.copy() node = next(iter(tmp)) tmp.discard(node) subgraph = nx.subgraph_view(G, filter_node=lambda n: n in tmp) connectivity = comb( len(tmp), k) - connectivity_metrics.pairwise_connectivity(subgraph) vertices = [node] maximum = connectivity while tmp: node = next(iter(tmp)) tmp.discard(node) nodes.discard(node) subgraph = nx.subgraph_view(G, filter_node=lambda n: n in nodes) connectivity = comb( len(tmp), k) - connectivity_metrics.pairwise_connectivity(subgraph) if connectivity > maximum: vertices = [node] maximum = connectivity elif connectivity == maximum: vertices.append(node) nodes.add(node) return vertices
def get_pairwise_connectivity(exclude=None): if exclude is None: exclude = {} S = set(exclude) subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S) return connectivity_metric.pairwise_connectivity(subgraph)
def get_critical_nodes_ga(G, k): S, _ = cnp.genetic_algorithm(G, k) subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S) fitness = connectivity_metrics.pairwise_connectivity(subgraph) print(fitness) return fitness
def get_critical_nodes_greedy(G, k): S = cnp.greedy_cnp(G, k) subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S) fitness = connectivity_metrics.pairwise_connectivity(subgraph) print(fitness) return fitness
def _update(G, best_S, P, alpha): subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in best_S) metric = connectivity_metric.pairwise_connectivity(subgraph) avg = 0 for S in P: avg += len(S.intersection(best_S)) avg /= len(P) try: gamma = (alpha * metric) / avg except: gamma = 1 return gamma
def fitness_function(S): subgraph = nx.subgraph_view(G, filter_node=lambda n: n not in S) metric = connectivity_metric.pairwise_connectivity(subgraph) commonalities = S.intersection(best_S) return metric + gamma * len(commonalities)