# 概览

• 使用场景
• 直观解释
• 计算 & 拓展
• 举个栗子（python）
• 算法复杂度

`最后真推荐歌了~`

# 使用场景

user-item graph

`这里就不考虑基于用户的cf了，确实基于用户的cf是可以发现摇滚和民谣的关系滴~`

Two objects are similar if they are related to similar objects.

image.png

# 直观解释

item-item关系图

item-item关系图

## 复现计算过程

6个用户收听5首歌的情景，通过log生成左边的二部图，再通过用户作为联系生成右边的item关系图

image.png

image.png

image.png

# 基于MR模型的计算方法

image.png

### 拓展的simrank

1. delta-simrank
2. simrank++

1. 深度优先搜索
1. 基于矩阵乘法的计算

# python的local Example

``````
r=0.8
max_iter=10
eps=1e-4
nodes = G.nodes()   #['1', '0', '3', '2', '4']

pred_func = G.predecessors if isinstance(G, nx.DiGraph) else G.neighbors
#  nodes_i      {'0': 1, '1': 0, '2': 3, '3': 2, '4': 4}
nodes_i = {nodes[i]: i for i in range(0, len(nodes))}

sim_prev = numpy.zeros(len(nodes))  # array([ 0.,  0.,  0.,  0.,  0.])
sim = numpy.identity(len(nodes))
# ###  单位矩阵
# array([[ 1.,  0.,  0.,  0.,  0.],
#        [ 0.,  1.,  0.,  0.,  0.],
#        [ 0.,  0.,  1.,  0.,  0.],
#        [ 0.,  0.,  0.,  1.,  0.],
#        [ 0.,  0.,  0.,  0.,  1.]])
# ###

# round 1
sim_prev = numpy.copy(sim)

for u, v in itertools.product(nodes, nodes):
if u==v:continue
u_ps, v_ps = pred_func(u), pred_func(v)
s_uv = 0
for u_n, v_n in itertools.product(u_ps, v_ps):
s_uv += sim_prev[nodes_i[u_n]][nodes_i[v_n]]
sim[nodes_i[u]][nodes_i[v]] = (r * s_uv) / (len(u_ps) * len(v_ps) + DIV_EPS)

``````

``````<?xml version="1.0" encoding="utf-8"?><graphml xmlns="[http://graphml.graphdrawing.org/xmlns](http://graphml.graphdrawing.org/xmlns)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xsi:schemaLocation="[http://graphml.graphdrawing.org/xmlns](http://graphml.graphdrawing.org/xmlns) [http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd](http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd)">
<graph edgedefault="directed">
<node id="0" />
<node id="1" />
<node id="2" />
<node id="3" />
<node id="4" />
<edge source="0" target="1" />
<edge source="2" target="0" />
<edge source="1" target="2" />
<edge source="0" target="3" />
<edge source="3" target="4" />
<edge source="4" target="3" />
</graph>
</graphml>
``````

# 算法复杂度

n_i：item的数量
n：item对的数量，大概是n_i * n_i
neighbor：每个item的平均邻居数量
d：每一对vid对里的结点邻居平均数量乘积，大概 neighbor * neighbor