In [55]:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (6.0, 4.0)
matplotlib.rcParams['savefig.format'] = 'png'
In [58]:
G=nx.erdos_renyi_graph(50,0.1)
In [59]:
nx.draw(G)
In [60]:
nx.is_connected(G)
Out[60]:
In [61]:
components=list(nx.connected_components(G))
print(components)
In [64]:
colors=np.random.random(size=len(components))
node_color_dict={}
for idx, nodes in enumerate(components):
for n in nodes:
node_color_dict[n]=colors[idx]
nx.draw(G, node_color=[node_color_dict[n] for n in G.nodes()], pos=nx.layout.spring_layout(G))
In [37]:
huge_graph=nx.read_gpickle("../data/airport/big_airportnet.gpickle")
In [38]:
degs=list(huge_graph.degree().values())
In [39]:
plt.hist(degs)
plt.xlabel("degree", fontsize=30)
plt.ylabel("frequency", fontsize=30)
Out[39]:
In [40]:
cents=nx.centrality.betweenness_centrality(huge_graph)
In [66]:
sorted(cents.keys(), key=lambda x:cents[x])[-2]
Out[66]:
In [42]:
plt.hist(list(cents.values()), bins=10)
Out[42]:
Many real world networks tend to have these very few "hubs" with high centrality
In [43]:
J=nx.erdos_renyi_graph(8,0.4)
nx.draw(J)
In [69]:
print(nx.clustering(J))
pos = nx.spring_layout(J)
nx.draw(J, pos = pos)
nx.draw_networkx_labels(J, pos = pos)
Out[69]:
In [46]:
huge_clust=nx.clustering(huge_graph)
In [47]:
nx.draw(huge_graph, node_color=[huge_clust[n] for n in huge_graph.nodes()], pos=nx.layout.fruchterman_reingold_layout(huge_graph, scale=2.0, iterations=100))
In [48]:
plt.hist(list(huge_clust.values()))
Out[48]:
In [49]:
nx.average_shortest_path_length(huge_graph)
Out[49]:
In [50]:
size_array=range(50,1000,50)
avg_pathlens=[nx.average_shortest_path_length(nx.barabasi_albert_graph(n, 4)) for n in size_array]
In [51]:
plt.plot(size_array, avg_pathlens, 'go')
plt.xlabel("size", fontsize=30)
plt.ylabel("avg pathlengths", fontsize=30)
Out[51]:
In [52]:
plt.plot(np.log(np.array(size_array)), np.array(avg_pathlens), 'bo-')
plt.xlabel("log(size)", fontsize=30)
plt.ylabel("avg pathlengths", fontsize=30)
Out[52]:
In [73]:
J = nx.erdos_renyi_graph(20, 5.0/20.0)
nx.draw(J)
nx.diameter(J)
Out[73]:
In [76]:
T = nx.minimum_spanning_tree(J)
pos = nx.spring_layout(J)
nx.draw(J, pos = pos)
nx.draw_networkx_edges(T, edge_color='g', pos = pos, width = 5)
Out[76]:
In [ ]: