def in_degree_distribution(G): result = None #REMOVER in_degree = {} out_degree = {} v_in_d = [] v_out_d = [] v_degrees = [] InDegV = snap.TIntPrV() snap.GetNodeInDegV( G, InDegV ) #Retorna o id do vertice e o grau de entrada- inclusive se o grau for 0 for item in InDegV: node = item.GetVal1() degree = item.GetVal2() in_degree[node] = degree OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDegV) for item in OutDegV: node = item.GetVal1() degree = item.GetVal2() out_degree[node] = degree for k, v in in_degree.iteritems(): if k in out_degree: v_in_d.append(v) v_out_d.append(out_degree[k]) soma = v + out_degree[k] v_degrees.append(soma) return v_in_d, v_out_d, v_degrees #Retorna uma lista com in_degree e outra lista com out_degree, e mais uma com a soma dos graus de entrada e saída.
def in_out_degree_correlation(G): result = None #REMOVER in_degree = {} out_degree = {} v_in_d = [] v_out_d = [] InDegV = snap.TIntPrV() snap.GetNodeInDegV(G,InDegV) #Retorna o id do vertice e o grau de entrada- inclusive se o grau for 0 for item in InDegV: node = item.GetVal1() degree = item.GetVal2() in_degree[node] = degree OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDegV) for item in OutDegV: node = item.GetVal1() degree = item.GetVal2() out_degree[node] = degree for k,v in in_degree.iteritems(): if k in out_degree: v_in_d.append(v) v_out_d.append(out_degree[k]) result = pearsonr(v_in_d,v_out_d) #Retorna uma tupla (coef,p-value) return result[0] #Retorna apenas o coef
def _get_degree_in_graph(Graph, H, output_path): InDegV = snap.TIntPrV() snap.GetNodeInDegV(Graph, InDegV) InDeg_set = dict() for item in InDegV: username = H.GetKey(item.GetVal1()) InDeg = item.GetVal2() InDeg_set[username] = InDeg OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(Graph, OutDegV) OutDeg_set = dict() for item in OutDegV: username = H.GetKey(item.GetVal1()) OutDeg = item.GetVal2() OutDeg_set[username] = OutDeg dataset = list() tot = len(InDeg_set) num = 0 for username in InDeg_set: user_degree = dict() user_degree['username'] = username user_degree['in_degree'] = InDeg_set[username] user_degree['out_degree'] = OutDeg_set[username] profile_path = './data/Users/%s.json' % username if not os.path.exists(profile_path): continue with open(profile_path, 'r') as f: profile = json.load(f) in_set = set(profile['followers']) out_set = set(profile['following']) if user_degree['out_degree'] == 0: user_degree['balance'] = float(user_degree['in_degree']) / eps else: user_degree['balance'] = float(user_degree['in_degree']) / float( user_degree['out_degree']) bi = 0 for out_username in out_set: if out_username in in_set: try: ID = H.GetDat(out_username) if ID is not -1 and Graph.IsNode(ID): bi += 1 except Exception as e: print type(e) print e.args print e if user_degree['out_degree'] == 0: user_degree['reciprocity'] = float(bi) / eps else: user_degree['reciprocity'] = float(bi) / float( user_degree['out_degree']) dataset.append(user_degree) num += 1 print '%d/%d' % (num, tot) dataset = pd.DataFrame(dataset) dataset = dataset[[ 'username', 'in_degree', 'out_degree', 'balance', 'reciprocity' ]] dataset.to_csv(output_path, index=False, encoding='utf-8')
def get_robustness(file_path, LSCC_output_path, LWCC_output_path): frac_list = [ 0.0001, 0.001, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ] Graph, H = load_graph(file_path) InDegV = snap.TIntPrV() snap.GetNodeInDegV(Graph, InDegV) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(Graph, OutDegV) degree = dict() for item in InDegV: ID = item.GetVal1() InDeg = item.GetVal2() degree[ID] = InDeg for item in OutDegV: ID = item.GetVal1() OutDeg = item.GetVal2() degree[ID] += OutDeg sorted_degree = sorted(degree.items(), key=itemgetter(1), reverse=True) tot = len(sorted_degree) pos = [int(tot * frac) for frac in frac_list] print pos cur = 0 LSCC_robust = list() LWCC_robust = list() for i in range(tot): Graph.DelNode(sorted_degree[i][0]) if i == pos[cur] - 1: LSCC_frac = snap.GetMxSccSz(Graph) LWCC_frac = snap.GetMxWccSz(Graph) singleton_frac = 1.0 - 1.0 * snap.CntNonZNodes( Graph) / Graph.GetNodes() LSCC_robust.append({ 'removed': frac_list[cur], 'singleton': singleton_frac, 'middle': 1.0 - singleton_frac - LSCC_frac, 'LSCC': LSCC_frac }) LWCC_robust.append({ 'removed': frac_list[cur], 'singleton': singleton_frac, 'middle': 1.0 - singleton_frac - LWCC_frac, 'LWCC': LWCC_frac }) cur += 1 if cur >= len(pos): break LSCC_robust = pd.DataFrame(LSCC_robust) LSCC_robust = LSCC_robust[['removed', 'singleton', 'middle', 'LSCC']] LSCC_robust.to_csv(LSCC_output_path, index=False, encoding='utf-8') LWCC_robust = pd.DataFrame(LWCC_robust) LWCC_robust = LWCC_robust[['removed', 'singleton', 'middle', 'LWCC']] LWCC_robust.to_csv(LWCC_output_path, index=False, encoding='utf-8')
def computeDegreeCentrality(G, NodeAttributes): # # 1. Degree Centrality # Get In Degree and Out Degree for each node # InDegV = snap.TIntPrV() OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDegV) snap.GetNodeInDegV(G, InDegV) InDegreeList = [(item.GetVal1(), item.GetVal2()) for item in InDegV] OutDegreeList = [(item.GetVal1(), item.GetVal2()) for item in OutDegV] InDegreeList.sort(key=lambda x: x[1], reverse=True) OutDegreeList.sort(key=lambda x: x[1], reverse=True) minOutDegree = min(OutDegreeList, key=lambda x: x[1])[1] maxOutDegree = max(OutDegreeList, key=lambda x: x[1])[1] minInDegree = min(InDegreeList, key=lambda x: x[1])[1] maxInDegree = max(InDegreeList, key=lambda x: x[1])[1] # # Sanity Check #print maxOutDegree, minOutDegree, maxInDegree, minInDegree #print InDegreeList[0], InDegreeList[-1] for (nodeId, Degree) in InDegreeList: if not NodeAttributes.get(nodeId, None): NodeAttributes[nodeId] = dict() NodeAttributes[nodeId]['InDegree'] = Degree normalizedDegree = (float(Degree) - float(minInDegree)) / ( float(maxInDegree - float(minInDegree))) NodeAttributes[nodeId]['NormInDegree'] = normalizedDegree for (nodeId, Degree) in OutDegreeList: NodeAttributes[nodeId]['OutDegree'] = Degree normalizedDegree = (float(Degree) - float(minOutDegree)) / ( float(maxOutDegree - float(minOutDegree))) NodeAttributes[nodeId]['NormOutDegree'] = normalizedDegree # # Sanity Check # #print NodeAttributes[1874] #print NodeAttributes[893] return NodeAttributes
def plotDegDistr(G): DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) numNodes = G.GetNodes() print numNodes deg = [] nodes = [] tups = [] for item in DegToCntV: if item.GetVal1() == 0: numNodes -= item.GetVal2() continue deg.append(item.GetVal1()) nodes.append(item.GetVal2()) #float(item.GetVal2()) / float(numNodes)) tups.append((item.GetVal1(), float(item.GetVal2()))) fig = plt.figure() ax = plt.gca() G1Dots, = ax.plot(deg, [float(x) / float(numNodes) for x in nodes], 'o', c='blue', alpha=0.75, markeredgecolor='none') ax.set_yscale('log') ax.set_xscale('log') ax.set_ylabel('Proportion of Users') ax.set_xlabel('Number of Friends') plt.show()
def avgDegreeDist(family, direction, numSamples, apiGraph): path = 'data/graphs/' + family + '/' files = os.listdir(path) if apiGraph: graph_files = filter(lambda x: '.apigraph' in x, files) else: graph_files = filter(lambda x: '.edges' in x, files) random.shuffle(graph_files) maxdeg = 0 if apiGraph: Gs = [snap.TNEANet.Load(snap.TFIn(path + f)) for f in graph_files[:numSamples]] else: Gs = [snap.LoadEdgeList(snap.PNEANet, path + f, 0, 1) for f in graph_files[:numSamples]] if direction == 'in': maxdeg = max([G.GetNI((snap.GetMxInDegNId(G))).GetInDeg() for G in Gs]) else: maxdeg = max([G.GetNI((snap.GetMxOutDegNId(G))).GetOutDeg() for G in Gs]) avg_deg_dist = np.zeros(maxdeg + 1) for G in Gs: DegToCntV = snap.TIntPrV() if direction == 'in': snap.GetInDegCnt(G, DegToCntV) else: snap.GetOutDegCnt(G, DegToCntV) for item in DegToCntV: deg = item.GetVal1() avg_deg_dist[deg] += item.GetVal2() avg_deg_dist = avg_deg_dist / numSamples return avg_deg_dist
def main(): G = snap.LoadEdgeList(snap.PNGraph, "wiki-Vote.txt", 0, 1) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDegV) dict = {} for item in OutDegV: node = item.GetVal2() if node in dict: dict[node] += 1 else: dict[node] = 1 x = [] y = [] for key, values in dict.items(): if key > 0 and values > 0: x.append(math.log(key, 10)) y.append(math.log(values, 10)) z1 = np.polyfit(x, y, 1) p1 = np.poly1d(z1) yvals = p1(x) plt.plot(x, y, '*', label='original values') plt.plot(x, yvals, 'r', label='polyfit values') plt.xlabel('x=log(Out-Grades)') plt.ylabel('y=log(Sum of the Nodes)') plt.show()
def saveDegreeDistribution(graph, filename): degToCntV = snap.TIntPrV() snap.GetDegCnt(graph, degToCntV) with open(filename, 'w') as f: for item in degToCntV: dist = float(item.GetVal2()) / float(graph.GetNodes()) f.write('%d\t%f\n' % (item.GetVal1(), dist))
def get_node_degree(UGraph, graph_type, attributes): degree = np.zeros((UGraph.GetNodes(), )) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(UGraph, OutDegV) for item in OutDegV: degree[item.GetVal1()] = item.GetVal2() attributes['Degree'] = degree
def analyzeDegrees(FNGraph): t1 = time.time() print "Started analysing network degrees: \n" DegToCntV = snap.TIntPrV() snap.GetDegCnt(FNGraph, DegToCntV) avgDeg = 0 xVec = list() yVec = list() for item in DegToCntV: avgDeg += int(item.GetVal2()) * int(item.GetVal1()) xVec.append(item.GetVal1()) yVec.append(item.GetVal2()) avgDeg = avgDeg/FNGraph.GetNodes() print "\tNetwork average degree %d" % avgDeg # plot degree distribution plt.figure(0) plt.plot(xVec, yVec, 'b-') plt.title("Degree distribution for Football network \n Average degree: %d" % avgDeg) plt.ylabel("Number of nodes") plt.xlabel("Degrees") plt.savefig('DegreeDistribution.png') print "\nFinished calculating in %.3f seconds\n" % (time.time()-t1)
def getBasicInfo(strPath, net): G = snap.LoadEdgeList(snap.PUNGraph,strPath,0,1) GraphInfo = {} GraphInfo['nodes'] = G.GetNodes() GraphInfo['edges'] = G.GetEdges() GraphInfo['zeroDegNodes'] = snap.CntDegNodes(G, 0) GraphInfo['zeroInDegNodes'] = snap.CntInDegNodes(G, 0) GraphInfo['zeroOutDegNodes'] = snap.CntOutDegNodes(G, 0) GraphInfo['nonZeroIn-OutDegNodes'] = snap.CntNonZNodes(G) GraphInfo['uniqueDirectedEdges'] = snap.CntUniqDirEdges(G) GraphInfo['uniqueUndirectedEdges'] = snap.CntUniqUndirEdges(G) GraphInfo['selfEdges'] = snap.CntSelfEdges(G) GraphInfo['biDirEdges'] = snap.CntUniqBiDirEdges(G) NTestNodes = 10 IsDir = False GraphInfo['approxFullDiameter'] = snap.GetBfsEffDiam(G, NTestNodes, IsDir) GraphInfo['90effectiveDiameter'] = snap.GetAnfEffDiam(G) DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) sumofNode = G.GetNodes() L = [item.GetVal1()*item.GetVal2() for item in DegToCntV] GraphInfo['averageDegree'] = float(sum(L))/(sumofNode) (DegreeCountMax ,Degree, DegreeCount, CluDegree, Clu) = getGraphInfo(G) # creatNet(G,net) return GraphInfo,DegreeCountMax , Degree, DegreeCount, CluDegree, Clu
def plotDegreeDist(Graph, title, c, x_u, y_d): distribution = snap.TIntPrV() snap.GetInDegCnt(Graph, distribution) nodes = Graph.GetNodes() X, Y = [], [] for d in distribution: X.append(d.GetVal1()) Y.append(float(d.GetVal2()) / nodes) ''' plt.loglog(X, Y, color = c) plt.title(title) plt.xlabel('Node Degree (log)') plt.ylabel('fraction of nodes(log)') plt.show() ''' g = plt.scatter(X, Y, marker='.', color=c) plt.xlabel("degree") plt.ylabel("fraction of nodes") plt.title(title) plt.xscale('log') plt.yscale('log') plt.xlim(1, x_u) plt.ylim(y_d, 0.1) plt.show()
def plotDegreeDistribution(G): # # Get Degree Distribution # OutDegToCntV = snap.TIntPrV() snap.GetOutDegCnt(G, OutDegToCntV) count = 0 nodeList = [] degreeList = [] for item in OutDegToCntV: (n, d) = (item.GetVal2(), item.GetVal1()) nodeList.append(n) degreeList.append(d) x = np.array([ np.log10(item.GetVal1()) for itemm in OutDegToCntV if item.GetVal1() > 0 ]) y = np.array([ np.log10(item.GetVal2()) for item in OutDegToCntV if item.GetVal2() > 0 ]) # # Plot Degree Distribution # plt.figure(figsize=(15, 15)) loglog(degreeList, nodeList, 'bo') #plt.plot(x_plot, 10**b*x_plot**a, 'r-') plt.title("LogLog plot of out-degree distribution") plt.show() return
def solve_degree_based_questions(G, GName): #Number of nodes with degre seven CntV = snap.TIntPrV() snap.GetOutDegCnt(G, CntV) flag = 0 for p in CntV: if p.GetVal1() == 7: flag = p.GetVal2() break print "Number of nodes with degree=7 in %s: %d" % (GName[:-10], flag) #To find the number of nodes with maximum degree and thier IDs MaxDegree = CntV[len(CntV) - 1].GetVal1() Nodes_with_max_deg = [] for NI in G.Nodes(): if NI.GetOutDeg() == MaxDegree: Nodes_with_max_deg.append(str(NI.GetId())) string_of_nodes_with_max_deg = ",".join(Nodes_with_max_deg) print "Node id (s) with highest degree in {0}: {1}".format( GName[:-10], string_of_nodes_with_max_deg) #Plots the Degree Distribution filename = "outDeg." + GName[:-10] + ".png" snap.PlotOutDegDistr(G, GName[:-10], GName[:-10] + " - out-degree Distribution") print "Degree distribution of {0} is in: {1}".format(GName[:-10], filename)
def test_snap(self): """Test that snap.py installed correctly. """ import snap num_nodes = 20 # Generate different undirected graphs full_graph = snap.GenFull(snap.PUNGraph, num_nodes) star_graph = snap.GenStar(snap.PUNGraph, num_nodes) random_graph = snap.GenRndGnm(snap.PUNGraph, num_nodes, num_nodes * 3) # Basic statistics on the graphs self.assertEqual(snap.CntInDegNodes(full_graph, num_nodes - 1), num_nodes) self.assertEqual(snap.CntOutDegNodes(full_graph, num_nodes - 1), num_nodes) self.assertEqual(snap.GetMxInDegNId(star_graph), 0) self.assertEqual(snap.GetMxOutDegNId(star_graph), 0) # Iterator degree_to_count = snap.TIntPrV() snap.GetInDegCnt(full_graph, degree_to_count) # There should be only one entry (num_nodes - 1, num_nodes) for item in degree_to_count: self.assertEqual(num_nodes - 1, item.GetVal1()) self.assertEqual(num_nodes, item.GetVal2()) # Rewiring rewired_graph = snap.GenRewire(random_graph) for n1 in random_graph.Nodes(): for n2 in rewired_graph.Nodes(): if n1.GetId() == n2.GetId(): self.assertEqual(n1.GetOutDeg() + n1.GetInDeg(), n2.GetOutDeg() + n2.GetInDeg())
def OutDeg(graph): outdir = 'temp/' tmp_arr = [] out_arr = snap.TIntPrV() snap.GetOutDegCnt(graph, out_arr) for item in out_arr: cnt = item.GetVal2() deg = item.GetVal1() tmp_arr.append((deg, cnt)) tmp_arr = np.array(tmp_arr) out_fname = os.path.join('temp', 'outdegdistr.png') plt.clf() plt.figure(1) plt.subplots_adjust(left=0.075, bottom=0.075, right=1., top=1., wspace=0., hspace=0.) plt.plot(tmp_arr[:, 0], tmp_arr[:, 1], '-x') plt.yscale('log') if tmp_arr[:, 0].max() > MAX_XTICKS_NUM: skip = int(tmp_arr[:, 0].max()) / MAX_XTICKS_NUM plt.xticks( np.arange(0, tmp_arr[:, 0].max() + 1 + skip, skip) ) else: plt.xticks(np.arange(tmp_arr[:, 0].max() + 1)) plt.xlim(0, tmp_arr[:, 0].max()) plt.ylim(0, tmp_arr[:, 1].max()) plt.xlabel('Out-degrees', fontsize=16) plt.ylabel('Number of nodes', fontsize=16) plt.grid(True) plt.savefig(out_fname, dpi=300, format='png')
def fit_deg_dist(Graph): def func(x, a, b): return a * np.exp(-b * x) DegToCntV = snap.TIntPrV() X, Y = get_in_degree_distribution(Graph) plt.subplot(2, 1, 1) X = np.array(X) Y = np.array(Y) popt, pcov = curve_fit(func, X, Y) a, b = popt l0, = plt.plot(X, Y, color='olive', label='Degree Distribution') l1, = plt.plot(X, [func(x, a, b) for x in X]) plt.legend([l0, l1], ['C. Elegans, in degree', 'Fitted a*exp(-b*x) curve']) plt.xlabel('Degree') plt.ylabel('Number of nodes') X, Y = get_out_degree_distribution(Graph) plt.subplot(2, 1, 2) X = np.array(X) Y = np.array(Y) popt, pcov = curve_fit(func, X, Y) a, b = popt l0, = plt.plot(X, Y, color='olive', label='Degree Distribution') l1, = plt.plot(X, [func(x, a, b) for x in X]) plt.legend([l0, l1], ['C. Elegans, out degree', 'Fitted a*exp(-b*x) curve']) plt.xlabel('Degree') plt.ylabel('Number of nodes') plt.show()
def get_dists(G): deg_counts = [] degs = [] deg_vect = snap.TIntPrV() snap.GetDegCnt(G, deg_vect) for item in deg_vect: deg = item.GetVal1() cnt = item.GetVal2() deg_counts.append(cnt) degs.append(deg) out_deg = [] out_counts = [] cur_deg = min(degs) for deg, cnt in zip(degs, deg_counts): # while cur_deg < deg: # out_deg.append(cur_deg) # out_counts.append(0) # cur_deg += 1 out_deg.append(deg) out_counts.append(cnt) cur_deg += 1 deg_counts = np.asarray(out_counts) degs = np.asarray(out_deg) pdf = deg_counts.astype(float) / sum(deg_counts) cdf = np.cumsum(pdf) cdf = np.insert(cdf, 0, 0) ccdf = 1 - cdf return deg_counts, degs, cdf, ccdf, pdf
def getGraphInfo(G): Degree = [] DegreeCount = [] CluDegree = [] Clu = [] DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) DegreeCountMax = 0 for item in DegToCntV: tempy = item.GetVal2() if tempy > DegreeCountMax: DegreeCountMax = tempy Degree.append(item.GetVal1()) DegreeCount.append(tempy) CfVec = snap.TFltPrV() Cf = snap.GetClustCf(G, CfVec, -1) for pair in CfVec: CluDegree.append(pair.GetVal1()) Clu.append(pair.GetVal2()) return DegreeCountMax,Degree,DegreeCount,CluDegree,Clu
def _initialize(self, mu, sigma_ratio): indeg = snap.TIntPrV() snap.GetNodeInDegV(self._graph, indeg) for item in indeg: nid, deg = item.GetVal1(), item.GetVal2() if deg == 0: continue node = self._graph.GetNI(nid) # Sample a random probability for each in link p = np.clip( np.random.normal( np.ones(deg, dtype=np.float32) / deg, sigma_ratio / np.ones(deg)), 0., 1.) # Handle corner cases if p.sum() == 0: p = np.ones(deg, dtype=np.float32) p /= p.sum() for i in range(deg): edge = self._graph.GetEI(node.GetInNId(i), node.GetId()) self._graph.AddFltAttrDatE(edge, p[i], self.prob)
def degree_distribution_graphs(): InDegV = snap.TIntPrV() snap.GetNodeInDegV(G, InDegV) a = np.arange(1, snap.CntNonZNodes(G) - snap.CntInDegNodes(G, 0) + 2) i = 0 for item in InDegV: if item.GetVal2() > 0: i = i + 1 a[i] = item.GetVal2() bars, bins = np.histogram(a, bins=np.arange(1, max(a))) plt.hist(bars, bins) plt.grid() plt.show() plt.loglog(bins[0:-1], bars) plt.ylabel('# users per degree') plt.xlabel('in-degree') plt.grid() plt.show() plt.loglog(bins[0:-1], sum(bars) - np.cumsum(bars)) plt.ylabel('# users with degree larger or equal than x') plt.xlabel('in-degree') plt.grid() plt.show()
def calculate_stats(): # create similarities folder if not os.path.exists(config.DATASET_DIR / 'similarities'): os.makedirs(config.DATASET_DIR / 'similarities') if config.CALCULATE_EGO_GRAPHS: print(f'Calculating ego graphs for {config.DATASET_DIR }...') if not (config.DATASET_DIR / 'ego_graphs.txt').exists() or config.OVERRIDE: ego_graph_dict = {} for node in snap_graph.Nodes(): node_id = int(node.GetId()) nodes_vec = snap.TIntV() snap.GetNodesAtHop(snap_graph, node_id, 1, nodes_vec, False) ego_graph_dict[node_id] = list(nodes_vec) with open(str(config.DATASET_DIR / 'ego_graphs.txt'), 'w') as f: json.dump(ego_graph_dict, f) if config.CALCULATE_DEGREE_SEQUENCE: print(f'Calculating degree sequences for {config.DATASET_DIR}...') if not (config.DATASET_DIR / 'degree_sequence.txt').exists() or config.OVERRIDE: n_nodes = len(list(snap_graph.Nodes())) degrees = {} InDegV = snap.TIntPrV() snap.GetNodeInDegV(snap_graph, InDegV) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(snap_graph, OutDegV) for item1, item2 in zip(InDegV, OutDegV): degrees[item1.GetVal1()] = item1.GetVal2() with open(str(config.DATASET_DIR / 'degree_sequence.txt'), 'w') as f: json.dump(degrees, f) if config.CALCULATE_SHORTEST_PATHS: print(f'Calculating shortest paths for {config.DATASET_DIR}...') if not (config.DATASET_DIR / 'shortest_path_matrix.npy').exists() or config.OVERRIDE: with multiprocessing.Pool(processes=config.N_PROCESSSES) as pool: shortest_paths = pool.map(get_shortest_path, node_ids) all_shortest_paths = np.stack(shortest_paths) np.save(str(config.DATASET_DIR / 'shortest_path_matrix.npy'), all_shortest_paths)
def __init__(self): self.G = snap.LoadEdgeList(snap.PNGraph, 'data/snap-web-2016-09-links-clean-1.txt', 0, 1) self.out_deg_v = snap.TIntPrV() snap.GetNodeOutDegV(self.G, self.out_deg_v) self.deg_freq = self.get_deg_freq_map(self.out_deg_v)
def outdegree(rankCommands, Graph, conn, cur): OutDegV = snap.TIntPrV() before_time = time.time() snap.GetNodeOutDegV(Graph, OutDegV) print "Total handling time is: ", (time.time() - before_time) DegH = snap.TIntIntH() slist = sortNodes(OutDegV, DegH) createTable(rankCommands, slist, DegH, conn, cur)
def number_of_bridges(G, GName): EdgeV = snap.TIntPrV() snap.GetEdgeBridges(G, EdgeV) bridges_in_graph = len(EdgeV) print "Number of edge bridges in {0}: {1}".format(GName[:-10], bridges_in_graph)
def getNodesByDegree(self): result = snap.TIntPrV() nodesByDegree = [] snap.GetNodeInDegV(self.rawGraph, result) for x in result: nodesByDegree.append((self.node(x.GetVal1()), x.GetVal2())) return sorted(nodesByDegree,key=lambda e: e[1], reverse=True)
def get_landmarks_ids(Graph, nL, savedir): ''' Choose landmarks based on weighted distribution ''' # get node degree for each node # node ids are not in consecutive order print "Getting Nodal Degrees..." OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(Graph, OutDegV) node_degree = zeros((V, 2)) for i, item in enumerate(OutDegV): node_degree[i, 1] = item.GetVal2() node_degree[i, 0] = item.GetVal1() node_degree = node_degree[node_degree[:, 0].argsort()] node_degree2 = node_degree[node_degree[:, 1].argsort()] node_degree = node_degree.astype(int) node_degree2 = flipud(node_degree2.astype(int)) node_list = sort(node_degree[:, 0]).astype(int) # sample from nodal degree # values = arange(len(node_degree[:,0])) node_deg = node_degree[:, 1].copy() node_deg[node_deg <= 2] = 0 # set probab to zero for deg <= n probabilities = 1.0 * node_deg / sum(node_deg) Llist = [] land_idx = weighted_values(node_degree[:, 0], probabilities, 1) Llist.append(land_idx[0]) lcount = 1 check_random_n = min(16, nL) while lcount < nL: # sample from degree distribution curr_land_idx = weighted_values(node_degree[:, 0], probabilities, 1)[0] # check if sample is not in current list if curr_land_idx not in Llist: # # compute distances to current landmarks # dtemp = [] # count = 0 # Llist_shuffled = Llist[:] # copy list # random.shuffle(Llist_shuffled) # for li,l in reversed(list(enumerate(Llist_shuffled))): # if count <= check_random_n: # dtemp.append(snap.GetShortPath(Graph, l, curr_land_idx)) # count += 1 # # keep only if distances >= 3 # if all(array(dtemp) >= 3): Llist.append(curr_land_idx) lcount += 1 print lcount, " landmarks so far..." land_ids0 = sort(node_degree[Llist][:, 0])[:nL] print node_degree[Llist][:, 1] # create directory print "Creating directory for input data..." os.system('mkdir ' + savedir) return land_ids0, node_list
def component_distribution(g): print 'executing component distribution --- getting components' ComponentDist = snap.TIntPrV() snap.GetWccSzCnt(g, ComponentDist) f = open('component_distribution.txt', 'w') f.write("Size - Number of Components:\n") for comp in ComponentDist: f.write("% d \t % d\n" % (comp.GetVal1(), comp.GetVal2())) f.close() print 'finshed componet distribution'
def degree_distribution(): # Get node with max degree NId = snap.GetMxDegNId(G) print("max degree node", NId) # Get degree distribution DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) for item in DegToCntV: print("%d nodes with degree %d" % (item.GetVal2(), item.GetVal1()))