From 777590adc347815c208e03e79a32ab4c0838ff8d Mon Sep 17 00:00:00 2001 From: txiong000 Date: Thu, 7 Feb 2019 17:15:52 -0600 Subject: [PATCH] getallsocialpath and questions --- projects/graph/README.md | 4 ++-- projects/graph/social/social.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/projects/graph/README.md b/projects/graph/README.md index a704252a46..187d917743 100644 --- a/projects/graph/README.md +++ b/projects/graph/README.md @@ -118,12 +118,12 @@ Note that in this sample, Users 3, 4 and 9 are not in User 1's extended social n ## 3. Questions 1. To create 100 users with an average of 10 friends each, how many times would you need to call `addFriendship()`? Why? - +100 * 10 / 2 = 500 times 2. If you create 1000 users with an average of 5 random friends each, what percentage of other users will be in a particular user's extended social network? What is the average degree of separation between a user and those in his/her extended network? - +log(1000) / log(5) - 4.3% degree of seperation ## 4. Stretch Goal diff --git a/projects/graph/social/social.py b/projects/graph/social/social.py index 0bc357ea49..4bc1984383 100644 --- a/projects/graph/social/social.py +++ b/projects/graph/social/social.py @@ -86,21 +86,22 @@ def getAllSocialPaths(self, userID): visited = {} # Note that this is a dictionary, not a set # !!!! IMPLEMENT ME - queue = [] - queue.append([userID]) - print(queue) - while len(queue) > 0: - path = queue.pop() + queue = Queue() + queue.enqueue([userID]) + while queue.size() > 0: + path = queue.dequeue() v = path[-1] - for child_v in self.friendships[v]: - new_path = list(path) - new_path.append(child_v) - visited[child_v] = new_path + if v not in visited: + visited[v] = path + for child_v in self.friendships[v]: + if child_v not in visited: + new_path = list(path) + new_path.append(child_v) + queue.enqueue(new_path) return visited - if __name__ == '__main__': sg = SocialGraph() sg.populateGraph(10, 2)