You are given a social graph and a User class that stores a user id and the set of direct friends for each user. Complete a recommendation module with the following parts:
-
Fix
valid_recommend(user, candidate)
so it returns
false
when the candidate is the same as the user or is already a direct friend, and
true
only for valid unseen candidates.
-
Implement
random_recommend(user, k)
to return up to
k
valid recommended users chosen uniformly at random from all valid candidates.
-
Implement
top_k_mutual_friend_recommend(user, k)
that ranks valid candidates by the number of mutual friends with the target user, breaks ties deterministically, and returns the top
k
.
Explain the time complexity of your recommendation approach and the data structures you use.