Skip to content

Instantly share code, notes, and snippets.

@marshareb
Last active February 8, 2017 00:12
Show Gist options
  • Save marshareb/763d4e6ecfca952eb7f25ad79e4dc0b2 to your computer and use it in GitHub Desktop.
Save marshareb/763d4e6ecfca952eb7f25ad79e4dc0b2 to your computer and use it in GitHub Desktop.
Does google's pagerank algorithm on a directed graph. Requires networkx for the graphs.
try:
import networkx as nx
except ImportError:
raise ImportError('Requires networkx')
def pageranker(g, damp, iterations):
#Does this for a set amount of iterations.
#In the future, may add convergence conditions.
#Not optimal for large graphs. Should probably store the connected_to list somewhere instead of calling it each time.
dict_of_ranks = {}
prior_values_of_rank = {}
for i in g.nodes():
dict_of_ranks[i] = 1
prior_values_of_rank[i] = 1
for k in range(iterations):
for i in g.nodes():
pagerank_of_node = 0
#PR(x) = (1-d) + d*(PR(T1)/C(T1) + ... + PR(TN)/C(TN)) for T1,...,TN connected to x.
#PR = pagerank
#C(Ti) = outdegree of Ti
if len(connected_to(g, i)) == 0:
pagerank_of_node = (1-damp)
else:
for j in connected_to(g, i):
pagerank_of_node += (dict_of_ranks[j]/g.out_degree(j))
pagerank_of_node = pagerank_of_node * damp
pagerank_of_node += (1-damp)
prior_values_of_rank[i] = pagerank_of_node
for i in prior_values_of_rank:
dict_of_ranks[i] = prior_values_of_rank[i]
for i in dict_of_ranks:
dict_of_ranks[i] = dict_of_ranks[i]/len(g.nodes())
return dict_of_ranks
def connected_to(g, node):
list_of_connected = []
for i in g.nodes():
if node in g[i]:
list_of_connected.append(i)
return list_of_connected
@marshareb
Copy link
Author

Note this gives an estimate, it does not give an accurate value. Leaving room to further update to make it more accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment