def square_clustering(G, nodes=None):
 if (nodes is None):
  node_iter = G
 else:
  node_iter = G.nbunch_iter(nodes)
 clustering = {}
 for v in node_iter:
  clustering[v] = 0
  potential = 0
  for (u, w) in combinations(G[v], 2):
   squares = len(((set(G[u]) & set(G[w])) - set([v])))
   clustering[v] += squares
   degm = (squares + 1)
   if (w in G[u]):
    degm += 1
   potential += (((len(G[u]) - degm) * (len(G[w]) - degm)) + squares)
  if (potential > 0):
   clustering[v] /= potential
 if (nodes in G):
  return clustering[nodes]
 return clustering

def square_clustering ( G , nodes = None ) : if ( nodes is None ) : node_iter = G else : node_iter = G . nbunch_iter ( nodes ) clustering = { } for v in node_iter : clustering [ v ] = 0 potential = 0 for ( u , w ) in combinations ( G [ v ] , 2 ) : squares = len ( ( ( set ( G [ u ] ) & set ( G [ w ] ) ) - set ( [ v ] ) ) ) clustering [ v ] += squares degm = ( squares + 1 ) if ( w in G [ u ] ) : degm += 1 potential += ( ( ( len ( G [ u ] ) - degm ) * ( len ( G [ w ] ) - degm ) ) + squares ) if ( potential > 0 ) : clustering [ v ] /= potential if ( nodes in G ) : return clustering [ nodes ] return clustering
compute the squares clustering coefficient for nodes .