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')
Graph with 77 nodes and 254 edges
undirected
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)
The average degree is 6.597402597402597
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)
node_betweenness_centralityfloat64
0.0 - 0.5699890527836184
closeness_centralityfloat64
0.25675675675675674 - 0.6440677966101694
Valjean
0.5699890527836184
0.6440677966101694
Gavroche
0.16511250242584766
0.5135135135135135
Marius
0.132032488621946
0.5314685314685315
Javert
0.05433155966478436
0.5170068027210885
Thenardier
0.07490122123424225
0.5170068027210885
Fantine
0.12964454098819422
0.46060606060606063
Enjolras
0.0425533568221771
0.4810126582278481
Bossuet
0.03075365017995782
0.475
Courfeyrac
0.00526702988198833
0.4
Bahorel
0.0021854883087570067
0.39378238341968913
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')
we have 18 bridges in the graph
density = nx.density(G)
print('The edge density is %s' % density)
The edge density is 0.08680792891319207
triadic_closure = nx.transitivity(G)
print("Triadic closure:", triadic_closure)
Triadic closure: 0.49893162393162394
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)
we found 5 communities
community_1 is:
Marguerite, Labarre, MmeMagloire, CountessDeLo, MotherInnocent, Count, Geborand, Napoleon, Valjean, Scaufflaire, Chenildieu, Fauchelevent, Gervais, MlleBaptistine, Cravatte, Isabeau, Champtercier, Gribier, MmeDeR, Myriel, Brevet, Champmathieu, Woman1, OldMan, Judge, Cochepaille
community_2 is:
MmeHucheloup, Joly, Mabeuf, Child2, Prouvaire, MmeBurgon, Combeferre, Grantaire, Jondrette, Gavroche, Courfeyrac, Feuilly, MotherPlutarch, Bahorel, Bossuet, Enjolras, Child1
community_3 is:
Perpetue, Gueulemer, Babet, MmeThenardier, Anzelma, Eponine, Javert, Brujon, Bamatabois, Boulatruelle, Fantine, Montparnasse, Claquesous, Simplice, Thenardier
community_4 is:
Gillenormand, Marius, MlleVaubois, Pontmercy, BaronessT, Magnon, Tholomyes, MmePontmercy, LtGillenormand, Toussaint, Woman2, Cosette, MlleGillenormand
community_5 is:
Dahlia, Listolier, Zephine, Favourite, Fameuil, Blacheville
The modularity of this communities is: 0.4729424449732302
The coverage of this communities is: 0.7322834645669292
and the perfomance is: 0.815105946684894
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)
we found 5 communities
community_1 is:
MmeHucheloup, Joly, Mabeuf, Prouvaire, Combeferre, Grantaire, Valjean, Marius, Gavroche, Courfeyrac, Feuilly, Bahorel, Bossuet, Enjolras
community_2 is:
Listolier, Favourite, Tholomyes, Dahlia, Zephine, Fameuil, Fantine, Blacheville
community_3 is:
Gillenormand, Marius, LtGillenormand, Valjean, Cosette, MlleGillenormand
community_4 is:
Brevet, Champmathieu, Bamatabois, Valjean, Chenildieu, Judge, Cochepaille
community_5 is:
Gueulemer, MmeThenardier, Babet, Claquesous, Valjean, Cosette, Thenardier, Gavroche, Eponine, Brujon, Fantine, Montparnasse, Javert
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)
we found 6 communities
community_1 is:
MmeBurgon, Jondrette
community_2 is:
Gervais, Isabeau, Labarre, MmeDeR, Brevet, Champmathieu, Bamatabois, Woman1, Valjean, Scaufflaire, Chenildieu, Judge, Cochepaille
community_3 is:
Gillenormand, BaronessT, Mabeuf, Prouvaire, Combeferre, MlleVaubois, Enjolras, Bahorel, MlleGillenormand, Child1, MmeHucheloup, Joly, Child2, Grantaire, Marius, Gavroche, Courfeyrac, MotherPlutarch, LtGillenormand, Bossuet, Feuilly
community_4 is:
MlleBaptistine, Cravatte, Champtercier, MmeMagloire, CountessDeLo, Myriel, Count, Geborand, Napoleon, OldMan
community_5 is:
Thenardier, Gueulemer, Pontmercy, MmeThenardier, Babet, Magnon, Eponine, Anzelma, Brujon, MmePontmercy, Boulatruelle, Montparnasse, Toussaint, Claquesous, Woman2, Cosette, Javert
community_6 is:
Listolier, Marguerite, Perpetue, Favourite, Tholomyes, Gribier, MotherInnocent, Dahlia, Zephine, Fantine, Fameuil, Blacheville, Simplice, Fauchelevent
The coverage of this communities is: 0.7874015748031497
and the perfomance is: 0.8581681476418318