def W(G, C):
    """Density metric W function. This is essentially the scoring function which we use to define our communitites.
    The formula used to calculate density :  W = 2 * e_c / c ; 
    where e_c is number of edges with both endpoints in cluster 'C' and c is total number of nodes in cluster"""
    e_c = 0
    c = len(C)

    community = G.subgraph(C)
    for e in community.edges():
        if (e[0] in C) and (e[1] in C):
            e_c = e_c + 1

    if c > 0:
        return (2*e_c/c)
    else:
        return 0