def pageRank(): fileName = input("Please enter desired file name") #must know file name to open with open(fileName) as f: #assuming in same directory A = [] #empty primary array for line in f: line = line.split() #removing spaces if line: #eliminating blank lines line = [float(i) for i in line] #using float because of rank A.append(line) #adds into (double) array vertex = [] #for vertex IDs n = len(A) #number of vertices for v in range(n): #for number of vertices vertex.append(int(A[v][0])) #entering in IDs adjList = [-1] * 200 #initializing adjacency list for index in range(200): #for all indices in list for a in range(len(vertex)): #for all IDs if index == vertex[a]: #if the index matches the ID adjList[index] = A[a] #information for vertex is entered id = adjList[index][0] #just bc is used more than once edge = [] #array for edge grouping L = [] #array for edges for b in range(1,len(adjList[index])): #for all poss edge values edge.append(int(adjList[index][b])) #creates set L.append(edge) #adds set to edge array rank = [1/n] * a #initializing rank array sum = 0 #for sum of edge for t in range (0,50): #to iterate 50 times for e in range(len(L[a])): #number of edges if vertex[a] == L[index][e]: #to find ranks of linked vertices u = L[index][e] sum += rank[u][t]/adjList[u][1] #sum of ranks divided by the linked vertices' out degrees x = t + 1 rank[a][x] = 0.9*sum + 0.1/n #algorithm ask = input("Enter 0 to display results on the screen, enter 1 to write to a file.") if int(ask) == 0: print(id,"(",rank[a][50],"):",str(L[a]).strip('[]')) #displays results else: with open(Pagerank.txt) as p: p.write(id,"(",rank[a][50],"):",str(L[a]).strip('[]')) #writes to file
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question