Skip to content

Commit 5b9a5b5

Browse files
committed
updated
1 parent 26cdf84 commit 5b9a5b5

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Next exercise proposition:
2222

2323
Where N = number of time that the user has share something with that user.
2424

25-
- If distance between 2 users that never shared directly, is less than 40, that user should appears as suggestion.
26-
- If distance between 2 users that never shared directly, is less than 10, that user should appear as recommendation as friend.
25+
- If distance between 2 users that never shared directly, is less or equal than 40, that user should appears as suggestion.
26+
- If distance between 2 users that never shared directly, is less or equal than 10, that user should appear as recommendation as friend.

exercise.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import heapq
2+
import networkx as nx
3+
import matplotlib.pyplot as plt
4+
5+
class User:
6+
def __init__(self, name):
7+
self.name = name
8+
self.shared_with = {} # Dictionary to keep track of shared times with other users
9+
10+
def update_distance(user1, user2):
11+
# Update shared times between user1 and user2
12+
if user2.name not in user1.shared_with:
13+
user1.shared_with[user2.name] = 1
14+
else:
15+
user1.shared_with[user2.name] += 1
16+
#user2.shared_with[user1.name] += 1
17+
18+
if user1.name not in user2.shared_with:
19+
user2.shared_with[user1.name] = 1
20+
else:
21+
user2.shared_with[user1.name] += 1
22+
23+
def dijkstra(graph, start):
24+
distances = {node.name: float('infinity') for node in graph}
25+
distances[start.name] = 0
26+
shortest_paths = {}
27+
28+
priority_queue = [(0, start.name)]
29+
30+
while priority_queue:
31+
current_distance, current_user_name = heapq.heappop(priority_queue)
32+
current_user = next(user for user in graph if user.name == current_user_name)
33+
34+
if current_distance > distances[current_user_name]:
35+
continue
36+
37+
for neighbor, weight in graph[current_user].items():
38+
neighbor_name = neighbor.name
39+
distance = current_distance + weight
40+
if distance < distances[neighbor_name]:
41+
distances[neighbor_name] = distance
42+
shortest_paths[neighbor_name] = current_user_name
43+
heapq.heappush(priority_queue, (distance, neighbor_name))
44+
45+
return distances, shortest_paths
46+
47+
def calculate_distance(shared_times):
48+
if shared_times == 0:
49+
return 100 # Initial distance
50+
else:
51+
return 100 / (shared_times + 1)
52+
53+
def find_suggestions(users, distances):
54+
suggestions = []
55+
for user1 in users:
56+
for user2 in users:
57+
if user1 != user2 and distances[user1.name][user2.name] < 40:
58+
suggestions.append(user2.name)
59+
return suggestions
60+
61+
def find_friend_recommendations(users, distances):
62+
recommendations = []
63+
for user1 in users:
64+
for user2 in users:
65+
if user1 != user2 and distances[user1.name][user2.name] <= 10:
66+
recommendations.append(user2.name)
67+
return recommendations
68+
69+
def draw_graph(graph, suggestions, recommendations):
70+
G = nx.DiGraph()
71+
72+
for user, edges in graph.items():
73+
for friend, weight in edges.items():
74+
G.add_edge(user.name, friend.name, weight=weight, label=f"{weight:.2f}") # Add edge labels
75+
76+
pos = nx.spring_layout(G, seed=42)
77+
nx.draw(G, pos, with_labels=True, node_size=1000, node_color='lightblue', font_size=10)
78+
79+
# Draw edge labels
80+
edge_labels = nx.get_edge_attributes(G, 'label')
81+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
82+
83+
# Highlight suggested users
84+
nx.draw_networkx_nodes(G, pos, nodelist=suggestions, node_color='lightgreen', node_size=1500)
85+
86+
# Highlight recommended users
87+
nx.draw_networkx_nodes(G, pos, nodelist=recommendations, node_color='lightcoral', node_size=1500)
88+
89+
plt.title("Users and Recommendations")
90+
plt.show()
91+
92+
# Example usage, simulating some users:
93+
users = [User("Alice"), User("Bob"), User("Charlie"), User("Jhon")]
94+
95+
#creation of some simulated interaction between users:
96+
update_distance(users[0], users[1]) # Alice shares with Bob
97+
update_distance(users[1], users[2]) # Bob shares with Charlie
98+
update_distance(users[2], users[0]) # Charlie shares with Alice
99+
update_distance(users[1], users[0]) # Bob shares again with Alice
100+
update_distance(users[1], users[0]) # Bob shares again with Alice
101+
update_distance(users[1], users[0]) # Bob shares again with Alice
102+
update_distance(users[0], users[1]) # Alice shares with Bob
103+
update_distance(users[1], users[0]) # Bob shares again with Alice
104+
update_distance(users[1], users[2]) # Bob shares with Charlie
105+
update_distance(users[0], users[1]) # Alice shares with Bob
106+
update_distance(users[0], users[1]) # Alice shares with Bob
107+
update_distance(users[1], users[0]) # Bob shares again with Alice
108+
update_distance(users[0], users[3]) # Alice shares with Jhon
109+
110+
# Modify the graph creation to use user objects as keys
111+
graph = {user: {} for user in users}
112+
for user1 in users:
113+
for user2 in users:
114+
if user1 != user2:
115+
shared_times = user1.shared_with.get(user2.name, 0)
116+
distance = calculate_distance(shared_times)
117+
graph[user1][user2] = distance
118+
119+
# Apply Dijkstra's algorithm to find shortest paths
120+
distances = {}
121+
for user in users:
122+
dist, _ = dijkstra(graph, user)
123+
distances[user.name] = dist
124+
125+
# Find suggestions and friend recommendations
126+
suggestions = find_suggestions(users, distances)
127+
recommendations = find_friend_recommendations(users, distances)
128+
129+
130+
print("Suggestions:", suggestions)
131+
print("Friend recommendations:", recommendations)
132+
133+
# Draw graph with suggestions and recommendations
134+
draw_graph(graph, suggestions, recommendations)

0 commit comments

Comments
 (0)