#NEWLINE# def clustering_coefficient_unweighted( user ) : #NEWLINE# #INDENT# '\n    The clustering coefficient of the user in the unweighted, undirected ego\n    network.\n\n    It is defined by counting the number of closed triplets including the current user:\n\n    .. math::\n        C = \\frac{2 * \\text{closed triplets}}{ \\text{degree} \\, (\\text{degree - 1})}\n\n    where ``degree`` is the degree of the current user in the network.\n    ' #NEWLINE# matrix = matrix_undirected_unweighted ( user ) #NEWLINE# closed_triplets = 0 #NEWLINE# for ( a , b ) in combinations ( xrange ( len ( matrix ) ) , 2 ) : #NEWLINE# #INDENT# ( a_b , a_c , b_c ) = ( matrix [ a ] [ b ] , matrix [ a ] [ 0 ] , matrix [ b ] [ 0 ] ) #NEWLINE# if ( ( a_b is not None ) and ( a_c is not None ) and ( b_c is not None ) ) : #NEWLINE# #INDENT# if ( ( a_b > 0 ) and ( a_c > 0 ) and ( b_c > 0 ) ) : #NEWLINE# #INDENT# closed_triplets += 1.0 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# d_ego = sum ( matrix [ 0 ] ) #NEWLINE# return ( ( ( 2 * closed_triplets ) / ( d_ego * ( d_ego - 1 ) ) ) if ( d_ego > 1 ) else 0 )