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]:
<generator object connected_components at 0x114d84db0>

In [27]:
#dictionary of nodes and degrees
g.degree()


Out[27]:
{0: 5, 1: 6, 2: 4, 3: 4, 4: 5, 5: 5, 6: 4, 7: 3, 8: 4, 9: 6, 12: 1, 'A': 1}

In [28]:
#clustering coefficient
nx.clustering(g)


Out[28]:
{0: 0.5,
 1: 0.4666666666666667,
 2: 0.5,
 3: 0.5,
 4: 0.5,
 5: 0.4,
 6: 0.5,
 7: 0.3333333333333333,
 8: 0.3333333333333333,
 9: 0.4,
 12: 0.0,
 'A': 0.0}

In [29]:
#example betweenness centrality
nx.betweenness_centrality(g)


Out[29]:
{0: 0.03636363636363636,
 1: 0.057575757575757565,
 2: 0.02121212121212121,
 3: 0.021212121212121213,
 4: 0.04545454545454544,
 5: 0.05757575757575757,
 6: 0.02424242424242424,
 7: 0.01212121212121212,
 8: 0.036363636363636355,
 9: 0.08787878787878788,
 12: 0.0,
 'A': 0.0}

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)


  (0, 1)	1
  (0, 3)	1
  (0, 4)	1
  (0, 8)	1
  (0, 9)	1
  (1, 0)	1
  (1, 4)	1
  (1, 5)	1
  (1, 6)	1
  (1, 8)	1
  (1, 9)	1
  (2, 3)	1
  (2, 5)	1
  (2, 6)	1
  (2, 9)	1
  (3, 0)	1
  (3, 2)	1
  (3, 5)	1
  (3, 9)	1
  (4, 0)	1
  (4, 1)	1
  (4, 6)	1
  (4, 7)	1
  (4, 9)	1
  (5, 1)	1
  (5, 2)	1
  (5, 3)	1
  (5, 6)	1
  (5, 8)	1
  (6, 1)	1
  (6, 2)	1
  (6, 4)	1
  (6, 5)	1
  (7, 4)	1
  (7, 8)	1
  (7, 9)	1
  (8, 0)	1
  (8, 1)	1
  (8, 5)	1
  (8, 7)	1
  (9, 0)	1
  (9, 1)	1
  (9, 2)	1
  (9, 3)	1
  (9, 4)	1
  (9, 7)	1
  (10, 11)	1
  (11, 10)	1

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)


{0: 5, 1: 7, 2: 3, 3: 3, 4: 5, 5: 4, 6: 3, 7: 1, 8: 2, 9: 6, 12: 0, 'A': 0}

In [38]:
#transitivity
O = nx.transitivity(g)
print(O)


0.4482758620689655

In [39]:
#node clustering co-efficient
P = nx.clustering(g)
print(P)


{0: 0.5, 1: 0.4666666666666667, 2: 0.5, 3: 0.5, 4: 0.5, 5: 0.4, 6: 0.5, 7: 0.3333333333333333, 8: 0.3333333333333333, 9: 0.4, 12: 0.0, 'A': 0.0}

In [41]:
#average clustering co-efficient of nodes
Q = nx.average_clustering(g)
print(Q)


0.36944444444444446

In [42]:
#squares clustering co-efficient of nodes
R = nx.square_clustering(g)
print(R)


{0: 0.28846153846153844, 1: 0.2261904761904762, 2: 0.22580645161290322, 3: 0.2222222222222222, 4: 0.2708333333333333, 5: 0.20833333333333334, 6: 0.2222222222222222, 7: 0.46153846153846156, 8: 0.3103448275862069, 9: 0.23943661971830985, 12: 0.0, 'A': 0.0}

In [44]:
#k-components
S = nx.k_components(g)
print(S)


{4: [{0, 1, 2, 3, 4, 5, 6, 9}], 3: [{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}], 2: [{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}], 1: [{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {'A', 12}]}

In [ ]: