简体   繁体   English

JavaScript中时间复杂度为O(n)的好友推荐算法

[英]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 的两个重要要求是

  1. The returned list of recommended friends must be sorted by the most number of mutual friends they have with the requested user返回的推荐好友列表必须按照他们与请求用户的最多共同好友数量排序
  2. they should only return top k recommend friends (k is a cutoff)他们应该只返回前 k 个推荐的朋友(k 是一个截止)

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.

相关问题 这个算法的时间和空间复杂度是 O(n) 还是 O(1)? - Does this algorithm have time and space complexity O(n) or O(1)? 该函数的时间复杂度是O(N)还是O(N ^ 2)? - Is the time complexity for this function O(N) or O(N^2)? 如何在javascript中返回O(n)时间复杂度相同的字母元素? - how to return same letters elements with O(n) time complexity in javascript? 为什么这个阶乘查找算法的运行时复杂度不是 O(n!)? - Why isn't this factorial finding algorithm O(n!) run-time complexity? Javascript - 在时间复杂度为 O(n) 且空间复杂度为 O(1) 的给定字符串中删除交替重复字符的最佳方法是什么? - Javascript - What is the best way to remove alternate repeating character in a given string with time complexity O(n) and Space Complexity was O(1)? Javascript-查找字谜的更好解决方案-时间复杂度O(n log n) - Javascript - Better solution for finding anagrams - Time complexity O (n log n) 这个函数的时间复杂度是否为O(log n)? - Is the time complexity of this function O(log n)? 简化O(nm)和O(n + m)时间复杂度 - Simplifying O(nm) and O(n + m) time complexity 此解决方案O(N)或O(LogN)的时间复杂度是多少? - What is time complexity of this solution O(N) or O(LogN)? 我对最小差分算法问题的解决方案是否具有最佳时空复杂度 (O(nLog(n) + mLog(m)))? - Does my solution to the Smallest Difference algorithm problem have optimal space time complexity (O(nLog(n) + mLog(m)))?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM