HITS 算法分析与实现
本文转载自:https://blog.csdn.net/hguisu/article/details/8013489
1、内容详细介绍参见以下博文
2、代码实现
from pygraph.classes.digraph import digraph
from math import sqrt
class HITSIterator:
__doc__ = '''计算图中的Hub,Authority值'''
def __init__(self, dg):
self.max_iterators = 100 # 最大迭代次数
self.min_delta = 0.0001 # 判断是否迭代结束
self.graph = dg
self.hub = {}
self.authority = {}
for node in self.graph.nodes():
self.hub[node] = 1
self.authority[node] = 1
def hits(self):
"""
计算每个页面的Hub,Authority值
"""
if not self.graph:
return
flag = False
for i in range(self.max_iterators):
change = 0.0 # 变化量
norm = 0
tmp = {}
tmp = self.authority.copy()
# 计算每个页面的authority值
for node in self.graph.nodes():
self.authority[node] = 0
for incident_page in self.graph.incidents(node): # 入射边
self.authority[node] += self.hub[incident_page]
norm += pow(self.authority[node], 2)
# 标准化
norm = sqrt(norm)
for node in self.graph.nodes():
self.authority[node] /= norm
change += abs(tmp[node] - self.authority[node])