<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hugo</title>
    <description>The latest articles on DEV Community by Hugo (@hugos).</description>
    <link>https://dev.to/hugos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1051072%2Fe451df3a-4d11-47cc-92c2-884f83c0e56b.jpeg</url>
      <title>DEV Community: Hugo</title>
      <link>https://dev.to/hugos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hugos"/>
    <language>en</language>
    <item>
      <title>What is Depth First Search?</title>
      <dc:creator>Hugo</dc:creator>
      <pubDate>Fri, 14 Apr 2023 15:44:26 +0000</pubDate>
      <link>https://dev.to/hugos/depth-first-search-made-easy-119g</link>
      <guid>https://dev.to/hugos/depth-first-search-made-easy-119g</guid>
      <description>&lt;h2&gt;
  
  
  What is Depth First Search?
&lt;/h2&gt;

&lt;p&gt;Depth-First Search (DFS), is a graph/tree traversal algorithm that explores as far as possible along each branch, before backtracking.&lt;/p&gt;

&lt;p&gt;This algorithm keeps track of the visited nodes to avoid revisiting them and uses a stack to remember the nodes that still need to be explored.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can implement DFS &lt;strong&gt;recursively&lt;/strong&gt; or &lt;strong&gt;iteratively&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's important to note however, that in the Recursive implementation, the &lt;strong&gt;function call stack&lt;/strong&gt; is used as a stack data structure, while in the iterative implementation, an explicit stack is used instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why is this used?
&lt;/h4&gt;

&lt;p&gt;DFS is commonly used to solve problems involving graphs and trees, such as finding connected components, detecting cycles, and traversing a tree in a specific order.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Cool Fact: A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre Trémaux, as a strategy for solving mazes. You can read more about it &lt;a href="https://en.wikipedia.org/wiki/Maze-solving_algorithm" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  When should you use DFS:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;If solutions are frequent and located deep in the tree.&lt;/li&gt;
&lt;li&gt;Determining whether a path exists between two nodes.&lt;/li&gt;
&lt;li&gt;If the tree is very wide.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The Three types of Searching Orders
&lt;/h2&gt;

&lt;p&gt;There's 3 types of Traversal methods in DFS, these are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pre-Order&lt;/li&gt;
&lt;li&gt;In-Order&lt;/li&gt;
&lt;li&gt;Post-Order&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Pre-Order Traversal
&lt;/h3&gt;

&lt;p&gt;Pre-order traversal is to visit the root first. Then traverse the left subtree. And finally, traverse the right subtree.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ROOT-LEFT-RIGHT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" alt="Tree data structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: F-B-A-D-C-E-G-I-H &lt;/p&gt;




&lt;h3&gt;
  
  
  In-Order Traversal
&lt;/h3&gt;

&lt;p&gt;In Order is to traverse the left subtree first. Then visit the Root node. And finally, traverse the right subtree.&lt;br&gt;
And typically, for binary search tree, we can retrieve all the data in &lt;strong&gt;sorted&lt;/strong&gt; order using this type of traversal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;LEFT-ROOT-RIGHT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" alt="Tree data structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output: A-B-C-D-E-F-G-H-I&lt;/p&gt;




&lt;h3&gt;
  
  
  Post-Order Traversal
&lt;/h3&gt;

&lt;p&gt;In Order is to traverse the left subtree first. Then traverse the right subtree. And then we visit the root.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;LEFT-RIGHT-ROOT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zbbml8wxsycww8xbogk.png" alt="Tree data structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output: A-C-E-D-B-H-I-G-F&lt;/p&gt;




&lt;p&gt;Here's how to implement each traversal method in code:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that I'm using normal Recursion here. Tail Recursion however, would be more efficient. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Pre-Order:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function preorderTraversal(root) {
    if (!root) return [];
    let result = [];

    result.push(root.val);

    if (root.left) {
        result = [...result, ...preorderTraversal(root.left)]
    }
    if (root.right) {
        result = [...result, ...preorderTraversal(root.right)]
    }

    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  In-Order:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function preorderTraversal(root) {
    if (!root) return [];
    let result = [];

    if (root.left) {
        result = [...result, ...preorderTraversal(root.left)]
    }

    result.push(root.val);

    if (root.right) {
        result = [...result, ...preorderTraversal(root.right)]
    }

    return result;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Post-Order:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function preorderTraversal(root) {
    if (!root) return [];
    let result = [];

    if (root.left) {
        result = [...result, ...preorderTraversal(root.left)]
    }

    if (root.right) {
        result = [...result, ...preorderTraversal(root.right)]
    }

    result.push(root.val);
    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I try to make these "hard" concepts a bit more easy to understand. Please let me know your thoughts and if you would like to see more of these! 😄&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hugo&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>leetcode</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What is Dynamic Programming?</title>
      <dc:creator>Hugo</dc:creator>
      <pubDate>Fri, 24 Mar 2023 00:49:02 +0000</pubDate>
      <link>https://dev.to/hugos/what-is-dynamic-programming-16f7</link>
      <guid>https://dev.to/hugos/what-is-dynamic-programming-16f7</guid>
      <description>&lt;p&gt;Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming.&lt;br&gt;
The idea is to simply store the results of subproblems, so that we do not have to re-compute them when needed later. &lt;/p&gt;

&lt;p&gt;This simple optimization reduces time complexities from exponential to linear.&lt;/p&gt;

&lt;p&gt;For example, if we write a simple recursive solution for Fibonacci, we get exponential time complexity and if we optimize it by storing solutions of sub-problems, time complexity reduces to linear.&lt;br&gt;
 &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're not familiar with Big O Notation, I'll be writing a post eventually. But simply put, Big O notation is used to classify algorithms according to how their run time or space requirements grow as the input size grows.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  In other words:
&lt;/h4&gt;

&lt;p&gt;Dynamic Programming is simply an optimization technique using Cache, to store previously calculated values and return these values whenever they're needed, without having to re-calculate them again. &lt;br&gt;
We save these values in Cache (usually called memo), using an Hashmap data structure, for fast access.&lt;/p&gt;




&lt;h3&gt;
  
  
  Types of Dynamic Programming
&lt;/h3&gt;

&lt;p&gt;It's important to note that there's two approaches to dynamic programming. Memoization and Tabulation.&lt;/p&gt;

&lt;p&gt;One of the most used examples when introducing Dynamic Programming and recursion is the Fibonnaci sequence.&lt;/p&gt;

&lt;p&gt;Take a look at the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frmey6v2vmtvp8uj0hxka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frmey6v2vmtvp8uj0hxka.png" alt="fibonnaci sequence"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;br&gt;
In this image we're calculating the Fibonnaci of 5. &lt;br&gt;
The highlighted values represent repeated calculations, which we can store in cache whenever when we calculate them for the first time, this will ensure we not re-calculate them again, whenever these values are needed.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;This is a simple example. However, to give you a broader view of the great improvement of Dynamic Programming, imagine we needed to calculate the Fibonnaci of 30, here's how many calculations we would do:&lt;/p&gt;

&lt;p&gt;Without DP: &lt;em&gt;2692537&lt;/em&gt; calculations.&lt;br&gt;
With DP: &lt;em&gt;59&lt;/em&gt; calculations.&lt;/p&gt;




&lt;p&gt;A pretty mind-blowing difference, don't you think?&lt;/p&gt;

&lt;p&gt;Let's see how we can implement DP by improving the recursive Fibonnaci solution.&lt;/p&gt;

&lt;h4&gt;
  
  
  Recursive Fibbonaci
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fib(n) {
    if (n &amp;lt; 2) {
        return n;
    } else {
        return fib(n-1) + fib(n-2)
    }
}
// Time Complexity - O(2^n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Fibbonaci with Memoization
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function dynamicFib(n, cache) {
    let cache = cache || {};
    if(cache[n]) return cache[n]

    // base case
    if (n &amp;lt; 2) return n;

    return cache[n] = dynamicFib(n-1, cache[n]) + dynamicFib(n-2, cache[n])
}
// Time Complexity - O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To practice your new gained knowledge, I encourage you to try solving the following leetcode problem: &lt;a href="https://leetcode.com/problems/climbing-stairs/" rel="noopener noreferrer"&gt;Climbing Stairs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
