 Hello everyone. This is Alice Gao. In this video, I'm going to discuss the depth-first search algorithm. Recall the generic search algorithm. Our strategy for selecting and removing a path from the frontier determines the search algorithm. Depth-first search treats the frontier as a stack. A stack is last seen first out. Think of a stack of plates. We add and remove items at the same end of the stack. Since the frontier is a stack, we choose to remove the most recent path added to the frontier at every step. Let's trace the FS on a search graph. We will add nodes to the frontier in alphabetical order. It is crucial to specify the order of adding nodes to the frontier. The order of adding nodes to the frontier determines the order of removing nodes from the frontier during the algorithm execution. While tracing the algorithm, you should draw the search tree, keep track of the nodes added to and removed from the frontier, and label the nodes in the order of expansion. Pause the video and trace the algorithm yourself. Then keep watching. Here's the final search tree when the FS terminates. I will describe the tracing process in a separate video. As you can see, the FS goes deep into the tree and searches one path to completion. If the path doesn't lead to a goal node, then the FS backtracks and tries another path. Let me discuss some properties of the FS. I will discuss space complexity, time complexity, completeness, and optimality. For space complexity, I will quantify the maximum size of the frontier. For time complexity, I will count the number of nodes that the algorithm has to visit in the worst case. For completeness, I will determine whether the FS is guaranteed to find the solution if a solution exists. Finally, for optimality, I will determine whether the FS is guaranteed to find the path with the least total cost if it terminates on a problem. When characterizing the complexities, I will make use of three quantities. B is the branching factor, or the maximum number of successors or neighbors that any node has. Intuitively, B determines how fat or how wide the search tree is. M is the maximum depth of the search tree, and D is the depth of the shallow's goal node. What is the relationship between M and D? Let's look at the picture. Let the triangle represent the search tree, and let the Gs represent the goal nodes in different locations. If at least two goal nodes are at different depth, M is strictly greater than D. Otherwise, if all the goal nodes are at the same depth, then M and D are the same. Let's look at space complexity. What is the size of the frontier for DFS in the worst case? Note that it's sufficient for us to store the frontier in memory. There is no need to store the search graph or the search tree. Why is this the case? First, the search graph or the search tree may be large or infinite, so it may not be possible to store them in memory. Second, the frontier contains sufficient information for us to execute the search algorithm, so there is no need to store more than that. Let's come back to space complexity. What is the size of the frontier in the worst case? Let the triangle represent the search tree. DFS explores one path at a time. In the worst case, the current path has M nodes, where M is the maximum depth of the search tree. DFS must remember these M nodes in memory. If the current path does not lead to a goal, DFS must backtrack and try another path. To do this, DFS needs to remember the alternative nodes at every level. These alternative nodes are the siblings of the nodes on the current path. How many siblings does each node have? The maximum number of siblings is B, the branching factor. In summary, DFS must remember at most M nodes on the current path. Furthermore, for each of the M nodes, DFS needs to remember at most B siblings. Therefore, the space complexity is big O of B multiplied by M. The space complexity is linear in M, the maximum depth of the search tree. Let's think about time complexity. How many nodes does DFS need to visit in the worst case? In the worst case, DFS will not find the goal node until it has explored the entire search tree. How many nodes are there in the search tree? The tree has M levels. As we move down in the tree, the number of nodes on each level increases exponentially. Level 0 has one node, level 1 has at most B nodes, level 2 has at most B square nodes, and the bottom level has at most B to the power of M nodes. The size of the bottom level is the dominating term in the summation. Therefore, the time complexity is big O of B to the power of M. Note that the time complexity is exponential in M, the maximum depth of the search tree. So far, I've discussed the space and time complexities of DFS. How should we interpret them? Are they good or bad? Are they surprising or expected? Our search problem is challenging. The search tree has an exponential number of nodes, and the algorithm may have to visit all of them in the worst case. Given this, having an exponential time complexity is expected. Most search algorithms will have exponential space and time complexity. However, DFS stands out by having a linear space complexity. Let me discuss completeness. A search algorithm is complete if and only if it is guaranteed to find a solution if a solution exists. Unfortunately, DFS is not complete. If the search tree has an infinite path, DFS will get stuck in the infinite path and will not terminate. The search tree may have an infinite path for several reasons. For example, if the search graph has a cycle, then the corresponding search tree will have an infinite path. Alternatively, the search tree may have an infinite path not because of a cycle. I encourage you to find an example where the search tree has an infinite path, but the search graph has no cycles. Finally, is DFS optimal? If DFS terminates on a problem, is it guaranteed to return the path with the least total cost? Unfortunately, the answer is no. DFS is oblivious of the costs. Even if DFS finds a solution, it has no guarantee on the quality of the solution found. That's everything on the depth first search algorithm. Let me summarize. After watching this video, you should be able to do the following. Explain DFS at a high level. Trace DFS on a search graph. Describe properties of DFS, including space complexity, time complexity, completeness, and optimality. Thank you very much for watching. I will see you in the next video. Bye for now.