def dispersion(G, u=None, v=None, normalized=True, alpha=1.0, b=0.0, c=0.0):
 def _dispersion(G_u, u, v):
  "dispersion for all nodes 'v' in a ego network G_u of node 'u'"
  u_nbrs = set(G_u[u])
  ST = set((n for n in G_u[v] if (n in u_nbrs)))
  set_uv = set([u, v])
  possib = combinations(ST, 2)
  total = 0
  for (s, t) in possib:
   nbrs_s = (u_nbrs.intersection(G_u[s]) - set_uv)
   if (not (t in nbrs_s)):
    if nbrs_s.isdisjoint(G_u[t]):
     total += 1
  embededness = len(ST)
  if normalized:
   if ((embededness + c) != 0):
    norm_disp = (((total + b) ** alpha) / (embededness + c))
   else:
    norm_disp = ((total + b) ** alpha)
   dispersion = norm_disp
  else:
   dispersion = total
  return dispersion
 if (u is None):
  if (v is None):
   results = dict(((n, {}) for n in G))
   for u in G:
    for v in G[u]:
     results[u][v] = _dispersion(G, u, v)
  else:
   results = dict.fromkeys(G[v], {})
   for u in G[v]:
    results[u] = _dispersion(G, v, u)
 elif (v is None):
  results = dict.fromkeys(G[u], {})
  for v in G[u]:
   results[v] = _dispersion(G, u, v)
 else:
  results = _dispersion(G, u, v)
 return results

def dispersion ( G , u = None , v = None , normalized = True , alpha = 1.0 , b = 0.0 , c = 0.0 ) : def _dispersion ( G_u , u , v ) : u_nbrs = set ( G_u [ u ] ) ST = set ( ( n for n in G_u [ v ] if ( n in u_nbrs ) ) ) set_uv = set ( [ u , v ] ) possib = combinations ( ST , 2 ) total = 0 for ( s , t ) in possib : nbrs_s = ( u_nbrs . intersection ( G_u [ s ] ) - set_uv ) if ( not ( t in nbrs_s ) ) : if nbrs_s . isdisjoint ( G_u [ t ] ) : total += 1 embededness = len ( ST ) if normalized : if ( ( embededness + c ) != 0 ) : norm_disp = ( ( ( total + b ) ** alpha ) / ( embededness + c ) ) else : norm_disp = ( ( total + b ) ** alpha ) dispersion = norm_disp else : dispersion = total return dispersion if ( u is None ) : if ( v is None ) : results = dict ( ( ( n , { } ) for n in G ) ) for u in G : for v in G [ u ] : results [ u ] [ v ] = _dispersion ( G , u , v ) else : results = dict . fromkeys ( G [ v ] , { } ) for u in G [ v ] : results [ u ] = _dispersion ( G , v , u ) elif ( v is None ) : results = dict . fromkeys ( G [ u ] , { } ) for v in G [ u ] : results [ v ] = _dispersion ( G , u , v ) else : results = _dispersion ( G , u , v ) return results
compute the *dispersion* of polynomials .