Answers.Body: <p>Either one. The BFS algorithm and DFS algorithm do not specify.</p>
  <p>Typically, it's programmed as left-to-right, just because that's the way programmers think about trees. It doesn't have to be.</p>
  <p>Note that DFS isn't &quot;deepest node first&quot; either. Imagine that nodes H and I in your tree did not exist; D, J, K, E, B would be a perfectly valid DFS traversal of B, even though J and K are deeper than D. So would B, D, E, J, K even though E is the parent of J and K! DFS says that you look at a node's children before you look at other nodes on the same layer, but it doesn't say you have to look at the node's children before the node itself. In fact there are three well-known variants (pre-order, post-order and in-order) depending on whether you visit each node before, after or in the middle of its children.</p>
  <p>Now, if this is for an AI then you probably do care about the order. If this tree represents a game tree, then you probably want to estimate which node is likely to have the best outcome for the AI player, and check that one first. This can be called &quot;best-first search&quot;.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-22T21:02:42.983Z
Answers.Text: Your first 3 paragraphs are correct, but I think it may be misleading to say that DFS is not "deepest node first". You go as deep as possible in the current path, then backtrack and do that again. So, in your example, D is really the deepest node in that path. This is the reason why it's called depth-first, but I understand your example. In your last paragraph, note that best-first search is a family of algorithms, of which A* is one instantiation. The difference between these best-first search algorithms and e.g. BFS is that you use an heuristic function to guide the search.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-22T16:25:57.140Z
Answers.LastActivityDate: 2021-10-22T16:25:57.140Z
Answers.UserDisplayName: Criticize SE actions means ban
Answers.Body: <p>BFS and DFS are <strong>usually</strong> applied to <strong>unweighted graphs</strong> (or, equivalently, to graphs where the edges have all the same weights). In this case, BFS is optimal, i.e., assuming a finite branching factor, it <em>eventually</em> finds the optimal solution, but it doesn't mean that it takes a short time, in fact, it might well be the opposite, depending on the search space. DFS would not be optimal because it may not terminate if there is an infinite path.</p>
  <p>In the case of unweighted graphs, BFS really proceeds <strong>level-by-level</strong>. In other words, it starts at the root (level <span class="math-container">$l=0$</span>), then <em>expands</em> (i.e. adds to the FIFO queue) all children of the root (level <span class="math-container">$l=1$</span>), then it expands all children of the children of the root (level <span class="math-container">$l=2$</span>), and so on. So, for example, all children at level <span class="math-container">$l=2$</span> have a distance of <span class="math-container">$2$</span> to the root (if we assume that edges have a weight of <span class="math-container">$1$</span>).</p>
  <p>The order in which you choose nodes at a certain level to add to your FIFO queue is, as far as I know, typically from left-to-right, but you could, in principle, choose them in different ways (e.g. from right-to-left). So, this is a convention. Of course, this choice may affect when (and <em>if</em>, in the case of DFS) you find the solution.</p>
  <p>Now, if we applied BFS to a weighted graph, then what happens here? It depends. If you ignore the weights, then it is the usual BFS. If you take into account the weights, it depends on how you take them into account. For example, if you choose to expand (i.e. add to the queue) nodes with the shortest path so far to the root, then this becomes a <em>uniform-cost search</em> (this is explained in the AIMA book, e.g. 3rd edition, section 3.4.2, p. 83).</p>
  <p>So, maybe your professor has in mind the uniform-cost search when he's telling you to choose the &quot;shallowest node first&quot;, but you need to talk to him, tell him about uniform-cost search, and ask him if that's what he means.</p>
  <p>It also seems to me that your original idea of how DFS works is correct. It goes deep, then backtracks (without taking into account weights). If you take into account the weights, I don't know what DFS could turn into.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-22T16:34:59.700Z
Answers.LastActivityDate: 2021-10-22T16:34:59.700Z
Answers.UserDisplayName: nbro
Body: <p>I am currently taking an Artificial Intelligence course and learning about DFS and BFS.</p>
  <p>If we take the following example:</p>
  <p><a href="https://i.sstatic.net/5Ifcs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5Ifcs.png" alt="enter image description here" /></a></p>
  <p>From my understanding, the BFS algorithm will explore the first level containing <span class="math-container">$B$</span> and <span class="math-container">$C$</span>, then the second level containing <span class="math-container">$D,E,F$</span> and <span class="math-container">$G$</span>, etc., till it reaches the last level.</p>
  <p>I am lost concerning which node between <span class="math-container">$B$</span> and <span class="math-container">$C$</span> (for example) will the BFS expand first?</p>
  <p>Originally, I thought it is different every time, and, by convention, we choose to illustrate that it's done from the left to the right (so exploring <span class="math-container">$B$</span> then <span class="math-container">$C$</span>), but my professor said that our choice between <span class="math-container">$B$</span> and <span class="math-container">$C$</span> depends on each case and we choose the &quot;shallowest node first&quot;.</p>
  <p>In made examples, there isn't a distance factor between <span class="math-container">$A$</span> and <span class="math-container">$B$</span>, and <span class="math-container">$A$</span> and <span class="math-container">$C$</span>, so how could one choose then?</p>
  <p>My question is the same concerning DFS where I was told to choose the &quot;deepest node first&quot;. I am aware that there are pre-order versions and others, but the book &quot;Artificial Intelligence - A Modern Approach, by Stuart Russel&quot; didn't get into them.</p>
  <p>I tried checking the CLRE algorithms book for more help but the expansion is done based on the order in the adjacency list which didn't really help.</p>
  
Tags: <search><breadth-first-search><depth-first-search>
Title: How do the BFS and DFS search algorithms choose between nodes with the "same priority"?
UserDisplayName: Sergio
ThreadText: # How do the BFS and DFS search algorithms choose between nodes with the "same priority"?
  
  (Asked by: username_0 on 2021-10-22)
  
  I am currently taking an Artificial Intelligence course and learning about DFS and BFS.
  
  If we take the following example:
  
  [![enter image description here](https://i.sstatic.net/5Ifcs.png)](https://i.sstatic.net/5Ifcs.png)
  
  From my understanding, the BFS algorithm will explore the first level containing $B$ and $C$, then the second level containing $D,E,F$ and $G$, etc., till it reaches the last level.
  
  I am lost concerning which node between $B$ and $C$ (for example) will the BFS expand first?
  
  Originally, I thought it is different every time, and, by convention, we choose to illustrate that it's done from the left to the right (so exploring $B$ then $C$), but my professor said that our choice between $B$ and $C$ depends on each case and we choose the "shallowest node first".
  
  In made examples, there isn't a distance factor between $A$ and $B$, and $A$ and $C$, so how could one choose then?
  
  My question is the same concerning DFS where I was told to choose the "deepest node first". I am aware that there are pre-order versions and others, but the book "Artificial Intelligence - A Modern Approach, by Stuart Russel" didn't get into them.
  
  I tried checking the CLRE algorithms book for more help but the expansion is done based on the order in the adjacency list which didn't really help.
  
  **Tags:** search, breadth-first-search, depth-first-search
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2021-10-22. Score: 2 | Accepted answer
  
  BFS and DFS are **usually** applied to **unweighted graphs** (or, equivalently, to graphs where the edges have all the same weights). In this case, BFS is optimal, i.e., assuming a finite branching factor, it *eventually* finds the optimal solution, but it doesn't mean that it takes a short time, in fact, it might well be the opposite, depending on the search space. DFS would not be optimal because it may not terminate if there is an infinite path.
  
  In the case of unweighted graphs, BFS really proceeds **level-by-level**. In other words, it starts at the root (level $l=0$), then *expands* (i.e. adds to the FIFO queue) all children of the root (level $l=1$), then it expands all children of the children of the root (level $l=2$), and so on. So, for example, all children at level $l=2$ have a distance of $2$ to the root (if we assume that edges have a weight of $1$).
  
  The order in which you choose nodes at a certain level to add to your FIFO queue is, as far as I know, typically from left-to-right, but you could, in principle, choose them in different ways (e.g. from right-to-left). So, this is a convention. Of course, this choice may affect when (and *if*, in the case of DFS) you find the solution.
  
  Now, if we applied BFS to a weighted graph, then what happens here? It depends. If you ignore the weights, then it is the usual BFS. If you take into account the weights, it depends on how you take them into account. For example, if you choose to expand (i.e. add to the queue) nodes with the shortest path so far to the root, then this becomes a *uniform-cost search* (this is explained in the AIMA book, e.g. 3rd edition, section 3.4.2, p. 83).
  
  So, maybe your professor has in mind the uniform-cost search when he's telling you to choose the "shallowest node first", but you need to talk to him, tell him about uniform-cost search, and ask him if that's what he means.
  
  It also seems to me that your original idea of how DFS works is correct. It goes deep, then backtracks (without taking into account weights). If you take into account the weights, I don't know what DFS could turn into.
  
  
  ---
  
  ### Answer by username_1 on 2021-10-22. Score: 1
  
  Either one. The BFS algorithm and DFS algorithm do not specify.
  
  Typically, it's programmed as left-to-right, just because that's the way programmers think about trees. It doesn't have to be.
  
  Note that DFS isn't "deepest node first" either. Imagine that nodes H and I in your tree did not exist; D, J, K, E, B would be a perfectly valid DFS traversal of B, even though J and K are deeper than D. So would B, D, E, J, K even though E is the parent of J and K! DFS says that you look at a node's children before you look at other nodes on the same layer, but it doesn't say you have to look at the node's children before the node itself. In fact there are three well-known variants (pre-order, post-order and in-order) depending on whether you visit each node before, after or in the middle of its children.
  
  Now, if this is for an AI then you probably do care about the order. If this tree represents a game tree, then you probably want to estimate which node is likely to have the best outcome for the AI player, and check that one first. This can be called "best-first search".
  
  > **Comments:**
  > **username_2**: Your first 3 paragraphs are correct, but I think it may be misleading to say that DFS is not "deepest node first". You go as deep as possible in the current path, then backtrack and do that again. So, in your example, D is really the deepest node in that path. This is the reason why it's called depth-first, but I understand your example. In your last paragraph, note that best-first search is a family of algorithms, of which A* is one instantiation. The difference between these best-first search algorithms and e.g. BFS is that you use an heuristic function to guide the search.  
  
  ---
  
  