A simple case and problem extension of the graph model based on the nearest neighbor method in the recommendation system - [leetcode 1311. Get the videos your friends have watched] problem solution

        In the previous reading notes, the graphical model based on the nearest neighbor method was mentioned . Coincidentally, I encountered such a question when I was brushing leetcode on the weekend, and I think it is very suitable for inspiration for the neighbor recommendation of the graph model. Afterwards, AC will issue it for your reference.

text

Please click on the link for the topic description: leetcode 1311. Get the videos your friends have watched

        The difficulty of this leetcode title is medium, and the essence is the shortest path problem. It mainly examines breadth-first search (BFS), Hash and sorting. Just pay attention to the following points:

  1. Record the visited nodes to avoid repeated visits - if A and B are friends, then A and B exist in each other's friend list
  2. BFS does not need to go to the deepest level, just traverse as many levels as the input parameters
  3. The results are sorted first by value and then by key. The rough way is to sort the keys first, and then sort the values, but this is equivalent to performing two sorts. The efficiency is low, and a better way is to build a priority queue.

Running results and code

Show off my running results first

insert image description here

python code

In the final result, I used two sorts to achieve the sorting effect expected by the topic, but it is actually relatively inefficient. It would be better to use a priority queue.

class Solution(object):
     def watchedVideosByFriends(self, watched_videos, friends, id, level):
        """
        :type watched_videos: List[List[str]]
        :type friends: List[List[int]]
        :type id: int
        :type level: int
        :rtype: List[str]
        """
        user_depth = {
    
    id: 0}
        has_friends_count = len(friends)
        q = [id]

        level_watched_videos = {
    
    }

        while len(q) > 0:
            cur_user_id = q.pop(0)
            depth = user_depth[cur_user_id] + 1

            if user_depth[cur_user_id] == level:
                for video in watched_videos[cur_user_id]:
                    if level_watched_videos.__contains__(video):
                        level_watched_videos[video] += 1
                    else:
                        level_watched_videos[video] = 1

            if cur_user_id < has_friends_count and depth <= level:
                for friend_user_id in friends[cur_user_id]:
                    if not user_depth.__contains__(friend_user_id):
                        user_depth[friend_user_id] = depth
                        q.append(friend_user_id)

        res = sorted(level_watched_videos.items(), key=lambda obj: obj[0])
        res = sorted(res, key=lambda obj: obj[1])
        ret = []
        for video in res:
            ret.append(video[0])

        return ret

Problem extension

        This question finally found all the works that friends at the first level had seen. In fact, this is also the user-based neighbor recommendation model in the recommendation system—the neighbors are defined by the friend list. The lower the friend's level and the closer the neighbor, the higher the probability that the video they have watched is what the current user needs.

        The model shown in this question is rough. In practice, if we want to recommend neighbors, we must at least consider the following issues:

  1. The distance from a friend is defined as level=1, the friend of a friend is defined as level=2, and so on. Obviously, there is a threshold for level. When the level exceeds a certain threshold, it can be considered that it has nothing to do with the origin (current user). Past works may not be considered . This threshold can be judged based on actual needs, or can be obtained based on experience or machine learning.
  2. The videos watched by friends with lower levels are more likely to meet the needs of current users. At the same time, the more videos watched by friends, the higher the possibility of meeting the needs of the current user. The key is to realize that different items are of different importance to the same user .
  3. When there are a lot of videos, it is not necessary for users to browse all the recommended videos. In fact, it is enough to push the k videos with the highest probability to users. This is also a manifestation of the top-K problem in the recommendation system.
  4. A mathematical model can be formed based on the user level, the number of friends who have seen together, and many other factors. Based on this model, the weights (user-item) of each edge in the graph model of the neighbors can be obtained. At this time, the problem can be expressed is the shortest path problem for weighted graphs .

The specific solution to each point is actually to adjust the calculation model of the weight. It needs to be discussed in detail according to the specific situation, and here is a tentative thought.

Guess you like

Origin blog.csdn.net/qq_23937195/article/details/103952034