[英]Friends recommendation algorithm with O(n) time complexity in JavaScript
const data = [
{
name: 'Bob',
friends: ['Alice', 'Eve', 'Charlie']
},
{
name: 'Alice',
friends: ['Bob', 'Dan']
},
{
name: 'Dan',
friends: ['Alice', 'Eve']
},
{
name: 'Charlie',
friends: ['Bob']
}
];
function get_top_k_recommended_friends(name, cutoff) {
}
get_top_k_recommended_friends('Alice', 2) // [Eve, Charlie]
get_top_k_recommended_friends('Alice', 1) // [Eve]
这个 function 接受两个整数(user_id,cutoff_k)并向这个特定的 user_id 提供新朋友的推荐(表示为整数列表)。 推荐朋友的定义如下:
A recommended friend is a friend who has mutual friends with the original user_id’s friends.
For example, assume Alice is friends with Bob and Bob is friends with Eve but Alice is not friends with Eve. So when you call get_recommended_friends(Alice), you get Eve. You also get Alice if you call get_recommended_friends(Eve).
If Bob also is friends with Charlie but Alice is not friends with Charlie, then calling get_recommended_friends(Alice) should yield [Eve, Charlie].
编写 get_recommended_friends 的两个重要要求是
根据提供的数据调用 get_top_k_recommended_friends(Alice, 2) 应该产生 [Eve, Charlie] 其中 Eve 在 Charlie 之前被订购,因为 Eve 是 Alice 的两个朋友(Bob 和 Dan)的朋友,而 Charlie 只是 Alice 的一个朋友的朋友(鲍勃)。 get_top_k_recommended_friends(Alice, 1) 将产生 [Eve]。
图上与根(原始用户)的距离为 2 并且连接到至少一个距离为 1 的节点的节点应该是共同的朋友。
(例如 Eve 和 Charlie 与 Alice 的距离为 2,并且都与距离为 1 的节点相连(Bob 和 Dan))
因此,对于距离为 2 ( f2
) 的每个朋友,您可以计算它连接到的距离为 1 的节点的数量 ( mutual_count
)。
get_top_k_recommended_friends(user, cutoff)
for f1 in friends[user]:
for f2 in friends[f1] - friends[user]: // friends of f1 but not of user
for f3 in friends[f2]: // check each edge(f2, f3)
if edge(f2, f3) not visited && f3 != f1 && f3 in friends[user]:
// f2 is a mutual friend of f1 and f3
// f1 and f3 are both friends of user, so f2 is a recommended friend
mutual_count[f2] += 1
mark edge(f2, f3) and edge(f3, f2) as visited
return k_largest(mutual_count, cutoff)
检查集合中是否存在元素可以在 O(1) 中完成。 该算法只会访问距离根(及其边缘)距离为 1 和 2 的朋友,因此return k_largest(mutual_count, cutoff)
之前的所有内容都应该是 O(n+m),其中 n 和 m 是上述节点的数量,并且边缘,分别。
对于k_largest
,可以使用quickselect算法找到第 k 个最大值,然后过滤掉并排序 k 个最大值,其平均复杂度应该是复杂度 O(n + k log k)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.