{"id":3546,"date":"2018-01-14T19:51:59","date_gmt":"2018-01-14T14:21:59","guid":{"rendered":"http:\/\/theoryofprogramming.azurewebsites.net\/?p=3546"},"modified":"2018-01-14T19:51:59","modified_gmt":"2018-01-14T14:21:59","slug":"iterative-deepening-depth-first-search-iddfs","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2018\/01\/14\/iterative-deepening-depth-first-search-iddfs\/","title":{"rendered":"Iterative Deepening Depth First Search (IDDFS)"},"content":{"rendered":"<p>Hello people! In this post we will talk about another search algorithm Iterative deepening depth first search (IDDFS) or Iterative deepening search (IDS). This algorithm is used when you have a goal directed agent in an infinite search space (or search tree).<\/p>\n<p>Why do Breadth First Search (BFS) and Depth First Search (DFS) fail in the case of an infinite search space?<\/p>\n<ul>\n<li>In DFS, you would recursively look at a node&#8217;s adjacent vertex. DFS may not end in an infinite search space. Also, DFS may not find the shortest path to the goal. DFS needs O(d) space, where d is depth of search.<\/li>\n<li>BFS consumes too much memory. BFS needs to store all the elements in the same level. In the case of a tree, the last level has N \/ 2 leaf nodes, the second last level has N \/ 4. So, BFS needs O(N) space.<\/li>\n<\/ul>\n<p>Iterative deepening depth first search (IDDFS) is a hybrid of BFS and DFS. In IDDFS, we perform DFS up to a certain &#8220;limited depth,&#8221; and keep increasing this &#8220;limited depth&#8221; after every iteration.<\/p>\n<p>Let us take an example to understand this &#8211;<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3576 size-full\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?resize=695%2C661&#038;ssl=1\" alt=\"\" width=\"695\" height=\"661\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?w=695&amp;ssl=1 695w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?resize=300%2C285&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?resize=230%2C219&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?resize=350%2C333&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-1-1.jpg?resize=480%2C457&amp;ssl=1 480w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" data-recalc-dims=\"1\" \/><\/a>Our starting node (A) is at a depth of 0. Our goal node (R) is at a depth of 4. The above example is a finite tree, but think of the above tree as an infinitely long tree and only up to depth = 4 is shown in the diagram.<\/p>\n<p>As stated earlier, in IDDFS, we perform DFS up to a certain depth and keep incrementing this allowed depth. Performing DFS upto a certain allowed depth is called <strong>Depth Limited Search<\/strong> (DLS). As Depth Limited Search (DLS) is important for IDDFS, let us take time to understand it first.<\/p>\n<p>Let us understand DLS, by performing DLS on the above example. In Depth Limited Search, we first set a constraint on how deep (or how far from root) will we go. Let&#8217;s say our limit (<em>DEPTH<\/em>) is 2. Now, in the above diagram, place your hand to cover the nodes at depth 3 and 4. Now, by looking at the rest of the nodes, can you tell the order in which a normal DFS would visit them? It would be as follows &#8211;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nA B E F C G D H\n<\/pre>\n<p>Can you do it for <em>DEPTH<\/em> = {0, 1, 2, 3, 4} ? Just cover the nodes you don&#8217;t need with your hand and try to perform DFS in you mind. You should get answers like this &#8211;<\/p>\n<div class=\"su-table su-table-alternate\">\n<table>\n<tbody>\n<tr>\n<td><em>DEPTH<\/em><\/td>\n<td>DLS traversal<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>A<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>A B C D<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>A B E F C G D H<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>A B E I F J K C G L D H M N<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>A B E I F J K O P C G L R D H M N S<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now that you have got an idea of Depth Limited Search, Iterative deepening depth first search is just one loop away! The pseudo-code for IDDFS is as below &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nIDDFS(tree):\n    for depth = 0 to infinity:\n        if (DLS(tree, depth)):\n            return true\n\n    return false\n<\/pre>\n<p>Before you race off to code, here are a few things &#8211;<\/p>\n<ul>\n<li>IDDFS is used to check if the goal is reachable from start node. So its return type is boolean.<\/li>\n<li>IDDFS is only used to check, not return the path from start node to goal. So we don&#8217;t maintain anything like parent array (like in DFS).<\/li>\n<li>IDDFS is meant to run DLS from 0 \u2192 \u221e, but we will write our IDDFS program to run DLS from 0 \u2192 MAX_DEPTH. Because in real world we never run anything up to \u221e.<\/li>\n<li>First code the DLS method, then add the IDDFS method which calls the DLS method.<\/li>\n<li>IDDFS is meant to run in an infinite space tree. So, you can use a binary tree if you want, but in my opinion using an N-ary tree makes more sense. So, in my code below I use N-ary tree, the code taken from my article on <a href=\"http:\/\/theoryofprogramming.azurewebsites.net\/2018\/01\/14\/n-ary-tree-k-way-tree-data-structure\/\">N-ary tree<\/a>.<\/li>\n<\/ul>\n<p>You should be capable of writing the code for Iterative deepening depth first search now. Try it, I&#8217;m sure you can \ud83d\ude09 You can refer to my code if you get stuck &#8211;<\/p>\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Code<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Output<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Code\">\n<strong>Link to code &#8211; <a href=\"https:\/\/github.com\/VamsiSangam\/theoryofprogramming\/blob\/master\/Artificial%20Intelligence\/Iterative%20Deepening%20Depth%20First%20Search\/Java\/IterativeDeepeningDepthFirstSearch.java\">GitHub<\/a><\/strong><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Output\">\n[code]\nA<br \/>\n   B<br \/>\n      E<br \/>\n         I<br \/>\n      F<br \/>\n         J<br \/>\n         K<br \/>\n            O<br \/>\n            P<br \/>\n   C<br \/>\n      G<br \/>\n         L<br \/>\n            R<br \/>\n   D<br \/>\n      H<br \/>\n         M<br \/>\n         N<br \/>\n            S<br \/>\nDepth = 0, DLS Traversal =&gt; A,<br \/>\nDepth = 1, DLS Traversal =&gt; A, B, C, D,<br \/>\nDepth = 2, DLS Traversal =&gt; A, B, E, F, C, G, D, H,<br \/>\nDepth = 3, DLS Traversal =&gt; A, B, E, I, F, J, K, C, G, L, D, H, M, N,<br \/>\nGoal node = R is not reachable at a depth of 3<br \/>\nDepth = 0, DLS Traversal =&gt; A,<br \/>\nDepth = 1, DLS Traversal =&gt; A, B, C, D,<br \/>\nDepth = 2, DLS Traversal =&gt; A, B, E, F, C, G, D, H,<br \/>\nDepth = 3, DLS Traversal =&gt; A, B, E, I, F, J, K, C, G, L, D, H, M, N,<br \/>\nDepth = 4, DLS Traversal =&gt; A, B, E, I, F, J, K, O, P, C, G, L, R,<br \/>\nGoal node = R is reachable at a depth of 4<br \/>\n[\/code]\n<\/div><\/div><\/div>\n<p>In the output, the tree is printed first, then the IDDFS traversals. Purposefully, I took the goal node as a node which is not reachable by <em>depth = 3<\/em> but is reachable by <em>depth = 4<\/em>. As you have noticed from the output above, we visit the nodes at <em>depth = 0<\/em> a lot, the nodes at <em>depth = 2<\/em> a little fewer but we visit them multiple times too, and we visit the nodes at <em>depth = DEPTH_MAX<\/em> only once. This may seem inefficient, but it is actually not. This is because, there are very few nodes at <em>depth = 0<\/em>, but a lot of nodes at <em>depth = DEPTH_MAX<\/em>. If &#8216;<em>d<\/em>&#8216; is depth, and &#8216;<em>b<\/em>&#8216; is the branching factor in the search tree (this would be N for an N-ary tree), then mathematically &#8211;<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?ssl=1\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3580\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?resize=614%2C69&#038;ssl=1\" alt=\"Iterative deepening depth first search time complexity Theory of Programming\" width=\"614\" height=\"69\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?w=614&amp;ssl=1 614w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?resize=300%2C34&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?resize=230%2C26&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?resize=350%2C39&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2018\/01\/iddfs-time-complexity.png?resize=480%2C54&amp;ssl=1 480w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" data-recalc-dims=\"1\" \/><\/a>The time complexity remains O(b<sup>d<\/sup>) but the constants are large, so IDDFS is slower than BFS and DFS (which also have time complexity of O(b<sup>d<\/sup>)).<\/p>\n<div class=\"su-table su-table-alternate\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Time complexity<\/td>\n<td>Space complexity<\/td>\n<\/tr>\n<tr>\n<td>DFS<\/td>\n<td>O(b<sup>d<\/sup>)<\/td>\n<td>O(d)<\/td>\n<\/tr>\n<tr>\n<td>BFS<\/td>\n<td>O(b<sup>d<\/sup>)<\/td>\n<td>O(b<sup>d<\/sup>)<\/td>\n<\/tr>\n<tr>\n<td>IDDFS<\/td>\n<td>O(b<sup>d<\/sup>)<\/td>\n<td>O(bd)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Iterative deepening depth first search may not be directly used in practical applications but the technique of iteratively progressing your search in an infinite search space is pretty useful and can be applied in many AI applications.<\/p>\n<p>Congrats, your AI just got better! Keep practicing! Happy coding! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello people! In this post we will talk about another search algorithm Iterative deepening depth first search (IDDFS) or Iterative deepening search [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[19,23,41,99],"class_list":["post-3546","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","tag-algorithms","tag-artificial-intelligence","tag-depth-first-search","tag-search-algorithms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/3546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=3546"}],"version-history":[{"count":0,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/3546\/revisions"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=3546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=3546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=3546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}