From a4bd6712f1c7eacf5f40b50b941479df8ef71268 Mon Sep 17 00:00:00 2001 From: Kronykus Date: Tue, 22 Sep 2020 15:31:19 -0500 Subject: [PATCH 1/4] day 1 --- projects/graph/graph.py | 137 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 126 insertions(+), 11 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 59fecae4b..a0de4ffa9 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -13,42 +13,100 @@ 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 in self.vertices and v2 in self.vertices: + self.vertices[ v1 ].add( v2 ) + else: + raise LookupError( "Problem adding edge: Vertex not found" ) 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): """ Print each vertex in breadth-first order beginning from starting_vertex. """ - pass # TODO + que = Queue() + visited = set() + + que.enqueue( starting_vertex ) + + while que.size(): + vert = que.dequeue() + + if vert not in visited: + visited.add( vert ) + + neighbors = self.get_neighbors( vert ) + for neighbor in neighbors: + que.enqueue( neighbor ) + + print( vert ) + def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ - pass # TODO + stack = Stack() + visited = set() + + stack.push( starting_vertex ) + + while stack.size(): + vert = stack.pop() + + if vert not in visited: + visited.add( vert ) + + neighbors = self.get_neighbors( vert ) + for neighbor in neighbors: + stack.push( neighbor ) + + print( vert ) - def dft_recursive(self, starting_vertex): + def dft_recursive( self, starting_vertex, visited=set() ): """ Print each vertex in depth-first order beginning from starting_vertex. This should be done using recursion. """ - pass # TODO + + ### Works but backwards + + """ visited = set() + + def recur( vertex ): + if vertex not in visited: + visited.add( vertex ) + + for neighbor in self.get_neighbors( vertex ): + recur( neighbor ) + + print( vertex ) + + recur( starting_vertex ) """ + + print( starting_vertex ) + + visited.add( starting_vertex ) + + for neighbor in self.get_neighbors( starting_vertex ): + if neighbor not in visited: + visited.add( neighbor ) + + self.dft_recursive( neighbor, visited ) def bfs(self, starting_vertex, destination_vertex): """ @@ -56,7 +114,25 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ - pass # TODO + que = Queue() + visited = set() + + que.enqueue( [ starting_vertex ] ) + + while que.size(): + path = que.dequeue() + node = path[ -1 ] + + if node not in visited: + visited.add( node ) + + if node == destination_vertex: + return path + + for neighbor in self.get_neighbors( node ): + que.enqueue( path + [ neighbor ] ) + + return None def dfs(self, starting_vertex, destination_vertex): """ @@ -64,9 +140,27 @@ def dfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in depth-first order. """ - pass # TODO + stack = Stack() + visited = set() + + stack.push( [ starting_vertex ] ) + + while stack.size(): + path = stack.pop() + node = path[ -1 ] - def dfs_recursive(self, starting_vertex, destination_vertex): + if node not in visited: + visited.add( node ) + + if node == destination_vertex: + return path + + for neighbor in self.get_neighbors( node ): + stack.push( path + [ neighbor ] ) + + return None + + def dfs_recursive( self, starting_vertex, destination_vertex, visited=set(), path=[] ): """ Return a list containing a path from starting_vertex to destination_vertex in @@ -74,7 +168,28 @@ def dfs_recursive(self, starting_vertex, destination_vertex): This should be done using recursion. """ - pass # TODO + if path == []: + path = [ starting_vertex ] + + if starting_vertex not in visited: + visited.add( starting_vertex ) + + if starting_vertex == destination_vertex: + return path + + for neighbor in self.get_neighbors( starting_vertex ): + res = self.dfs_recursive( + neighbor, + destination_vertex, + visited, + path + [ neighbor ] + ) + + if res: + return res + + return None + if __name__ == '__main__': graph = Graph() # Instantiate your graph From bb5567a26bcd3a48211e6b89df546ce2eaf9287d Mon Sep 17 00:00:00 2001 From: Kronykus Date: Tue, 22 Sep 2020 15:34:27 -0500 Subject: [PATCH 2/4] removed commented code --- projects/graph/graph.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index a0de4ffa9..719d858c4 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -82,22 +82,6 @@ def dft_recursive( self, starting_vertex, visited=set() ): This should be done using recursion. """ - - ### Works but backwards - - """ visited = set() - - def recur( vertex ): - if vertex not in visited: - visited.add( vertex ) - - for neighbor in self.get_neighbors( vertex ): - recur( neighbor ) - - print( vertex ) - - recur( starting_vertex ) """ - print( starting_vertex ) visited.add( starting_vertex ) @@ -105,7 +89,7 @@ def recur( vertex ): for neighbor in self.get_neighbors( starting_vertex ): if neighbor not in visited: visited.add( neighbor ) - + self.dft_recursive( neighbor, visited ) def bfs(self, starting_vertex, destination_vertex): From a43ac50a7b58f18274dca7bf89c67f7f4c8c58d5 Mon Sep 17 00:00:00 2001 From: Kronykus Date: Tue, 22 Sep 2020 19:31:55 -0500 Subject: [PATCH 3/4] day 2 --- projects/ancestor/ancestor.py | 48 +++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/projects/ancestor/ancestor.py b/projects/ancestor/ancestor.py index 3bd003098..34272a579 100644 --- a/projects/ancestor/ancestor.py +++ b/projects/ancestor/ancestor.py @@ -1,3 +1,47 @@ +class Stack(): + def __init__( self ): + self.stack = [] -def earliest_ancestor(ancestors, starting_node): - pass \ No newline at end of file + 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 ) + +def dfs( starting_vertex, graph ): + stack = Stack() + visited = [] + + stack.push( [ starting_vertex ] ) + + while stack.size(): + path = stack.pop() + node = path[ -1 ] + + if node not in visited: + visited.append( node ) + + for parent in graph.get( node, [] ): + stack.push( path + [ parent ] ) + + return visited[ -1 ] + +def earliest_ancestor( ancestors, starting_node ): + graph = {} + + for parent, child in ancestors: + if child not in graph: + graph[ child ] = [ parent ] + else: + graph[ child ].append( parent ) + + if starting_node not in graph: + return -1 + + return dfs( starting_node, graph )\ From b5933e87ddd18d7225032dee54aa07b26980e081 Mon Sep 17 00:00:00 2001 From: Kronykus Date: Wed, 23 Sep 2020 18:28:59 -0500 Subject: [PATCH 4/4] day 3 --- projects/social/social.py | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/projects/social/social.py b/projects/social/social.py index 8609d8800..13fdfc100 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -1,3 +1,23 @@ +from random import shuffle + + +class Queue(): + def __init__( self ): + self.que = [] + + def enqueue( self, value ): + self.que.append( value ) + + def dequeue( self ): + if self.size() > 0: + return self.que.pop( 0 ) + else: + return None + + def size( self ): + return len( self.que ) + + class User: def __init__(self, name): self.name = name @@ -45,8 +65,22 @@ def populate_graph(self, num_users, avg_friendships): # !!!! IMPLEMENT ME # Add users + for u in range( num_users ): + self.add_user( u ) # Create friendships + friends = [] + + for uid in self.users: + for fid in range( uid + 1, self.last_id + 1 ): + friends.append( ( uid, fid ) ) + + shuffle( friends ) + + rng = num_users * avg_friendships // 2 + for i in range( rng ): + uid, fid = friends[ i ] + self.add_friendship( uid, fid ) def get_all_social_paths(self, user_id): """ @@ -59,6 +93,22 @@ def get_all_social_paths(self, user_id): """ visited = {} # Note that this is a dictionary, not a set # !!!! IMPLEMENT ME + que = Queue() + + que.enqueue( [ user_id ] ) + + while que.size(): + path = que.dequeue() + node = path[ -1 ] + + if node not in visited: + visited[ node ] = path + + friends = self.friendships[ node ] + + for friend in friends: + que.enqueue( path + [ friend ] ) + return visited