-
Notifications
You must be signed in to change notification settings - Fork 0
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
Graph bfs dfs #9
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this!
not confident about graphs in general. that is to say, given that there are so | ||
many applications of graphs, even implementing something 'basic' like dfs or bfs | ||
is quite daunting because of all the potential applications, constraints, and so | ||
on. for example, i've read through the word search ladder and the knights tour a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The word search ladder and knights tour are both quite tricky applications, and involve a lot of nuance that's kind of unrelated to graphs.
|
||
terminate when you find what you are looking for, or the queue is empty. | ||
|
||
an empty queue means that all nodes have been searched. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, all reachable nodes have been searched
return info | ||
|
||
for v in graph[vertex]: | ||
if v not in info["visited"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that info["visited"]
is a list, what's the running time implication on (i) this step, (ii) the entire process? What would be a data structure that gives you an improved running time?
info = { | ||
"target": target, | ||
"found": False, | ||
"visited": [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the consequences of generating a new visited
array on each iteration? What happens if you run this on a graph with a cycle?
info = { | ||
"target": target, | ||
"found": False, | ||
"visited": [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found it helpful to give a clear description of what "node is in visited" means. For example, does it mean:
- I've added the node to the queue (and don't need to add it again)
- I've dequeued the node from the queue and checked it (so if I dequeue the same node again, I can ignore it)
|
||
return info | ||
|
||
def minimum_depth_of_binary_tree(self, root): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!! This is a really nice solution.
finished the prework, and have a bunch of questions. those are in the .md file.
thanks in advance for your comments as usual, @robot-dreams!