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 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 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 partOneAndTwo(WikiG): # WikiG.Dump() print('1. Number of nodes: '+str(WikiG.GetNodes())) selfloop_cnt = 0 for node in WikiG.Nodes(): # print(node.GetId()) if WikiG.IsEdge(node.GetId(), node.GetId()): selfloop_cnt += 1 print('2. Self loop Node: {}'.format(selfloop_cnt)) cnt_dir = snap.CntUniqDirEdges(WikiG) print('3. The number of directed edges: {}'.format(cnt_dir)) cnt_undir = snap.CntUniqUndirEdges(WikiG) print("4. The number of unique undirected edges is %d" % cnt_undir) print("5. The number of reciprocated edges is %d" % (cnt_dir - cnt_undir)) cnt_in = snap.CntInDegNodes(WikiG, 0) print("6. The number of nodes of zero out-degree is %d" % cnt_in) cnt_out = snap.CntOutDegNodes(WikiG, 0) print("7. The number of nodes of zero in-degree is %d" % cnt_out) cnt_deg_above_10 = 0 cnt_deg_less_10 = 0 for node in WikiG.Nodes(): if node.GetOutDeg() > 10: cnt_deg_above_10 += 1 if node.GetInDeg() < 10: cnt_deg_less_10 += 1 print("8. The number of nodes with more than 10 outgoing edges is %d" % cnt_deg_above_10) print("9. The number of nodes with fewer than 10 incoming edges is %d" % cnt_deg_less_10) # Part 2 out_file_name = 'wiki' snap.PlotInDegDistr(WikiG, out_file_name, "Directed graph - in-degree Distribution") snap.PlotOutDegDistr(WikiG, out_file_name, "Directed graph - out-degree Distribution") InDegDistr = np.loadtxt("inDeg."+out_file_name+".tab") InDegDistr = InDegDistr[InDegDistr[:, 0] > 0] OutDegDistr = np.loadtxt("OutDeg."+out_file_name+".tab") # print(OutDegDistr.shape) OutDegDistr = OutDegDistr[OutDegDistr[:, 0] > 0] # print(OutDegDistr.shape) coff = np.polyfit(np.log10(OutDegDistr)[:, 0], np.log10(OutDegDistr)[:, 1], 1) print(coff) plt.figure() plt.subplot(211) plt.loglog(InDegDistr[:, 0], InDegDistr[:, 1]) plt.title('In deg Distr') plt.subplot(212) plt.loglog(OutDegDistr[:, 0], OutDegDistr[:, 1]) plt.loglog(OutDegDistr[:, 0], np.power(10, coff[1])*np.power(OutDegDistr[:, 0], coff[0])) plt.title('Out deg Distr & Last-Square Reg Line in log-log plot') plt.show()
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")
graphfilename = "C:\Python27\HW1\wiki-vote.txt" schema = snap.Schema() context = snap.TTableContext() schema.Add(snap.TStrTAttrPr("srcID", snap.atStr)) schema.Add(snap.TStrTAttrPr("dstID", snap.atStr)) sample_table = snap.TTable.LoadSS(schema, graphfilename, context, "\t", snap.TBool(False)) # graph will be an object of type snap.PNGraph graph = snap.ToGraph(snap.PNGraph, sample_table, "srcID", "dstID", snap.aaFirst) #no of nodes 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)
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
DegToCnt = snap.TIntPrV() snap.GetOutDegCnt(G, DegToCnt) degree_count = {} for item in DegToCnt: degree_count[item.GetVal1()] = item.GetVal2() OutDeg = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDeg) node_deg = {} for item in OutDeg: node_deg[item.GetVal1()] = item.GetVal2() max_deg_nodes = [k for k, v in node_deg.items() if v == max(node_deg.values())] # Output sentences print("Number of nodes with degree=7: {}".format(snap.CntOutDegNodes(G, 7))) print("Node id(s) with highest degree: ", end=" ") for node in max_deg_nodes: if node == max_deg_nodes[-1]: print(node) else: print(str(node) + ", ", end=" ") # Plot Degree Distribution plot_filename = 'deg_dist_' + graph_filename[:-6] + '.png' plot_filedir = os.path.join(plotpath, plot_filename) plt.figure() plt.scatter(list(degree_count.keys()), list(degree_count.values()), s=10) plt.xlabel("Degree") plt.ylabel("Number of nodes") plt.title("Degree Distribution ({})".format(graph_filename[:-6]))
# 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." % ( snap.CntOutDegNodes(wikiGraph, 0))) # 1.7 print("The number of nodes of zero in-degree is %s." % ( snap.CntInDegNodes(wikiGraph, 0))) # 1.8 outDegreeToCount = snap.TIntPrV() snap.GetOutDegCnt(wikiGraph, outDegreeToCount) numNodesLargeOutDegree = sum([item.GetVal2() for item in outDegreeToCount if item.GetVal1() > DEGREE_BOUNDARY]) print("The number of nodes with more than %s outgoing edges is %s." % ( DEGREE_BOUNDARY, numNodesLargeOutDegree)) # 1.9
# 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: print('There are {} nodes having 0 in-degree'.format( snap.CntInDegNodes(G1, 0))) # Answer 8: DegToCntV = snap.TIntPrV() snap.GetOutDegCnt(G1, DegToCntV) Out10_cnt = 0 for item in DegToCntV: if item.GetVal1() > 10: Out10_cnt += 1 print('There are {} nodes having >10 out-degree'.format(Out10_cnt)) # Answer 9:
snap.GetDegCnt(Graph, DegToCntV) N = Graph.GetNodes() X, Y = [item.GetVal1() for item in DegToCntV ], [np.true_divide(item.GetVal2(), N) for item in DegToCntV] ############################################################################ return X, Y G = snap.LoadEdgeList(snap.PNGraph, "team_data_bipartite.csv", 0, 1, ",") # if the graph is loan id to user id, Out/In deg count, and set item.GetVal2() to zero print "Loan nodes: %d" % (snap.CntInDegNodes(G, 0)) print "Lender nodes: %d" % (snap.CntOutDegNodes(G, 0)) print "Loan-Lender Edges: %d" % (G.GetEdges()) OutDegV = snap.TIntPrV() snap.GetNodeOutDegV(G, OutDegV) # Lender = [item.GetVal1() for item in OutDegV if item.GetVal2() == 0] # # NG = snap.TUNGraph.New() # j = 0 # for i in Lender: # print j # if not NG.IsNode(i): # NG.AddNode(i) # NodeVec = snap.TIntV() # snap.GetNodesAtHop(G, i, 2, NodeVec, False)
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) # number of nodes with in-degree < 10 count = 0 for i in range(10): count += snap.CntInDegNodes(wiki, i)
def ratioOutdeg1(G): return snap.CntOutDegNodes(G, 1) / float(G.GetNodes())