In [198]:
import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nv
In [199]:
G = nx.barbell_graph(m1=8, m2=1)
In [200]:
nx.draw(G)
plt.show()
In [201]:
deg_cent = nx.degree_centrality(G)
deg_cent
Out[201]:
In [202]:
for n in G.nodes():
node = G.node[n]
node['degree'] = nx.degree(G,n)
node['dc'] = deg_cent[n]
In [203]:
a = nv.ArcPlot(G,node_order='degree',node_color='dc')
a.draw()
plt.show()
In [204]:
betw_cent = nx.betweenness_centrality(G)
betw_cent
Out[204]:
In [205]:
for n in G.nodes():
G.node[n]['bc'] = betw_cent[n]
In [206]:
a = nv.ArcPlot(G,node_order='degree',node_color='bc')
a.draw()
plt.show()
In [207]:
close_cent = nx.closeness_centrality(G)
close_cent
Out[207]:
In [208]:
for n in G.nodes():
G.node[n]['cc'] = close_cent[n]
In [209]:
a = nv.ArcPlot(G,node_order='degree',node_color='cc')
a.draw()
plt.show()
In [210]:
eig_cent = nx.eigenvector_centrality(G)
eig_cent
Out[210]:
In [211]:
for n in G.nodes():
G.node[n]['ec'] = eig_cent[n]
In [212]:
a = nv.ArcPlot(G,node_order='degree',node_color='ec')
a.draw()
plt.show()
In [213]:
cliques = list(nx.find_cliques(G))
cliques
Out[213]:
In [214]:
nx.draw(G.subgraph(cliques[0]))
plt.show()
In [215]:
nx.draw(G.subgraph(cliques[-1]))
plt.show()
In [216]:
T = nx.erdos_renyi_graph(n=20, p=.2)
print('N','=',len(T.nodes()),'|','E','=',len(T.edges()))
In [217]:
noc = nx.number_connected_components(G)
print('# of components',noc)
In [218]:
c = nv.CircosPlot(T)
c.draw()
plt.show()
In [219]:
cliques = sorted(nx.find_cliques(T), key = lambda x: len(x))
cliques
Out[219]:
In [220]:
largest_clique = cliques[-1]
largest_clique
Out[220]:
In [221]:
sub_nodes = set(largest_clique)
for node in largest_clique:
for neighbor in T.neighbors(node):
sub_nodes.add(neighbor)
sub_nodes
Out[221]:
In [222]:
sub_graph = T.subgraph(sub_nodes)
nx.draw(sub_graph)
plt.show()
In [223]:
for node in sub_graph.nodes():
sub_graph.node[node]['deg'] = sub_graph.degree(node)
a = nv.ArcPlot(sub_graph,node_order='deg')
a.draw()
plt.show()
In [224]:
from itertools import combinations
from collections import defaultdict
In [225]:
recommended = defaultdict(int)
for n,d in sub_graph.nodes(data=True):
for n1,n2 in combinations(sub_graph.neighbors(n),2):
if not sub_graph.has_edge(n1,n2):
recommended[(n1, n2)] += 1
recommended
Out[225]:
In [ ]: