Example #1
0
    def test_subgraph_centrality_big_graph(self):
        g199 = nx.complete_graph(199)
        g200 = nx.complete_graph(200)

        comm199 = nx.subgraph_centrality(g199)
        comm199_exp = nx.subgraph_centrality_exp(g199)

        comm200 = nx.subgraph_centrality(g200)
        comm200_exp = nx.subgraph_centrality_exp(g200)
Example #2
0
    def test_subgraph_centrality_big_graph(self):
        g199 = nx.complete_graph(199)
        g200 = nx.complete_graph(200)

        comm199 = nx.subgraph_centrality(g199)
        comm199_exp = nx.subgraph_centrality_exp(g199)

        comm200 = nx.subgraph_centrality(g200)
        comm200_exp = nx.subgraph_centrality_exp(g200)
Example #3
0
G.add_edges_from([
    ("a", "b"),
    ("b", "c"),
    ("c", "d"),
    ("d", "b"),
])

nx.draw_networkx(G)

V = G.nodes()
n = len(V)
E = G.edges()
m = len(E)

print("n", n)
print("nodos", V)
print("m", m)
print("aristas", E)
print("excentricidad (distancia máxima a otros nodos)", nx.eccentricity(G))
print("centro (nodos con mínima excentricidad)", nx.center(G))
print("periferia (nodos con máxima excentricidad)", nx.periphery(G))
print("radio:", nx.radius(G))
print("centrality (networkx)", nx.subgraph_centrality_exp(G))
cc = nx.closeness_centrality(G)
print("cercanía (networkx)", cc)
for (k, v) in cc.items():
    cc[k] = v / (n - 1)
print("cercanía (libro Barrat)", cc)
print("intermediación (networkx)", nx.betweenness_centrality(G))

plt.show()
    def compute_subgraph_center(self, subgraph):

        if self.method == 'betweenness_centrality':
            d = nx.betweenness_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'betweenness_centrality_subset':
            d = nx.betweenness_centrality_subset(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'information_centrality':
            d = nx.information_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'local_reaching_centrality':

            d = {}
            for n in self.G.nodes():
                d[n] = nx.local_reaching_centrality(self.G, n, weight='weight')

            center = max(d, key=d.get)

        elif self.method == 'voterank':
            d = nx.voterank(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'percolation_centrality':
            d = nx.percolation_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'subgraph_centrality':
            d = nx.subgraph_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'subgraph_centrality_exp':
            d = nx.subgraph_centrality_exp(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'estrada_index':
            d = nx.estrada_index(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'second_order_centrality':
            d = nx.second_order_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'eigenvector_centrality':

            d = nx.eigenvector_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)
        elif self.method == 'load_centrality':

            d = nx.load_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'closeness_centrality':
            d = nx.closeness_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'current_flow_closeness_centrality':
            d = nx.current_flow_closeness_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'current_flow_betweenness_centrality':
            d = nx.current_flow_betweenness_centrality(subgraph,
                                                       weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'current_flow_betweenness_centrality_subset':
            d = nx.current_flow_betweenness_centrality_subset(subgraph,
                                                              weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'approximate_current_flow_betweenness_centrality':
            d = nx.approximate_current_flow_betweenness_centrality(
                subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'harmonic_centrality':
            d = nx.harmonic_centrality(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'page_rank':

            d = nx.pagerank(subgraph, weight='weight')
            center = max(d, key=d.get)

        elif self.method == 'hits':

            d = nx.hits(subgraph)
            center = max(d, key=d.get)

        elif self.method == 'katz_centrality':
            d = nx.katz_centrality(subgraph, weight='weight')
            center = max(d, key=d.get)

        else:
            new_centers = nx.center(subgraph)

            # new_centers gives a list of centers and here we just pick one randomly --not good for stability
            # to do : find a better way to choose the center--make it stable

            index = random.randint(0, len(new_centers) - 1)

            center = new_centers[index]
        return center
Example #5
0
def mergegraph(graphs, pos_old, labels_old, edge_prob=0.3, edge_num=0.4):
    nodes = []
    edges = []
    pos = {}
    node_cnt = 0
    val = 0.9
    shift_value = [[-val, val], [val, val], [-val, -val], [val, -val]]

    comm_lables = []

    for i, g in enumerate(graphs):
        tmp_nodes = list(g.nodes())
        tmp_edges = list(g.edges())

        comm_lables += [i] * len(tmp_nodes)

        node_map = {k: node_cnt + i for k, i in enumerate(tmp_nodes)}
        node_cnt += len(tmp_nodes)

        new_nodes = [node_map[n] for n in tmp_nodes]
        new_edges = [(node_map[u], node_map[v]) for u, v in tmp_edges]

        for k, v in pos_old[i].items():
            pos_old[i][k][0] += shift_value[i][0]
            pos_old[i][k][1] += shift_value[i][1]

        new_pos = {node_map[n]: v for n, v in pos_old[i].items()}

        nodes += new_nodes
        edges += new_edges
        pos.update(new_pos)

    G = nx.DiGraph()
    G.add_edges_from(edges)

    random.shuffle(nodes)
    l = int(edge_num * len(nodes))
    u = nodes[0:l]
    random.shuffle(nodes)
    v = nodes[0:l]

    for s, t in zip(u, v):
        if random.random() < edge_prob:
            G.add_edge(s, t)
            G.add_edge(t, s)
    nodes_deg = [G.degree[i] for i in G.nodes()]

    centrality = nx.closeness_centrality(G)
    labels_central = get_labels(centrality)
    print('centrality done!')

    inf_cent = nx.information_centrality(G.to_undirected())
    labels_inf_central = get_labels(inf_cent)
    print('info centrality done!')

    betweenness = nx.betweenness_centrality(G.to_undirected())
    labels_betweenness = get_labels(betweenness)
    print('betweenness done!')

    loads = nx.load_centrality(G.to_undirected())
    labels_load = get_labels(loads)
    print('load centrality done!')

    cmm_bet = nx.communicability_betweenness_centrality(G.to_undirected())
    labels_cmm_bet = get_labels(cmm_bet)
    print('commu betweenness done!')

    sce = nx.subgraph_centrality_exp(G.to_undirected())
    labels_sce = get_labels(sce)
    print('subgraph centrality done!')

    harm = nx.harmonic_centrality(G.to_undirected())
    labels_harm = get_labels(harm)
    print('harmonic done!')

    lrc = {
        v: nx.local_reaching_centrality(G.to_undirected(), v)
        for v in G.nodes()
    }
    labels_lrc = get_labels(lrc)
    print('lrc done!')

    unq_lbl = np.unique(nodes_deg)
    lbl_map = {unq_lbl[i]: i for i in range(len(unq_lbl))}
    labels = [lbl_map[k] for k in nodes_deg]
    return G, pos, labels, comm_lables, labels_central, labels_inf_central, labels_betweenness, labels_load, labels_cmm_bet, labels_sce, labels_harm, labels_lrc