<?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>Forem: Dave Saunders</title>
    <description>The latest articles on Forem by Dave Saunders (@davejsaunders).</description>
    <link>https://forem.com/davejsaunders</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%2F19232%2Fdc0f4db8-078d-473f-a6ff-fa673439f895.jpg</url>
      <title>Forem: Dave Saunders</title>
      <link>https://forem.com/davejsaunders</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/davejsaunders"/>
    <language>en</language>
    <item>
      <title>What is the DRY principle?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Thu, 24 Mar 2022 10:25:59 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-the-dry-principle-26c9</link>
      <guid>https://forem.com/davejsaunders/what-is-the-dry-principle-26c9</guid>
      <description>&lt;p&gt;.. and why it is NOT about duplicating code!&lt;/p&gt;

&lt;p&gt;(originally sent to the &lt;a href="https://www.baseclass.io/newsletter"&gt;:BaseClass Newsletter&lt;/a&gt;)&lt;/p&gt;




&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;The '&lt;strong&gt;DRY&lt;/strong&gt;' Principle - or '&lt;strong&gt;D&lt;/strong&gt;on't &lt;strong&gt;R&lt;/strong&gt;epeat &lt;strong&gt;Y&lt;/strong&gt;ourself' is one of the most commonly quoted, but most commonly misunderstood pieces of guidance in programming.&lt;/p&gt;

&lt;p&gt;While the premise is simple, &lt;strong&gt;it can lead to over-abstraction and hard to maintain code when misinterpreted&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Don't Repeat Yourself
&lt;/h2&gt;

&lt;p&gt;The DRY principle was introduced in the book &lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;The Pragmatic Programmer&lt;/a&gt; in 1999.&lt;/p&gt;

&lt;p&gt;The original definition is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what does that mean?&lt;/p&gt;

&lt;p&gt;Let's start with what it &lt;em&gt;does not&lt;/em&gt; mean...&lt;/p&gt;




&lt;h3&gt;
  
  
  Misunderstanding the DRY principle
&lt;/h3&gt;

&lt;p&gt;DRY is commonly used as an argument against writing the same line of code twice.  &lt;/p&gt;

&lt;p&gt;That's understandable. If we have to copy/paste some code we've already used, we immediately want to move it into a common abstraction - it's in our nature as programmers!&lt;/p&gt;

&lt;p&gt;But writing the same line of code twice is not necessarily bad, and it is &lt;em&gt;not&lt;/em&gt; what DRY is talking about.&lt;/p&gt;

&lt;p&gt;Here's what one of the authors of the book that coined the term - Dave Thomas - said in a &lt;a href="https://changelog.com/podcast/352"&gt;podcast interview&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DRY has come to mean “Don’t cut and paste”, but the original “Don’t repeat yourself” was nothing to do with code, it was to do with knowledge.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  The perils of over-abstraction
&lt;/h3&gt;

&lt;p&gt;There are some valid reasons to write the same block of code twice; the code might do the same thing but for different reasons or in different contexts. &lt;/p&gt;

&lt;p&gt;Next time you're tempted to abstract two areas that look similar, ask yourself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do these sections of code have different reasons to change in future?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If so, abstraction might not be the right choice. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once you've abstracted that code, you can't change one area without affecting the other - they are now &lt;em&gt;coupled&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Maybe that's the right decision, and you might choose to abstract them anyway, but DRY doesn't insist that you should.&lt;/p&gt;




&lt;h3&gt;
  
  
  DRY is about 'knowledge'
&lt;/h3&gt;

&lt;p&gt;DRY is about ensuring that &lt;strong&gt;any change to the functionality of your code happens in &lt;em&gt;one place&lt;/em&gt; only&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We've all worked on code where a simple functional change needs far too many code changes.&lt;/p&gt;

&lt;p&gt;If I want to change the way we format a user's name, I have to change it on the profile page, the confirmation emails, the invoice generator, the administration dashboard... you get the idea.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This&lt;/em&gt; is what DRY is warning against; there should be one place to make this change. The &lt;em&gt;knowledge&lt;/em&gt; of how to format a user's name should be contained in &lt;br&gt;
just one area of your code.&lt;/p&gt;

&lt;p&gt;Next time I need to make that change, I know exactly where to go. Otherwise, it's only a matter of time before I forget one of the many areas I need to change and cause a bug.&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pragprog.com/titles/tpp20/the-pragmatic-programmer-20th-anniversary-edition/"&gt;'The Pragmatic Programmer' book. Required reading for developers!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://changelog.com/posts/why-do-so-many-developers-get-dry-wrong"&gt;A short blog post discussing why DRY is misunderstood&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://changelog.com/podcast/352"&gt;A podcast episode with the authors of the original book&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>What is Dynamic Programming?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Wed, 09 Mar 2022 15:57:09 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-dynamic-programming-3n0</link>
      <guid>https://forem.com/davejsaunders/what-is-dynamic-programming-3n0</guid>
      <description>&lt;p&gt;(If you like this, you'll also like the &lt;a href="https://www.baseclass.io/newsletter/" rel="noopener noreferrer"&gt;BaseClass newsletter&lt;/a&gt;!)&lt;/p&gt;




&lt;h2&gt;
  
  
  What is it, and why should I care?
&lt;/h2&gt;

&lt;p&gt;Dynamic programming is the process of breaking down a larger problem into smaller problems. &lt;/p&gt;

&lt;p&gt;By using the answers to those smaller problems, we can find the overall solution more efficiently.&lt;/p&gt;

&lt;p&gt;We'll also learn about the term 'memoization', and how it relates to dynamic programming.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;The usual example of dynamic programming is the Fibonacci Sequence. It's a good example, so we'll use it here too.&lt;/p&gt;

&lt;p&gt;If you're already familiar with the Fibonacci Sequence (and calculating it using recursion), then feel free to skip this next section. &lt;/p&gt;

&lt;p&gt;If you're not sure what that means, or you want a quick refresher, read on...&lt;/p&gt;

&lt;p&gt;This is a sequence of numbers, where each number is calculated by adding the previous two together:&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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffibonacci.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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffibonacci.png" alt="The Fibonacci Sequence: 1, 1, 2, 3, 5, 8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine I ask you to programmatically calculate the 6th number in the sequence (which is &lt;code&gt;8&lt;/code&gt;, as shown above).&lt;/p&gt;

&lt;p&gt;How would you calculate it?&lt;/p&gt;

&lt;p&gt;Here's some JavaScript code for a function that does just that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function f(n) {
    // The first and second values are always 1
    if (n == 1 || n == 2)
      return 1;

    // Calculate the previous two values in the sequence
    // using this same function
    return f(n-1) + f(n-2)
  }

  // Calculate the 6th value in the sequence
  let answer = f(6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work fine, but it's slow.&lt;/p&gt;

&lt;p&gt;You can see that the function calls itself; it is &lt;em&gt;recursive&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;To calculate the 6th Fibonacci number, we first need to calculate the 4th and 5th Fibonacci numbers.&lt;/p&gt;

&lt;p&gt;Each of &lt;em&gt;those&lt;/em&gt; then have to calculate &lt;em&gt;their&lt;/em&gt; previous numbers, and this repeats all the way down to the beginning of the sequence.&lt;/p&gt;

&lt;p&gt;Here's what that looks like if we draw it out as a graph.&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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffib6.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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffib6.png" alt="Graph of the tree of Fibonacci calls, showing lots of duplication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that there's a lot of duplication here. For example, the 2nd value in the sequence is calculated 5 times!&lt;/p&gt;

&lt;p&gt;For small numbers in the sequence, this isn't too bad, but it quickly gets worse. For later numbers in the sequence, this would soon become impractical, even on a modern computer.&lt;/p&gt;

&lt;p&gt;So how do we do better?&lt;/p&gt;




&lt;h2&gt;
  
  
  Dynamic programming and memoization
&lt;/h2&gt;

&lt;p&gt;One way we could improve this function is to store the results of our previous calculations as we go along.&lt;/p&gt;

&lt;p&gt;That way, we only need to calculate each number in the Fibonacci sequence once.&lt;/p&gt;

&lt;p&gt;This is the essence of dynamic programming:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Dynamic programming is breaking the &lt;em&gt;larger&lt;/em&gt; problem into &lt;em&gt;smaller&lt;/em&gt; problems, and using &lt;em&gt;those&lt;/em&gt; to get to the answer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because we're achieving this by storing the previous results for later, we're also using 'Memoization':&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Memoization is storing the result of a previous calculation so that we can use it later, to make the overall algorithm more efficient.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We could implement these concepts on the recursive approach above, but it's easier to follow if we use a 'bottom up' approach.&lt;/p&gt;

&lt;p&gt;Let's look at the code first, and then we can discuss why this is called a 'bottom up' algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function f(n) {
    // The first and second values are always 1
    if (n == 1 || n == 2)
      return 1

    let result = 0

    // Used to store the previous two results
    let lastButOneValue = 1
    let lastValue = 1

    // Work from item 3 of the sequence (items 1 and 2 are a 
    // special case, see above), calculate each value in turn
    for (let i = 3; i &amp;lt;= n; i++) {
      // Calculate this number by adding together the 
      // previous two
      result = lastValue + lastButOneValue

      // Update the values of the 
      // previous two items in the sequence
      lastButOneValue = lastValue
      lastValue = result
    }

    return result
  }

  // Calculate the 6th value in the sequence
  let answer = f(6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we calculate the Fibonacci sequence in order - all the way from the beginning - until we get to the number in the sequence that we need. &lt;/p&gt;

&lt;p&gt;As we go along, we store the results of the previous value, and the one before that. &lt;/p&gt;

&lt;p&gt;Getting the next value is then trivial.. just add those two together.&lt;/p&gt;

&lt;p&gt;Here's the graph from the original (inefficient) algorithm again, but we've highlighted only the calculations we're making in this new version:&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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffib6_top_down.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%2Fwww.baseclass.io%2Fnewsletter%2Fdynamic-programming%2Ffib6_top_down.png" alt="Same graph as before. this time showing only one branch highlighted and an arrow from bottom to top"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that this time, rather than starting from the answer and working backwards, we work &lt;em&gt;forwards&lt;/em&gt; until we reach the &lt;br&gt;
value we need.&lt;/p&gt;

&lt;p&gt;When we visualise it, we're working from the bottom of the graph upwards - hence a 'bottom up' approach.&lt;/p&gt;

&lt;p&gt;This algorithm is &lt;em&gt;much&lt;/em&gt; more efficient, there's no duplication at all!&lt;/p&gt;

&lt;p&gt;We learned some new terms here, so let's recap:&lt;/p&gt;




&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;Our latest algorithm uses &lt;strong&gt;&lt;em&gt;dynamic programming&lt;/em&gt;, because we're breaking the &lt;em&gt;bigger&lt;/em&gt; problem into &lt;em&gt;smaller&lt;/em&gt; problems, and using their results &lt;br&gt;
to get to the overall answer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It also uses &lt;strong&gt;&lt;em&gt;memoization&lt;/em&gt;, because we're storing the results of the previous step to avoid re-calculating them again later&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It was a &lt;strong&gt;'bottom up' approach, because when we visualize the problem as a graph, we're working from the base of the graph upwards&lt;/strong&gt; (rather than top-down, as in the less efficient algorithm).&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.blog/2022/01/31/the-complete-beginners-guide-to-dynamic-programming/" rel="noopener noreferrer"&gt;A very good Stack Overflow blog post on dynamic programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://web.mit.edu/15.053/www/AMP-Chapter-11.pdf" rel="noopener noreferrer"&gt;Dynamic programming in a book from MIT. Informative, but heavy going&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is TCP?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Wed, 23 Feb 2022 08:27:31 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-tcp-4nmj</link>
      <guid>https://forem.com/davejsaunders/what-is-tcp-4nmj</guid>
      <description>&lt;p&gt;(my &lt;a href="https://www.baseclass.io/newsletter/"&gt;newsletter&lt;/a&gt; subscribers received this first)&lt;/p&gt;

&lt;p&gt;TCP (Transmission Control Protocol) is a protocol for machines to communicate over a network, and is the foundation on which the internet is built.&lt;/p&gt;

&lt;p&gt;One of the most useful characteristics of TCP is that it is 'resilient', which means it can cope with an unreliable network without losing any data.&lt;/p&gt;

&lt;p&gt;This is possible because the machine &lt;em&gt;receiving&lt;/em&gt; the data 'acknowledges' that it has received it. If the &lt;em&gt;sender&lt;/em&gt; doesn't get this acknowledgement, it knows to re-send the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iR_0Url8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/ack.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iR_0Url8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/ack.png" alt="A server receiving, and acknowledging a cat picture" width="443" height="258"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;When we send data over a network, we normally group that data into 'packets':&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M_FiT9B8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/packets.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M_FiT9B8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/packets.png" alt="A client sending data to the server as 'packets'" width="453" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the beginning of each packet is the 'header'; a section of meta-data used to describe what is in the packet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JZd6rDe5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/header.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JZd6rDe5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/header.png" alt="A packet layout, showing the header followed by the data below" width="433" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The TCP header has a special block called the 'Sequence Number'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Sequence Number is used to keep track of how many bytes have been sent and received by the two machines talking to each other.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The machine &lt;em&gt;sending&lt;/em&gt; the data sets the Sequence Number to the number of bytes that have been sent &lt;em&gt;in total&lt;/em&gt; during this conversation. It then sends the next block of data.&lt;/li&gt;
&lt;li&gt;The machine &lt;em&gt;receiving&lt;/em&gt; 'acknowledges' receipt of the data by &lt;strong&gt;adding the number of bytes it has received to the Sequence Number, and passing it back to the sender&lt;/strong&gt;. It does this by setting a value in another part of the TCP header; the 'Acknowledgement Number'.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dlaXkCKJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/data_exchange.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dlaXkCKJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/data_exchange.png" alt="A diagram showing the server receiving data and replying by increasing the Sequence Number" width="447" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This simple mechanism allows us to detect when data has gone missing; &lt;strong&gt;if the &lt;em&gt;sender&lt;/em&gt; sends 20 bytes, but the &lt;em&gt;receiver&lt;/em&gt; only acknowledges 10, we know that some data has not been received&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are various mechanisms for dealing with this, but the simplest is for the &lt;em&gt;sender&lt;/em&gt; to simply re-send the data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Randomising the Sequence Number
&lt;/h2&gt;

&lt;p&gt;For security reasons, we don't want the Sequence Number to be predictable. If it was, it would be possible to 'hijack' the communication.&lt;/p&gt;

&lt;p&gt;So, even though the Sequence Numbers track how many bytes have been exchanged, they don't start from 0. Instead, both parties generate random starting values for their Sequence Number.&lt;/p&gt;

&lt;p&gt;But if these numbers are random, how do both clients agree on them?&lt;/p&gt;

&lt;p&gt;This is where the 'TCP Handshake' comes in...&lt;/p&gt;




&lt;h2&gt;
  
  
  The TCP Handshake
&lt;/h2&gt;

&lt;p&gt;When a connection is started, the first machine decides on its random 'Sequence Number' and sends it to the other party.&lt;/p&gt;

&lt;p&gt;This transmission is called the &lt;code&gt;SYN&lt;/code&gt;, because it is intended to '&lt;strong&gt;SYN&lt;/strong&gt;chronise' the Sequence Number. &lt;/p&gt;

&lt;p&gt;The other side knows this is a &lt;code&gt;SYN&lt;/code&gt; request, because we turn on a special bit in the packet header when sending it.&lt;/p&gt;

&lt;p&gt;In this example, we've chosen &lt;code&gt;1234&lt;/code&gt; as the random 'Sequence Number'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--egofdZIU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--egofdZIU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-1.png" alt="TCP Handshake part 1: The client sends a ransom Sequence Number" width="443" height="187"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The other party must now &lt;code&gt;ACK&lt;/code&gt; or '&lt;strong&gt;ACK&lt;/strong&gt;nowledge' the Sequence Number.&lt;/p&gt;

&lt;p&gt;It does this by adding &lt;code&gt;1&lt;/code&gt; to it, and passing it back in the 'Acknowledgement Number' section of the header (it also sets a special &lt;code&gt;ACK&lt;/code&gt; bit in the header).&lt;/p&gt;

&lt;p&gt;It now generates its own random Sequence Number and sends it back to the other machine.&lt;/p&gt;

&lt;p&gt;In this example, it generated &lt;code&gt;5678&lt;/code&gt; as its Sequence Number:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LkWm1-Dx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LkWm1-Dx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-2.png" alt="TCP Handshake part 2: The server replies by incrementing 1 to the Sequence Number and sending its own random number" width="443" height="257"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Finally, the original party acknowledges the other machine's Sequence Number, also by adding 1 to it and sending it back in the 'Acknowledgement Number' section of the header:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HUyWalqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HUyWalqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/tcp-handshake/handshake-3.png" alt="TCP Handshake part 3: The client acknowledges the server's Sequence Number" width="443" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Handshake is now complete, and the clients can start communicating.&lt;/p&gt;

&lt;p&gt;You might also see the TCP Handshake written as &lt;code&gt;SYN, SYN/ACK, ACK&lt;/code&gt;, referring to the order the handshake packets are sent in.&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.ibm.com/docs/en/zos-basic-skills?topic=layer-transmission-control-protocol-tcp#znetwork_45__tcpheader"&gt;An in-depth explanation of TCP by IBM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:the-internet/xcae6f4a7ff015e7d:transporting-packets/a/transmission-control-protocol--tcp"&gt;Another summary of TCP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://madpackets.com/2018/04/25/tcp-sequence-and-acknowledgement-numbers-explained/"&gt;TCP Sequence Numbers Explained&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Depth-First Search?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Sun, 20 Feb 2022 13:28:15 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-depth-first-search-2cko</link>
      <guid>https://forem.com/davejsaunders/what-is-depth-first-search-2cko</guid>
      <description>&lt;p&gt;(my &lt;a href="https://www.baseclass.io/newsletter"&gt;newsletter &lt;/a&gt;subscribers received this first)&lt;/p&gt;




&lt;p&gt;In the &lt;a href="https://www.baseclass.io/newsletter/breadth-first-search"&gt;last issue&lt;/a&gt;, we talked about 'Breadth-First Search' and 'Depth-First Search' as two of the most common algorithms we use when working with graphs.&lt;/p&gt;

&lt;p&gt;Last time, we looked at Breadth-First Search (BFS).&lt;/p&gt;

&lt;p&gt;Today, we'll cover the other, Depth-First Search (DFS).&lt;/p&gt;




&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;A lot of algorithms are implemented for you as part of your chosen language. That means that they are interesting to learn about, but that you'll &lt;br&gt;
rarely write them yourself.&lt;/p&gt;

&lt;p&gt;Graph traversal algorithms are different. &lt;/p&gt;

&lt;p&gt;We use graphs all the time, from linking related products in an e-commerce application to mapping relationships between people in a social network.&lt;/p&gt;

&lt;p&gt;Searching a graph is something that is not only useful in theory, but that you will almost certainly need to do in practice too.&lt;/p&gt;




&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;Here's a graph data structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O3VSi1hC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/graph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O3VSi1hC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/graph.png" alt="an image of a graph" width="320" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The 'nodes' in the graph (A-F) are called 'vertices'. Each vertex is connected to one or more others with 'edges', which are the lines between the nodes.&lt;/p&gt;

&lt;p&gt;But a graph is only useful if we can do something with it; we might want to find out whether a certain element is stored in our graph, or how many 'hops' it takes to get between two elements.&lt;/p&gt;

&lt;p&gt;These kinds of problems are called 'graph traversal', and 'Depth-First Search' (or 'DFS') is one algorithm to do this.&lt;/p&gt;

&lt;p&gt;Let's take a look...&lt;/p&gt;




&lt;h3&gt;
  
  
  How Depth-First Search works
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://www.baseclass.io/newsletter/linear-data-structures"&gt;this issue&lt;/a&gt;, we looked at the 'stack' data structure.&lt;/p&gt;

&lt;p&gt;You'll recall that it is a 'First In Last Out' data structure; &lt;strong&gt;the first item to be added to the stack will be the &lt;em&gt;last&lt;/em&gt; item to be removed&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3tmlBMgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/stack.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3tmlBMgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/stack.png" alt="an image of a queue" width="404" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The stack is the basis for Depth First Search.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a summary of the algorithm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pick any unvisited connected node, add it to the stack, and mark it as 'visited'&lt;/li&gt;
&lt;li&gt;From the node we just picked, do the same thing again. &lt;/li&gt;
&lt;li&gt;Repeat until we end up at a node with no unvisited connections&lt;/li&gt;
&lt;li&gt;Pop the top item from the stack, and repeat the whole process again from the next item down&lt;/li&gt;
&lt;li&gt;When the stack is empty, we're done!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This sounds much more confusing than it is in practice, so let's walk through an example...&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing DFS
&lt;/h3&gt;

&lt;p&gt;We start by picking a place to start, we'll choose &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The first step is to add &lt;code&gt;A&lt;/code&gt; to the stack and mark it as 'visited':&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nSfX103c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nSfX103c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-a.png" alt="A is in the stack" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we need to repeat the following steps:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Pick any unvisited connected node, add it to the stack, and mark it as 'visited'&lt;/li&gt;
&lt;li&gt;From the node we just picked, do the same thing again. &lt;/li&gt;
&lt;li&gt;Repeat until we end up at a node with no unvisited connections&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, let's pick a connected node and get started. &lt;/p&gt;

&lt;p&gt;Both &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; are connected to &lt;code&gt;A&lt;/code&gt;.  We haven't visited either, so we can pick either one to visit next. Let's pick &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Z2WVX3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Z2WVX3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-b.png" alt="B is in the stack" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From &lt;code&gt;B&lt;/code&gt;, we can either visit &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt; or &lt;code&gt;E&lt;/code&gt;. We've already visited &lt;code&gt;A&lt;/code&gt;, so we ignore that one. Let's pick &lt;code&gt;D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We add &lt;code&gt;D&lt;/code&gt; to the stack and mark it as 'visited', just like we did before:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--drh2eFrP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--drh2eFrP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-d.png" alt="D is in the stack" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From &lt;code&gt;D&lt;/code&gt; there are no unvisited nodes to visit. It's only connected to &lt;code&gt;B&lt;/code&gt;, and we've just been there.&lt;/p&gt;

&lt;p&gt;So, now it's time to do this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Pop the top item from the stack, and repeat the whole process again from the next item down&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll pop &lt;code&gt;D&lt;/code&gt; off of the stack and go back to the node underneath it.. which is &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rjBcM8ft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-d-at-b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rjBcM8ft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-d-at-b.png" alt="D is in the stack, currently at B" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we're back at &lt;code&gt;B&lt;/code&gt;, we'll just do the same thing all over again...&lt;/p&gt;

&lt;p&gt;There's one unvisited node - &lt;code&gt;E&lt;/code&gt; - so we'll visit it and add it to the stack:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bBB0J7Lv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bBB0J7Lv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-e.png" alt="E is in the stack" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;E&lt;/code&gt; has no unvisited neighbours. When we pop that off of the stack we'll see that neither does &lt;code&gt;B&lt;/code&gt;, so we pop that one off too:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bNbApLTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-a-second-time.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bNbApLTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-a-second-time.png" alt="Back at A again" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That has brought us right back up the top of the graph again, to &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I'm sure you have the hang of this by now; we'll keep picking an unvisited connected vertex and adding it to the stack.&lt;/p&gt;

&lt;p&gt;After adding &lt;code&gt;C&lt;/code&gt; and &lt;code&gt;F&lt;/code&gt; in that way, this is what the stack looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5usu7c1W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-c-f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5usu7c1W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-c-f.png" alt="F is in the stack" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;F&lt;/code&gt; has no unvisited connections, we pop it off of the stack. The same will apply to the next item down - &lt;code&gt;C&lt;/code&gt; - and then &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That leaves us an empty stack, meaning we're done!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xHY30hqr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-done.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xHY30hqr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/depth-first-search/stack-done.png" alt="Stack is empty, we're done" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Applications of DFS
&lt;/h3&gt;

&lt;p&gt;The DFS algorithm is useful when we are looking for an item that we know is likely to be at the bottom of the graph. Unlike Breadth-First search, DFS dives straight to the bottom of the graph, before working its way back up. &lt;/p&gt;

&lt;p&gt;Imagine we have a family tree, and we're looking for the youngest member. We know they're going to be at the bottom of the tree, so DFS might be a better choice in that case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In cases where the item you are searching for is likely to be at the top of the graph, consider 'Breadth-First search'. When the item is likely to be nearer to the bottom, consider 'Depth-First search'&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.finxter.com/graph-applications/"&gt;Applications of graphs in general (not specific to BFS)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm"&gt;Another description of DFS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is Breadth-First Search?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Thu, 03 Feb 2022 18:26:45 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-breadth-first-search-5c1d</link>
      <guid>https://forem.com/davejsaunders/what-is-breadth-first-search-5c1d</guid>
      <description>&lt;p&gt;(my &lt;a href="https://www.baseclass.io/newsletter/"&gt;newsletter&lt;/a&gt; subscribers received this first)&lt;/p&gt;

&lt;p&gt;Breadth-First Search and Depth-First Search are two of the most common algorithms we use when working with graphs.&lt;/p&gt;

&lt;p&gt;Here we'll look at the first of those, Breadth-First Search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;A lot of algorithms are implemented for you as part of your chosen language. That means that they are interesting to learn about, but that you'll &lt;br&gt;
rarely write them yourself.&lt;/p&gt;

&lt;p&gt;Graph traversal algorithms are different. &lt;/p&gt;

&lt;p&gt;We use graphs all the time, from linking related products in an e-commerce application to mapping relationships between people in a social network.&lt;/p&gt;

&lt;p&gt;Searching a graph is something that is not only useful in theory, but that you will almost certainly need to do in practice too.&lt;/p&gt;

&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;Here's a graph data structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oqk4eCWq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/graph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oqk4eCWq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/graph.png" alt="an image of a graph" width="320" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The 'nodes' in the graph (A-F) are called 'vertices'. Each vertex is connected to one or more others with 'edges', which are the lines between the nodes.&lt;/p&gt;

&lt;p&gt;But a graph is only useful if we can do something with it; we might want to find out whether a certain element is stored in our graph, &lt;br&gt;
or how many 'hops' it takes to get between two elements.&lt;/p&gt;

&lt;p&gt;These kinds of problems are called 'graph traversal', and 'Breadth-First Search' (or 'BFS') is one algorithm to do this.&lt;/p&gt;

&lt;p&gt;Let's take a look...&lt;/p&gt;

&lt;h3&gt;
  
  
  How Breadth-First Search works
&lt;/h3&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/davejsaunders/what-are-linear-data-structures-3o5i"&gt;last issue&lt;/a&gt;, we looked at the 'queue' data structure. &lt;/p&gt;

&lt;p&gt;You'll remember that it's a 'First In First Out' data structure; the first element to be added is the first element to be processed (or 'dequeued').&lt;br&gt;
If you're last in the queue, you'll be processed last:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PLUrl6Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/queue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PLUrl6Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/queue.png" alt="an image of a queue" width="475" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll use a queue to implement Breadth-First Search (BFS).&lt;/p&gt;

&lt;p&gt;This is the BFS algorithm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pull the next 'vertex' from the queue &lt;/li&gt;
&lt;li&gt;For each vertex connected to this one (that we haven't already visited) mark it as 'visited' and add it to the queue&lt;/li&gt;
&lt;li&gt;Repeat until the queue is empty&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By doing so, &lt;strong&gt;we're radiating outwards from our starting point&lt;/strong&gt;; visiting all of the nodes directly connected to the starting point first. Then, visiting all &lt;br&gt;
of the nodes connected to &lt;em&gt;those&lt;/em&gt;, etc.&lt;/p&gt;

&lt;p&gt;This will make more sense as we work through it...&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing BFS
&lt;/h3&gt;

&lt;p&gt;We start by picking a place to start, we'll choose &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, the first step is to add &lt;code&gt;A&lt;/code&gt; to the queue and mark it as 'visited':&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x2SgRGaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x2SgRGaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-a.png" alt="A is in the queue" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll remember that the BFS algorithm requires us to repeat the following steps until the queue is empty:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Pull the next 'vertex' from the queue &lt;/li&gt;
&lt;li&gt;For each vertex connected to this one (that we haven't already visited) mark it as 'visited' and add it to the queue&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;A&lt;/code&gt; is the first (and only) element in the queue. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;A&lt;/code&gt; is connected to both &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;. We haven't visited either of those, so we'll add them to the queue and mark them as visited:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JEezKUlQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-b-c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JEezKUlQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-b-c.png" alt="B and C are in the queue" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we repeat the same thing again.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;B&lt;/code&gt; is next in the queue.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;B&lt;/code&gt; is connected to &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt; and &lt;code&gt;E&lt;/code&gt;. We've already visited &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;, so we only queue up &lt;code&gt;D&lt;/code&gt; and &lt;code&gt;E&lt;/code&gt; (and mark them as visited):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VaWkHT6n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-e-d-c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VaWkHT6n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-e-d-c.png" alt="E, D and C are in the queue" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can probably see where this is going...&lt;/p&gt;

&lt;p&gt;Next, we dequeue &lt;code&gt;C&lt;/code&gt;. It is connected to &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;F&lt;/code&gt;. The only one we haven't already visited is &lt;code&gt;F&lt;/code&gt;, so we add that to the queue and mark it as 'visited':&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---74GWXBy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-f-e-d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---74GWXBy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/queue-f-e-d.png" alt="F, E and D are in the queue" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now have &lt;code&gt;F&lt;/code&gt;, &lt;code&gt;E&lt;/code&gt; and &lt;code&gt;D&lt;/code&gt; left in the queue...&lt;/p&gt;

&lt;p&gt;We'll dequeue each of them in turn, looking for any connected vertices that we haven't visited yet - but we won't find any.&lt;/p&gt;

&lt;p&gt;After we've checked each of those, the queue is empty and we're done - we've visited every node:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0K_9aPeQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/end-state.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0K_9aPeQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/breadth-first-search/end-state.png" alt="At the end, the queue is empty and we're done!" width="500" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Applications of BFS
&lt;/h3&gt;

&lt;p&gt;This is all very well, but what would we use it for?&lt;/p&gt;

&lt;p&gt;Suppose we're building a social network like LinkedIn.&lt;/p&gt;

&lt;p&gt;The 'graph' in this case would be the map of all connections between people.&lt;/p&gt;

&lt;p&gt;If we wanted to find out whether 'Bob' knew 'Jennie' through one or more mutual friends, BFS would be a great choice.&lt;/p&gt;

&lt;p&gt;We'd start at Bob, and radiate outwards until we found Jennie (or until we were far enough away from Bob that we've given up).&lt;/p&gt;

&lt;p&gt;Or, the graph could be a map of a subway, and the 'how many mutual friends between Bob and Jennie' problem could instead be &lt;br&gt;
'how many changes does it take to get between two stations'.&lt;/p&gt;

&lt;p&gt;Once you learn about graphs and algorithms like Breadth-First Search, a lot of problems start to look like these.&lt;/p&gt;

&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.finxter.com/graph-applications/"&gt;Applications of graphs in general (not specific to BFS)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_traversal.htm"&gt;Another description of BFS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.khanacademy.org/computing/computer-science/algorithms/breadth-first-search/a/analysis-of-breadth-first-search"&gt;Exploring the time complexity of BFS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What are Linear Data Structures?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Wed, 02 Jun 2021 19:58:14 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-are-linear-data-structures-3o5i</link>
      <guid>https://forem.com/davejsaunders/what-are-linear-data-structures-3o5i</guid>
      <description>&lt;p&gt;(my &lt;a href="https://www.baseclass.io/newsletter/"&gt;newsletter&lt;/a&gt; subscribers received this first)&lt;/p&gt;

&lt;p&gt;We say a data structure is 'linear' if the items inside it are stored in a sequence. &lt;/p&gt;

&lt;p&gt;Arrays, linked lists, and stacks are all linear data structures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;We use these data structures every day in programming. &lt;/p&gt;

&lt;p&gt;Even if you're already familiar with them, it's helpful to recap them occasionally.&lt;/p&gt;




&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;As we said in the introduction, a data structure is 'linear' if the elements form a sequence. &lt;/p&gt;

&lt;p&gt;That means that the data structure has a first and last element, and each element is connected to its previous and next element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An 'array' &lt;em&gt;is&lt;/em&gt; a linear data structure; the items are stores sequentially.&lt;/li&gt;
&lt;li&gt;A 'graph' is &lt;em&gt;not&lt;/em&gt; a linear data structure; any node can be linked to any other node in the graph - there is no fixed 'sequence'.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(if you're not familiar with graphs, don't worry - there's a newsletter coming up that explores them in detail).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xMJcGx4U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/linear-non-linear.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xMJcGx4U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/linear-non-linear.png" alt="linear data structures represented as a series of boxes. Non-linear structures look like a 'graph'"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take a look at some common linear data structures...&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;p&gt;If you've done any programming, you're almost certainly familiar with the concept of arrays.&lt;/p&gt;

&lt;p&gt;An array is like a bookcase; the items are stored next to each other, but we can jump to any item we like to read it.&lt;/p&gt;

&lt;p&gt;The items in the array have an 'index' that allows us to reference them directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h5k1xmrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h5k1xmrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/array.png" alt="image on an array with some elements in"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ability to jump to any item we like to read its value is called 'random access', and is a huge advantage of an array.&lt;/p&gt;

&lt;p&gt;We take this for granted, but this is not a property that many other 'linear data structures' have, as you'll see below.&lt;/p&gt;

&lt;p&gt;When we allocate an array, we have to determine up-front how much space we need.&lt;/p&gt;

&lt;p&gt;If we fill our array, we have to stop and allocate some more space. That means that while normal inserts into the array are very fast, &lt;br&gt;
occasionally we have to pause for a short time to make the array bigger - which takes some time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linked Lists
&lt;/h3&gt;

&lt;p&gt;A linked list is a data structure where each item points to the next. &lt;strong&gt;We can't jump to any element directly like we can with &lt;br&gt;
an array&lt;/strong&gt;. Instead, we have to access them in turn:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iABT8Zcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/linked-list.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iABT8Zcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/linked-list.png" alt="an image of a linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Linked lists are useful because, unlike an array, we don't need to decide up-front how much space we need. If we need to add a new item, &lt;br&gt;
we simply add it to the end. &lt;/p&gt;

&lt;p&gt;That means that adding the 200th item has the same cost as adding the 2nd item. This predictable performance is an advantage of a linked list.&lt;/p&gt;

&lt;p&gt;It's also easy to add or remove an item from the middle of the linked list, by simply changing some 'next' pointers.&lt;/p&gt;

&lt;p&gt;Here's how we'd remove an item from a linked list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zf2SniCW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/remove-from-linked-list.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zf2SniCW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/remove-from-linked-list.png" alt="removing an item from a linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This would be more difficult to do in an array, as we'd have to shift all of the remaining items to account for the new or removed item.&lt;/p&gt;

&lt;p&gt;A variant of the linked list is the 'doubly-linked list', where each element not only points to the &lt;em&gt;next&lt;/em&gt; element, but also the &lt;br&gt;
&lt;em&gt;previous&lt;/em&gt; one. &lt;/p&gt;

&lt;p&gt;This means we can traverse the data structure in either order, but still have the advantages of a linked list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H5kZXTi7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/doubly-linked-list.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H5kZXTi7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/doubly-linked-list.png" alt="an image of a doubly-linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Queues
&lt;/h3&gt;

&lt;p&gt;The queue is a 'First In First Out' (FIFO) data structure. That means that items are read in the same order that they are inserted. &lt;/p&gt;

&lt;p&gt;This is just like the queue at the store, the first person to join the queue&lt;br&gt;
is the first person to be served.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PLUrl6Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/queue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PLUrl6Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/queue.png" alt="an image of a queue"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A printer queue is a good example of this data structure in use. The printer will print items in the order in which they were queued. If you &lt;br&gt;
send your document to the printer last, it will be the last thing to be printed.&lt;/p&gt;

&lt;p&gt;Queues are also useful as 'buffers'. &lt;/p&gt;

&lt;p&gt;Suppose we have two separate systems, one that reads messages and one that processes them. We don't want the system &lt;br&gt;
that &lt;em&gt;reads&lt;/em&gt; messages to have to wait for each message to be &lt;em&gt;processed&lt;/em&gt; before listening for another. &lt;/p&gt;

&lt;p&gt;Putting a queue between them allows us to &lt;br&gt;
'de-couple' those systems. The &lt;em&gt;read&lt;/em&gt; process can keep adding items to the queue, safe in the knowledge the the &lt;em&gt;processing&lt;/em&gt; side will pull the items &lt;br&gt;
off of the queue and process them eventually - no waiting required.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stacks
&lt;/h3&gt;

&lt;p&gt;The stack is a 'Last In First Out' data structure; the &lt;em&gt;last&lt;/em&gt; item to be added is the &lt;em&gt;first&lt;/em&gt; item to be read.&lt;/p&gt;

&lt;p&gt;You can think of this like a stack of plates, the last plate to be added to the pile is the first plate we'd take off again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3tmlBMgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/stack.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3tmlBMgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/newsletter/linear-data-structures/stack.png" alt="an image of a stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might use a stack to implement 'undo' functionality, for example. The &lt;em&gt;last&lt;/em&gt; task the user performed is the &lt;em&gt;first&lt;/em&gt; one to be reversed when they click the 'undo' button.&lt;/p&gt;

&lt;p&gt;The 'call stack' we see every day when running code is also a great example of a stack, but that's a topic for a future article!&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/List_of_data_structures#Linear_data_structures"&gt;A list of linear data structures from Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.faceprep.in/data-structures/types-of-queue-data-structure/"&gt;A more in-depth article on queues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javascripttutorial.net/javascript-call-stack/"&gt;An introduction to the call stack (this article is specifically about JavaScript, but is applicable to 
most languages)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>What is Jaro-Winkler Similarity?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Fri, 14 May 2021 12:20:30 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-jaro-winkler-similarity-pdp</link>
      <guid>https://forem.com/davejsaunders/what-is-jaro-winkler-similarity-pdp</guid>
      <description>&lt;p&gt;Jaro-Winkler similarity is a way of measuring how similar two strings are. It is fairly easy to understand and quick &lt;br&gt;
to implement.&lt;/p&gt;

&lt;p&gt;(this was originally sent to my &lt;a href="https://www.baseclass.io/newsletter" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt; subscribers)&lt;/p&gt;




&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;String similarity metrics have various uses; from user-facing search functionality to spell checkers.&lt;/p&gt;

&lt;p&gt;There are a few common string similarity metrics. Knowing a little about each will help you to choose the right one, &lt;br&gt;
should you ever need to implement something like this yourself.&lt;/p&gt;

&lt;p&gt;Jaro Similarity, and the modified version - Jaro-Winkler - are two common ones.&lt;/p&gt;




&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;Imagine we're building the search functionality for an app store. &lt;/p&gt;

&lt;p&gt;If a user misspells their search, we'd like to be able to suggest the app we &lt;em&gt;think&lt;/em&gt; they were looking for.&lt;/p&gt;

&lt;p&gt;For example; the user is searching for the 2009 viral hit &lt;code&gt;farmville&lt;/code&gt;, but badly mistypes it as:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fsearch.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fsearch.png" alt="'faremviel' in search box"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we could compare this search string to all of the titles in our app store, we could show the user the apps that &lt;br&gt;
most closely match what they typed.&lt;/p&gt;

&lt;p&gt;This is where the Jaro Similarity metric comes in...&lt;/p&gt;




&lt;h3&gt;
  
  
  Jaro Similarity
&lt;/h3&gt;

&lt;p&gt;Let's calculate the similarity between the user's search string and the correct app title:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fboth_strings.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fboth_strings.png" alt="farmville vs farmville"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Created by Matthew A. Jaro in 1989, the Jaro Similarity metric compares two strings and gives us a score that &lt;br&gt;
represents how similar they are.&lt;/p&gt;

&lt;p&gt;The result is a number between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;, where &lt;code&gt;0&lt;/code&gt; means the strings are completely different and &lt;code&gt;1&lt;/code&gt; means they match exactly.&lt;/p&gt;

&lt;p&gt;The first step to calculating the Jaro similarity is to &lt;strong&gt;count the characters that match between the two strings&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But, &lt;strong&gt;to be considered a 'match', the characters do not need to be in the same place in both strings - &lt;br&gt;
they just need to be &lt;em&gt;near&lt;/em&gt; to each other&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This accounts for the common typing mistake where you accidentally enter some characters in the wrong order.&lt;/p&gt;

&lt;p&gt;How near those characters need to be before we consider them a match is calculated as follows:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fwindow.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fwindow.png" alt="(length of longest string/2)-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both of our strings are &lt;code&gt;9&lt;/code&gt; characters long. That gives us a result of &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That means that any two characters in our strings 'match' if they are either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the same place in both strings&lt;/li&gt;
&lt;li&gt;No further than &lt;code&gt;3&lt;/code&gt; characters away from each other&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's what it looks like if we draw these matches:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fmatches.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fmatches.png" alt="matching characters"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there were no matches, we wouldn't need to go any further - the Jaro Similarity would simply be &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We have &lt;code&gt;8&lt;/code&gt; matching characters though, so the next step is to calculate the number of 'transpositions'.&lt;/p&gt;

&lt;p&gt;Transpositions are  &lt;strong&gt;the characters that match, but are in the wrong order&lt;/strong&gt;. We count them, and then we &lt;strong&gt;half that number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our strings have &lt;code&gt;2&lt;/code&gt; matching characters that are in a different order (the final &lt;code&gt;e&lt;/code&gt; and &lt;code&gt;l&lt;/code&gt; are backwards in the user's search term). &lt;br&gt;
Halving this gives us  &lt;code&gt;1&lt;/code&gt; 'transposition'.&lt;/p&gt;

&lt;p&gt;Now all we have to do is plug these numbers into the following formula &lt;br&gt;
(we use the term &lt;code&gt;simj&lt;/code&gt; to mean 'Jaro Similarity' - the thing we're calculating):&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fjaro.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fjaro.png" alt="jaro formula"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This looks complex, but we really only need a few values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;|S1|&lt;/code&gt; and &lt;code&gt;|S2|&lt;/code&gt; are the lengths of the two strings we are comparing (ours are both &lt;code&gt;9&lt;/code&gt; characters long)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; is the number of matches - we have &lt;code&gt;8&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;t&lt;/code&gt; is the number of 'transpositions' - we have &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given those values, this is the Jaro Similarity for &lt;code&gt;faremviel&lt;/code&gt; vs &lt;code&gt;farmville&lt;/code&gt;:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Four_result.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Four_result.png" alt="jaro formula with our values = 0.88"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our strings have a similarity of &lt;code&gt;0.88&lt;/code&gt;, which means that they are very similar. &lt;/p&gt;

&lt;p&gt;If we calculate the Jaro Similarity of the user's search term against other games in our app store, &lt;br&gt;
it becomes clear what the user was intending to search for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'faremviel' vs 'farmville': &lt;code&gt;0.88&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;'faremviel' vs 'farmville 2': &lt;code&gt;0.83&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;'faremviel' vs 'clash of clans': &lt;code&gt;0.46&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;'faremviel' vs 'minecraft': &lt;code&gt;0.31&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Jaro-Winkler Similarity
&lt;/h3&gt;

&lt;p&gt;This modification of Jaro Similarity was proposed in 1990 by William E. Winkler.&lt;/p&gt;

&lt;p&gt;The 'Jaro-Winkler' metric takes the Jaro Similarity above, and increases the score if the characters at the start of both strings are the same.&lt;/p&gt;

&lt;p&gt;In other words, &lt;strong&gt;Jaro-Winkler favours two strings that have the same beginning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the formula for the 'Jaro-Winkler Similarity':&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fjaro_winkler.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Fjaro_winkler.png" alt="jaro winkler formula"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need the following values to use it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;simj&lt;/code&gt; is the Jaro Similarity of our comparison above (&lt;code&gt;0.88&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;l&lt;/code&gt; is the number of characters that are the same at the start of both strings (up to a maximum of 4). 
Our strings both start with &lt;code&gt;f&lt;/code&gt; &lt;code&gt;a&lt;/code&gt; &lt;code&gt;r&lt;/code&gt;, so we use a value of &lt;code&gt;3&lt;/code&gt; for this.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p&lt;/code&gt; is the 'scaling factor'. &lt;code&gt;0.1&lt;/code&gt; is usually used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the Jaro-Winkler calculation for &lt;code&gt;faremviel&lt;/code&gt; vs &lt;code&gt;farmville&lt;/code&gt;:&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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Four_result_jaro_winkler.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%2Fwww.baseclass.io%2Fnewsletter%2Fjaro-winkler%2Four_result_jaro_winkler.png" alt="our jaro winkler calculation = 0.92"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two strings with &lt;em&gt;no&lt;/em&gt; matching characters at the start would keep the same score, but because our &lt;br&gt;
strings have letters in common at the beginning, this version of the metric has boosted our score &lt;br&gt;
from &lt;code&gt;0.88&lt;/code&gt; up to &lt;code&gt;0.92&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whether Jaro or Jaro-Winkler is the right choice depends on your specific use case. Try both (and other string similarity algorithms), and see what &lt;br&gt;
works best for your data.&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://rosettacode.org/wiki/Jaro_distance" rel="noopener noreferrer"&gt;Examples of these two metrics implemented in common programming languages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@appaloosastore/string-similarity-algorithms-compared-3f7b4d12f0ff" rel="noopener noreferrer"&gt;An interesting Medium post comparing string similarity algorithms for app store search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://asecuritysite.com/forensics/simstring" rel="noopener noreferrer"&gt;An online strings similarity comparison, using various metrics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>What Are Floating-point Numbers?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Fri, 30 Apr 2021 18:51:22 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-are-floating-point-numbers-4oc3</link>
      <guid>https://forem.com/davejsaunders/what-are-floating-point-numbers-4oc3</guid>
      <description>&lt;p&gt;(from last week's &lt;a href="https://www.baseclass.io/newsletter" rel="noopener noreferrer"&gt;BaseClass newsletter&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Floating-point is a way of storing numbers in binary. It allows us to store a very large range &lt;br&gt;
of values using a fixed amount of space.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;Have you ever wanted to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why &lt;code&gt;0.1&lt;/code&gt; + &lt;code&gt;0.3&lt;/code&gt; does not always equal &lt;code&gt;0.4&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;How we store non-integer numbers in binary?&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;You may be familiar with how we represent a number in binary.&lt;/p&gt;

&lt;p&gt;Each bit represents a power of 2, and by combining them we can produce every whole number:&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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Fbinary.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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Fbinary.png" alt="rounding error screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what happens when we need to represent something that isn't an integer, like &lt;code&gt;2.5&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Let's split the 8 bits in half.&lt;/p&gt;

&lt;p&gt;We'll use the 'left' half to represent the number before the decimal point (&lt;code&gt;2&lt;/code&gt; in our example), &lt;br&gt;
and the 'right' half to represent the fraction &lt;em&gt;after&lt;/em&gt; the decimal point (&lt;code&gt;0.5&lt;/code&gt;, or ½ in this case).&lt;/p&gt;

&lt;p&gt;In this system, &lt;code&gt;2.5&lt;/code&gt; would be represented as &lt;code&gt;0 0 1 0 1 0 0 0&lt;/code&gt;:&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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2F2_5_binary.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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2F2_5_binary.png" alt="2.5 in our fixed point system is 00101000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can represent fractions now, but we've lost a lot of range. &lt;/p&gt;

&lt;p&gt;We can't represent &lt;code&gt;16.0&lt;/code&gt; in this format, for example. There just aren't enough bits on the left of the point.&lt;/p&gt;

&lt;p&gt;We could just keep adding more bits to store larger numbers, but this format is still quite limited.&lt;/p&gt;

&lt;p&gt;Sometimes we want to store very large numbers, in which case we'd like more bits on the left. Other times, we'd &lt;br&gt;
like to store very small fractions, in which case we would need fewer bits on the left, and more precision on the right,&lt;/p&gt;

&lt;p&gt;This is what floating-point is; &lt;strong&gt;a way of storing numbers that allows the point to move to represent a larger &lt;br&gt;
range of values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The standard for float values is called 'IEEE 754', which defines both 32 and 64-bit floating-point (or 'float') values.&lt;/p&gt;

&lt;p&gt;Each 32 or 64-bit float is split into 3 sections.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first bit represents the 'sign'; &lt;code&gt;0&lt;/code&gt; for a positive number, or &lt;code&gt;1&lt;/code&gt; for a negative number&lt;/li&gt;
&lt;li&gt;The next 8 bits are called the 'Exponent'&lt;/li&gt;
&lt;li&gt;The final 23 bits are the 'Mantissa'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use these 3 values in a formula, which gives us the number that the float represents:&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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Flayout.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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Flayout.png" alt="diagram of the float"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You don't need to understand this formula, just know that this way of storing &lt;br&gt;
numbers means we can represent a much larger range. &lt;/p&gt;

&lt;p&gt;By making the 'exponent' value larger or smaller we can represent very small fractions, or very &lt;br&gt;
large numbers.&lt;/p&gt;

&lt;p&gt;That's why it's called 'floating-point', it doesn't have a fixed point like our original example. Instead, &lt;strong&gt;the &lt;br&gt;
point 'floats', or moves, depending on the size of the 'exponent'&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  Rounding errors
&lt;/h3&gt;

&lt;p&gt;There are some numbers we can't represent exactly using our standard decimal system.&lt;/p&gt;

&lt;p&gt;For example, we can't represent ⅓ &lt;em&gt;exactly&lt;/em&gt; in decimal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;⅓ = 0.3333333...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is also true of some numbers in binary; they cannot be represented exactly.&lt;/p&gt;

&lt;p&gt;For example, let's try to represent &lt;code&gt;0.1&lt;/code&gt; in binary.&lt;/p&gt;

&lt;p&gt;Again, the values after the decimal point represent the fractions:&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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Fpoint_one.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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Fpoint_one.png" alt="01 in floating-point"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is &lt;em&gt;very close&lt;/em&gt; to &lt;code&gt;0.1&lt;/code&gt; - and it's the best we can do in this example - but it is not &lt;em&gt;exactly&lt;/em&gt; &lt;code&gt;0.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fact that our point can 'move' means that we can add more precision to this fraction if we like, &lt;br&gt;
but the fact remains that we just cannot represent some numbers &lt;em&gt;exactly&lt;/em&gt; in binary.&lt;/p&gt;

&lt;p&gt;This can lead to some interesting rounding errors.&lt;/p&gt;

&lt;p&gt;If you try to calculate &lt;code&gt;0.2&lt;/code&gt; + &lt;code&gt;0.1&lt;/code&gt; in JavaScript, you'll get an answer of &lt;code&gt;0.30000000000000004&lt;/code&gt;:&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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Frounding-error-screenshot.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%2Fwww.baseclass.io%2Fnewsletter%2Ffloating-point-numbers%2Frounding-error-screenshot.png" alt="rounding error screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is &lt;em&gt;very close&lt;/em&gt; to the correct answer, but it's not &lt;em&gt;exactly&lt;/em&gt; the correct answer.&lt;/p&gt;

&lt;p&gt;With this in mind, we usually &lt;strong&gt;compare floating-point numbers by checking the difference &lt;br&gt;
between them&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For example, instead of &lt;br&gt;
checking whether two numbers are &lt;em&gt;equal&lt;/em&gt;, we would check that the difference between them is very small.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.abs(result1 - result2) &amp;lt; 0.001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simplified example, of course. In reality, you would choose a tolerance appropriate to the calculation you are doing.&lt;/p&gt;




&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://floating-point-gui.de/formats/fp/" rel="noopener noreferrer"&gt;The Floating Point Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bartaz.github.io/ieee754-visualization/" rel="noopener noreferrer"&gt;See how any number would be represented as a float in binary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://web.archive.org/web/20180419150129/http://grouper.ieee.org/groups/754/" rel="noopener noreferrer"&gt;IEEE 754 Standard for Binary Floating-Point Arithmetic&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What is CAP Theorem?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Fri, 16 Apr 2021 08:14:38 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-cap-theorem-3b8p</link>
      <guid>https://forem.com/davejsaunders/what-is-cap-theorem-3b8p</guid>
      <description>&lt;p&gt;CAP Theorem describes the decisions we have to make when building a distributed data store. &lt;/p&gt;

&lt;p&gt;Let's take a look at CAP Theorem, in under 5 minutes...&lt;/p&gt;

&lt;p&gt;(my &lt;a href="https://www.baseclass.io/newsletter/cap-theorem?utm_source=devto&amp;amp;utm_medium=top-link&amp;amp;utm_campaign=cap-theorem" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt; subscribers received this first)&lt;/p&gt;




&lt;h2&gt;
  
  
  Why should I care?
&lt;/h2&gt;

&lt;p&gt;Have you ever wanted to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How distributed databases handle network failures?&lt;/li&gt;
&lt;li&gt;What tradeoffs we must make when designing distributed data stores?&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  In 5 minutes or less
&lt;/h2&gt;

&lt;p&gt;Eric Brewer's &lt;strong&gt;CAP Theorem&lt;/strong&gt; tells us that a distributed data store must choose no more than two of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;onsistency&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;vailability &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;P&lt;/strong&gt;artition Tolerance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What do those definitions mean, and why can't we have all three?&lt;/p&gt;

&lt;p&gt;Let's imagine we're designing a distributed database. &lt;/p&gt;

&lt;p&gt;For simplicity our database will have just two interconnected instances, or 'nodes':&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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fconnected.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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fconnected.png" alt="Node returning no when disconnected"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Partition Tolerance
&lt;/h3&gt;

&lt;p&gt;Partition Tolerance means that one or more nodes in our distributed system can be split up and unable to communicate with each other (&lt;em&gt;partitioned&lt;/em&gt;), and the system can still function.&lt;/p&gt;

&lt;p&gt;Only a complete network failure is allowed to cause the system to respond incorrectly, anything else must be tolerated.&lt;/p&gt;

&lt;p&gt;If we have a single node then there can be no partitions. But, in a distributed system, faults are inevitable given enough time. Therefore &lt;strong&gt;we cannot sacrifice  Partition Tolerance&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;With that in mind, we can actually re-state the problem like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the event of a Partition, should we choose either Consistency or Availability?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine there's a fault in our system, and the connection to one of the nodes is broken. Our distributed database is now &lt;em&gt;partitioned&lt;/em&gt;. &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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fdisconnected.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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fdisconnected.png" alt="Node returning no when disconnected"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Somebody tries to update a record on one of the nodes.. What do we do now?&lt;/p&gt;




&lt;h3&gt;
  
  
  Choosing Consistency
&lt;/h3&gt;

&lt;p&gt;'Consistency' in CAP Theorem means that I can update a record on one node, and somebody reading from another node will &lt;em&gt;immediately&lt;/em&gt; see the &lt;br&gt;
effect of my update.&lt;/p&gt;

&lt;p&gt;(Note: this is specifically called 'linearizable consistency').&lt;/p&gt;

&lt;p&gt;Obviously, &lt;em&gt;instant&lt;/em&gt; communication isn't realistic in a distributed environment, so in practice, the goal is to reduce this to a level where we don't notice it.&lt;/p&gt;

&lt;p&gt;This is a really useful property if you are a banking system. It would be a big problem if I could withdraw money from one ATM, then walk down the road to another ATM and withdraw the same amount again, because the database was not consistent across all nodes.&lt;/p&gt;

&lt;p&gt;The nodes in our example database can no longer communicate, so we cannot reflect a change across all nodes 'instantly'. &lt;/p&gt;

&lt;p&gt;So, if we want to make sure both nodes stay 'consistent', there are a few ways to handle this.  We could shut down entirely, or refuse all updates and only allow reads, for example.&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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fnope.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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Fnope.png" alt="Node returning no when disconnected"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because we can't accept the update request though, we are sacrificing availability...&lt;/p&gt;




&lt;h3&gt;
  
  
  Choosing Availability
&lt;/h3&gt;

&lt;p&gt;'Availability' means that if we make a request to any working node, we &lt;em&gt;must&lt;/em&gt; get a non-error response, regardless of any partitions in the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The data is allowed to be out of date, or 'stale', but it must be available.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Twitter is a good example of a system where we might choose &lt;strong&gt;A&lt;/strong&gt;vailability. If I 'like' a tweet, but the other nodes don't reflect that 'like' immediately, it's really not the end of the world.&lt;/p&gt;

&lt;p&gt;In this case, it's more important for the system to be 'available' and accept the update than to be 'consistent'.&lt;/p&gt;

&lt;p&gt;In our example scenario, we could continue to allow clients to read and write to nodes on &lt;em&gt;both sides of the partition&lt;/em&gt;:&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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Flikes.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%2Fwww.baseclass.io%2F%2Fnewsletter%2Fcap-theorem%2Flikes.png" alt="Node returning no when disconnected"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That would give us a system that is &lt;strong&gt;A&lt;/strong&gt;vailable, but because there's no way for those nodes to keep their information in sync while partitioned, it's impossible for this to be (immediately) &lt;strong&gt;C&lt;/strong&gt;onsistent too.&lt;/p&gt;

&lt;p&gt;This problem is the root of CAP Theorem. &lt;strong&gt;We cannot have both Consistency and Availability in the event of a partition (a loss of communication between nodes).. we must choose between the two&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Want to know more?
&lt;/h2&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://codahale.com/you-cant-sacrifice-partition-tolerance/" rel="noopener noreferrer"&gt;You Can’t Sacrifice Partition Tolerance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed/" rel="noopener noreferrer"&gt;CAP Twelve Years Later: How the "Rules" Have Changed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martin.kleppmann.com/2015/05/11/please-stop-calling-databases-cp-or-ap.html" rel="noopener noreferrer"&gt;Please stop calling databases CP or AP&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>100daysofcode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Comparing strings in JavaScript</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Tue, 13 Apr 2021 04:39:46 +0000</pubDate>
      <link>https://forem.com/davejsaunders/comparing-strings-in-javascript-4bj0</link>
      <guid>https://forem.com/davejsaunders/comparing-strings-in-javascript-4bj0</guid>
      <description>&lt;p&gt;This is extracted from the much larger post: &lt;a href="https://www.baseclass.io/guides/string-handling-modern-js"&gt;The Complete Guide to Working With Strings in Modern JavaScript&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Equality
&lt;/h1&gt;

&lt;p&gt;When you know you are comparing two string primitives, you can use either the &lt;code&gt;==&lt;/code&gt; or &lt;code&gt;===&lt;/code&gt; operators:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"abba" === "abba" // true
"abba" == "abba" // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you are comparing a string primitive to something that is &lt;em&gt;not&lt;/em&gt; a string, &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;===&lt;/code&gt; behave differently.&lt;/p&gt;

&lt;p&gt;When using the &lt;code&gt;==&lt;/code&gt; operator, the non-string will be coerced to a string. That means that JavaScript will try and make &lt;br&gt;
it a string before comparing the values.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9 == "9"
// becomes 
"9" == "9" //true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For a strict comparison, where non-strings are not coerced to strings, use &lt;code&gt;===&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9 === "9" // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The same is true of the not-equal operators, &lt;code&gt;!=&lt;/code&gt; and &lt;code&gt;!==&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9 != "9" // false
9 !== "9" // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you're not sure what to use, prefer strict equality using &lt;code&gt;====&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When using &lt;strong&gt;String&lt;/strong&gt; objects, two objects with the same value are &lt;em&gt;not&lt;/em&gt; considered equal:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   new String("js") == new String("js") // false
   new String("js") === new String("js") // false
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Case sensitivity
&lt;/h1&gt;

&lt;p&gt;For case-insensitive comparisons, do not convert both strings to upper or lowercase and then compare them, as this is unreliable with some characters.&lt;/p&gt;

&lt;p&gt;Instead use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare"&gt;localeCompare&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 'Résumé';
let b = 'RESUME';

// Incorrect: returns 'false'
a.toLowerCase() === b.toLowerCase()

// Correct: returns '1'
a.localeCompare(b, undefined, { sensitivity: 'accent' })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;localeCompare is supported in the following browsers&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oJ3CyOaQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/guides/string-handling-modern-js/localeCompare_browser_support.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oJ3CyOaQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/guides/string-handling-modern-js/localeCompare_browser_support.png" alt="localeCompare browser support: Chrome 24+, Edge 12+, FireFox 29+, Safari 10+, Opera 15+"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Greater/less than
&lt;/h1&gt;

&lt;p&gt;When comparing strings using the &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; operators, JavaScript will compare each character in 'lexicographical order'.&lt;/p&gt;

&lt;p&gt;That means that they are compared letter by letter, in the order they would appear in a dictionary:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"aardvark" &amp;lt; "animal" // true
"gamma" &amp;gt; "zulu" // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;When comparing strings using &lt;code&gt;&amp;lt;&lt;/code&gt; &lt;code&gt;&amp;gt;&lt;/code&gt;, lowercase letters are considered larger than uppercase.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"aardvark" &amp;gt; "Animal" // true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is because JavaScript is actually using each character's value in Unicode, where lowercase letters  are &lt;em&gt;after&lt;/em&gt; uppercase letters.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  True or false strings
&lt;/h1&gt;

&lt;p&gt;Empty strings in JavaScript are considered false when compared with the &lt;code&gt;==&lt;/code&gt; operator &lt;br&gt;
(but not when using &lt;code&gt;===&lt;/code&gt;)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("" == false)  // true
("" === false) // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Strings with a value are 'true', so you can do things like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (someString) {
    // string has a value
} else {
    // string is empty or undefined
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>What is Bubble Sort?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Fri, 02 Apr 2021 14:02:49 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-bubble-sort-2clh</link>
      <guid>https://forem.com/davejsaunders/what-is-bubble-sort-2clh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally sent to subscribers of the &lt;a href="https://www.baseclass.io/newsletter" rel="noopener noreferrer"&gt;BaseClass newsletter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Probably the simplest of sorting algorithms, Bubble Sort is inefficient at scale but quick to write and works fine on a handful of elements.&lt;/p&gt;

&lt;p&gt;It is an excellent introduction to more complex sorting algorithms.&lt;/p&gt;




&lt;h2&gt;
  
  
  In 5 minutes or less:
&lt;/h2&gt;

&lt;p&gt;Let's sort this array into ascending order:&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F1.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F1.png" alt="Array containing 6,9,4,3"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 1: Compare pairs of elements
&lt;/h3&gt;

&lt;p&gt;We're going to loop through &lt;strong&gt;each pair of elements in turn&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;If a pair of elements aren't in the right order, &lt;strong&gt;we'll swap them&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's go...&lt;/p&gt;

&lt;p&gt;The first pair is already in the correct order, so we can ignore them.&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F2.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F2.png" alt="Array containing 6,9,4,3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On to the next pair. These elements are in the wrong order, so we'll swap them.&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F3.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F3.png" alt="Array containing 6,4,9,3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, the last pair, which also need to be swapped:&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F4.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F4.png" alt="Array containing 6,4,3,9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We've now looped through all of the pairs, so our first pass through the array is done. &lt;/p&gt;

&lt;p&gt;This is how the array looked at the beginning and end of this first pass:&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F5.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F5.png" alt="Array after the fist pass"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how the highest value, 9, moved up through the array and into the correct place:&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F6.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F6.png" alt="Array showing that 9 has 'bubbled' up to the top"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It has 'bubbled up' to the correct position - hence 'Bubble Sort'.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Repeat
&lt;/h3&gt;

&lt;p&gt;Our first pass moved the highest element, 9, into the correct place.&lt;/p&gt;

&lt;p&gt;Each time we repeat this loop, we move the &lt;em&gt;next highest element&lt;/em&gt; into place.&lt;/p&gt;

&lt;p&gt;Now, we repeat this process - comparing each pair in turn and swapping them if required - until the array is completely sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We have 4 elements in the list, which means we'll need to repeat our loop 3 times&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Why 3? Because once 3 of the elements are in the correct place in the array, the remaining one must also be correct. &lt;/p&gt;

&lt;p&gt;If the number of elements in our array is &lt;code&gt;n&lt;/code&gt;, the number of loops we'll need is &lt;code&gt;n-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the state of our array after each pass. The sorted elements after each loop are highlighted.&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F7.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F7.png" alt="All stages of the algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the JavaScript code for this algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // We need to repeat the algorithm n-1 times
  for (let loop = 0; loop &amp;lt; array.length - 1; loop++) {

    // Loop through each pair of elements
    for (let pair = 0; pair &amp;lt; array.length - 1; pair++) {

      // Is this pair the wrong way around?
      if (array[pair] &amp;gt; array[pair + 1]) {   
        // Make the swap (using temporary variable)
        let tmp = array[pair]
        array[pair] = array[pair + 1]
        array[pair + 1] = tmp
      }      
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can improve on this basic algorithm though, with a couple of optimisations.&lt;/p&gt;




&lt;h3&gt;
  
  
  Optimisation #1
&lt;/h3&gt;

&lt;p&gt;Remember how the first pass of the algorithm caused the 9 to bubble up into the correct position?&lt;/p&gt;

&lt;p&gt;After that first pass, we know that the last element is correctly placed, so we can ignore it on our next loop. After the second pass, the second-to-last element is sorted, and so on.&lt;/p&gt;

&lt;p&gt;We'll change our code to ignore the last element after the first loop, the last &lt;em&gt;two&lt;/em&gt; after the second loop, and so on.&lt;/p&gt;

&lt;p&gt;This will make our algorithm very slightly quicker.&lt;/p&gt;

&lt;p&gt;Here's the updated code. Notice that the limit for the inner loop is now &lt;code&gt;array.length - loop - 1&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let loop = 0; loop &amp;lt; array.length - 1; loop++) {
  for (let pair = 0; pair &amp;lt; array.length - loop - 1; pair++) {
    if (array[pair] &amp;gt; array[pair + 1]) {
      let tmp = array[pair]
      array[pair] = array[pair + 1]
      array[pair + 1] = tmp
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Optimistion #2
&lt;/h3&gt;

&lt;p&gt;Imagine our algorithm was passed an array like this:&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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F9.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%2Fwww.baseclass.io%2Fnewsletter%2Fbubble-sort%2F9.png" alt="A pre-sorted array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This array is already sorted, doing three loops of our sorting algorithm is a complete waste of time.&lt;/p&gt;

&lt;p&gt;This leads us to our next optimisation; &lt;strong&gt;if we ever complete a loop without swapping any elements, we know the array is sorted and we can stop early&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That could be a big time saving when the array is already sorted - or nearly sorted - before we even start our Bubble Sort.&lt;/p&gt;

&lt;p&gt;With that code added, our final Bubble Sort algorithm looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  for (let loop = 0; loop &amp;lt; array.length - 1; loop++) {

    let hasSwapped = false;    

    for (let pair = 0; pair &amp;lt; array.length - loop - 1; pair++) {
      if (array[pair] &amp;gt; array[pair + 1]) {
        let tmp = array[pair]
        array[pair] = array[pair + 1]
        array[pair + 1] = tmp      
        hasSwapped = true;
      }
    }    

    if (!hasSwapped) {
      // No swaps, the array is now sorted
      break;
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Want to know more?
&lt;/h3&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://visualgo.net/en/sorting" rel="noopener noreferrer"&gt;A visual representation of bubble sort&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://techieme.in/improving-bubble-sort/" rel="noopener noreferrer"&gt;A more detailed article about the improvements mentioned above&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>100daysofcode</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What is The Travelling Salesman Problem?</title>
      <dc:creator>Dave Saunders</dc:creator>
      <pubDate>Sat, 13 Mar 2021 08:43:09 +0000</pubDate>
      <link>https://forem.com/davejsaunders/what-is-the-travelling-salesman-problem-35m6</link>
      <guid>https://forem.com/davejsaunders/what-is-the-travelling-salesman-problem-35m6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally sent to subscribers of the &lt;a href="https://www.baseclass.io/"&gt;BaseClass newsletter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;The Travelling Salesman Problem is a classic computer science problem, known for having no efficient solution.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why should I care?
&lt;/h1&gt;

&lt;p&gt;There are many real-life problems that are very similar to the Travelling Salesman Problem.&lt;/p&gt;

&lt;p&gt;Learning about problems like this will help you to recognize when you're facing something equally difficult to solve.&lt;/p&gt;




&lt;h1&gt;
  
  
  In 5 minutes or less:
&lt;/h1&gt;

&lt;p&gt;This is the 'Travelling Salesman Problem' (or TSP):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a list of cities, what is the shortest route that visits each city once and then returns to the origin city?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It sounds simple, but when we add enough cities it becomes &lt;strong&gt;impossible for a computer to solve in a realistic time frame&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's see why..&lt;/p&gt;




&lt;h2&gt;
  
  
  The 'brute force' approach.
&lt;/h2&gt;

&lt;p&gt;There's only one way to find the shortest solution to the Travelling Salesman Problem, &lt;strong&gt;we have to try every possible option in turn&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll pick a simple example, four cities:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xOJ8tdZa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xOJ8tdZa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/1.png" alt="Island with 4 cities" width="439" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll start from A. We can go to either B, C or D next, Let's imagine that we go to B. From there, we have the option to visit either C or D. Once we get to either of those, we then travel to the remaining city before returning back to A.&lt;/p&gt;

&lt;p&gt;To recap; from our first city we have &lt;code&gt;3&lt;/code&gt; choices of where to go next. Once we pick one of those, we have &lt;code&gt;2&lt;/code&gt; remaining cities to choose from. From there, there’s only &lt;code&gt;1&lt;/code&gt; city left.&lt;/p&gt;

&lt;p&gt;That means that the total number of routes we must try is &lt;code&gt;3 x 2 x 1 = 6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's add another city, 'E':&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3YLruN8U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3YLruN8U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/2.png" alt="Island with 5 cities" width="439" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we pick a place to start, we now have a choice of &lt;code&gt;4&lt;/code&gt; others cities to visit. From each of those, we have to pick one of the remaining &lt;code&gt;3&lt;/code&gt; to visit next. From each of &lt;em&gt;those&lt;/em&gt;, there are &lt;code&gt;2&lt;/code&gt; cities to pick from.. you get the idea.&lt;/p&gt;

&lt;p&gt;The number of possible permutations for five cities is &lt;code&gt;4 x 3 x 2 x 1 = 24&lt;/code&gt;. We've added one city, but there are now four times the number of options!&lt;/p&gt;

&lt;p&gt;Some of these routes are duplicates (we travel the same route, but in reverse) so they can be discounted. That doesn't really help us though, as the number of possible routes will still grow &lt;em&gt;very&lt;/em&gt; quickly. &lt;/p&gt;

&lt;p&gt;With only 10 cities, there are &lt;code&gt;181,440&lt;/code&gt; possible routes. Add &lt;em&gt;one more&lt;/em&gt; and there are now over &lt;code&gt;1.8 million&lt;/code&gt;. By the time we get to just 15 cities, there are over &lt;code&gt;43 Billion&lt;/code&gt; possible routes!&lt;/p&gt;

&lt;p&gt;With enough cities, the number of routes becomes so large that we just couldn't compute it in a reasonable timescale.&lt;/p&gt;

&lt;p&gt;There is a relatively more efficient method to calculate all possible permutations using &lt;a href="https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b76d7552e8dd"&gt;dynamic programming&lt;/a&gt;, but it doesn't matter - it still becomes too slow at scale.&lt;/p&gt;




&lt;h2&gt;
  
  
  'Solving' the TSP
&lt;/h2&gt;

&lt;p&gt;Since we can't truly solve this problem, the best we can do is look for a good approximation. &lt;/p&gt;

&lt;p&gt;One way to do this is using the &lt;strong&gt;Nearest Neighbor&lt;/strong&gt; method:&lt;/p&gt;

&lt;p&gt;Starting from a random city, pick the &lt;strong&gt;nearest&lt;/strong&gt; city and go there. Keep picking the nearest city until you've been to all of them once, before returning back to the starting point.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qGxCk2jL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qGxCk2jL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.baseclass.io/travelling-salesman-problem/3.png" alt="Nearest Neighbor" width="565" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the simplest algorithm to find an approximate solution, but there are other algorithms (with varying complexity) that can usually find shorter routes.&lt;/p&gt;

&lt;p&gt;Until recently, the best has been an algorithm developed in 1976 by Nicos Christofides. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Christofides' algorithm is capable of finding solutions that are &lt;em&gt;at most&lt;/em&gt; 50% longer than the 'perfect' trip&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In 2019, Karlin, Klein and Oveis Gharan proved that an algorithm originally developed in 2010 could actually beat Christofides' algorithm by a tiny fraction of a percent.&lt;/p&gt;

&lt;p&gt;This may not sound significant, but it proves that it &lt;em&gt;is&lt;/em&gt; possible to improve on that algorithm, and opens the door for more solutions.&lt;/p&gt;




&lt;h2&gt;
  
  
  The TSP in disguise
&lt;/h2&gt;

&lt;p&gt;It's easy to dismiss the Travelling Salesman Problem as a purely academic problem, not applicable to every day development. It does, however, have real world implications.&lt;/p&gt;

&lt;p&gt;'Last Mile Delivery' is the commonly used example. This is the process of delivering something from a transport hub to its final destination. For example, the Amazon truck delivering hundreds of parcels to individual houses.&lt;/p&gt;

&lt;p&gt;When you look closer though, other problems start to look sightly like the TSP:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the quickest way to pick items in a warehouse to fulfill an order?&lt;/li&gt;
&lt;li&gt;How can we schedule a bus route to visit all of the stops in the shortest time/distance?&lt;/li&gt;
&lt;li&gt;What is the shortest way to route the wiring in an electrical component?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Travelling Salesman Problem is in a class of problems called 'NP-hard'&lt;/strong&gt;. We'll cover 'NP-hard' in another issue, but learning about NP-hard problems like the TSP will help you to recognise when you're facing something similar in your own work.&lt;/p&gt;

&lt;p&gt;Need to calculate every possible permutation of things in a collection? That sounds a lot like the Travelling Salesman Problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Want to know more?
&lt;/h2&gt;

&lt;p&gt;Check out these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cse442-17f.github.io/Traveling-Salesman-Algorithms/"&gt;An excellent page demonstrating multiple algorithms for the TSP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/basecs/speeding-up-the-traveling-salesman-using-dynamic-programming-b76d7552e8dd"&gt;Speeding Up The Traveling Salesman Using Dynamic Programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.wired.com/story/computer-scientists-break-the-traveling-salesperson-record/"&gt;A Wired article about the 2019 improvements, and an explanation of the new algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://apps.dtic.mil/dtic/tr/fulltext/u2/a025602.pdf"&gt;Nicos Christofides' paper from 1976, describing his algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://arxiv.org/abs/1908.00227"&gt;Karlin, Klein and Oveis Gharan's paper from 2019&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
