Skip to content
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

day 1 #748

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

day 1 #748

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
19 changes: 15 additions & 4 deletions projects/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,37 @@ def add_vertex(self, vertex_id):
"""
Add a vertex to the graph.
"""
pass # TODO
self.vertices[vertices_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)

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
q = Queue()
q.enqueue(starting_vertex)
visited = set()
while q.size() > 0:
v = q.dequeue()
if v not in visited:
visited.add(v)
print(v)
for next_vertex in self.get_neighbors(v):
q.enqueue(next_vertex)


def dft(self, starting_vertex):
"""
Expand Down