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 quick_properties(graph, name, dic_path): """Get quick properties of the graph "name". dic_path is the path of the dict {players: id} """ n_edges = graph.GetEdges() n_nodes = graph.GetNodes() print("##########") print("Quick overview of {} Network".format(name)) print("##########") print("{} Nodes, {} Edges").format(n_nodes, n_edges) print("{} Self-edges ".format(snap.CntSelfEdges(graph))) print("{} Directed edges, {} Undirected edges".format( snap.CntUniqDirEdges(graph), snap.CntUniqUndirEdges(graph))) print("{} Reciprocated edges".format(snap.CntUniqBiDirEdges(graph))) print("{} 0-out-degree nodes, {} 0-in-degree nodes".format( snap.CntOutDegNodes(graph, 0), snap.CntInDegNodes(graph, 0))) node_in = graph.GetNI(snap.GetMxInDegNId(graph)) node_out = graph.GetNI(snap.GetMxOutDegNId(graph)) print("Maximum node in-degree: {}, maximum node out-degree: {}".format( node_in.GetDeg(), node_out.GetDeg())) print("###") components = snap.TCnComV() snap.GetWccs(graph, components) max_wcc = snap.GetMxWcc(graph) print "{} Weakly connected components".format(components.Len()) print "Largest Wcc: {} Nodes, {} Edges".format(max_wcc.GetNodes(), max_wcc.GetEdges()) prankH = snap.TIntFltH() snap.GetPageRank(graph, prankH) sorted_prankH = sorted(prankH, key=lambda key: prankH[key], reverse=True) NIdHubH = snap.TIntFltH() NIdAuthH = snap.TIntFltH() snap.GetHits(graph, NIdHubH, NIdAuthH) sorted_NIdHubH = sorted(NIdHubH, key=lambda key: NIdHubH[key], reverse=True) sorted_NIdAuthH = sorted(NIdAuthH, key=lambda key: NIdAuthH[key], reverse=True) with open(dic_path, 'rb') as dic_id: mydict = pickle.load(dic_id) print("3 most central players by PageRank scores: {}, {}, {}".format( list(mydict.keys())[list(mydict.values()).index(sorted_prankH[0])], list(mydict.keys())[list(mydict.values()).index(sorted_prankH[1])], list(mydict.keys())[list(mydict.values()).index( sorted_prankH[2])])) print("Top 3 hubs: {}, {}, {}".format( list(mydict.keys())[list(mydict.values()).index( sorted_NIdHubH[0])], list(mydict.keys())[list(mydict.values()).index( sorted_NIdHubH[1])], list(mydict.keys())[list(mydict.values()).index( sorted_NIdHubH[2])])) print("Top 3 authorities: {}, {}, {}".format( list(mydict.keys())[list(mydict.values()).index( sorted_NIdAuthH[0])], list(mydict.keys())[list(mydict.values()).index( sorted_NIdAuthH[1])], list(mydict.keys())[list(mydict.values()).index( sorted_NIdAuthH[2])]))
def print_stats(G=None, print_mode=False): node_count = G.GetNodes() edge_count = G.GetEdges() if print_mode: print "G: Nodes %d, Edges %d" % (node_count, edge_count) # 1.2 nodes with self edge self_edge_count = snap.CntSelfEdges(G) if print_mode: print "Count of self edges is G is %d" % self_edge_count return node_count, edge_count, self_edge_count
def loadCollabNet(path): """ :param - path: path to edge list file return type: snap.PUNGraph return: Graph loaded from edge list at `path and self edges removed Do not forget to remove the self edges! """ ############################################################################ # TODO: Your code here! Graph = snap.LoadEdgeList_PUNGraph(path, 0, 1) for n in Graph.Nodes(): Graph.DelEdge(n.GetId(), n.GetId()) Count = snap.CntSelfEdges(Graph) print("Directed Graph: Count of self edges is %d" % Count) ############################################################################ return Graph
def wikiVotingNetwork(): Component = snap.TIntPrV() #Loding the graph Wiki = snap.LoadEdgeList(snap.PNGraph, "Wiki-Vote.txt", 0, 1) #Printing Number of Nodes in the Graph print "Number of Nodes: ", Wiki.GetNodes() #Printing Number of Edges in the Graph print "Number of Edges: ", Wiki.GetEdges() #Printing Number of Directed Edges in the Graph print "Number of Directed Edges: ", snap.CntUniqDirEdges(Wiki) #Printing Number of Un-Directed Edges in the Graph print "Number of Undirected Edges: ", snap.CntUniqUndirEdges(Wiki) #Printing Number of Directed Edges in the Graph print "Number of Self-Edges: ", snap.CntSelfEdges(Wiki) #Printing Number of Zero InDeg Nodes in the Graph print "Number of Zero InDeg Nodes: ", snap.CntInDegNodes(Wiki, 0) #Printing Number of Zero OutDeg Nodes in the Graph print "Number of Zero OutDeg Nodes: ", snap.CntOutDegNodes(Wiki, 0) #Printing Node ID with maximum degree in the Graph print "Node ID with maximum degree: ", snap.GetMxDegNId(Wiki) snap.GetSccSzCnt(Wiki, Component) for comp in Component: #printing number of strongly connected components with size print "Size: %d - Number of Strongly Connected Components: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing size of largest connected components print "Size of largest connected component: ", snap.GetMxSccSz(Wiki) snap.GetWccSzCnt(Wiki, Component) for comp in Component: #printing number of weekly connected components with size print "Size: %d - Number of Weekly Connected Component Wikipedia: %d" % ( comp.GetVal1(), comp.GetVal2()) #printing size of weekly connected components print "Size of Weakly connected component: ", snap.GetMxWccSz(Wiki) #plotting out-degree distribution snap.PlotOutDegDistr(Wiki, "wiki-analysis", "Directed graph - Out-Degree Distribution")
def GetBasic(self): loopCount = snap.CntSelfEdges(self.graph) eMax = self.graph.GetNodes() * (self.graph.GetNodes() - 1) / 2.0 networkDensity = round(self.graph.GetEdges() / eMax, 3) networkAverageDegree = round( 2 * self.graph.GetEdges() / self.graph.GetNodes(), 3) table0 = PrettyTable( ['Nodes', 'Edges Sum', 'Loop', 'Density', 'Average Degree']) table0.add_row([ self.graph.GetNodes(), self.graph.GetEdges(), loopCount, networkDensity, networkAverageDegree ]) print table0 print "" table1 = PrettyTable(['Nodes', 'Out Degree', 'In Degree']) if self.graph.Nodes() == 0: print 'The network is empty' else: for NI in self.graph.Nodes(): table1.add_row([NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()]) print table1
def quick_properties(graph, name, dic_path): """Get quick properties of the graph "name". dic_path is the path of the dict {players: id} """ results = {} n_edges = graph.GetEdges() n_nodes = graph.GetNodes() n_self_edges = snap.CntSelfEdges(graph) n_directed_edges, n_undirected_edges = snap.CntUniqDirEdges( graph), snap.CntUniqUndirEdges(graph) n_reciprocated_edges = snap.CntUniqBiDirEdges(graph) n_zero_out_nodes, n_zero_in_nodes = snap.CntOutDegNodes( graph, 0), snap.CntInDegNodes(graph, 0) max_node_in = graph.GetNI(snap.GetMxInDegNId(graph)).GetDeg() max_node_out = graph.GetNI(snap.GetMxOutDegNId(graph)).GetDeg() components = snap.TCnComV() snap.GetWccs(graph, components) max_wcc = snap.GetMxWcc(graph) results["a. Nodes"] = n_nodes results["b. Edges"] = n_edges results["c. Self-edges"] = n_self_edges results["d. Directed edges"] = n_directed_edges results["e. Undirected edges"] = n_undirected_edges results["f. Reciprocated edges"] = n_reciprocated_edges results["g. 0 out-degree nodes"] = n_zero_out_nodes results["h. 0 in-degree nodes"] = n_zero_in_nodes results["i. Maximum node out-degree"] = max_node_out results["j. Maximum node in-degree"] = max_node_in results["k. Weakly connected components"] = components.Len() results["l. Nodes, edges of largest WCC"] = (max_wcc.GetNodes(), max_wcc.GetEdges()) print("##########") print("Quick overview of {} Network".format(name)) print("##########") print("{} Nodes, {} Edges".format(n_nodes, n_edges)) print("{} Self-edges ".format(n_self_edges)) print("{} Directed edges, {} Undirected edges".format( n_directed_edges, n_undirected_edges)) print("{} Reciprocated edges".format(n_reciprocated_edges)) print("{} 0-out-degree nodes, {} 0-in-degree nodes".format( n_zero_out_nodes, n_zero_in_nodes)) print("Maximum node in-degree: {}, maximum node out-degree: {}".format( max_node_in, max_node_out)) print("###") print "{} Weakly connected components".format(components.Len()) print "Largest Wcc: {} Nodes, {} Edges".format(max_wcc.GetNodes(), max_wcc.GetEdges()) prankH = snap.TIntFltH() snap.GetPageRank(graph, prankH) sorted_prankH = sorted(prankH, key=lambda key: prankH[key], reverse=True) NIdHubH = snap.TIntFltH() NIdAuthH = snap.TIntFltH() snap.GetHits(graph, NIdHubH, NIdAuthH) sorted_NIdHubH = sorted(NIdHubH, key=lambda key: NIdHubH[key], reverse=True) sorted_NIdAuthH = sorted(NIdAuthH, key=lambda key: NIdAuthH[key], reverse=True) with open(dic_path, 'rb') as dic_id: mydict = pickle.load(dic_id) print("3 most central players by PageRank scores: {}, {}, {}".format( name_from_index(sorted_prankH, 0, mydict), name_from_index(sorted_prankH, 1, mydict), name_from_index(sorted_prankH, 2, mydict))) print("Top 3 hubs: {}, {}, {}".format( name_from_index(sorted_NIdHubH, 0, mydict), name_from_index(sorted_NIdHubH, 1, mydict), name_from_index(sorted_NIdHubH, 2, mydict))) print("Top 3 authorities: {}, {}, {}".format( name_from_index(sorted_NIdAuthH, 0, mydict), name_from_index(sorted_NIdAuthH, 1, mydict), name_from_index(sorted_NIdAuthH, 2, mydict))) results["m. Three top PageRank"] = (name_from_index( sorted_prankH, 0, mydict), name_from_index( sorted_prankH, 1, mydict), name_from_index(sorted_prankH, 2, mydict)) results["n. Three top hubs"] = (name_from_index( sorted_NIdHubH, 0, mydict), name_from_index(sorted_NIdHubH, 1, mydict), name_from_index( sorted_NIdHubH, 2, mydict)) results["o. Three top authorities"] = (name_from_index( sorted_NIdAuthH, 0, mydict), name_from_index(sorted_NIdAuthH, 1, mydict), name_from_index( sorted_NIdAuthH, 2, mydict)) return results
import snap import numpy import matplotlib.pyplot as plt # Section 1 # # Load data from file, then construct a directed graph wiki_g = snap.LoadEdgeListStr(snap.PNGraph, "Wiki-Vote.txt", 0, 1) # solution to hw print('*' * 10 + ' Section I ' + '*' * 10) print("The wiki-vote graph has " + str(wiki_g.GetNodes()) + " nodes.") # self_loop_nodes = 0 # for edge in wiki_g.Edges(): # if edge.GetSrcNId() == edge.GetDstNId(): # self_loop_nodes = self_loop_nodes + 1 # Better use built-in functions to count the self-edges... print("The wiki-vote graph has " + str(snap.CntSelfEdges(wiki_g)) + " self-looped nodes.") print("The wiki-vote graph has " + str(snap.CntUniqDirEdges(wiki_g)) + " unique directed edges.") print("The wiki-vote graph has " + str(snap.CntUniqUndirEdges(wiki_g)) + " unique undirected edges.") print("The wiki-vote graph has " + str(int(snap.CntUniqUndirEdges(wiki_g) / 2)) + " reciprocated edges.") nodes_zero_out_degree = 0 nodes_zero_in_degree = 0 nodes_more_than_10_outgoing_edges = 0 nodes_fewer_than_10_incoming_edges = 0 min_out_degree = 1 max_out_degree = 1
return post_df ########################################################################## f_in = snap.TFIn(FOLDED_POSTID_GRAPH_PATH) post_graph = snap.TUNGraph.Load(f_in) print "nodes", post_graph.GetNodes() print "edges", post_graph.GetEdges() COMMUNITIES_PATH = path.join(BASE_PATH, 'postid-communities-with-postbodies.txt') COMMUNITIES_VEC_PATH = path.join(BASE_PATH, 'postid-communities.vector') # remove degree-1 nodes assert snap.CntSelfEdges(post_graph) == 0 snap.DelDegKNodes(post_graph, 1, 1) comm_vec = snap.TCnComV() modularity = snap.CommunityCNM(post_graph, comm_vec) f_out = snap.TFOut(COMMUNITIES_VEC_PATH) comm_vec.Save(f_out) f_out.Flush() f_in = snap.TFIn(COMMUNITIES_VEC_PATH) comm_vec = snap.TCnComV() comm_vec.Load(f_in) print "communities", len(comm_vec)
def f(): snap = self.snap return snap.CntSelfEdges(self.graph)
import snap SOURCE_FILE = './data/wiki-Vote.txt' DEGREE_BOUNDARY = 10 wikiGraph = snap.LoadEdgeList(snap.PNGraph, SOURCE_FILE, 0, 1) assert 103689 == wikiGraph.GetEdges() assert 7115 == wikiGraph.GetNodes() # 1.1 print("The number of nodes in the network is %s." % ( wikiGraph.GetNodes())) # 1.2 print("The number of nodes with a self-edge is %s." % ( snap.CntSelfEdges(wikiGraph))) # 1.3 print("The number of directed edges %s." % ( snap.CntUniqDirEdges(wikiGraph))) # 1.4 print("The number of undirected edges is %s." % ( snap.CntUniqUndirEdges(wikiGraph))) # 1.5 print("The number of reciprocated edges is %s." % ( snap.CntUniqDirEdges(wikiGraph) - snap.CntUniqUndirEdges(wikiGraph))) # 1.6 print("The number of nodes of zero out-degree is %s." % (
values = [int(s) for s in line.split()] X.append(values[0]) Y.append(values[1]) else: ctr += 1 plt.plot(X, Y) plt.title("dist of outDeg of nodes") plt.show() if __name__ == '__main__': G = snap.LoadEdgeList(snap.PNGraph, "wiki-Vote.txt", 0, 1) print("nodes: %d, edges: %d" % (G.GetNodes(), G.GetEdges())) print("num of self directed egdes: %d" % (snap.CntSelfEdges(G))) print("num of directed edges: %d" % (snap.CntUniqDirEdges(G))) nodeOutDegZero = 0 for node in G.Nodes(): if node.GetOutDeg() == 0: nodeOutDegZero += 1 print("number of nodes with zero out degree: %d" % nodeOutDegZero) '''for NI in G.Nodes(): if G.IsEdge(NI.GetId(),NI.GetId()): print("%d"% (NI.GetId()))''' outDistPlot(G) LSRegression()
import snap wiki = snap.LoadEdgeList(snap.PNGraph, "wiki-Vote.txt", 0, 1, '\t') # number of nodes print("Node number:", wiki.GetNodes()) # number of self-edge print("Self Edges:", snap.CntSelfEdges(wiki)) # number of directed edges print("Directed Edges:", wiki.GetEdges() - snap.CntSelfEdges(wiki)) # number of undirected edges (A->B and B->A counts a single undirected edge) print("Undirected Edges:", snap.CntUniqUndirEdges(wiki)) # number of reciprocated edges print("Reciprocated Edges:", snap.CntUniqBiDirEdges(wiki)) # nubmber of nodes of zero out-degree print("# of zero out-degree:", snap.CntOutDegNodes(wiki, 0)) # number of nodes of zero in-degree print("# of zero in-degree:", snap.CntInDegNodes(wiki, 0)) # number of nodes with out-degree > 10 count = 0 for i in range(11): count += snap.CntOutDegNodes(wiki, i) temp = wiki.GetNodes() - count print("Nodes with out-degree > 10:", temp)
def numSelfEdges(self): return snap.CntSelfEdges(self.rawGraph)
# Answer 1: print('The number of nodes: {}'.format(len(list(G1.Nodes())))) # self_loop_v_cnt = 0 # direct_edge_cnt = 0 # for edge in G1.Edges(): # src, dst = edge.GetSrcNId(), edge.GetDstNId() # # print(src, dst) # if src == dst: # self_loop_v_cnt += 1 # else: # direct_edge_cnt += 1 # Answer 2: print('There are {} self-loop nodes'.format(snap.CntSelfEdges(G1))) # Answer 3: print('Thre are {} directed edges'.format(snap.CntUniqDirEdges(G1))) # Answer 4: print('There are {} undirect edges'.format(snap.CntUniqUndirEdges(G1))) # Answer 5: print('There are {} reciprocated edges'.format(snap.CntUniqBiDirEdges(G1))) # Answer 6: print('There are {} nodes having 0 out-degree'.format( snap.CntOutDegNodes(G1, 0))) # Answer 7:
Count = snap.CntNonZNodes(graph) print "Count of nodes with degree greater than 0 is %d" % Count #no of edges Count = snap.CntOutDegNodes(graph, 0) print "Count of nodes with out-degree 0 is %d" % Count #no of nodes with zero in-degree Count = snap.CntInDegNodes(graph, 0) print "Count of nodes with in-degree 0 is %d" % Count #no of directed edges Count = snap.CntUniqDirEdges(graph) print "Count of directed edges is %d" % Count #no of undirected edges Count = snap.CntUniqUndirEdges(graph) print "Count of undirected edges is %d" % Count #no of self edges Count = snap.CntSelfEdges(graph) print "Count of self edges is %d" % Count #no of unique bi-directional/reciprocated edges Count = snap.CntUniqBiDirEdges(graph) print "Count of unique bidirectional edges is %d" % Count #no of nodes with out-degree greater than 10 OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(graph, OutDegV) count_od = 0 for item in OutDegV: if (item.GetVal2() > 10): count_od = count_od + 1 print "Count of nodes with more than 10 outgoing edges %d" % count_od #no of nodes with in-degree greater than 10
def numSelfLoops(G): return snap.CntSelfEdges(G)