def pageRank_components(g): print 'executing pagerank components ---- getting components for page rank' Components = snap.TCnComV() snap.GetWccs(g, Components) f = open('component_pr.txt', 'w') cgraphs = [] for com in Components: v = snap.TIntV() for ni in com: v.Add(ni) cgraphs.append(snap.GetSubGraph_PNGraph(g, v)) print 'components retrived for pagerank' f.write('Total components:' + str(len(cgraphs)) + '\n') for graph in cgraphs: if graph.GetNodes() == 2: continue sprank = snap.TIntFltH() snap.GetPageRank_PNGraph(graph, sprank) sprank.SortByDat(False) f.write( str(graph.GetNodes()) + ' ' + str(sprank[sprank.BegI().GetKey()]) + '\n') f.close() print 'finished writing pagerank components values'
def pagerank(g): print 'executing pagerank.' PRank = snap.TIntFltH() snap.GetPageRank_PNGraph(g, PRank) print 'page rank done' PRank.SortByDat(False) f = open('pagerank.txt', 'w') i = 0 for item in PRank: i += 1 f.write(str(item) + ' ' + str(PRank[item]) + str('\n')) f.close() print 'writte page rank values to file'