From 1784aa43ca49775dc88158cc2d14dcac484c9c22 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Wed, 11 Nov 2020 19:07:16 -0800 Subject: [PATCH 01/10] added traversal directions --- projects/adventure/adv.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index 8bc540b5e..9cae1bc20 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -10,10 +10,10 @@ # You may uncomment the smaller graphs for development and testing purposes. -# map_file = "maps/test_line.txt" -# map_file = "maps/test_cross.txt" -# map_file = "maps/test_loop.txt" -# map_file = "maps/test_loop_fork.txt" +map_file = "maps/test_line.txt" +map_file = "maps/test_cross.txt" +map_file = "maps/test_loop.txt" +map_file = "maps/test_loop_fork.txt" map_file = "maps/main_maze.txt" # Loads the map into a dictionary @@ -27,7 +27,7 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] -traversal_path = [] +traversal_path = ['n', 's', 'w', 'e'] From 49b3a85d96515cad66da4c78bb919ff4346c6e19 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Wed, 11 Nov 2020 19:27:19 -0800 Subject: [PATCH 02/10] Moving over to the correct project to do. --- projects/adventure/adv.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index 9cae1bc20..934922fef 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -51,12 +51,12 @@ ####### # UNCOMMENT TO WALK AROUND ####### -player.current_room.print_room_description(player) -while True: - cmds = input("-> ").lower().split(" ") - if cmds[0] in ["n", "s", "e", "w"]: - player.travel(cmds[0], True) - elif cmds[0] == "q": - break - else: - print("I did not understand that command.") +# player.current_room.print_room_description(player) +# while True: +# cmds = input("-> ").lower().split(" ") +# if cmds[0] in ["n", "s", "e", "w"]: +# player.travel(cmds[0], True) +# elif cmds[0] == "q": +# break +# else: +# print("I did not understand that command.") From 721b6af27aa4c1297b010f4e9d8ce786daa767d1 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Wed, 11 Nov 2020 19:50:21 -0800 Subject: [PATCH 03/10] Now, commiting methods. Working on traversals --- projects/graph/graph.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 59fecae4b..a08448cb4 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -13,19 +13,22 @@ def add_vertex(self, vertex_id): """ Add a vertex to the graph. """ - pass # TODO + self.vertices[vertex_id] = set() def add_edge(self, v1, v2): """ Add a directed edge to the graph. """ - pass # TODO + if v1 not in self.vertices or v2 not in self.vertices: + print("non-existing edge") + return + self.vertices[v1].add(v2) def get_neighbors(self, vertex_id): """ Get all neighbors (edges) of a vertex. """ - pass # TODO + return self.vertices[vertex_id] def bft(self, starting_vertex): """ From aaa2fbbcb76116ec9ab0eac499a6e25c6cf9814d Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Thu, 12 Nov 2020 16:08:31 -0800 Subject: [PATCH 04/10] pushing for retro --- projects/graph/graph.py | 20 ++++++++++++++++++-- projects/graph/test_graph.py | 12 ++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index a08448cb4..b2d31066f 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -2,6 +2,7 @@ Simple graph implementation """ from util import Stack, Queue # These may come in handy +from collections import deque class Graph: @@ -35,14 +36,29 @@ def bft(self, starting_vertex): Print each vertex in breadth-first order beginning from starting_vertex. """ - pass # TODO + visited = set() + queue = deque() + queue.append(starting_vertex) + + while len(queue) > 0: + currNode = queue.popleft() + if currNode not in visited: + visited.add(currNode) + print(currNode) + for neighbor in self.vertices[currNode]: + queue.append(neighbor) def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ - pass # TODO + visited = set() + stack = deque() + stack.append(starting_vertex) + + while len(stack) > 0: + def dft_recursive(self, starting_vertex): """ diff --git a/projects/graph/test_graph.py b/projects/graph/test_graph.py index 4a00d2bb9..b71211f55 100644 --- a/projects/graph/test_graph.py +++ b/projects/graph/test_graph.py @@ -108,12 +108,12 @@ def test_dfs(self): ] self.assertIn(self.graph.dfs(1,6), dfs) - def test_dfs_recursive(self): - dfs = [ - [1, 2, 4, 6], - [1, 2, 4, 7, 6] - ] - self.assertIn(self.graph.dfs_recursive(1,6), dfs) + # def test_dfs_recursive(self): + # dfs = [ + # [1, 2, 4, 6], + # [1, 2, 4, 7, 6] + # ] + # self.assertIn(self.graph.dfs_recursive(1,6), dfs) if __name__ == '__main__': unittest.main() From 88e61077ae78432abb2906e2c711d9f21ecad136 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Fri, 13 Nov 2020 18:45:12 -0800 Subject: [PATCH 05/10] Added code from Graphs 1 --- projects/graph/graph.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index b2d31066f..4a0725d42 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -58,7 +58,12 @@ def dft(self, starting_vertex): stack.append(starting_vertex) while len(stack) > 0: - + currNode = stack.pop() + if currNode not in visited: + visited.add(currNode) + print(currNode) + for neighbor in self.vertices[currNode]: + stack.append(neighbor) def dft_recursive(self, starting_vertex): """ @@ -67,7 +72,7 @@ def dft_recursive(self, starting_vertex): This should be done using recursion. """ - pass # TODO + pass def bfs(self, starting_vertex, destination_vertex): """ @@ -83,7 +88,21 @@ def dfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in depth-first order. """ - pass # TODO + visited = set() + stack = deque() + stack.append([starting_vertex]) + + while len(stack) > 0: + currPath = stack.pop() + currNode = currPath[-1] + if currNode == destination_vertex: + return currPath + if currNode not in visited: + visited.add(currNode) + for neighbor in self.vertices[currNode]: + newPath = list(currPath) + newPath.append(neighbor) + stack.append(newPath) def dfs_recursive(self, starting_vertex, destination_vertex): """ From 070f0065d3e5f2381adf96c55f3dde938ebdeebf Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Fri, 13 Nov 2020 19:22:59 -0800 Subject: [PATCH 06/10] Finished code for graph project. --- projects/graph/graph.py | 47 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 4a0725d42..05b036fee 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -72,7 +72,16 @@ def dft_recursive(self, starting_vertex): This should be done using recursion. """ - pass + visited = set() + self.dft_recursive_helper(starting_vertex, visited) + + def dft_recursive_helper(self, curr_vertex, visited): + visited.add(curr_vertex) + print(curr_vertex) + + for neighbor in self.vertices[curr_vertex]: + if neighbor not in visited: + self.dft_recursive_helper(neighbor, visited) def bfs(self, starting_vertex, destination_vertex): """ @@ -80,7 +89,22 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ - pass # TODO + visited = set() + queue = deque() + queue.append ([starting_vertex]) + + while len(queue) > 0: + currPath = queue.popleft() + currNode = currPath[-1] + if currNode == destination_vertex: + return currPath + if currNode not in visited: + visited.add(currNode) + for neighbor in self.vertices[currNode]: + newPath = list(currPath) + newPath.append(neighbor) + queue.append(newPath) + return [] def dfs(self, starting_vertex, destination_vertex): """ @@ -112,7 +136,24 @@ def dfs_recursive(self, starting_vertex, destination_vertex): This should be done using recursion. """ - pass # TODO + visited = set() + return self.dfs_recursive_helper([starting_vertex], destination_vertex, visited) + + def dfs_recursive_helper(self, curr_path, destination_vertex, visited): + curr_vertex = curr_path[-1] + + if curr_vertex == destination_vertex: + return curr_path + visited.add(curr_vertex) + + for neighbor in self.vertices[curr_vertex]: + if neighbor not in visited: + newPath = list(curr_path) + newPath.append(neighbor) + res = self.dfs_recursive_helper(newPath, destination_vertex, visited) + if len(res) > 0: + return res + return [] if __name__ == '__main__': graph = Graph() # Instantiate your graph From 15b69d2c24e0a3459fe9b8c133b5f73f4f85e096 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 14 Nov 2020 11:30:35 -0800 Subject: [PATCH 07/10] Test not working. Trying to fix bug. --- projects/ancestor/ancestor.py | 57 ++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/projects/ancestor/ancestor.py b/projects/ancestor/ancestor.py index 3bd003098..ff487df34 100644 --- a/projects/ancestor/ancestor.py +++ b/projects/ancestor/ancestor.py @@ -1,3 +1,58 @@ +from collections import deque def earliest_ancestor(ancestors, starting_node): - pass \ No newline at end of file + """ + Understand: + Make a function that takes any given number in a dataset and returns the earliest known ancestor. + + Plan: + + 1. Translate into graph terminoloy + vertex = individual + edge = connection of parent and child + weight = None + + 2. Build the graph + We will need to do a dfs to traverse the graph all the way down to the earliest (lowest) ancestor (number). We will stop once we find the earliest ancestor which is why we use a DFS. + + """ + class Stack(): + def __init__(self): + self.stack = [] + def push(self, value): + self.stack.append(value) + def pop(self): + if self.size() > 0: + return self.stack.pop() + else: + return None + def size(self): + return len(self.stack) + + ancestors = {} + + visited = set() + stack = deque() + stack.append([starting_node]) + + # while len(stack) > 0: + # currPath = stack.pop() + # currNode = currPath[-1] + # if currNode == destination_vertex: + # return currPath + # if currNode not in visited: + # visited.add(currNode) + # for neighbor in self.vertices[currNode]: + # newPath = list(currPath) + # newPath.append(neighbor) + # stack.append(newPath) + + while len(stack) > 0: + currPath = stack.pop() + currNode = currPath[-1] + if currNode not in visited: + visited.add(currNode) + for individual in ancestors[currNode]: + newPath = list(currPath) + newPath.append(individual) + stack.append(newPath) \ No newline at end of file From b7649e63523df996eee9868485a91b5ff2ba492e Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 17 Nov 2020 19:05:34 -0800 Subject: [PATCH 08/10] Did ancestors cide from Mari --- projects/ancestor/ancestor.py | 80 +++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/projects/ancestor/ancestor.py b/projects/ancestor/ancestor.py index ff487df34..637033c24 100644 --- a/projects/ancestor/ancestor.py +++ b/projects/ancestor/ancestor.py @@ -1,21 +1,23 @@ -from collections import deque - -def earliest_ancestor(ancestors, starting_node): - """ - Understand: - Make a function that takes any given number in a dataset and returns the earliest known ancestor. +""" +Understand: +Make a function that takes any given number in a dataset and returns the earliest known ancestor. - Plan: +Plan: - 1. Translate into graph terminoloy +1. Translate into graph terminoloy vertex = individual edge = connection of parent and child weight = None - 2. Build the graph - We will need to do a dfs to traverse the graph all the way down to the earliest (lowest) ancestor (number). We will stop once we find the earliest ancestor which is why we use a DFS. +2. Build the graph +We will need to do a dfs to traverse the graph all the way down to the earliest (lowest) ancestor (number). We will stop once we find the earliest ancestor which is why we use a DFS. + +""" + +from collections import deque +from collections import defaultdict - """ +def earliest_ancestor(ancestors, starting_node): class Stack(): def __init__(self): self.stack = [] @@ -29,11 +31,11 @@ def pop(self): def size(self): return len(self.stack) - ancestors = {} + # ancestors = {} - visited = set() - stack = deque() - stack.append([starting_node]) + # visited = set() + # stack = deque() + # stack.append([starting_node]) # while len(stack) > 0: # currPath = stack.pop() @@ -47,12 +49,44 @@ def size(self): # newPath.append(neighbor) # stack.append(newPath) + # while len(stack) > 0: + # currPath = stack.pop() + # currNode = currPath[-1] + # if currNode not in visited: + # visited.add(currNode) + # for individual in ancestors[currNode]: + # newPath = list(currPath) + # newPath.append(individual) + # stack.append(newPath) + + # Mari's code... + + graph = createGraph(ancestors) + + earliestAncestor = ((starting_node, 0)) + stack = deque() + stack.append((starting_node, 0)) + visited = set() + while len(stack) > 0: - currPath = stack.pop() - currNode = currPath[-1] - if currNode not in visited: - visited.add(currNode) - for individual in ancestors[currNode]: - newPath = list(currPath) - newPath.append(individual) - stack.append(newPath) \ No newline at end of file + curr = stack.pop() + currNode, distance = curr[0], curr[1] + visited.add(curr) + + if currNode not in graph: + if distance > earliestAncestor[1]: + earliestAncestor = curr + elif distance == earliestAncestor[1] and currNode < earliestAncestor[0]: + earliestAncestor = curr + else: + for ancestor in graph[currNode]: + if ancestor not in visited: + stack.append((ancestor, distance + 1)) + return earliestAncestor[0] if earliestAncestor[0] != starting_node else - 1 + +def createGraph(edges): + graph = defaultdict(set) + for edge in edges: + ancestor, child = edge[0], edge[1] + graph[child].add(ancestor) + return graph \ No newline at end of file From 90c0e0ed0088d83111fa40e068dbc8632a355493 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 17 Nov 2020 20:31:06 -0800 Subject: [PATCH 09/10] Working on questions. --- projects/social/social.py | 43 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/projects/social/social.py b/projects/social/social.py index 8609d8800..0209cd8c0 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -1,3 +1,7 @@ +import random +import math +from collections import deque + class User: def __init__(self, name): self.name = name @@ -45,8 +49,23 @@ def populate_graph(self, num_users, avg_friendships): # !!!! IMPLEMENT ME # Add users + for i in range(0, num_users): + self.add_user(f"User {i}") + possible_friendships = [] # Create 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)) + + # Shuffle the entire array of possible friendships + random.shuffle(possible_friendships) + + # Select the first num_users * avg_friendships / 2 + # We / 2 because a friendship is a bidirectional edge (we're essentially adding two edges) + for i in range(0, math.floor(num_users * avg_friendships / 2)): + friendship = possible_friendships[i] + self.add_friendship(friendship[0], friendship[1]) def get_all_social_paths(self, user_id): """ @@ -59,12 +78,30 @@ def get_all_social_paths(self, user_id): """ visited = {} # Note that this is a dictionary, not a set # !!!! IMPLEMENT ME + queue = deque() + queue.append([user_id]) + + while len(queue) > 0: + currPath = queue.popleft() + currNode = currPath[-1] + visited[currNode] = currPath + for friend in self.friendships[currNode]: + if friend not in visited: + newPath = currPath.copy() + newPath.append(friend) + queue.append(newPath) + return visited if __name__ == '__main__': sg = SocialGraph() - sg.populate_graph(10, 2) + sg.populate_graph(8, 4) print(sg.friendships) - connections = sg.get_all_social_paths(1) - print(connections) + # connections = sg.get_all_social_paths(1) + # print(connections) + +""" +Questions: + 1. +""" From 22b8c61f2170b7142ae80e215f1ee699e78ea6e0 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Thu, 19 Nov 2020 19:07:28 -0800 Subject: [PATCH 10/10] Got rest of social code --- projects/social/social.py | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/projects/social/social.py b/projects/social/social.py index 0209cd8c0..82f0aa30f 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -93,13 +93,50 @@ def get_all_social_paths(self, user_id): return visited + # Returns True if user_id and friend_id have successfully been added ad friends + def add_friendship_linear(self, user_id, friend_id): + if user_id == friend_id: + return False + # Check if friend_id and user_id are not already friends with each other + elif friend_id in self.friendships[user_id] or user_id in self.friendships[friend_id]: + return False + else: + self.friendships[user_id].add(friend_id) + self.friendships[friend_id].add(user_id) + return True + + def populate_graph_linear(self, num_users, avg_friendships): + # Return graph + self.last_id = 0 + self.users = {} + self.friendships = {} + + # Add users into the graph + for i in range(num_users): + self.add_users(f"User {i}") + + # Create random friendships until we've hit target number of friendships + target_friendships = num_users * avg_friendships + total_friendships = 0 + collisions = 0 + + while total_friendships < target_friendships: + # keep adding friendships + user_id = random.randint(1, self.last_id) + friend_id = random.randint(1, self.last_id) + if self.add_friendship_linear(user_id, friend_id): + total_friendships += 2 + else: + collisions += 1 + print(f"Collisions: {collisions}") + if __name__ == '__main__': sg = SocialGraph() sg.populate_graph(8, 4) print(sg.friendships) - # connections = sg.get_all_social_paths(1) - # print(connections) + connections = sg.get_all_social_paths(1) + print(connections) """ Questions: