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

Graph bfs dfs #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Graph bfs dfs #9

wants to merge 4 commits into from

Conversation

janiceshiu
Copy link
Owner

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!

Copy link

@robot-dreams robot-dreams left a 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

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.

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"]:

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": [],

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": [],

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):

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants