From bc0d6159f9526695e42e6a754d4c0132a27448ba Mon Sep 17 00:00:00 2001 From: Sahar Jafari Date: Wed, 10 Mar 2021 23:40:43 -0800 Subject: [PATCH 1/2] populate_graph --- projects/social/social.py | 74 +++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/projects/social/social.py b/projects/social/social.py index 8609d8800..5518e38a0 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -1,7 +1,12 @@ +import string +import random + + class User: def __init__(self, name): self.name = name + class SocialGraph: def __init__(self): self.last_id = 0 @@ -45,8 +50,20 @@ def populate_graph(self, num_users, avg_friendships): # !!!! IMPLEMENT ME # Add users + names = list(string.ascii_uppercase) + for i in range(num_users): + self.add_user(names[i]) # Create friendships + for user in self.users: + potential_friends = list(range(1, num_users + 1)) + potential_friends.remove(user) + random.shuffle(potential_friends) + # Need to modify the randomization of length + length = random.randint(0, 2) + for n in potential_friends[0:length+1]: + if user < n: + self.add_friendship(user, n) def get_all_social_paths(self, user_id): """ @@ -59,12 +76,57 @@ def get_all_social_paths(self, user_id): """ visited = {} # Note that this is a dictionary, not a set # !!!! IMPLEMENT ME + # create an empty queue + # enqueue the first path. + # while the q is not empty. + # dequeue the path. + # set a newuser_id to the last element in the path [-1] + # if the newuser_id is not in visited. + # set the visited at the key of newuser_id to the path. + # for every friend_id in the friendships at the key of newuser_id + # make a copy of the path called new_path. + # append the friend_id to the new_path. + # enqueue the new path. + # return the populated visited dict to the caller + q = [] + q.append(user_id) + length = len(self.friendships[user_id]) + 1 + # counter = 1 + + prevPath = set([user_id]) + + # TODO should remove checking the length. it's here to keep the loop short + while len(q) > 0 and length > 0: + print("----") + current = q.pop(0) + # print(prevPath) + # if current == user_id: + # print(list(self.friendships[current])) + # q.append(list(self.friendships[current])) + # else: + for i in self.friendships[current]: + print(i) + print(prevPath) + if i in visited: + prevPath = prevPath.union(visited[i]) + # print(visited[i]) + else: + prevPath.add(i) + q.append(i) + + if current not in visited: + visited[current] = prevPath + # else: + # combine paths of the prev nodes + # counter = counter + 1 + length = length - 1 + return visited -if __name__ == '__main__': - sg = SocialGraph() - sg.populate_graph(10, 2) - print(sg.friendships) - connections = sg.get_all_social_paths(1) - print(connections) +# if __name__ == '__main__': +sg = SocialGraph() +sg.populate_graph(10, 2) +print(sg.friendships) +connections = sg.get_all_social_paths(1) +print(connections) From 10927fd085bd541da8f059c7e49526c131fa9d80 Mon Sep 17 00:00:00 2001 From: Sahar Jafari Date: Thu, 11 Mar 2021 17:00:09 -0800 Subject: [PATCH 2/2] get_all_social_paths --- projects/social/social.py | 98 +++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/projects/social/social.py b/projects/social/social.py index 5518e38a0..d1464907b 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -2,6 +2,23 @@ import random +class Queue: + def __init__(self): + self.storage = [] + + def enqueue(self, value): + self.storage.append(value) + + def dequeue(self): + if self.size() > 0: + return self.storage.pop(0) + else: + return None + + def size(self): + return len(self.storage) + + class User: def __init__(self, name): self.name = name @@ -50,20 +67,22 @@ def populate_graph(self, num_users, avg_friendships): # !!!! IMPLEMENT ME # Add users - names = list(string.ascii_uppercase) for i in range(num_users): - self.add_user(names[i]) + self.add_user((f"User {i+1}")) # Create friendships - for user in self.users: - potential_friends = list(range(1, num_users + 1)) - potential_friends.remove(user) - random.shuffle(potential_friends) - # Need to modify the randomization of length - length = random.randint(0, 2) - for n in potential_friends[0:length+1]: - if user < n: - self.add_friendship(user, n) + possible_friendships = [] + for user_id in self.users: + for friend_id in range(user_id + 1, self.last_id + 1): + possible_friendships.append((user_id, friend_id)) + + random.shuffle(possible_friendships) + + for i in range(num_users * avg_friendships // 2): + friendship = possible_friendships[i] + user_id = friendship[0] + friend_id = friendship[1] + self.add_friendship(user_id, friend_id) def get_all_social_paths(self, user_id): """ @@ -88,45 +107,26 @@ def get_all_social_paths(self, user_id): # append the friend_id to the new_path. # enqueue the new path. # return the populated visited dict to the caller - q = [] - q.append(user_id) - length = len(self.friendships[user_id]) + 1 - # counter = 1 - - prevPath = set([user_id]) - - # TODO should remove checking the length. it's here to keep the loop short - while len(q) > 0 and length > 0: - print("----") - current = q.pop(0) - # print(prevPath) - # if current == user_id: - # print(list(self.friendships[current])) - # q.append(list(self.friendships[current])) - # else: - for i in self.friendships[current]: - print(i) - print(prevPath) - if i in visited: - prevPath = prevPath.union(visited[i]) - # print(visited[i]) - else: - prevPath.add(i) - q.append(i) - - if current not in visited: - visited[current] = prevPath - # else: - # combine paths of the prev nodes - # counter = counter + 1 - length = length - 1 + q = Queue() + q.enqueue([user_id]) + + while q.size() > 0: + path = q.dequeue() + + newuser_id = path[-1] + if newuser_id not in visited: + visited[newuser_id] = path + for friend_id in self.friendships[newuser_id]: + new_path = path.copy() + new_path.append(friend_id) + q.enqueue(new_path) return visited -# if __name__ == '__main__': -sg = SocialGraph() -sg.populate_graph(10, 2) -print(sg.friendships) -connections = sg.get_all_social_paths(1) -print(connections) +if __name__ == '__main__': + sg = SocialGraph() + sg.populate_graph(10, 2) + print(sg.friendships) + connections = sg.get_all_social_paths(1) + print(connections)