import networkx as nx from partA import read_graph import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt G = read_graph('partA_egofb.txt') def ASPL_task(G): p = nx.all_pairs_shortest_path_length(G) N = G.number_of_nodes() APL = 0. for v in p: for u in p[v]: APL += p[u][v] APL = APL / N / (N-1) print("Average Shortest Path Length: %f" % APL) def CC_task(G): CC = nx.closeness_centrality(G) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.hist([CC[i] for i in CC], 20, normed=True) fig.savefig('egofb_cc.png') sorted_nodes = sorted(CC, key=lambda node: CC[node], reverse = True) for i in range(10): print(sorted_nodes[i], CC[sorted_nodes[i]])
import networkx as nx from partA import read_graph import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt G = read_graph('partA_hepth.txt') def ASPL_task(G): lwcc = None MM = 0 for g in nx.weakly_connected_component_subgraphs(G): if MM < g.number_of_nodes(): lwcc = g MM = g.number_of_nodes() sd = 0 sum_pairs = 0 n = 0 for node in lwcc: x = nx.shortest_path_length(lwcc, node) sd += sum(x[i] for i in x) sum_pairs += (len(x) - 1) n += 1 if n % 100 ==0: print(n) print(sd / n / (n-1)) print(sum_pairs) print(n)
degree = d[node] if degree not in distri: distri[degree] = 0 distri[degree] += 1 degree = sorted(distri) fraction = [distri[x] for x in degree] return (degree, fraction) def plot_degree_distribution(G): (degree, fraction) = degree_distribution(G) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(degree, fraction, 'o') ax.set_xscale('log') ax.set_yscale('log') return fig if __name__ == '__main__': egofb = read_graph('partA_egofb.txt') for xmin in range(1, 30): print('egofb:', xmin, count_alpha(egofb, xmin)) plot_degree_distribution(egofb).savefig('egofb_dd.png') hepth = read_graph('partA_hepth.txt') hepthu = hepth.to_undirected() for xmin in range(1, 30): print('hepth:', xmin, count_alpha(hepthu, xmin)) plot_degree_distribution(hepthu).savefig('hepth_dd.png')