Tree and Graph Data Structures

Depth-First & Breadth-First Search Usage

Tree and Graph Data Structures

Check out a free preview of the full Tree and Graph Data Structures course

The "Depth-First & Breadth-First Search Usage" Lesson is part of the full, Tree and Graph Data Structures course featured in this preview video. Here's what you'd learn in this lesson:

Bianca leads a discussion about which type of search to use in the implementation of the breakfast recommendation tree, along with common uses for the two search methods.


Transcript from the "Depth-First & Breadth-First Search Usage" Lesson

>> Bianca Gandolfo: So the question is, for our recommendation, what do you think is better? So, the path we wanna take is, we want to find a person and then find a mutual interest. Like a mutual thing that I don't already like, you guys have a sense of what would be better?

>> Speaker 2: That first date.
>> Bianca Gandolfo: Why?
>> Speaker 2: Well, you're gonna go immediately to your food, which is gonna go back to another person, and then the third thing is gonna be into food, maybe. Actually, I take it back. I take it back.
>> Bianca Gandolfo: You take it back?
>> Speaker 2: Yeah.

>> Bianca Gandolfo: So you think breadth first search then?
>> Speaker 2: Uh-huh.
>> Bianca Gandolfo: Okay. What do other people think?
>> Speaker 3: Are you the starting note in this,
>> Bianca Gandolfo: What's that?
>> Speaker 3: Are you the starting note?
>> Bianca Gandolfo: I am the starting note in this.
>> Speaker 3: So we do browse first, and we'll see all the things that you like already first, right?

>> Bianca Gandolfo: Mm-hm.
>> Speaker 3: So would it make sense to go depth first to get away from the things that you're already connected to?
>> Bianca Gandolfo: Yeah. That could be a strategy like, the furthest thing away from me would definitely be something that, let's see if I have, where is that graph?

>> Bianca Gandolfo: Here's a different version of the graph. So if we go depth first, right? We can make it there faster. Whereas, breadth first would do this first and then the people, and then it wouldn't be this one, right? So I think my recommendation would be, if you're just trying to find one thing, right?

Like we just need one recommendation. Do that first, but if you wanna have multiple recommendations it might be nice to do breadth first because then you would just have that list at the level. You know what I mean? All at the same time.
>> Speaker 3: It's just, I feel that it should be two different trees that you're connecting.

It shouldn't be in the same, like that. It shouldn't look like this.
>> Bianca Gandolfo: Why?
>> Speaker 3: Because, maybe you could have your, I guess it can look like this but,
>> Speaker 3: Because I don't know, it's organized better like if you have people's recommendations and then. Then how are the people connected to each other, and then the food that heir people are connected to, that's one way to do it.

>> Bianca Gandolfo: Mm-hm.
>> Speaker 3: Or, I guess, here you have your food, your first breakfast, your number one breakfast repetition.
>> Bianca Gandolfo: Mm-hm.
>> Speaker 3: And then you have your people, and then their recommendation.
>> Bianca Gandolfo: Yeah, this is just a traversal, actually the graph looks like this.
>> Speaker 3: Right. That's, yeah.
>> Bianca Gandolfo: But when you traverse it, it kind of starts to look at a tree, right?

>> Speaker 3: Right.
>> Bianca Gandolfo: Cuz you only visit each day once.
>> Speaker 3: Okay, I see what you're saying.
>> Bianca Gandolfo: Yeah, so they're actually the same thing. This is just like a traversal.
>> Speaker 3: I see what you're saying.
>> Bianca Gandolfo: Yeah.
>> Bianca Gandolfo: It's like a tree of the traversal.
>> Speaker 3: Right.
>> Bianca Gandolfo: But the graph still is the same as it was.

>> Speaker 3: But this traversal, it shouldn't the traversal be different for the DFS versus BFS, or are you asking?
>> Bianca Gandolfo: The traversal, the DFS goes like this.
>> Speaker 3: Yup.
>> Bianca Gandolfo: And then the BFS goes like this.
>> Speaker 3: I see what you're saying.
>> Bianca Gandolfo: Yeah.
>> Speaker 3: So the DFS looks better because, at least you'll know if there's recommendations right away.

>> Bianca Gandolfo: Yup, yeah, I mean you do a lot less work.
>> Speaker 3: To get a recommendation.
>> Bianca Gandolfo: Yeah, right, so like this one, we would have to do one, two, three, four, five, and if there was even more, right, keep going until we got here. And you can imagine this could be huge.

Yeah, I like those few things, but there are a lot of things I like for breakfast, and there's a lot of things that my friends like for breakfast. So, yeah.
>> Speaker 4: Okay, there is no issue with your code, there's just an issue with my code.
>> Bianca Gandolfo: Okay [LAUGH] all right, great.

All right, so just like thinking more about breadth first search and depth first search, and just these traversals in general, what they're used for? What kind of questions come up, that these are suited for. So, we have logging and validating which we've been doing a lot of, right?

We're logging all over the place, but validating is useful to which you get to like binary search trees which is, there's like certain rules that has to follow and traversing that and validating it is important. So, there's similarly like copying is another thing that you might wanna do, counting edges or vertices.

There's another one or you even leaves that we were doing earlier. Identifying if a component is connected in a graph like I mentioned, you can add a note to a graph, but it doesn't necessarily have to have an edge to another node. And so you can test if things are connected and sort of like a social media world this might look like is someone in your LinkedIn network, right.

Or, like are there clicks in,
>> Bianca Gandolfo: I don't know, Twitter, what else? And then finding paths or cycles are pretty common interview question. So a path is, can you get from point A to point B, right? And then cycles is, can you start at point A and come back to point A through this graph?

So detecting cycles, finding paths, shortest paths, things like that. So shortest path's gonna be breadth first search for sure. There's a few different algorithms to get you there, but they're all breadth first search base algorithms with optimizations.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now