Skip to content

Commit

Permalink
getallsocialpath and questions
Browse files Browse the repository at this point in the history
  • Loading branch information
txiong000 committed Feb 7, 2019
1 parent 129d771 commit 777590a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions projects/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 11 additions & 10 deletions projects/graph/social/social.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 777590a

Please sign in to comment.