Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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)
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
    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)