Skip to content

patrick-devincentis #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion projects/ancestor/ancestor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
from collections import deque, defaultdict


def earliest_ancestor(ancestors, starting_node):
pass
graph = create_graph(ancestors)
stack = deque()
paths = []

stack.append([starting_node])

while len(stack):
current_path = stack.pop()
current_vertex = current_path[-1]

if current_vertex in graph:
for neighbor in graph[current_vertex]:
new_path = list(current_path)
new_path.append(neighbor)
stack.append(new_path)
elif current_vertex != starting_node:
if not len(paths):
paths.append(current_path)
elif len(current_path) > len(paths[0]) or (
len(current_path) == len(paths[0]) and current_path[-1] < paths[0][-1]
):
paths = [current_path]

return paths[0][-1] if len(paths) else -1

def create_graph(edges):

graph = defaultdict(set)
for edge in edges:
ancestor, child = edge[0], edge[1]
graph[child].add(ancestor)
return graph




121 changes: 108 additions & 13 deletions projects/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Simple graph implementation
"""
from util import Stack, Queue # These may come in handy

from collections import deque
class Graph:

"""Represent a graph as a dictionary of vertices mapping labels to edges."""
Expand All @@ -13,68 +13,163 @@ 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(' Can not add edges to vertices that do not exist')
else:
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):
"""
Print each vertex in breadth-first order
beginning from starting_vertex.
"""
pass # TODO

queue = deque()
queue.append(starting_vertex)
visited = set()

while len(queue):
current_vertex = queue.popleft()
if current_vertex not in visited:
visited.add(current_vertex)
print(current_vertex)
for neighbor in self.get_neighbors(current_vertex):
queue.append(neighbor)




def dft(self, starting_vertex):
"""
Print each vertex in depth-first order
beginning from starting_vertex.
"""
pass # TODO
stack = deque()
stack.append(starting_vertex)
visited = set()

def dft_recursive(self, starting_vertex):
while len(stack):
current_vertex = stack.pop()
if current_vertex not in visited:
print(current_vertex)
visited.add(current_vertex)
for neighbor in self.get_neighbors(current_vertex):
stack.append(neighbor)


def dft_recursive(self, starting_vertex, visited = None):
"""
Print each vertex in depth-first order
beginning from starting_vertex.

This should be done using recursion.
"""
pass # TODO

# create a visited
# if start isnt in visited add it
# print start
# call recursive funciton for neighbors of vertix using neighbor and visited

if not visited:
visited = set()

if starting_vertex not in visited:
visited.add(starting_vertex)
print(starting_vertex)
for neighbor in self.get_neighbors(starting_vertex):
self.dft_recursive(neighbor, visited)

def bfs(self, starting_vertex, destination_vertex):
"""
Return a list containing the shortest path from
starting_vertex to destination_vertex in
breath-first order.
"""
pass # TODO
queue = deque()
visited = set()
queue.append([starting_vertex])

while len(queue):
current_path = queue.popleft()
current_vertex = current_path[-1]

if current_vertex == destination_vertex:
return current_path

if current_vertex not in visited:
visited.add(current_vertex)
for neighbor in self.get_neighbors(current_vertex):
new_path = list(current_path)
new_path.append(neighbor)
queue.append(new_path)

def dfs(self, starting_vertex, destination_vertex):
"""
Return a list containing a path from
starting_vertex to destination_vertex in
depth-first order.
"""
pass # TODO
queue = deque()
visited = set()
queue.append([starting_vertex])

while len(queue):
current_path = queue.pop()
current_vertex = current_path[-1]

if current_vertex == destination_vertex:
return current_path

if current_vertex not in visited:
visited.add(current_vertex)
for neighbor in self.get_neighbors(current_vertex):
new_path = list(current_path)
new_path.append(neighbor)
queue.append(new_path)

def dfs_recursive(self, starting_vertex, destination_vertex):
def dfs_recursive(self, starting_vertex, destination_vertex, visited = None, path = None):
"""
Return a list containing a path from
starting_vertex to destination_vertex in
depth-first order.

This should be done using recursion.
"""
pass # TODO
# create a path
# path starts with starting vertex
# if starting vertex not in visited add to visited
# print vertex
# grab the neighbors of the vertex

if not visited:
visited = set()
if not path:
path = []

visited.add(starting_vertex)
path = path + [starting_vertex]

if starting_vertex == destination_vertex:
return path

for neighbor in self.get_neighbors(starting_vertex):
if neighbor not in visited:
res = self.dfs_recursive(neighbor, destination_vertex, visited, path)
if res:
return res
return []


if __name__ == '__main__':
graph = Graph() # Instantiate your graph
Expand Down
17 changes: 15 additions & 2 deletions projects/social/social.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import deque

class User:
def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -57,8 +59,19 @@ def get_all_social_paths(self, user_id):

The key is the friend's ID and the value is the path.
"""
visited = {} # Note that this is a dictionary, not a set
# !!!! IMPLEMENT ME
visited = {}
queue = deque()
queue.append([user_id])

while len(queue) > 0:
curr_Path = queue.popleft()
currNode = curr_Path[-1]
visited[currNode] = curr_Path
for friend in self.friendships[currNode]:
if friend not in visited:
new_path = curr_Path.copy()
new_path.append(friend)
queue.append(new_path)
return visited


Expand Down