[00:00:19] SY: This season of the Basecs Podcast is brought to you by our wonderful friends at Twilio. If you haven't checked out their API yet, you totally should. With Twilio, your app can send text messages and make phone calls with just five lines of code. And for a lot of developers, Twilio is the first API they've ever used. And that's first time you type a few lines of code, run your program and see your phone write up with the text message? It's kind of magical. And as a thank you for being a podcast listener, you get a promo code for twenty dollars in free Twilio credit. Just text your name to 480-485-4321. You'll also get a link to a quick start guide for your favorite programming language. So text 480-485-4321. Link to that is in your show notes. Ok. Let's get started.

[00:01:18] SY: DFS. DFS. I like the better. Ok. 'Cause depth for—like the first time I remember hearing someone say that, I was like "what is this word you've created? I just thought was one: depth-first search. Depth-first—ok anyways. (Laughing) It is an algorithm. Can we, can we just talk about what algorithms are? And before, before you tell me what algorithms are, I'm gonna tell you what I don't want to hear. Ok?

[00:01:39] VJ: Ok. All right.

[00:01:40] SY: I hate when I ask a developer what an algorithm is and they're like, "it's a recipe. It's like if you have flour and sugar and the steps are like: first, get the flour. Then, get the sugar. And then you put it in the oven. And then you have brownies." That's the entire definition of an algorithm. And I'm like I'm not making brownies. I'm trying to, I'm trying to code like this is—you need to relate it back to computer science in some way. You can't just end the definition of algorithms with brownies. And it makes me so mad. So don't do that.

[00:02:13] VJ: All right.

[00:02:14] SY: No pressure.

[00:02:15] VJ: Okay, okay. I understand how you feel the brownies now.

[00:02:20] SY: I've been holding onto that for a while. (Laughing)

[00:02:23] VJ: I think what everybody's trying to get at but then seems too simple to say or seems too hard to get to the point of what they're actually trying to get at is it's just a set of instructions. That's what it is. In the recipe metaphor, I think what people are always trying to say is that, you know, it's the instructions on the box. Another great metaphor I've heard for algorithm's is it's like when you see dancers and you see them doing choreography. If you've ever seen like a rehearsal, they'll do the same steps again and again and again and they'll repeat it until it's right. And that's basically what algorithms—that's a hard word—algorithms also are. They're just instructions for how to do something. So when we talk about depth-first search, or DFS as we're gonna talk about today that is basically a set of instructions for how to do a type of search. Oh no. Did I, did I do the same definition?

[00:03:11] SY: Yeah. Yeah you did.

[00:03:12] VJ: Oh man.

[00:03:12] SY: It's ok. It's ok. I love you so it's fine. (Laughing) But it's too abstract. It's like in that case, Vaidehi, turn on your mic. Vaidehi record this episode. Vaidehi upload—now I'm an algorithm. Do know what I mean? Like you can apply that to, to like anything. So, you know, if it really is that simple, if it really is just a set of instructions, why is it so scary? Why is there a whole computer science, you know, course or courses dedicated to it? Why is it this huge hurdle if it's just a recipe?

[00:03:43] VJ: Yeah. Well so it is that simple, but I think, I think the reason it's so hard is it's like this really weird, abstract thing in the context of a data structure or like people will be like it's this algorithm to find this thing inside of this, you know, inside of the structure. So like there will be, there will be graph algorithms where there's like nobody saw the algorithm of the traveling salesman, which is really just a traversal algorithm, which we haven't talked about on this podcast. This is a little foreshadowing for you. But basically, there are people who try to find the shortest path between different nodes on a graph, which we talked about in our previous episode, actually. And there are just a set of constraints and so people have tried to find, you know, what is the best way of finding the instructions, the repeated steps that we can take. What are the instructions that we can give a program to achieve this result in the most efficient way or the quickest way or the one that uses less memory? And I think all of those little constraints combined is what makes them hard because the algorithm itself is just the instructions. It's all of the qualifications of "don't make it take too much time" and "be efficient" and, you know, "don't repeat yourself" and "are you using memory?" like "are you using too much memory?" All of those things is—they make algorithms seem really hard because now you suddenly have all these little like boxes you have to check off. So it's like if I told you to make brownies, but then I'm like "don't use this type of flower and make sure your chocolate chips are, you know, this size, and you know, the oven, if it's above 350, well that's inefficient.” Like, you know, like all these crazy qualifications. And you're like I just wanna make brownies. Don't you just combine stuff? And it's like yeah, you can make brownies 500 ways, but we're trying to find the best brownie.

[00:05:30] SY: That actually makes a lot—thank you so much. That makes so much sense. So it's not the idea of an algorithm that's complicated and scary, it's the fact that in computer science, we are trying to do things a very fast, specific, efficient way, and there are lots of different options. And finding that fast, specific, best way of doing it might be hard or might just involve, you know, a complicated equation or set of steps. And it's the, the context of that that can make it hard.

[00:06:02] VJ: Yeah, yeah. And there's... Sometimes, actually—and I think this is another thing that makes it feel hard—there are so many limitations or the problem you're trying to solve, the problem that you're trying to create these instructions for is so difficult that nobody actually can come up with a recipe, an algorithm, a set of instructions that fulfills all the constraints. People get close...

[00:06:22] SY: Oh.

[00:06:23] VJ: ...but they're not actually able to find the solution, which is totally also a thing in computer science because there are algorithms that no one has solved yet. Or there are problems, rather, I should say...

[00:06:34] SY: Yeah.

[00:06:34] VJ: ...there are problems that no one has solved using algorithms.

[00:06:37] SY: Ok. So that's like when I want a chocolate ice cream that has no fat or sugar but also tastes really good.

[00:06:48] VJ: Yep.

[00:06:49] SY: That's not a thing. There's just no recipe for that. You know, I can like fake it with honey and maybe I use frozen Greek yogurt—this has been a recent thing that I've tried to find. I couldn't find it—Greek, you know, Greek yogurt. And I, you know, I try a bunch of products that—but it never, it never quite tastes good, you know? It doesn't make me excited to have it. And there is just no algorithm for that. That's, that's what it comes down to.

[00:07:12] VJ: Yeah. I think that the term for that is "NP-complete," which is there, or there are some, there are some algorithms. It's actually like computational complexity theory. I don't even know that much about it, but there are some algorithms that are so hard and impossible...

[00:07:26] SY: Sounds fancy.

[00:07:26] VJ: Yeah. Sounds fancy, but really what it means is nobody has solved this.

[00:07:30] SY: Yeah

[00:07:30] VJ: And we don't think it's possible to solve. So what I think you're describing with this brownie that taste good but also apparently has nothing in it.

[00:07:43] VJ: My point is you can't do that. And that's a NP-complete problem.

[00:07:49] SY: This ice cream is NP-complete. Worst ice cream ever. Ok so now that we have a very, very great, I think, definition and understanding of algorithms, let's go into a specific algorithm: the depth-first search, the DFS algorithm. Every I see depth-first search I think of scuba diving.

[00:08:11] VJ: Oh.

[00:08:11] SY: I don't know why. I think it, you know, like I, I see like a scuba diver with a—do they use flashlights under water? (Laughing)

[00:08:21] VJ: I think they have headlamps.

[00:08:23] SY: Headlamps. Thank you. Headlamps. Thank you. Like, you know, like, like looking for stuff. You know, just going straight to the bottom of the ocean and just like looking around trying to find like their lost glasses or I don't know, whatever people...

[00:08:38] VJ: You just buy a new one. It's not worth going down into the trench for your glasses like that point. Treasure, on the other hand... Maybe.

[00:08:45] SY: Well those glasses of sentimental value, ok? But yeah, that's like—in my head, that's what I think of. I think of, you know, just going really, really deep all the way to the bottom, like looking around, seeing what you got and then going all the way up. Is there any relation to that image and how it actually works?

[00:09:02] VJ: Yes. Actually, yeah, that's a great image. I, I can't believe I have never heard that correlation before. But I think the way to really talk about depth-first search with the, this image of, you know, a scuba diver going deep into the ocean is to kind of contrast it with the other type of swimming—scuba diving—that (Laughing) you could do, which is…

[00:09:22] SY: This is the water episode. (Laughing) Water and brownies.

[00:09:29] VJ: Hopefully not watery brownies. That seems like a bad algorithm.

[00:09:34] SY: Oh, yeah. That's the worst recipe ever.

[00:09:38] VJ: Anyways, I guess what I was trying to say is you can have scuba divers that go deep, but then is it snorkeling, where you, you are always kind of near the surface? You don't go beyond...

[00:09:48] SY: That sounds right.

[00:09:48] VJ: ...a certain, I don't know, number of feet. Basically—or you could just say swimming, also, right? Where you just kind of...

[00:09:55] SY: Like doggy paddling, you know? Just...

[00:09:57] VJ: Yeah. You're just kind of like going, going wide. That's another algorithm actually that we'll, we haven't talked about yet. But I want to kind of contrast it with that because we had talked earlier about how you could have different approaches to solving the same problem. And you can have many, many different ways of doing things. So in the same vein, you can have many, many different algorithms that are trying to kind of solve the same problem. So the depth-first search approach is one way of traversing through tree structures and also graph structures because trees are just graphs but with more rules.

[00:10:28] SY: Oh yeah.

[00:10:29] VJ: But then the contrast to depth-first search is something called breadth-first search, which we won't go into today but is very similarly related. And that's going wide. So kind of like swimming along the surface of the sea rather than diving in and just going super deep into the ocean.

[00:10:47] SY: Okay. So when we're talking about depth-first search and breath-first—no. Nope. BFS. (Laughing) We are—are we mostly talking about trees? Is that the, the world we're in?

[00:11:18] VJ: I—Yes. So, ok. To answer your question, we're only gonna talk about depth-first search in the context of trees to start with. It's easier to visualize. It's easier to wrap your head around especially when you are working with a very specific type of tree called a binary search tree. There are depth-first and breadth-first search algorithms for graphs, too, but graphs are, as you might remember, more loosey-goosey. They're not as...

[00:11:41] SY: Yeah.

[00:11:41] VJ: ...you know, strict with their rules of how they have to look. So that can be I think a harder thing to wrap your head around if you haven't been exposed to the algorithms so far. So I think starting with trees is probably a good thing to do.

[00:11:57] SY: Ok. So in my scuba diver example, we are looking for treasure. When we are doing depth-first search on a tree, what is the point?

[00:12:12] VJ: Where is the treasure?

[00:12:14] SY: Where is the treasure?

[00:12:14] VJ: You're not into it if there's no treasure.

[00:12:16] SY: I need my treasure.

[00:12:19] VJ: Well, I guess usually, what you're trying to do when you are searching through a tree, which the other term that you might hear for that is called traversing, traversing through a tree, a tree search or a tree traversal usually happens because you are trying to do something with the data inside the tree structure. So usually, what you're trying to do is either just reading the data so that basically means like iterating through or checking all the nodes in the, in the tree, or you might want to go through and modify them. You might wanna update all of them. So if you have a binary search tree, you might wanna suddenly go through and update all the values and multiply them by two. Or maybe you want to take this tree structure, which might have tons and tons of data in it and put it in order and sort it, which is actually a very common thing you would wanna do is sort your data from least to greatest or something like that. And in that case, you do need to go through all the nodes 'cause you need to know what's the smallest, what's the biggest value and then what value goes where. Tree structures are not as obvious in how they're ordered. You might want to do something with those nodes to try to manipulate them in some way either by reordering them or by retrieving values from them or by changing the values that they contain.

[00:13:36] SY: So we need to traverse—well traversing is just how we go through all the nodes—but we'll probably be looking, just comparing the nodes, looking at them, updating them. So it's all about just getting to the nodes. Like when we talk about the search part of depth-first search we are searching for the treasure node.

[00:13:57] VJ: Yes.

[00:13:57] SY: Is that fair? Ok.

[00:13:58] VJ: Yeah, I think that's a, that's a good assessment. You're usually searching for one node or maybe you're trying to search through the nodes for some reason maybe to order them or something like that.

[00:14:09] SY: Ok, so back to my scuba diver example, I love how I'm talking about this as if I've ever been in more than like five feet of water. (Laughing) So in my scuba diver example, I would—I bet someone who scuba dives is like "that's not how it works"—I (Laughing) would, I would think that I go straight to the bottom, right? I'm like ok, I know where the treasure is: at the bottom. I'm gonna go right at the bottom. And maybe along the way, I see a pool of fish—fish come in pools, right? No, schools. Dammit.

[00:14:38] VJ: They come in schools.

[00:14:39] SY: So the, so I, I encounter a school of fish, and then I have to like navigate around the school, you know? Or like maybe I see a big—I was gonna say leaf, but that doesn't make sense. (Laughing) I don't know, so... I'm trying like...

[00:14:53] VJ: Seaweed?

[00:14:53] SY: I'm like what is in the ocean? Like, you know, just other—maybe, maybe there was like, you know, just—maybe there's a ship, ok? Like a sunken—no, if it's a sunken ship, we're already at the bottom. Ok, but you know what I mean? Like maybe there's just stuff along the way, and I need to like go left and go right and, you know, find my way. So even though the most efficient way would be like to go straight down, I can't because there's all this crap in the way and I have to navigate around that, right? So when we talk about traversing a tree and trying to get to that node, are there similar obstacles? Are there like paths? Navigation strategies? Like how do I, how do I get to the thing, treasure node as fast as possible?

[00:15:35] VJ: There is only one way to get to a node, which is through a link, something that links to it. And usually what that means is if you wanna get to like a, a great-great grandchild node, well you have to have gotten there through that grandchild's parent and that parent and that parent. So basically, the path that you're kind of talking about is the path through which you arrived at that node, which means how you traversed from the route of the tree and which parent nodes and children nodes you chose to walk down in order to get to the final leaf node, which would be the treasure node if you're trying to find, for example, some sort of node at the end, at the bottom. The traversing has to do with whether you're in a binary search tree going to go left or right because if you'll recall in a binary search tree, every single parent node can only have two children, which means every single node if it has children will have a left and a right. So every time you get to a node, your only option is: do I go left? Do I go right? So that's kind of like your obstacle in that you have to make the choice...

[00:16:42] SY: Yeah.

[00:16:42] VJ: Do I go left? Do I go right?—every time you reach a node.

[00:16:44] SY: Ok, so how do I know where to go?

[00:16:49] VJ: Good question. So in depth-first search, the way that you find your path the way that you traverse your tree is that you start at the root node, and once you choose a path—if you're going left or right—once you start going down that path, you don't go up back to another level, which means if I go to a child node, I'm only gonna go down through it's children. I'm not gonna go back right away and like go to another level. That basically means once I've chosen a child node and I've chosen another child node, I'm not gonna go up and go to its uncle or go to it's aunt, if that makes sense. So...

[00:17:25] SY: Yes.

[00:17:26] VJ: ...another way of thinking about it is in depth-first search, you traverse through all of the children and the grandchildren on the path that you're on before you ever go to another path, which means if you're visualizing how a tree might look, you're gonna go all the way down from parent to parent to child to child to grandchild to great grandchild until you get all the way to a leaf node. And then you have nowhere left to go. And then you backtrack up and then you go back down again another path all the way down as far down as you can possibly go.

[00:18:02] SY: Ok. So how do I even pick a path? 'Cause I—well I guess if there's the binary search tree, there's—I'm at the root, but then I, I have to pick. Do I go left or do I go right? Right? That's like the first decision that I would make?

[00:18:18] VJ: Yeah.

[00:18:18] SY: So how do I know if to pick the left child or the right child?

[00:18:23] VJ: So this is basically in your analogy of like baking brownies, this is like tiny little tweaks that you could make in a strategy. So some algorithms, some implementations of this depth-first search algorithm might say always choose the right node, then look at the next right node. Another algorithm might say, no just always choose the left node, and then look at the right code. And then, you know, traverse down the left child all the way down. So these are different implementations. And what you referred to these as like the technical term is the strategies. And there's three different strategies, which kind of makes sense because there's pretty much only so many nodes that you can have in a binary search tree, so there's only so many strategies, but the names for these strategies are pre-order, in order and post-order traversal. And these are—based on which strategy you're choosing, you might decide to always look at the right node first or always look at the left node first or always look at the root node first. And we won't talk about that too much today, but that's kind of like a little taste of how you might decide do I go left or right. But if we're ignoring strategies entirely, we could just say you pick one, and that's the one you stick with for now.

[00:19:34] SY: So these three strategies—the pre-order, in order and post-order—these are three ways to implement depth-first search.

[00:19:45] VJ: Yes.

[00:19:45] SY: Ok.

[00:19:45] VJ: Yeah. They're three that all adhere to depth-first search but they're slightly different in that they tweak how you, how you decide where to go even though they're all depth-first search. The interesting thing is all three of them will give you slightly different results because if can imagine, if you have a very large tree and you're always looking at the right node first, say you're going through the tree and you were just printing out the value of every single node—and the tree is just a bunch numbers. So say you're just printing out, "oh, this node is number four. I'm gonna print out number four. And this node is number seven. I'm gonna print out 7." And if you always choose the right node first, you might end up with a slightly different list of numbers that are printed out versus if you always choose the left load verses if you chose the center node first and then pick something else to go to. So they'll give you different results. And the interesting thing is the strategies can be useful depending on what it is that you're trying to do.

[00:20:38] SY: Oh, what, what does that mean? (Laughing)

[00:20:41] VJ: Well, a great example is imagine that you wanted to traverse through a tree and you wanted to delete the tree as you traverse through, which might be a nice operation to do if, you know, you have a data structure, and you're like, "oh. We've done everything we want to do at this tree. Can we delete it so we don't need to have it in memory anymore?" Totally a valid thing to do. So maybe you have an algorithm that's like "delete the whole tree." And now, you're trying to delete all of these nodes and all of their memory, but you have to kind of think a little bit about you're gonna delete it because if you wanna delete a tree, you can't just delete haphazardly. You actually have to think about the order of operations because if you wanna delete a tree, you have to delete the children nodes before you delete the parent node. And you can kind of imagine why...

[00:21:27] SY: Oh.

[00:21:27] VJ: ...because in a tree, every node has a reference. If I delete a parent and it has children, I now have lost the references to those children, and there's no way to go...

[00:21:38] SY: Yeah.

[00:21:38] VJ: ...find them and delete them.

[00:21:40] SY: Oh.

[00:21:40] VJ: And they're basically...

[00:21:44] SY: Oh poor babies.

[00:21:44] VJ: Yeah, they're orphaned children nodes. So you want to be able to delete them in order, in a specific quarter so that you make sure you delete the children first—there are no orphan references to nodes that might be floating around—and then you go up and delete the parent. So in that situation, you wanna always make sure you're deleting the left and right reference before deleting the parent reference, or rather the parent node. You want to delete the parent nodes'...

[00:22:10] SY: Yeah.

[00:22:10] VJ: ...children references so that you're sure that you don't have random nodes floating around. So in that case, order does matter because if you're traversing through a tree and you make one mistake, well, you could have a whole bunch of nodes you totally forgot about 'cause you didn't think about the order. And that's actually something called post-order traversal, which we will talk about later. But that's, that's the name for the algorithm that would allow you to delete a tree in that way.

[00:22:40] SY: Oh. Oh my goodness. And the post-order traversal is still within the world of depth-first search.

[00:22:51] VJ: Yep. Yeah it's, it's one implementation of depth-first search that because of the strategy that it uses, the steps that it uses in its algorithm, it proves to be very useful for deleting a tree because it specifically chooses which thing to delete, which node to look at next before it does anything else.

[00:23:10] SY: Ok. So what I am getting from that really awesome and a little sad example, you know, you don't wanna have orphan, orphan nodes (Laughing) is that the three strategies—pre-order, in order, post-order—it's not that one of them is better, but it sounds like they are pretty good or maybe not the great choice for specific tasks, specific goals that you have and then things you want to do with that tree.

[00:23:37] VJ: Exactly.

[00:23:38] SY: Is that fair?

[00:23:38] VJ: Yeah, yeah.

[00:23:40] SY: Ok. Interesting. Ok, so we talked about how some strategies are better than others, but does it really matter what strategy I use as long as I end up with brownies? What's the big deal? What happens if I use the wrong strategy?

[00:23:57] VJ: I guess a good way of answering that is to point out that depth-first search is one strategy, really, right? Because it is—if you go back to the swimming example, you could go searching for treasure, and you could decide "I'm going to swim all the way down to the bottom of this trench or wherever. Like I know exactly where the treasure is. That's great. I'm gonna go swim. Why will I go swim anywhere else 'cause I, I know it's probably gonna be around this area?" But imagine that you didn't really know where the treasure might be. You might wanna go and snorkel and maybe not go deep. You might just want to like, I don't know, take one of those metal detector thingies and just be like I'm gonna measure the magnetism something, something science. I don't know how you do that. (Laughing)

[00:24:36] SY: I was like "oh, Vaidehi knows about, about metal detector thingies."

[00:24:41] VJ: No, no. Not at all. I'm just. I'm just riding the wave of this metaphor is what I'm trying to do. (Laughing)

[00:24:50] SY: The wave of the metaphor. What? (Laughing) On fire.

[00:24:55] VJ: Well imagine that you know more about science and metal detectors than I do. And you are like...

[00:24:56] SY: Yes.

[00:24:56] VJ: I know I'm gonna narrow down where to search because the ocean is a big place, so I'm just gonna like vaguely swim out and like wherever I catch the signal or whatever, wherever I feel like there's a chance that there might be treasure here, that's the area I'll go swim deeper into. And maybe I'll be wrong, and I'll backtrack. And I might go out further into the ocean and then go a little bit deep and then backtrack. That's more of a breadth-first search strategy. So the depth-first search one where you're going deep into a part of the ocean, deep into a tree structure and not really looking back till you get to the bottom, that can be a good strategy if you kind of have a sense that where you're searching is like generally good. (Laughing) It is plausible to find a result.

[00:25:44] SY: Yeah. If you're confident.

[00:25:45] VJ: Yes. That's a great, that's a great way of putting it. If you're confident, yes. Depth -first search is probably gonna be the best strategy in terms of searching, but there is a risk which is probably the biggest con of depth-first search. If you imagine you have a binary search tree and you start searching down the left subtree because you remember binary trees are recursive so there are left and right subtrees. So say you're like, "oh, I'm gonna go down left. I don't, I don't actually know where this node is. I don't know where the treasure is, but I'm just gonna go down left." And it's a massive tree. And suddenly you're going deep, deep, deep, deep, deep, deep into this left subtree. And now you're like searching deep within the nodes there. And you spend all this time doing that. You come back up to the root node, to the surface and you go two nodes down the right subtree, and there's the thing you were looking for. So you spent all this time looking deep...

[00:26:33] SY: Aw man.

[00:26:33] VJ: ...into the left sub tree was kind of like...

[00:26:35] SY: Aw, that sucks.

[00:26:36] VJ: Yeah. It was a lot of wasted effort in a way. It's not super efficient. What I'm trying to get at is that it can be a gamble if you don't really have any idea which direction like left or right or like where out in the ocean you wanna go. So sometimes people will do combinations of breadth-first and depth-first. Some people will start with breadth-first and then based on some qualifications, will go deeper, which tends to be smarter rather than just being like the ocean's big, but you know what? (Laughing) Hey, this is the Gulf of Mexico. I'm just gonna go dive here. And, you know, the treasure could be, you know, somewhere (Music) in like Southeast Asia. That's very far from Mexico, and you're gonna be swimming a long time.

[00:27:18] SY: So next episode, we're gonna actually walk through an example of how each of those three strategies—the pre-order, the in order and the post-order—work. And then hopefully we'll get a nice, clear picture of how the actually operate. So I'm super excited about that. And that's the end of today's episode. If you liked what you heard, please leave us a review and make sure to check out Vaidehi's blog post. Link to that is in the show notes. Also wanna give a huge shoutout to Twilio for sponsoring the season. Make sure to check out their awesome API. To get your twenty dollars in free Twilio credit, text your name 480-485-4321. Phone number and link are in your show notes. Vaidehi, you wanna say good bye?

[00:27:58] VJ: Bye. And send us your brownie recipes.

[00:28:00] SY: And also brownies.

[00:28:03] VJ: Oh, yeah. That, too.

[00:28:03] SY: Thanks for listening. See you next week.

[00:28:06] VJ: Yes. We were... Our only—woah. What were those words? We (Laughing) That was a strange sound that wasn't a word.

[00:28:16] SY: I love when it's not just me that does that. (Laughing) It's great.

Thank you for
supporting the show!

Get a promocode for $20 in free
Twilio credit and a quickstart guide for your
favorite language.
Text your name to 480-485-4321.

The easiest cloud platform to deploy, manage, and scale applications of any size.
Try it for free with a $100 credit.