#!/usr/bin/env/python3# -*- coding: utf-8 -*-import time
defPersonaLRank(G, alpha, root, max_step):
rank =dict()
rank ={x:0for x in G.keys()}
rank[root]=1
sr =[str(x)for x in rank.keys()]print('\t'.join(sr))for k inrange(max_step):# print("*******%d***********" % k)
temp ={x:0for x in G.keys()}for i, ri in G.items():for j, wij in ri.items():if j notin temp:
temp[j]=0
temp[j]+= alpha * rank[i]/(1*len(ri))if j == root:
temp[j]+=1- alpha
rank = temp
# lst = sorted(rank.items(), key=lambda x: x[1], reverse=True)# for ele in lst:# print("%s:%.3f" % (ele[0], ele[1]))# print(lst)
lst =[]for r in sr:
lst.append(str(rank[r]))print('\t'.join(lst))return rank
if __name__ =="__main__":
start = time.time()
alpha =0.8# 大写字母表示用户,小写字母表示物品
G ={'A':{'a':1,'c':1},'B':{'a':1,'b':1,'c':1,'d':1},'C':{'c':1,'d':1},'a':{'A':1,'B':1},'b':{'B':1},'c':{'A':1,'B':1,'C':1},'d':{'B':1,'C':1}}
PersonaLRank(G, alpha,'b',30)
end = time.time()
cost_time = end - start
print("cost_time: ", cost_time)