Skip to content

Instantly share code, notes, and snippets.

@lgylym
Last active August 29, 2015 14:10
Show Gist options
  • Save lgylym/f6f01c847be13974a80c to your computer and use it in GitHub Desktop.
Save lgylym/f6f01c847be13974a80c to your computer and use it in GitHub Desktop.
pagerank, the simple case
def computePR(graph, oldPR):
newPR = {}
N = len(graph)
for node in graph:
pr = 0
for pred in graph[node][0]:
if len(graph[pred][1]) != 0:
pr += (oldPR[pred]+0.0)/len(graph[pred][1])
pr = 0.15 / N + 0.85 * pr
newPR[node] = pr
return newPR
#each node in graph is with two lists,
#node[0] is the list of incoming nodes,
#node[1] is the list of outgoing nodes.
graph = {1:[[2,3],[2,3]],2:[[1],[1]],3:[[1],[1]]}
oldPR = {}
for node in graph:
oldPR[node] = 1.0/len(graph)
for i in range(50):
newPR = computePR(graph, oldPR)
print newPR
oldPR,newPR = newPR, oldPR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment