def compute_cluster_coefficient(dataset): n = int(nsd.compute_node_number(dataset)) d = {} for x in xrange(n+1): d[x] = [] cc = [] with open(dataset, 'r') as f: for line in f.readlines(): i, j = [int(x) for x in line.strip().split(' ')] d[i].append(j) d[j].append(i) nv = 0 for k, v in d.iteritems(): if v == []: cc.append('Undefined') # cc of node without connection should be undefined elif len(v) == 1: cc.append(0.0) else: for u1 in v: for u2 in d[u1]: if u2 in v: nv += 1 nv = nv / 2.0 # (u1, u2) and (u2, u1) should be same, my algorithm counts twice,so here merge them cc.append((2 * nv)/ (len(v) * (len(v) - 1) )) # nv = 0 return cc
def compute_triangle_number(dataset): n = int(nsd.compute_node_number(dataset)) d = {} for x in xrange(n+1): d[x] = [] with open(dataset, 'r') as f: for line in f.readlines(): i, j = [int(x) for x in line.strip().split(' ')] d[i].append(j) d[j].append(i) nv = 0 ntr = 0 for k, v in d.iteritems(): if v == []: ntr += 0 elif len(v) == 1: ntr += 0 else: ntr += nCr(len(v), 2) for u1 in v: for u2 in d[u1]: if u2 in v: nv += 1 nv= nv / 2.0 # (u1, u2, u3) and (u2, u1, u3) and (u3, u2, u1) should be same, my algorithm counts triple,so here merge them return nv / ntr
def array_list(dataset): n = int(nsd.compute_node_number(dataset)) d = {} for x in xrange(n+1): d[x] = [] with open(dataset, 'r') as f: for line in f.readlines(): i, j = [int(x) for x in line.strip().split(' ')] d[i].append(j) d[j].append(i) return d