-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagerank_example.py
More file actions
35 lines (27 loc) · 965 Bytes
/
pagerank_example.py
File metadata and controls
35 lines (27 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# pagerank file example to use for final project. acutal pagerank used is in project/pagerank.
def pagerank(graph, epsilion = 0.02, p = 0.8):
inbounds = {}
keep_going_flag = True
n = len(graph)
# initialize each of the values to a set number
for key, value in graph.iteritems():
inbounds[key] = 0.25
# keep going until epsilon converges
while keep_going_flag:
for k, v in inbounds.iteritems():
inbounds[k] = (1-p)*(inbounds[k]/len(graph[k]))
if calculate_inbounds_epsilon(inbounds,epsilion,inbounds[k]):
print inbounds[k]*p/n
keep_going_flag = False
print "results converged"
return inbounds[k]*p/n
def calculate_inbounds_epsilon(inbounds,epsilion,pr):
# get difference of the sum of inbound values
# and the length of inbound values mult by pagerank
# to determine if new pagerank is in epsilion range
s = sum(inbounds.values())
b = len(inbounds.values())*pr
if (s-b)<=epsilion:
return True
else:
return False