[英]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]
This function accepts two integers (user_id, cutoff_k) and provides recommendations of new friends (represented as a list of integers) to this particular user_id.这个 function 接受两个整数(user_id,cutoff_k)并向这个特定的 user_id 提供新朋友的推荐(表示为整数列表)。 The definition of a recommended friend is as below:推荐朋友的定义如下:
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].
Two IMPORTANT requirements for writing get_recommended_friends is that编写 get_recommended_friends 的两个重要要求是
Based on the provided data calling get_top_k_recommended_friends(Alice, 2) should yield [Eve, Charlie] where Eve is ordered before Charlie as Eve is friends with two of Alice's friends (Bob and Dan) and Charlie is only friends with one of Alice's friends (Bob).根据提供的数据调用 get_top_k_recommended_friends(Alice, 2) 应该产生 [Eve, Charlie] 其中 Eve 在 Charlie 之前被订购,因为 Eve 是 Alice 的两个朋友(Bob 和 Dan)的朋友,而 Charlie 只是 Alice 的一个朋友的朋友(鲍勃)。 get_top_k_recommended_friends(Alice, 1) will yield [Eve]. get_top_k_recommended_friends(Alice, 1) 将产生 [Eve]。
Nodes on the graph that has a distance of 2 from the root(original user) and is connected to at least one node of distance 1 should be a mutual friend.图上与根(原始用户)的距离为 2 并且连接到至少一个距离为 1 的节点的节点应该是共同的朋友。
(eg Eve and Charlie are of distance 2 from Alice, and both are connected with nodes of distance 1 (Bob and Dan)) (例如 Eve 和 Charlie 与 Alice 的距离为 2,并且都与距离为 1 的节点相连(Bob 和 Dan))
Therefore, for each friend of distance 2 ( f2
), you can count how many nodes of distance 1 it is connected to ( mutual_count
).因此,对于距离为 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)
Checking if element exist in set can be done in O(1).检查集合中是否存在元素可以在 O(1) 中完成。 The algorithm will only visit friends of distance 1 and 2 from the root (and their edges), so everything before return k_largest(mutual_count, cutoff)
should be O(n+m), where n and m are the number of aforementioned nodes and edges, respectively.该算法只会访问距离根(及其边缘)距离为 1 和 2 的朋友,因此return k_largest(mutual_count, cutoff)
之前的所有内容都应该是 O(n+m),其中 n 和 m 是上述节点的数量,并且边缘,分别。
For k_largest
, you can use the quickselect algorithm to find the kth largest value, and then filter out and sort the k largest values, which should have an average complexity of complexity O(n + k log k)对于k_largest
,可以使用quickselect算法找到第 k 个最大值,然后过滤掉并排序 k 个最大值,其平均复杂度应该是复杂度 O(n + k log k)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.