import networkx as nx
import matplotlib.pyplot as plt
import pyvis as pv
import pandas as pd
def display_html(filename):
from IPython.core.display import display, HTML
with open(filename, 'r') as f:
display(HTML(f.read()))
G = nx.les_miserables_graph()
print(nx.info(G))
print('directed' if nx.is_directed(G) else 'undirected')
print('connected' if nx.is_connected(G) else 'not connected')
pos = nx.spring_layout(G)
nx.draw(G, pos=pos, node_color='green', alpha=0.5, width=0.5)
net = pv.network.Network()
net.from_nx(G)
net.show_buttons(filter_=['physics'])
net.show('graph.html')
display_html('graph.html')
nodes_degree = nx.degree(G)
display(nodes_degree)
degree_list = []
for (n,d) in nodes_degree:
degree_list.append(d)
av_degree = sum(degree_list) / len(degree_list)
print('The average degree is %s' % av_degree)
plt.hist(degree_list,label='Degree Distribution')
plt.axvline(av_degree,color='r',linestyle='dashed',label='Average Degree')
plt.legend()
plt.ylabel('Number of Nodes')
plt.title('Graph Node Degree')
degree_centrality = nx.degree_centrality(G)
display(degree_centrality)
closeness_centrality = nx.closeness_centrality(G)
display(closeness_centrality)
node_betweenness_centrality = nx.betweenness_centrality(G)
display(node_betweenness_centrality)
edge_betweenness_centrality = nx.edge_betweenness_centrality(G)
display(edge_betweenness_centrality)
centralities = {
'node_betweenness_centrality': node_betweenness_centrality,
'closeness_centrality': closeness_centrality,
'degree_centrality': degree_centrality,
}
centralities_df = pd.DataFrame.from_dict(centralities)
display(centralities_df)
bridges = []
for bridge in nx.bridges(G):
bridges.append(bridge)
non_bridges = G.edges - bridges
print('we have %s bridges in the graph' % len(bridges))
display(bridges)
nx.draw_networkx_edges(G, pos, edgelist=bridges, style='solid', width=1.5, edge_color='red')
nx.draw_networkx_edges(G, pos, edgelist=non_bridges, style='solid', width=0.7, edge_color='black')
nx.draw_networkx_nodes(G, pos, node_size=100, alpha=0.2)
display('the red edges are the bridges')
density = nx.density(G)
print('The edge density is %s' % density)
triadic_closure = nx.transitivity(G)
print("Triadic closure:", triadic_closure)
def display_communities(communities):
print("we found %s communities" % len(communities))
colors = ['red','green','blue','black','orange', 'yellow', 'purple']
counter = 0
for community in communities:
counter += 1
print("community_%s is:" % counter)
print(', '.join(community), '\n')
nx.draw_networkx_nodes(G, pos, nodelist=community, node_color=colors.pop(), alpha=0.5)
nx.draw_networkx_edges(G, pos, style='dashed', width=0.2)
communities = nx.algorithms.community.modularity_max.greedy_modularity_communities(G)
display_communities(communities)
modularity = nx.algorithms.community.quality.modularity(G, communities)
print("The modularity of this communities is: %s" % modularity)
partition_quality = nx.algorithms.community.quality.partition_quality(G, communities)
print("The coverage of this communities is: %s \nand the perfomance is: %s" % partition_quality)
k_clique_communities = []
for community in nx.algorithms.community.kclique.k_clique_communities(G, 5):
k_clique_communities.append(community)
display_communities(k_clique_communities)
fluid_communities = []
for community in nx.algorithms.community.asyn_fluid.asyn_fluidc(G, 6):
fluid_communities.append(community)
display_communities(fluid_communities)
partition_quality = nx.algorithms.community.quality.partition_quality(G, fluid_communities)
print("The coverage of this communities is: %s \nand the perfomance is: %s" % partition_quality)