In [6]:
import numpy as np
import networkx as nx
import matplotlib.pylab as plt
from __future__ import division
%matplotlib inline
In [24]:
#create an ER random graph with 10 nodes and probability of connection = 0.5
g = nx.erdos_renyi_graph(10, 0.5)
In [25]:
#add a node
g.add_node(12)
#add a new node "A" and connect it to node 12
g.add_edge(12,"A")
In [26]:
#connected components
nx.connected_components(g)
Out[26]:
In [27]:
#dictionary of nodes and degrees
g.degree()
Out[27]:
In [28]:
#clustering coefficient
nx.clustering(g)
Out[28]:
In [29]:
#example betweenness centrality
nx.betweenness_centrality(g)
Out[29]:
In [30]:
#random walks
#g = nx.DiGraph()
#g.add_edges_from([(1,3),(2,1),(2,4),(3,2),(3,4),(4,3),(5,1),(5,3)])
#visualize the graph
#nx.draw(g)
In [31]:
#getting adjacency matrix
M = nx.adjacency_matrix(g)
print(M)
In [32]:
#iterating
#for i in range(5):
# if (np.sum(M[i]) > 0):
#M[i] = M[i]/np.sum(M[i])
In [33]:
#print(M)
In [36]:
#computing triangles
N = nx.triangles(g)
print(N)
In [38]:
#transitivity
O = nx.transitivity(g)
print(O)
In [39]:
#node clustering co-efficient
P = nx.clustering(g)
print(P)
In [41]:
#average clustering co-efficient of nodes
Q = nx.average_clustering(g)
print(Q)
In [42]:
#squares clustering co-efficient of nodes
R = nx.square_clustering(g)
print(R)
In [44]:
#k-components
S = nx.k_components(g)
print(S)
In [ ]: