def load_genre_graphs(graph): if graph == "rock": filename = '../data/genre_graphs/rock_graph/rock_graph.txt' dict = chords_dict_rock else: filename = '../data/genre_graphs/jazz_graph/jazz_graph.txt' dict = chords_dict_jazz G_Multi = snap.LoadEdgeList(snap.PNEANet, filename, 0, 1) G_Directed = snap.LoadEdgeList(snap.PNGraph, filename, 0, 1) G_Undirected = snap.LoadEdgeList(snap.PUNGraph, filename, 0, 1) #return G_Multi, G_Directed, G_Undirected, dict return snap.GetKCore(G_Multi, 3), snap.GetKCore(G_Directed, 3), snap.GetKCore(G_Undirected, 3), dict
def main(genre): G_Multi, G_Directed, G_Undirected, dict = load_genre_graphs(genre) # clean up graph print(G_Undirected.GetNodes()) G_Undirected = snap.GetKCore(G_Undirected, 3) print(G_Undirected.GetNodes()) NIdEigenH = snap.TIntFltH() snap.GetEigenVectorCentr(G_Undirected, NIdEigenH) centralities = [] for item in NIdEigenH: centralities.append((dict[item], NIdEigenH[item])) centralities.sort(reverse=True, key=lambda l: l[1]) for centrality in centralities: print centrality
print "G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges()) # convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) print "G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges()) # get largest weakly connected component WccG = snap.GetMxWcc(G6) # generate a network using Forest Fire model G8 = snap.GenForestFire(1000, 0.35, 0.35) print "G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges()) # get a subgraph induced on nodes {0,1,2,3,4} SubG = snap.GetSubGraph(G8, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G8 Core3 = snap.GetKCore(G8, 3) print "Core3: Nodes %d, Edges %d" % (Core3.GetNodes(), Core3.GetEdges()) # delete nodes of out degree 3 and in degree 2 snap.DelDegKNodes(G8, 3, 2) # create a directed random graph on 10k nodes and 1k edges G9 = snap.GenRndGnm(snap.PNGraph, 10000, 1000) print "G9: Nodes %d, Edges %d" % (G9.GetNodes(), G9.GetEdges()) # define a vector of pairs of integers (size, count) and # get a distribution of connected components (component size, count) CntV = snap.TIntPrV() snap.GetWccSzCnt(G9, CntV) for p in CntV: print "size %d: count %d" % (p.GetVal1(), p.GetVal2())
#G6 = snap.GenForestFire(100000, 0.25, 0.25) G6 = snap.GenRndGnm(snap.PNGraph, 10000, 5000) print "type(G6) %s" % (type(G6)) print "G6 nodes %d, edges %d" % (G6.GetNodes(), G6.GetEdges()) G7 = snap.ConvertGraph(snap.PUNGraph, G6) print "type(G7) %s" % (type(G7)) print "G7 nodes %d, edges %d" % (G7.GetNodes(), G7.GetEdges()) WccG6 = snap.GetMxWcc(G6) print "type(WccG6) %s" % (type(WccG6)) print "WccG6 nodes %d, edges %d" % (WccG6.GetNodes(), WccG6.GetEdges()) G8 = snap.GenForestFire(1000, 0.35, 0.35) print "type(G8) %s" % (type(G8)) print "G8 nodes %d, edges %d" % (G8.GetNodes(), G8.GetEdges()) SubG8 = snap.GetSubGraph(G8, snap.TIntV.GetV(0,1,2,3,4)) print "type(SubG8) %s" % (type(SubG8)) print "SubG8 nodes %d, edges %d" % (SubG8.GetNodes(), SubG8.GetEdges()) Core3G8 = snap.GetKCore(G8, 3) print "type(Core3G8) %s" % (type(Core3G8)) print "CoreG8 nodes %d, edges %d" % (Core3G8.GetNodes(), Core3G8.GetEdges()) snap.DelDegKNodes(G8, 3, 2) print "type(G8) %s" % (type(G8)) print "G8 nodes %d, edges %d" % (G8.GetNodes(), G8.GetEdges())
def intro(): # create a graph PNGraph G1 = snap.TNGraph.New() G1.AddNode(1) G1.AddNode(5) G1.AddNode(32) G1.AddEdge(1, 5) G1.AddEdge(5, 1) G1.AddEdge(5, 32) print("G1: Nodes %d, Edges %d" % (G1.GetNodes(), G1.GetEdges())) # create a directed random graph on 100 nodes and 1k edges G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000) print("G2: Nodes %d, Edges %d" % (G2.GetNodes(), G2.GetEdges())) # traverse the nodes for NI in G2.Nodes(): print("node id %d with out-degree %d and in-degree %d" % (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg())) # traverse the edges for EI in G2.Edges(): print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())) # traverse the edges by nodes for NI in G2.Nodes(): for Id in NI.GetOutEdges(): print("edge (%d %d)" % (NI.GetId(), Id)) # generate a network using Forest Fire model G3 = snap.GenForestFire(1000, 0.35, 0.35) print("G3: Nodes %d, Edges %d" % (G3.GetNodes(), G3.GetEdges())) # save and load binary FOut = snap.TFOut("test.graph") G3.Save(FOut) FOut.Flush() FIn = snap.TFIn("test.graph") G4 = snap.TNGraph.Load(FIn) print("G4: Nodes %d, Edges %d" % (G4.GetNodes(), G4.GetEdges())) # save and load from a text file snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges") G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1) print("G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges())) # generate a network using Forest Fire model G6 = snap.GenForestFire(1000, 0.35, 0.35) print("G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) print("G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges())) # get largest weakly connected component of G WccG = snap.GetMxWcc(G6) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G Core3 = snap.GetKCore(G6, 3) # delete nodes of out degree 10 and in degree 5 snap.DelDegKNodes(G6, 10, 5) print("G6a: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3 G8 = snap.GenPrefAttach(1000, 3) print("G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges())) # vector of pairs of integers (size, count) CntV = snap.TIntPrV() # get distribution of connected components (component size, count) snap.GetWccSzCnt(G8, CntV) # get degree distribution pairs (degree, count) snap.GetOutDegCnt(G8, CntV) # vector of floats EigV = snap.TFltV() # get first eigenvector of graph adjacency matrix snap.GetEigVec(G8, EigV) # get diameter of G8 snap.GetBfsFullDiam(G8, 100) # count the number of triads in G8, get the clustering coefficient of G8 snap.GetTriads(G8) snap.GetClustCf(G8)
SccG6 = snap.GetMxScc(G6) print("type(SccG6) %s" % (type(SccG6))) print("SccG6 nodes %d, edges %d" % (SccG6.GetNodes(), SccG6.GetEdges())) SccG7 = snap.GetMxScc(G7) print("type(SccG7) %s" % (type(SccG7))) print("SccG7 nodes %d, edges %d" % (SccG7.GetNodes(), SccG7.GetEdges())) SubG6 = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) print("type(SubG6) %s" % (type(SubG6))) print("SubG6 nodes %d, edges %d" % (SubG6.GetNodes(), SubG6.GetEdges())) for EI in SubG6.Edges(): print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())) Core3G6 = snap.GetKCore(G6, 3) print("type(Core3G6) %s" % (type(Core3G6))) print("CoreG6 nodes %d, edges %d" % (Core3G6.GetNodes(), Core3G6.GetEdges())) G8 = snap.GenRndGnm(snap.PNGraph, 1000, 100000) print("type(G8) %s" % (type(G8))) print("G8 nodes %d, edges %d" % (G8.GetNodes(), G8.GetEdges())) WccG8 = snap.GetMxWcc(G8) print("type(WccG8) %s" % (type(WccG8))) print("WccG8 nodes %d, edges %d" % (WccG8.GetNodes(), WccG8.GetEdges())) WccG7 = snap.GetMxWcc(G7) print("type(WccG7) %s" % (type(WccG7))) print("WccG7 nodes %d, edges %d" % (WccG7.GetNodes(), WccG7.GetEdges()))
def get_k_core(graph, K=5): KCore = snap.GetKCore(graph, K)
for Val in EigV: print Val # get diameter of G8 print "Diameter of the network =", snap.GetBfsFullDiam(G1, 100) TriadV = snap.TIntTrV() # count the number of triads in G8, get the clustering coefficient of G8 print "Number of Triads in the network = ", snap.GetTriads(G1, TriadV) for triple in TriadV: print "For Node Id: ", triple.Val1( ), ",Number of open Triads are: ", triple.Val2( ), ", Number of closed Triads are: ", triple.Val3() print "Clustering Co-efficient of the network =", snap.GetClustCf(G1) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G1, snap.TIntV.GetV(2, 4, 6, 8, 10, 12, 14, 16, 20)) snap.DrawGViz(SubG, snap.gvlDot, "recosub.png", "Partial Network Diagram", True, snap.TIntStrH()) # get 3-core of G # get largest weakly connected component WccG = snap.GetMxWcc(G1) snap.DrawGViz(WccG, snap.gvlDot, "weakconn.png", "largest weakly connected component", True, snap.TIntStrH()) Core3 = snap.GetKCore(SubG, 3) snap.DrawGViz(Core3, snap.gvlDot, "recocore.png", "Partial Network Diagram", True, snap.TIntStrH())
FILE_LOG_NAME = 'LOG_File_' + (FILE_NAME[-1]) + (KCORE_VALUES) + ('.log') LOG_FILE = open(FILE_LOG_NAME, 'w') TEMP_INFO = 'Starting Community Detection Section on FILE [ ' + ( FILE_PATH) + ' ] ' + 'with k_core value ' + (KCORE_VALUES) LOG_FILE.write(TEMP_INFO) LOG_FILE.write('\n') G = snap.LoadEdgeList(snap.PUNGraph, FILE_PATH, 0, 1) LOG_FILE.write('Transaction: Parse External File Successful. \t') START_TIME = time.time() LOG_FILE.write('Finish Time: %f' % START_TIME) LOG_FILE.write('\n') #print G NodeV_G = create_node_vector(G) Kcore = snap.GetKCore(G, int(KCORE_VALUES)) NodeV_Kcore = create_node_vector(Kcore) #print NodeV_Kcore.Len() LOG_FILE.write('Transaction: k-core Search Successful. \t') LOG_FILE.write('Finish Time: %f' % time.time()) LOG_FILE.write('\n') #print NodeV_Kcore NodeV_Diff = get_diff_from_kcore(NodeV_G, NodeV_Kcore) SortH = sort_by_neighbor(NodeV_Diff, Kcore, G) Partition_KcoreH = community_partition(Kcore) LOG_FILE.write('Transaction: Partition on K-core Successful. \t') LOG_FILE.write('Finish Time: %f' % time.time()) LOG_FILE.write('\n')
G4 = snap.TNGraph.Load(FIn) # save and load from a text file snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges") G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1) # %% # Manipulate # generate a network using Forest Fire model G6 = snap.GenForestFire(1000, 0.35, 0.35) # convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) WccG = snap.GetMxWcc(G6) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G Core3 = snap.GetKCore(G6, 3) # delete nodes of out degree 10 and in degree 5 snap.DelDegKNodes(G6, 10, 5) # %% # stats # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3 G8 = snap.GenPrefAttach(1000, 3) # vector of pairs of integers (size, count) CntV = snap.TIntPrV() # get distribution of connected components (component size, count) snap.GetWccSzCnt(G8, CntV) # get degree distribution pairs (degree, count) snap.GetOutDegCnt(G8, CntV) # vector of floats EigV = snap.TFltV()