Skip to content

Commit f1aac98

Browse files
committed
edited the logic of the get_suggested_users method
1 parent a5e0f01 commit f1aac98

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

src/graph/network_analysis.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,31 @@ def get_suggested_users(self, user_id):
5656
if not self.graph.has_node(user_id):
5757
return []
5858

59-
# Get current friends
60-
current_friends = set(self.graph.successors(user_id))
59+
suggestions = set()
6160

62-
# Get all users from graph
63-
all_users = set(self.graph.nodes())
61+
# Get direct followers of the user
62+
direct_followers = set(self.graph.adjacency_list[user_id])
6463

65-
suggestion_scores = {}
64+
# For each direct follower
65+
for follower in direct_followers:
66+
# Get their followers
67+
followers_of_follower = set(self.graph.adjacency_list[follower])
68+
suggestions.update(followers_of_follower)
6669

67-
for potential_friend in all_users:
68-
# Skip if it's the user themselves or already a friend
69-
if potential_friend == user_id or potential_friend in current_friends:
70-
continue
70+
# Excluding from suggestions:
71+
# 1. The user themselves
72+
suggestions.discard(user_id)
7173

72-
score = 0
74+
# 2. Direct followers of the user
75+
suggestions.difference_update(direct_followers)
7376

74-
# Number of mutual friends
75-
mutual_friends = self.get_mutual_users([user_id, potential_friend])
76-
score += len(mutual_friends)
77+
# 3. Users where the target user is already in their follower list
78+
suggestions = {
79+
suggestion for suggestion in suggestions
80+
if user_id not in self.graph.adjacency_list[suggestion]
81+
}
7782

78-
if score > 0:
79-
suggestion_scores[potential_friend] = score
80-
81-
# Sort by score and return user IDs
82-
sorted_suggestions = sorted(
83-
suggestion_scores.items(),
84-
key=lambda x: (-x[1], x[0])
85-
)
86-
87-
return [user_id for user_id, _ in sorted_suggestions]
83+
return list(suggestions)
8884

8985

9086
## Test items

0 commit comments

Comments
 (0)