def get_deg_data(G): result_degree = snap.TIntV() snap.GetDegSeqV(G, result_degree) deg_data = [] for i in range(result_degree.Len()): deg_data.append(result_degree[i]) return deg_data
def f(): snap = self.snap nodes = self.nodes V = snap.TInt64V() snap.GetDegSeqV(self.graph, V) ret = [] for i in range(0, V.Len()): ret.append((nodes[i], V[i])) return ret
def configuration_model(): GUn = transform_directed_to_undirected() GUnDegSeqV = snap.TIntV() snap.GetDegSeqV(GUn, GUnDegSeqV) Rnd = snap.TRnd() GConfModel = snap.GenConfModel(GUnDegSeqV, Rnd) snap.PrintInfo(GConfModel, "Tweets ConfModel Stats", "Tweets_ConfModel-info.txt", False) f = open('Tweets_ConfModel-info.txt', 'r') file_contents = f.read() print(file_contents) f.close()
def GetMaxKDegree(self, k): self.seedNodes.clear() resultInDegree = snap.TIntV() resultOutDegree = snap.TIntV() snap.GetDegSeqV(self.graph, resultInDegree, resultOutDegree) count = len(resultOutDegree) listDegree = [] nodesId = [] for i in range(count): listDegree.append(resultOutDegree[i]) nodesId.append(i) # random.Random().shuffle(listDegree) return self.GetMaxK(listDegree, nodesId, k)
def getDataPointsToPlot(Graph): """ :param - Graph: snap.PUNGraph object representing an undirected graph return values: X: list of degrees Y: list of frequencies: Y[i] = fraction of nodes with degree X[i] """ ############################################################################ result_degree = snap.TIntV() snap.GetDegSeqV(Graph, result_degree) result_degree = np.array(result_degree) X, Y = np.unique(result_degree, return_counts=True) ############################################################################ return X, Y
def printDegrees(Graph): avg = 0 degMap = dict() degMap = defaultdict(lambda: 0, degMap) result_degree = snap.TIntV() snap.GetDegSeqV(Graph, result_degree) total = result_degree.Len() for i in range(0, result_degree.Len()): if result_degree[i] > 1: degMap[result_degree[i]] += 1 avg += result_degree[i] else: total -= 1 for key in degMap: print key, degMap[key] print(avg * 1.0) / (total * 1.0) print total
def compute_max_degree(self, graph): # Array placeholder. arr = [] # Retrieve node degrees. degrees = snap.TIntV() snap.GetDegSeqV(graph, degrees) # Populate the array. for i in range(0, degrees.Len()): arr.append([i, degrees[i]]) # Sort the array. arr.sort(key=lambda x: x[1], reverse=True) # Return top item. return arr[0]
def Q3sim(Graph, k): """ Function to simulate the effect of a dining event. :param - Graph: snap.PUNGraph object representing an undirected graph k: amount to be spent on the dining event return type: int return: The number of votes by which A wins (or loses), i.e. (number of votes of A - number of votes of B) Hint: Feel free to use initial_voting_state and iterate_voting functions. """ ########################################################################### init_conf = initial_voting_state(Graph) # change to 'A' according to k - high rollers result_degree = snap.TIntV() snap.GetDegSeqV(Graph, result_degree) for nid in np.flip(np.argsort(list(result_degree)))[:k // 1000]: init_conf[nid] = 'A' conf = iterate_voting(Graph, init_conf) return Counter(conf.values())['A'] - Counter(conf.values())['B']
def select_queries(Gs,k): """Creates the query set consisting of the k highest degree vertices Args: Gs: a sample graph k: the size of the query set Returns: greatest_degree_ids: a list containing the ids of the k vertices having the greatest degrees in Gs """ result_degree = snap.TIntV() snap.GetDegSeqV(Gs, result_degree) lst = [] nodeids = [] for v in Gs.Nodes(): nodeids.append(v.GetId()) for i in range(0, result_degree.Len()): lst.append((nodeids[i],result_degree[i])) greatest_degree_lst = nlargest(k, lst, key=lambda e:e[1]) greatest_degree_ids = [el[0] for el in greatest_degree_lst] return greatest_degree_ids
def test_snap(file_name): start = time.clock() g = sn.LoadEdgeList_PUNGraph("../data/" + file_name + ".gr", 0, 1) print "elapsed ", time.clock() - start print "#nodes ", g.GetNodes() print "#edges ", g.GetEdges() result_degree = sn.TIntV() sn.GetDegSeqV(g, result_degree) max_deg = max(result_degree) print "max_deg =", max_deg # transitivity (global clustering coefficient # start = time.clock() # clustering_coeff = sn.GetClustCf(g) # print "clustering_coeff =", clustering_coeff # print "elapsed ", time.clock() - start # BFS start = time.clock() s_Diam = sn.GetBfsFullDiam(g, N_BFS, False) print "s_Diam =", s_Diam print "elapsed ", time.clock() - start
ax2.plot(adpt_size, finish_steps, '.-') ax2.set_ylabel('step that the infection stops', color='C0') ax2.tick_params('y', colors='C0') ax2.set_title('SIR - # initial adopters') plt.xticks(adpt_size[::6]) plt.yticks(finish_steps) fig.savefig("SIR-adpt_size-finish_step.png") # largest degree adopters vs. smallest degree adopters if sys.argv[1] == '5' or sys.argv[1] == 'all': max_adopters = [] min_adopters = [] degrees = [0 for x in range(number_node)] result_in_degree = snap.TIntV() result_out_degree = snap.TIntV() snap.GetDegSeqV(G, result_in_degree, result_out_degree) for i in range(0, result_out_degree.Len()): degrees[i] = result_in_degree[i] sorted_degrees = sorted(degrees) for i in range(init_adopter): min_adopters.append(degrees.index(sorted_degrees[i])) max_adopters.append(degrees.index(sorted_degrees[-i - 1])) # ------ adopters with max degrees infectious_num, removed_num, final_states = runSIR(G, init_adopter, p, tl, max_adopters) steps = [x for x in range(0, len(infectious_num) + 1)] zero = [0] infectious_num = np.array(zero + infectious_num) removed_num = np.array(zero + removed_num) susceptible_num = np.array([
matrix = initiator for i in range(5): matrix = np.kron(matrix, initiator) generated = snap.TUNGraph.New() for i in range(len(matrix)): generated.AddNode(i) for i in range(len(matrix)): for j in range(i, len(matrix)): rand = random.random() if matrix[i][j] < rand: generated.AddEdge(i, j) result_degree = snap.TIntV() snap.GetDegSeqV(generated, result_degree) arr = [] for i in range(0, result_degree.Len()): arr.append((i, result_degree[i])) arr.sort(key=lambda x: -x[1], reverse=True) V = snap.TIntV() for i in range(len(matrix) - nodes): V.Add(arr[i][0]) # V = snap.TIntV() # rand_nodes = random.sample(range(0, len(matrix)), len(matrix)-nodes) # for node in rand_nodes: # V.Add(node)
CntV3[6].GetVal2()) if (sub_graph_name == "p2p-Gnutella04-subgraph"): CntV4 = snap.TIntPrV() # Get degree distribution pairs (degree, count) snap.GetDegCnt(p2p_gnutella04_subgraph, CntV4) print "Number of nodes of degree = 7 in p2p-Gnutella04-subgraph: " + str( CntV4[6].GetVal2()) # Task 1.2.2.2 if (sub_graph_name == "soc-Epinions1-subgraph"): Mx_degree_id = [] result_degree = snap.TIntV() snap.GetDegSeqV(soc_epinions1_subgraph, result_degree) for i in range(0, result_degree.Len()): if (result_degree[i] == CntV1[CntV1.Len() - 1].GetVal1()): Mx_degree_id.append(i) print "Node id(s) with highest degree in soc-Epinions1-subgraph: " + str( Mx_degree_id) if (sub_graph_name == "cit-HepPh-subgraph"): Mx_degree_id = [] result_degree = snap.TIntV() snap.GetDegSeqV(cit_heph_subgraph, result_degree) for i in range(0, result_degree.Len()): if (result_degree[i] == CntV2[CntV2.Len() - 1].GetVal1()): Mx_degree_id.append(i) print "Node id(s) with highest degree in cit-HepPh-subgraph: " + str(
# and at the maximum modularity, we shall output the community Structure value_modularity_old = 1 for i in range(G.GetEdges()): edge_betweenness = float(0.0) source_node = float(0.0) dest_node = float(0.0) result = float(0.0) #Compute Edge Betweenness between edges to delete the edges edge_weight = float(2 * G.GetEdges()) Nodes = snap.TIntFltH() Edges = snap.TIntPrFltH() snap.GetBetweennessCentr(G, Nodes, Edges, 1.0) # Compute node degree to be used in the Modularity # Given that this is an undirected graph, in-degree = out-degree degree = snap.TIntV() snap.GetDegSeqV(G, degree) #Modularity = 1/2m(A - (k_i*k_j/2m)) #A - represents edge weight between node i and node j #k_i and k_j are the sum of the weights of the edges attached to nodes i and j #2m - is the sum of all the edge weights in the graph for edge in Edges: if (edge_betweenness < Edges[edge]): edge_betweenness = Edges[edge] node_1 = edge.GetVal1() node_2 = edge.GetVal2() # Compute A - (k_i*k_j/2m) and keep on adding as per the formulae A = float(1.0) result = result + (A - ((degree[edge.GetVal1() - 1]) * (degree[edge.GetVal2() - 1])) / (edge_weight)) # Delete the edge which has the highest betweenness G.DelEdge(node_1, node_2)
#loading the graph given Graph = snap.LoadEdgeList(snap.PUNGraph,input_file, 0, 1) n = Graph.GetNodes() #declaring required lists degl = list() #degree sequence list cuml = list() #stores cumulative number of nodes greater than k countdl = list() #stores count of each degree ddl = list() #stores distinct degree values #obtaining degree sequence result_degree = snap.TIntV() snap.GetDegSeqV(Graph, result_degree) for i in range(0, result_degree.Len()): degl.append(result_degree[i]) #obtaining degree count for each degree DegToCntV = snap.TIntPrV() snap.GetDegCnt(Graph, DegToCntV) for item in DegToCntV: countdl.append(item.GetVal2()) ddl.append(item.GetVal1()) #plot 8.3 #considering only till 60 bins, since further will make graph right-skewed
def sortNodesByDeg(G): degrees = snap.TIntV() snap.GetDegSeqV(G, degrees) node_degrees = zip(range(len(degrees)), degrees) node_degrees.sort(key = lambda x: x[1], reverse=True) return node_degrees