def approx_neighborhood_function_statistics(G, n_nodes, n_approx=64, approx_type=APPROX_BFS_IGRAPH): aG = convert_networkx_to_SNAP(G) print "convert to SNAP graph: DONE" # TEST (fixed) # s_Diam = 20 # youtube # diameter s_Diam (lowerbound) start = time.clock() if approx_type == APPROX_ANF: s_Diam = sn.GetAnfEffDiam(aG, False, 0.99, n_approx) elif approx_type == APPROX_BFS: s_Diam = sn.GetBfsFullDiam(aG, N_BFS, False) # elif approx_type == APPROX_BFS_IGRAPH: s_APD_i, s_EDiam_i, s_CL_i, s_Diam = bfs_samples(G) # _i: igraph else: print "Wrong <approx_type> !" print "compute s_Diam, elapsed :", time.clock() - start # average distance s_APD DistNbrsV = sn.TIntFltKdV() MxDist = int(math.ceil(s_Diam)) print "MxDist =", MxDist start = time.clock() sn.GetAnf(aG, DistNbrsV, MxDist, False, n_approx) # n_approx=32, 64... print "GetAnf, elapsed :", time.clock() - start # for item in DistNbrsV: # print item.Key(), "-", item.Dat() sum_APD = 0.0 dist_list = [] # list of pairs for item in DistNbrsV: dist_list.append([item.Key(), item.Dat()]) num_APD = dist_list[-1][1] # WAY 2 - compute s_EDiam from dist_list s_EDiam = 0 for i in range(len(dist_list)): if dist_list[i][1] >= 0.9 * num_APD: s_EDiam = dist_list[i][0] break for i in range(len(dist_list) - 1, 1, -1): # do not subtract [0] from [1] ! dist_list[i][1] = dist_list[i][1] - dist_list[i - 1][ 1] # compute differences sum_APD += dist_list[i][0] * dist_list[i][1] s_APD = sum_APD / num_APD print "num_APD =", num_APD # for s_PDD print "s_PDD :" d_list = [] for item in dist_list: # print item[0], "-", item[1] d_list.append(item[1]) print d_list # WAY 1 - effective diameter s_EDiam ( SNAP) # start = time.clock() # if approx_type == APPROX_ANF: # s_EDiam = sn.GetAnfEffDiam(aG, False, 0.9, n_approx) # 90% # elif approx_type == APPROX_BFS: # s_EDiam = sn.GetBfsEffDiam(aG, 1000, False) # else: # print "Wrong <approx_type> !" # print "compute s_EDiam, elapsed :", time.clock() - start # connectivity length s_CL sum_CL = 0.0 for item in dist_list: if item[0] > 0: sum_CL += item[1] / item[0] # s_CL = n_nodes*(n_nodes-1)/sum_CL s_CL = num_APD / sum_CL # return s_APD, float(s_EDiam), s_CL, float(s_Diam), s_APD_i, float( s_EDiam_i), s_CL_i, dist_list
''' Created on Mar 24, 2014 @author: huunguye ''' import snap ################## Graph = snap.GenRndGnm(snap.PNGraph, 100, 1000) SrcNId = 0 DistNbrsV = snap.TIntFltKdV() snap.GetAnf(Graph, SrcNId, DistNbrsV, 3, False, 32) for item in DistNbrsV: print item.Dat() print "DONE" UGraph = snap.GenRndGnm(snap.PUNGraph, 100, 1000) SrcNId = 0 DistNbrsV = snap.TIntFltKdV() snap.GetAnf(UGraph, SrcNId, DistNbrsV, 3, False, 32) for item in DistNbrsV: print item.Dat() print "DONE" Network = snap.GenRndGnm(snap.PNEANet, 100, 1000) SrcNId = 0 DistNbrsV = snap.TIntFltKdV() snap.GetAnf(Network, SrcNId, DistNbrsV, 3, False, 32) for item in DistNbrsV:
def get_approximate_neighborhood(G, n, max_hops, directed=False, approx=32): DistNbrsV = snap.TIntFltKdV() snap.GetAnf(G, n, DistNbrsV, max_hops, directed, approx)
def hopSNAP( graph, maxnum = 10, qual = 1024): DistNbrsV = snap.TIntFltKdV() snap.GetAnf(graph, DistNbrsV, maxnum, False, qual) # adjust the approximation quality if too slow cnt = [ dn.Dat() for dn in DistNbrsV ] hop = [ dn.Key() for dn in DistNbrsV ] return [ hop, cnt ]