<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matt Edwards</title>
    <description>The latest articles on DEV Community by Matt Edwards (@mattedwards).</description>
    <link>https://dev.to/mattedwards</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%2F450974%2F2cbe49f5-6033-4717-90bb-7a90b5bb3987.jpg</url>
      <title>DEV Community: Matt Edwards</title>
      <link>https://dev.to/mattedwards</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mattedwards"/>
    <language>en</language>
    <item>
      <title>Towards fluency in JavaScript and other programming languages</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Sun, 26 Nov 2023 09:22:35 +0000</pubDate>
      <link>https://dev.to/mattedwards/towards-fluency-in-javascript-and-other-programming-languages-377</link>
      <guid>https://dev.to/mattedwards/towards-fluency-in-javascript-and-other-programming-languages-377</guid>
      <description>&lt;h2&gt;
  
  
  The joys of language
&lt;/h2&gt;

&lt;p&gt;The English language is endlessly amusing. For example, if the opposite of interior is exterior it surely follows that the opposite of increment must be excrement ... right?&lt;/p&gt;

&lt;p&gt;Wrong!&lt;/p&gt;

&lt;p&gt;For this and many other reasons I have been fascinated by language for as long as I can remember. At school in year 7 we were taught one lesson a week of Linguistics, which is essentially the study of language. In the end of year exam I scored 98%. It's fair to say, I have always been a linguistic nerd and I went on to study French and German at A-Level. So I know a little about learning languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  The journey from clunky to fluent
&lt;/h2&gt;

&lt;p&gt;Imagine a native English speaker learning German. In the early stages of the learning process, it's natural to think of a phrase in English and, in ones mind, systematically translate that phrase word-for-word into German. &lt;/p&gt;

&lt;p&gt;That process...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;think in English&lt;/li&gt;
&lt;li&gt;translate the words to German&lt;/li&gt;
&lt;li&gt;output the German phrase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...is inefficient and becomes a barrier to fluency. The only way to become fluent in a language is to &lt;strong&gt;think&lt;/strong&gt; in that language. Then you can skip the first two steps in the clunky process above and jump straight to &lt;code&gt;output the German phrase&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Fluency to that extent really only comes after interacting with a language day after day for an extended period.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what does that have to do with software?
&lt;/h2&gt;

&lt;p&gt;The other day an email arrived from a mailing list that I subscribed to a few years ago. The email was part of a series aimed at junior software engineers preparing for technical interviews. It included an example of the kind of coding exercise that a junior engineer might face. In this case: write a function that takes a string as a parameter and determines whether that string is a pallindrome.&lt;/p&gt;

&lt;p&gt;A pallindrome is a word, phrase or expression that reads the same backwards as it does forwards. So, if we assume the function is called &lt;code&gt;isPallindrome()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;isPallindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;level&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// should return true&lt;/span&gt;

&lt;span class="nf"&gt;isPallindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lever&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// should return false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example solution in the email looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isPallindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reversedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversedString&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reversedString&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Similarities with learning a spoken language
&lt;/h2&gt;

&lt;p&gt;This made me think about my own evolution as a programmer and the parallels that exist between the twin processes of learning a computer language (JavaScript in this example) and learning a spoken language.&lt;/p&gt;

&lt;p&gt;The example solution shown here looks like something I might have written in the early days of my programming journey - the email that contained it was, after all, part of a series of emails aimed at junior software engineers. You can quite clearly discern three steps that line up quite accurately with the three steps in the translation process for a novice German speaker outlined above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;think through the logic&lt;/li&gt;
&lt;li&gt;translate that flow of logic to steps in a program&lt;/li&gt;
&lt;li&gt;write those steps in your chosen language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That step-by-step logic is most evident in the conditional statement that determines the returned value. The flow of logic runs something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compare the two strings&lt;/li&gt;
&lt;li&gt;if they are the same, the input must be a pallindrome, so we should return true&lt;/li&gt;
&lt;li&gt;otherwise, the input is not a pallindrome, so we should return false&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Translating that step-by-step into JavaScript yields this conditional statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Towards fluency - understanding the comparison operator
&lt;/h2&gt;

&lt;p&gt;Going back to the German language example above, I suggested that, in order to become fluent in German, it's necessary to be able to think in German. What that really means is you need to process language the way that a native German speaker processes language.&lt;/p&gt;

&lt;p&gt;The same applies when writing a computer language. To become fluent we have to learn to process the language within the context of its built-in rules &amp;amp; structures.&lt;/p&gt;

&lt;p&gt;Let's examine one of the most fundamental structures of any programming language: the comparison operator. There is one included in the conditional statement above.&lt;/p&gt;

&lt;p&gt;The triple equals is a comparison operator and &lt;code&gt;a === b&lt;/code&gt; is a comparison operation. It is asking JavaScript to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compare the expression on the left of the comparison operator&lt;/li&gt;
&lt;li&gt;with the expression on the right of the comparison operator&lt;/li&gt;
&lt;li&gt;and let us know if they are the same&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A comparison operation evaluates to either true or false, as this Node REPL output demonstrates:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0_4eiNwX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omqj5bku2gagqzbqmse4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0_4eiNwX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omqj5bku2gagqzbqmse4.png" alt="comparison operator output" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As an aside, there is a subtle difference in JavaScript between the double &amp;amp; triple equals comparison operators. That's not relevant to this post but you can read more about it &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness"&gt;in MDN Web Docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Shortcut the return statement
&lt;/h2&gt;

&lt;p&gt;Look again at what the comparison operator is asking JavaScript to do. And compare that to the step-by-step logic that was translated into the conditional statement. You will notice that they do the same thing. In that case, one of them must be redundant in the conditional statement set out above.&lt;/p&gt;

&lt;p&gt;This tells us that we don't need to construct a conditional statement that returns a boolean because the comparison operation, that is a part of every conditional statement, already evaluates to a boolean.&lt;/p&gt;

&lt;p&gt;So we can replace...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;'Word-for-word' translation might well get the job done. But the better understanding of the fundamental structures of a language that comes with experience and practice (otherwise known as fluency) enables us to produce more efficient, concise code.&lt;/p&gt;

&lt;p&gt;The way that &lt;code&gt;reversedString&lt;/code&gt; is determined in the example solution could be refactored a little too but I will leave you to ponder that one yourself.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
    <item>
      <title>Grokking Algorithms in JavaScript - Part 3</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Sat, 15 Jan 2022 16:02:34 +0000</pubDate>
      <link>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-3-11cf</link>
      <guid>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-3-11cf</guid>
      <description>&lt;p&gt;In &lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-2-213o"&gt;Part 2&lt;/a&gt; of this series I introduced graphs. A graph is a representation of connections between nodes in a network. The connections between the nodes are called 'edges'. For example, in a geographical network nodes might be towns and edges might be the roads that connect the towns.&lt;/p&gt;

&lt;p&gt;I also introduced you to the breadth-first search ("BFS") algorithm: a means to find the shortest route through a graph. In the context of BFS, shortest route means the route that visits the fewest nodes. In this article I will add a little complexity to graphs by adding "weights" and introduce &lt;strong&gt;Dijkstra's Algorithm&lt;/strong&gt; which will find the shortest route through these more complex weighted graphs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Weighted graphs
&lt;/h2&gt;

&lt;p&gt;Imagine a graph with nodes representing cities (Manchester, Birmingham, Milton Keynes, London &amp;amp; Edinburgh) and the edges between them representing railway tracks.&lt;/p&gt;

&lt;p&gt;Here is a picture of that graph.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IR8XldnR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfg2dhujyx2w73rz9u2n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IR8XldnR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfg2dhujyx2w73rz9u2n.jpg" alt="Unweighted graph" width="640" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You want to get from Manchester to London by train. Which route should you take? Well, we know that BFS will find the shortest path so we feed the graph into the algorithm, set it running, and it confidently tells us to go via Edinburgh.&lt;/p&gt;

&lt;p&gt;Ok, that's the route to take if you want the fewest stops - which is what BFS tells you - in the context of BFS, shortest route means the route that visits the fewest nodes.&lt;/p&gt;

&lt;p&gt;Let's add distances between cities:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oa35GSqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux1tlyiwybamquri52l2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oa35GSqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux1tlyiwybamquri52l2.jpg" alt="Weighted graph" width="640" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can see quite clearly what we already knew: the shortest route is via Birmingham &amp;amp; Milton Keynes at 200 miles rather than the 610 miles via Edinburgh.&lt;/p&gt;

&lt;p&gt;In graph terminology the numbers that represent the distance between nodes are the &lt;strong&gt;weights&lt;/strong&gt; of those edges. Weights don't have to represent distance. It could represent the cost of getting from one node to the next, for example.&lt;/p&gt;

&lt;p&gt;If you want to find the shortest path in a weighted graph, BFS simply won't cut the mustard. You need another graph algorithm: you need Dijkstra's algorithm, named after computer scientist Edsger Dijkstra who conceived of the idea about 65 years ago.&lt;/p&gt;

&lt;p&gt;Dijkstra's will find the cheapest / shortest path (in other words, the one with the lowest combined edge weights) in a weighted graph.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;nodes on a geographical graph - Dijkstra's will find the shortest route, like the example above.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;nodes in a graph of transactions - Dijkstra's will find the lowest cost chain of transactions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dijkstra's - the steps
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set up a list of all nodes.&lt;/strong&gt; 
The list will hold the cumulative weight of getting to that node.
If you can't yet calculate the cumulative weight because your route hasn't yet reached that node, give it a cumulative weight of &lt;em&gt;positive infinity&lt;/em&gt; (this may sound odd but it's an integral part of the algorithm's workings)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;From the current node, find the lowest cost node.&lt;/strong&gt; ie. the node you get to by following the lowest weight edge&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For all neighbours of that node check if there is a lower cumulative-weight way to get there.&lt;/strong&gt; 
If so, update that node's cumulative weight in the list that you set up at the outset.
(Remember, any nodes where you can't calculate the cumulative weight from the current node have an infinite cumulative weight)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Repeat until you have done this for every node in the graph.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Then calculate the final path.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Clarification of the values that are being recorded here
&lt;/h3&gt;

&lt;p&gt;In the steps above you will notice that there are two different weight-related values. It's worth spending a moment to think those values through.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge weights&lt;/strong&gt; - this is the "cost" of travelling from one node to another along that particular edge. An edge's weight is a fixed value: it never changes throughout the progress of the algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node cumulative weights&lt;/strong&gt; - these are the values held in the list that was set up at the start. For a given node, this is the cumulative weight of all of the edges along which you have to travel to get to a specific node &lt;em&gt;if you follow the lowest cost route that the algorithm has calculated so far&lt;/em&gt;. These values are updated as the algorithm processes the nodes in the graph.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dijkstra's - initial setup
&lt;/h2&gt;

&lt;p&gt;We need a graph to work with. Here is a simple example that the remainder of this article will refer to:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nijuqjf---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvte9ep8l7yzfmw3tzwf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nijuqjf---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvte9ep8l7yzfmw3tzwf.jpg" alt="A simple graph" width="640" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we discovered with BFS, setting up the required data structures represents a significant part of the work in graph algorithms.&lt;/p&gt;

&lt;h4&gt;
  
  
  The graph
&lt;/h4&gt;

&lt;p&gt;First we need a hash table to represent the graph. In BFS each node was a key in the hash table and its value was an array of the node's neighbours. The graph we are building here has an extra data point for every connection: the weight of the edge. To cater for that each node in the hash table will hold its own hash table (as opposed to the simple array in BFS).&lt;/p&gt;

&lt;p&gt;The slightly confusing explanation in that previous paragraph will hopefully become clearer when you look at the code below. Again I am using JavaScript's Map() object as a hash table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Cumulative node weights
&lt;/h4&gt;

&lt;p&gt;Next we need a structure to keep track of the cumulative weight of every node. Again a Map() is the perfect data structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;POSITIVE_INFINITY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the "fin" node has a cumulative cost of &lt;code&gt;POSITIVE_INFINITY&lt;/code&gt; (a JavaScript constant). From the start node, we can't "see" the route to the finish node - all we know is that going to A "costs" 6 and going to B "costs" 2. Remember, any nodes where you can't calculate the cumulative weight from the current node have an infinite cumulative weight.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parents
&lt;/h4&gt;

&lt;p&gt;There is one data requirement that hasn't been mentioned yet. As the algorithm traces its way through the graph, plotting the "lowest cost" route, we need to keep track of that route. Dijkstra's does that by, for every node, keeping track of the previous node in the path. So every node (apart from the start node) will have a "parent" node.&lt;/p&gt;

&lt;p&gt;Every node's parent is recorded in a &lt;code&gt;parents&lt;/code&gt; hash table (or Map() in JavaScript). At the outset it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time a node's cumulative weight is updated (because a lower cost path has been found) the parent for that node also needs to be updated.&lt;/p&gt;

&lt;p&gt;Notice that the "fin" node's parent starts out having a &lt;code&gt;null&lt;/code&gt; value. That's because we won't know that node's parent until the routing process has got that far.&lt;/p&gt;

&lt;h4&gt;
  
  
  Processed nodes
&lt;/h4&gt;

&lt;p&gt;And the last part of the data structure setup - to avoid loops we need to keep track of nodes already visited. That just takes the form of an array called &lt;code&gt;processed&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;processed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Processing the graph
&lt;/h2&gt;

&lt;p&gt;Now that we have the initial data structures set up we can start processing the graph.&lt;/p&gt;

&lt;h4&gt;
  
  
  Lowest cost node
&lt;/h4&gt;

&lt;p&gt;The first activity on arriving at a new node is to find the lowest cost node that hasn't already been processed because that node will be the next one to visit. Remember that all nodes (apart from immediate neighbours of &lt;code&gt;start&lt;/code&gt;) were initially assigned a cumulative weight of &lt;code&gt;infinity&lt;/code&gt; and those figures are only updated as we visit their neighbours. So, ignoring nodes that have already been processed (held in the &lt;code&gt;processed&lt;/code&gt; array), the lowest cost node will automatically be a neighbour of the node we are currently processing and we just need to loop through all nodes in the costs hash table and do a comparison.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;findLowestCostNode()&lt;/code&gt; function looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findLowestCostNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;lowestCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;POSITIVE_INFINITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;lowestCostNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;lowestCost&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;processed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;lowestCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;lowestCostNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lowestCostNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Graph traversal
&lt;/h4&gt;

&lt;p&gt;We have set up the data structures and we have a function to decide which node to visit next. Now we just need to loop through the nodes and carry out the steps outlined above. Below is the code that achieves that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findLowestCostNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nodeCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;neighbours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;neighbours&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;neighbour&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;newNodeCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nodeCost&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;neighbour&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;newNodeCost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;neighbour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newNodeCost&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;neighbour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;processed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findLowestCostNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to define the first lowest cost node (ie. a neighbour of the start node) before entering the while loop because 'node' being truthy is the while loop condition. The lowest cost node is then updated at the end of each iteration until there are no nodes left to process.&lt;/p&gt;

&lt;p&gt;After the algorithm has finished processing the graph, the value of the "fin" node in the costs hash table will contain the cumulative cost of the lowest cost path. (In this case: 6)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;costs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find the actual path that the algorithm has plotted you need to begin with the end node and work backwards using the values in the parents hash table. In this simple example, the parents hash table looks like this after processing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, working backwards:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;from &lt;code&gt;fin&lt;/code&gt; go to &lt;code&gt;a&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;from &lt;code&gt;a&lt;/code&gt; go to &lt;code&gt;b&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;from &lt;code&gt;b&lt;/code&gt; go to &lt;code&gt;start&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There you have the lowest cost route.&lt;/p&gt;

&lt;h2&gt;
  
  
  Larger example
&lt;/h2&gt;

&lt;p&gt;It's fair to say that the graph we are working with here is trivially small. I am able to confirm however that the method does work on more complex graphs. Take a look at this problem: &lt;a href="https://adventofcode.com/2021/day/15"&gt;Part 1 of Day 15 of the 2021 Advent of Code&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The graph in this problem is a 100 x 100 matrix of digits (available &lt;a href="https://adventofcode.com/2021/day/15/input"&gt;here&lt;/a&gt;). Your job is to find the lowest cost route from top-left to bottom-right through the matrix, moving one node at a time up, down, left or right, where the cost increases by the value of each node visited.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/matt-j-e/advent-of-code-2021/blob/main/day15/day15.js"&gt;Here is my code to solve the problem&lt;/a&gt;. The first ~half of the code builds the graph hash map and the other data structures that are discussed in this article. The rest of the code is essentially the function and while loop shown above.&lt;/p&gt;

&lt;p&gt;On my ~9 year old Mac it took about 13 minutes to come up with the lowest cost route. I dare say there is a more efficient and/or elegant approach but the fact that it provided the correct answer is evidence that the algorithm does work with larger, more complex graphs.&lt;/p&gt;

&lt;p&gt;If you want to give it a whirl the correct answer is shown in a comment at the bottom of the file on GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article I have dug a little deeper into graphs and added weights to the edges. I have also taken you step by step through Dijkstra's algorithm to find the lowest cost route through a weighted graph.&lt;/p&gt;

&lt;p&gt;You have also learned how to put together the code that will carry out Dijkstra's algorithm.&lt;/p&gt;

&lt;p&gt;The next, and final, part in this series will look at dynamic programming algorithms and how to use one to solve the Knapsack Problem.&lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="https://unsplash.com/@genejeter?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Gene Jeter&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/weights?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Grokking Algorithms in JavaScript - Part 2</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Wed, 12 Jan 2022 06:48:33 +0000</pubDate>
      <link>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-2-213o</link>
      <guid>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-2-213o</guid>
      <description>&lt;p&gt;This is part 2 in a series of articles about the algorithms covered in &lt;strong&gt;Aditya Y. Bhargava's excellent book Grokking Algorithms&lt;/strong&gt;. In &lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-1-529a"&gt;Part 1&lt;/a&gt; I looked at binary search, selection sort and quicksort. In this part I will look at graphs and one of their associated algorithms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breadth-first search
&lt;/h2&gt;

&lt;p&gt;Breadth-first search ("BFS") is a means of searching through the nodes in a graph to find a node that has specific properties: eg. find a route from town A to town B, or; find a certain person in your social network.&lt;/p&gt;

&lt;p&gt;Given a graph, a start node and an end node, BFS can answer 2 questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;is there a path from start to end, and&lt;/li&gt;
&lt;li&gt;what is the shortest path (in terms of the number of nodes visited) from start to end&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  So what exactly is a graph?
&lt;/h2&gt;

&lt;p&gt;Graphs in this context don't have x and y axes: we're not talking about line graphs or bar charts. The graphs that we are talking about here are representations of connections in a network.&lt;/p&gt;

&lt;p&gt;A graph is made up of &lt;em&gt;nodes&lt;/em&gt; which are connected by &lt;em&gt;edges&lt;/em&gt;. For example, a graph might be a representation of a geographical area where nodes are towns and edges are the roads that connect them. Or it might represent a social network where nodes are people and edges represent the relationships between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our example graph
&lt;/h3&gt;

&lt;p&gt;The picture below shows a small social network with your immediate connections, their connections and their connections' connections; so, three levels of connection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j6u4Iiel--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbuo8vgcpyq7elfid7tb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j6u4Iiel--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbuo8vgcpyq7elfid7tb.jpg" alt="Example social network graph" width="800" height="1067"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Imagine that your central heating boiler has broken down and you want to know if your social network contains a heating engineer. You also want to find the heating engineer that is closest to you in the network.&lt;/p&gt;

&lt;p&gt;Before getting into the workings of BFS it's worth thinking about how we are going to represent the graph to be searched. Indeed, a lot of the work in writing graph algorithms is in constructing the graph representation in the first place. The perfect data structure for the job is a hash table and the Grokking Algorithms book devotes an entire chapter to the subject (which is well worth a read). In simple terms, a hash table stores data in key value pairs where keys are unique and values can hold any type of data, including arrays or even other hash tables.&lt;/p&gt;

&lt;p&gt;Python and JavaScript each has a native data structure that performs the job of a hash table: Python has its Dictionary data structure while in JavaScript we can use the Map() object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Representing the graph as a JavaScript Map() object
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;you&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bricklayer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dimitrios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chef&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;katie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;architect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;peter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dean&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;solicitor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aleksandra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;doctor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dimitrios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heating engineer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shefali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;driving instructor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;katie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shefali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;driving instructor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dean&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;james&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;graphic designer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aleksandra&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;molly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;software engineerr&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shefali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;andrew&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heating engineer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;amy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;barrister&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;james&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;molly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;andrew&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;amy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  BFS methodology
&lt;/h2&gt;

&lt;p&gt;A quick glance at the image of our small social network above tells you that there are 2 heating engineers in this 3-level network - Sam &amp;amp; Andrew - and that Sam is the nearest connection to you being a level 2 connection whereas Andrew is level 3. But how do you code that into an algorithm?&lt;/p&gt;

&lt;p&gt;BFS works by processing all closest connections to a node before moving on to the next closest connections, and so on. The way to enforce that "closest first" rule is to use a queue. A queue data structure works just the same as a queue in real life: people join the queue at the back and leave when they get to the front.&lt;/p&gt;

&lt;p&gt;So starting with the "you" node in the graph, the algorithm adds the "you" node's neighbours to the back of the queue. To find the neighbours of a node simply look up that node in the graph hash table. Remember that a hash table is a collection of key/value pairs. In the graph hash table the node is the key and its associated value is an array of the node's neighbours.&lt;/p&gt;

&lt;p&gt;The algorithm looks at the queue, removes the person from the front and checks whether they are a heating engineer. If so, the search is over - return that person's name. Otherwise add that person's neighbours to the back of the queue, take the person from the front and continue.&lt;/p&gt;

&lt;p&gt;This process repeats until: &lt;/p&gt;

&lt;p&gt;a. you find a heating engineer, or&lt;br&gt;
  b. the search queue is empty - which means there is no heating engineer in your social network.&lt;/p&gt;

&lt;p&gt;It's worth highlighting again here that the First In First Out (FIFO) nature of a queue means that 1st level connections are processed before 2nd level connection; 2nd level connections are processed before 3rd ... and so on. This guarantees that a search for a heating engineer in the above graph will always return Sam, not Andrew, ie: the shortest path.&lt;/p&gt;

&lt;p&gt;There is another important aspect to think about here. Have a look at the image above showing the social network. You will note that Shefali is a connection of Dimitrios and Katie. We don't want to add Shefali (and by extension her connections) to the search queue twice. To avoid that happening we can keep a list of people we have already considered. And at the stage above where a person is added to the back of the queue, that needs only happen if that person isn't in the "already processed" list. In the code below that list takes the form of a simple array called &lt;code&gt;searched&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code that implements the steps above is as follows. This is based on the example in Grokking Algorithms, extended a little and converted to JavaScript. The search function takes two parameters: the start node ("you" in this case) and the job to search for. It searches the &lt;code&gt;graph&lt;/code&gt; data structure set out above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;searchQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;searched&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;searchQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;searched&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;searchQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;searchQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nx"&gt;searched&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Nobody in your social network is a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;you&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heating engineer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "sam is a heating engineer"&lt;/span&gt;
&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;you&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;police officer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Nobody in your social network is a police officer"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article I have introduced the concept of graphs and how to use a hash table to represent a graph in code, specifically using the Map() structure in JavaScript. I have laid out the steps that a BFS algorithm takes to traverse a graph in search of a node with certain properties and demonstrated how such an algorithm might be implemented in JavaScript.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-3-11cf"&gt;the next article in this series&lt;/a&gt; I will build on the concept of graphs, introduce the idea of weighted graphs and take a look at another graph traversal algorithm called Dijkstra's algorithm. &lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="https://unsplash.com/@dnevozhai?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Denys Nevozhai&lt;/a&gt; on &lt;a href="https://unsplash.com/collections/1571191/complex-algorithm%2C-database?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Grokking Algorithms in JavaScript - Part 1</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Wed, 12 Jan 2022 06:48:18 +0000</pubDate>
      <link>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-1-529a</link>
      <guid>https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-1-529a</guid>
      <description>&lt;p&gt;Christmas 2021 - my favourite gift was the book &lt;strong&gt;&lt;em&gt;Grokking Algorithms by Aditya Y. Bhargava&lt;/em&gt;&lt;/strong&gt;. This book is perfect for somebody like me who has never formally studied computer science but has developed a deep interest in the subject.&lt;/p&gt;

&lt;p&gt;Over the festive period I worked through the chapters and the code examples, making the small changes required to get them to run in Python 3 (the book examples are written in Python 2), and then converting them to JavaScript.&lt;/p&gt;

&lt;p&gt;Below is my interpretation of some of the algorithms that the book focuses on, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary search&lt;/li&gt;
&lt;li&gt;Selection sort&lt;/li&gt;
&lt;li&gt;Quicksort&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In later parts I will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-2-213o"&gt;Breadth-first search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-3-11cf"&gt;Dijkstra's algorithm&lt;/a&gt; &amp;amp;&lt;/li&gt;
&lt;li&gt;Solving the Knapsack Problem with dynamic programming&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BINARY SEARCH
&lt;/h2&gt;

&lt;p&gt;Imagine you have a sorted array and you are searching for a specific element that may, or may not, be in that array. How would you approach the search?&lt;/p&gt;

&lt;p&gt;One way would be to start at array &lt;code&gt;index 0&lt;/code&gt; and work your way through each element until you find what you are looking for. If your target element is the last one in the array, or it isn't in the array at all, you will need to access every element. That's the worst case scenario and it's customary to compare algorithm efficiency based on the worst case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary search - steps
&lt;/h3&gt;

&lt;p&gt;Since the array is sorted you could use a binary search algorithm. Imagine you have a sorted array of 512 elements. Binary search works like this:&lt;/p&gt;

&lt;p&gt;Your first step is to look at the middle element (index 256) to see if it is the element you are looking for. If it is, happy days! Chances are though that it won't be, in which case you ask yourself: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Is this middle element higher or lower than my target element?"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If &lt;code&gt;array[256]&lt;/code&gt; is higher, you know that your target element must be in the lower half so you have immediately discarded half of the array.&lt;/p&gt;

&lt;p&gt;Next, look at the middle element from those that remain and go through the same steps. Again you have eliminated half of the remaining elements.&lt;/p&gt;

&lt;p&gt;Keep doing that until you either find your target element or discover that it's not in the array. Worst case scenario is that your target isn't in the array, or it's the very last element. But how many steps would it take you to find the solution in that worst case scenario? &lt;/p&gt;

&lt;p&gt;Well, in an array of 512 elements the answer is &lt;strong&gt;log2512&lt;/strong&gt;. In other words, to what power do you have to raise the number 2 to get 512? &lt;/p&gt;

&lt;p&gt;Answer: &lt;strong&gt;9 steps.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison with simple search
&lt;/h3&gt;

&lt;p&gt;Using the first method (known as simple search) on an array of 512 elements would take 512 steps (remember, we're looking at the worst case here). The 9 steps taken by binary search is clearly significantly quicker. And the difference is magnified with larger data sets. &lt;/p&gt;

&lt;p&gt;Imagine that you need to search an array of 1 billion elements and your super fast computer can process 1000 elements per second. Binary search would deliver an answer in 30 milliseconds (2&lt;sup&gt;30&lt;/sup&gt; = 1.073 billion) while simple search would take more than 11 days.&lt;/p&gt;

&lt;p&gt;Below is my JavaScript version of binary search.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SELECTION SORT
&lt;/h2&gt;

&lt;p&gt;The first algorithm that we looked at, binary search, only works on a sorted array. Selection sort is one method that you can use to get an array into a sorted state and it works as follows:&lt;/p&gt;

&lt;h3&gt;
  
  
  Selection sort - steps
&lt;/h3&gt;

&lt;p&gt;Loop through your unsorted array;&lt;br&gt;
Find the lowest value element;&lt;br&gt;
Extract said element and place it in a new array at index &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Loop through the remaining elements of the unsorted array;&lt;br&gt;
Find the lowest value element;&lt;br&gt;
Extract said element and add it to the end of the new array.&lt;/p&gt;

&lt;p&gt;Repeat until the original, unsorted array is empty by which time the new array is a sorted array of the same elements.&lt;/p&gt;

&lt;p&gt;Below is my JavaScript version of selection sort. The Python code in the book makes use of a for loop in the main selection_sort() function whose initial length is determined by the length of the original, unsorted array. I preferred to use a while loop to avoid the risk of referencing an out-of-range array index with the original array shrinking on each iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findSmallest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;smallestIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;smallestIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;smallestIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;selectionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findSmallest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;smallest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;selectionSort&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;// [ 2, 3, 5, 6, 10 ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;selectionSort&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grape&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kiwi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;//  'apple', 'banana', 'grape', 'kiwi' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Efficiency
&lt;/h3&gt;

&lt;p&gt;It's worth mentioning here that selection sort is a slow algorithm. For an unsorted array of &lt;code&gt;n&lt;/code&gt; items, that array has to be looped through &lt;code&gt;n&lt;/code&gt; times. It therefore takes n&lt;sup&gt;2&lt;/sup&gt; operations.&lt;/p&gt;

&lt;p&gt;But, hang on a minute, n reduces by 1 on each iteration so it's not n&lt;sup&gt;2&lt;/sup&gt;; surely it's more like 1/2n * n operations.&lt;/p&gt;

&lt;p&gt;That's true, but in the world of algorithm performance measurement, constants (like the 1/2 in the previous sentence) are ignored so selection sort has an efficiency of &lt;strong&gt;n&lt;sup&gt;2&lt;/sup&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  QUICKSORT
&lt;/h2&gt;

&lt;p&gt;As its name suggests, &lt;em&gt;quicksort&lt;/em&gt; is somewhat quicker than selection sort. It's what's known as a divide and conquer algorithm and uses a technique similar to that used in binary search in that it breaks down the problem into smaller and smaller chunks.&lt;/p&gt;

&lt;p&gt;It also relies on recursion, a subject that I won't go into in any depth here other than to say that it is a technique that relies on a function being able to call itself repeatedly until what's known as the "base case" is reached, at which point the function returns its result.&lt;/p&gt;

&lt;p&gt;Recursion also relies on the inner workings of the call stack. Until the base case is reached, every call to the function is incomplete and is held &lt;em&gt;in limbo&lt;/em&gt; in the call stack. When the base case is reached, and the function finally returns its result, the results of each preceding function call can then be passed down as each completed function is popped off the call stack and the final result is output from the initial call to the recursive function.&lt;/p&gt;

&lt;p&gt;It's vitally important to include a valid base case in a recursive function, otherwise the function will continue calling itself forever, or at least until the call stack overflows.&lt;/p&gt;

&lt;p&gt;That's probably a rather confusing explanation of the workings of recursion. If you want to understand it more fully I recommend getting your own copy of Grokking Algorithms. Aditya Bhargava does a wonderful job of explaining it with lots of hand-drawn illustrations. &lt;/p&gt;

&lt;p&gt;I can also recommend this talk by Al Sweigert on the subject:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=fhDsjfLSmVk"&gt;https://www.youtube.com/watch?v=fhDsjfLSmVk&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Quicksort steps
&lt;/h3&gt;

&lt;p&gt;Quicksort works by selecting an array element at random. This becomes the &lt;em&gt;"pivot"&lt;/em&gt;. Remaining elements are compared against the pivot and divided into "less than" and "greater than" arrays.&lt;/p&gt;

&lt;p&gt;Each of the less and greater arrays is run through the same process, and so on and so on until the base case is reached (ie. the array is only one element long so cannot be sorted) at which point all of the recursive function calls can return and everything is put back together at the end in sorted order.&lt;/p&gt;

&lt;p&gt;Below is my JavaScript take on quicksort based on the Python version in the book. The Python version is very succinct. It makes use of list comprehensions, a very neat technique, and Python's ability simply to add lists together.&lt;/p&gt;

&lt;p&gt;I used JavaScript's filter function in place of Python's list comprehensions and the array spread operator to facilitate the adding together of all of the elements in the recursive return statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;quicksort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pivotIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pivotIndex&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reduced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pivotIndex&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pivotIndex&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;less&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reduced&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reduced&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nf"&gt;quicksort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;less&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nf"&gt;quicksort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greater&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;quicksort&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt; &lt;span class="c1"&gt;// [ 2, 3, 5, 10 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any element can be the pivot in quicksort but choosing an element at random will yield the greatest time efficiency in the average case, namely: &lt;strong&gt;n log n&lt;/strong&gt;. (In algorithm efficiency terms, "log" is assumed always to refer to log2 and it's customary simply to omit the 2)&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This article introduced the concept of algorithms by looking at the simpler examples. Not all algorithms are created equally efficient and the idea of time efficiency was introduced.&lt;/p&gt;

&lt;p&gt;The subject of recursion also featured. Recursion is a technique often used in algorithms which is notoriously difficult for beginners to wrap their head around. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/mattedwards/grokking-algorithms-in-javascript-part-2-213o"&gt;Part 2&lt;/a&gt; of this series will look at graphs and breadth-first search.&lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="https://unsplash.com/@clemono?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Clem Onojeghuo&lt;/a&gt; on &lt;a href="https://unsplash.com/collections/1571191/complex-algorithm%2C-database?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Manchester Codes - Software Engineer FastTrack Bootcamp - Week 1</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Sun, 22 Nov 2020 12:16:09 +0000</pubDate>
      <link>https://dev.to/mattedwards/manchester-codes-software-engineer-fasttrack-bootcamp-week-1-514g</link>
      <guid>https://dev.to/mattedwards/manchester-codes-software-engineer-fasttrack-bootcamp-week-1-514g</guid>
      <description>&lt;p&gt;&lt;a href="https://www.manchestercodes.com/software-engineer-fasttrack/curriculum"&gt;Manchester Codes Software Engineer FastTrack&lt;/a&gt; is a 24 week, part-time bootcamp structured as two 3-hour lectures per week plus a fair amount of self-study in between. Owing to Covid restrictions it's currently a fully remote course with lectures delivered via Zoom. Tutors are also available for 1-2-1 assistance during Saturday "remote drop-in" sessions. The part-time structure appeals to me because I still work full-time in a non-tech field.&lt;/p&gt;

&lt;p&gt;I signed up for this bootcamp a few weeks ago. It seemed to take ages for the start date to roll around and I was excited to get going. &lt;a href="https://dev.to/mattedwards/manchester-codes-software-engineer-fasttrack-3770"&gt;I was also apprehensive&lt;/a&gt; because I knew I was going to be significantly older than my classmates. &lt;/p&gt;

&lt;h2&gt;
  
  
  Lecture 1
&lt;/h2&gt;

&lt;p&gt;Lecture 1 wasn't really a lecture, it was more of an introduction. We all introduced ourselves by giving a very brief bio and, as a bit of an ice breaker, offering 3 statements about ourselves two of which were true and the other a lie. There's a whole mix of people in my cohort and some of my fellow students have interesting back stories :-) And, at 54, I'm definitely the Grandaddy of the group. I keep telling myself it's all about mindset: I'm not "old", I "have experience"; and, my age is not a bug; it's a feature! One of these days I might even believe myself! Haha.&lt;/p&gt;

&lt;p&gt;The tutors Miguel and Jenny then gave us a brief introduction to the work we will be doing over the next 6 months. After that we all got down to business setting up our development environments. The tutors were on hand to help out when anybody got stuck. I've been playing with Python for a few months so I'm fairly comfortable working in the Mac terminal. I still found the whole process a little stressful.&lt;/p&gt;

&lt;p&gt;A Zoom classroom is a slightly weird environment - everybody on camera but quietly getting on with their own stuff except when asking for help. Somehow it still &lt;em&gt;feels&lt;/em&gt; like a classroom. My main weakness in a classroom setting is that I fundamentally lack self-confidence and I'm scared of being the one who just doesn't get it and holds everybody up. That self-imposed sense of time pressure causes mild to moderate anxiety that manifests itself as an impaired ability to follow simple instructions. (I have to stress here: it's &lt;strong&gt;entirely&lt;/strong&gt; self-imposed, the tutors put no pressure on us whatsoever; they are nothing but supportive and patient)&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up the development environment
&lt;/h3&gt;

&lt;p&gt;This comprised the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Node Version Manager&lt;/li&gt;
&lt;li&gt;Install Node Package Manager&lt;/li&gt;
&lt;li&gt;Install Node.js&lt;/li&gt;
&lt;li&gt;Install Git &lt;/li&gt;
&lt;li&gt;Generate SSH key and add to Github&lt;/li&gt;
&lt;li&gt;Install Visual Studio Code&lt;/li&gt;
&lt;li&gt;Install ESLint&lt;/li&gt;
&lt;li&gt;Install the ESLint extension to Visual Studio Code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This mostly went without a hitch. There was a fair amount of copying &amp;amp; pasting bash commands which I find it a little unnerving though because when you're simply copying commands you don't really know what's going on and I have an in-built desire to understand things from first principles as much as possible. I guess it's an unavoidable part of setting up new tools though.&lt;/p&gt;

&lt;p&gt;I took the copy/paste process a little too literally with the Node.js installation and failed to follow the simple instructions (for reasons outlined above) and installed an out of date version instead of the latest LTS version. With a little help from tutor Jenny I was able to rectify that the next morning so it was no biggie.&lt;/p&gt;

&lt;p&gt;The whole SSH thing this was a bit of a headscratcher! I have used Git before I never had cause to explore its SSH functionality. The GitHub instructions are pretty comprehensive but, again, I found them difficult to follow. I got through it eventually and received the confirmation email from Github. Phew!&lt;/p&gt;

&lt;p&gt;So that was it for lecture 1! All done with time to spare. I needn't have got so stressed. Now the first lesson is behind me I should hopefully find it easier to relax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Study material
&lt;/h2&gt;

&lt;p&gt;The bootcamp material includes a comprehensive online study pack to work through between classroom sessions. Tutors estimate that you need to spend 14-20 hours per week studying on top of the 6 hours of classes so it's a hefty commitment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lecture 2
&lt;/h2&gt;

&lt;p&gt;Each lecture session kicks off with a stand up: an industry-standard practice where everybody gives a brief description of what they have been working on, what has gone well and where they have struggled. Miguel then delivered a more traditional style lecture covering the command line interface. After that we all installed a pimped-up replacement for the humble Mac terminal called Oh-My-zsh. It includes Git integrations that should come in handy.&lt;/p&gt;

&lt;p&gt;Next we worked through an introduction to Git with the tutors on hand to help out with any questions. They can take people into a Zoom breakout room to deal with questions if necessary so that the rest of the class isn't disturbed. It's a pretty smooth setup.&lt;/p&gt;

&lt;p&gt;I spent the rest of the week working through the remaining command line and Git study material which included a Git tutorial, updating the Manchester Codes Student Roster Git repository with our own details and solving a murder mystery using nothing but the command line. I particularly enjoyed that last project, it was great fun.&lt;/p&gt;

&lt;p&gt;So, after being so excited ... and nervous ... how did the first week go? I have to say that I absolutely loved it!&lt;/p&gt;

&lt;p&gt;I've played around with programming in an amateur fashion for a while, simply because I enjoy it. I have dabbled in Python &amp;amp; Django. I have learned the basics of HTML &amp;amp; CSS and a bit of PHP. I completed Harvard's CS50 introduction to computer science course earlier this year. I am painfully disillusioned with my current career and would like to take programming beyond the amateur level but reailse that I'm unlikely to get anywhere with it on my own. &lt;/p&gt;

&lt;p&gt;Manchester Codes has given me a routemap to follow, a curriculum that offers a solid grounding in modern technologies and, more importantly for me, a community that I am part of and a way into the world of professional programming. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Week one has left me energised and optimistic about the future, both pretty rare commodities in these uncertain times. Bring on the next 23 weeks!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cover photo from &lt;a href="https://unsplash.com/@bamagal"&gt;Unsplash by @bamagal&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>Manchester Codes - Software Engineer FastTrack</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Tue, 15 Sep 2020 20:41:20 +0000</pubDate>
      <link>https://dev.to/mattedwards/manchester-codes-software-engineer-fasttrack-3770</link>
      <guid>https://dev.to/mattedwards/manchester-codes-software-engineer-fasttrack-3770</guid>
      <description>&lt;h1&gt;
  
  
  Should I sign up?
&lt;/h1&gt;

&lt;p&gt;The Software Engineer FastTrack is described as a 24-week immersive part-time bootcamp. More about it here &lt;a href="https://www.manchestercodes.com/"&gt;https://www.manchestercodes.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's a kind of bootcamp that you can attend while still working fulltime, which I do - in a totally unrelated field.&lt;/p&gt;

&lt;p&gt;I'm really tempted. I have a fair amount of amateur experience in various aspects of software development amassed over a few years. I love programming - always have done - and would love to give it a try professionally. The course pretty much ticks the boxes for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why the question at the top of the page?
&lt;/h2&gt;

&lt;p&gt;I'm looking to completely change direction in my career. &lt;/p&gt;

&lt;p&gt;"So what?", I hear you say. "Loads of people have successfully done that!"&lt;/p&gt;

&lt;p&gt;Thing is, I'm 54 years old. I will be 55 a few days after the course finishes next year.&lt;/p&gt;

&lt;p&gt;So, my question to you ... the Dev Community ... is, will I be taken seriously? &lt;/p&gt;

&lt;p&gt;Is it possible for somebody of my age to make the switch?&lt;br&gt;
Do you know anybody who has? &lt;br&gt;
How would you react to a junior developer who's old enough to be your dad?&lt;/p&gt;

&lt;p&gt;In the brief time that I have been a part of this community I have learned that it is a supportive and welcoming one. If that weren't the case then I don't think I would even be contemplating this. But all things have their limits.&lt;/p&gt;

&lt;p&gt;I'm really excited to press the enrol button! &lt;/p&gt;

&lt;p&gt;At the same time I'm shitting my pants!&lt;/p&gt;

&lt;p&gt;What should I do?&lt;/p&gt;

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

</description>
      <category>beginners</category>
      <category>discuss</category>
    </item>
    <item>
      <title>First tango with Django? How did the plan go?</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Sat, 05 Sep 2020 22:09:37 +0000</pubDate>
      <link>https://dev.to/mattedwards/first-tango-with-django-how-did-the-plan-go-3oo9</link>
      <guid>https://dev.to/mattedwards/first-tango-with-django-how-did-the-plan-go-3oo9</guid>
      <description>&lt;p&gt;&lt;strong&gt;I have just finished my first Django application as my CS50x final project (Yay!). It's a meal planning and shopping list application. Thrilling eh?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Actually, I am pretty thrilled as it happens.&lt;/p&gt;

&lt;p&gt;It's taken the best part of a month to get from blank screen to deployment. And that's while working full time in a completely unrelated job. &lt;/p&gt;

&lt;h2&gt;
  
  
  Learning curve
&lt;/h2&gt;

&lt;p&gt;I knew very little about Django at the outset, having only worked through the obligatory 'Hello World' and a basic blog tutorial. Tutorials are great but it's only when you start building your own projects that you really learn the workings of a framework or language. On more than one occasion I thought I had bitten off more than I could chew and seriously considered jacking it all in. I'm glad that I persevered though: I'm pretty pleased with the finished article.&lt;/p&gt;

&lt;p&gt;Because of the nature of the application, I spent a lot of time wrestling with Django's form-handling capabilities. HTML forms are never easy to work with. Django's built-in Forms module has its own intricacies, but overall I think it greatly simplifies the process.&lt;/p&gt;

&lt;p&gt;I also spent some time getting my head around Django's database abstraction API. This is an &lt;a href="https://en.wikipedia.org/wiki/Object-relational_mapping"&gt;ORM&lt;/a&gt;  tool that hugely simplifies interaction with a database. For this project I stuck with the SQLite database that effectively comes bundled with Django.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, why did I choose to build it?
&lt;/h2&gt;

&lt;p&gt;My wife and I switched to a vegan diet almost a year ago. One of the immediate practical effects was that we started to cook a lot more of our own meals. Clearly, before you can cook a meal you need to have all of the ingredients to hand so cooking also means shopping for ingredients. Before you can go shopping you need a list of ingredients to buy.&lt;/p&gt;

&lt;p&gt;Sometimes(!) I actually enjoy cooking. But making a shopping list and going to the supermarket to buy the ingredients is a royal pain in the arse! So, having a brain that naturally leans towards problem-solving, I wondered if I could program something to remove a bit of the pain from the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, what does it do?
&lt;/h2&gt;

&lt;p&gt;The app is based on the fundamental premise that a shopping list is composed of two elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;items that you need to prepare the meals that you plan to eat over the coming days; and,&lt;/li&gt;
&lt;li&gt;general 'stock items' to replenish your cupboards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The app enables you to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;build a database of meals, each with a list of items that you need to buy to cook the meal; and,&lt;/li&gt;
&lt;li&gt;build a database of items generally, some of which are marked as favourites (ie. the things that buy regularly) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of the items held in the database is associated with a storage location (ie. where you store that kind of item at home) and a shop department (ie. where you find that item in the supermarket). The storage locations and shop departments can be sorted according to the order that you visit them when making a list (storage locations) and navigating the supermarket (shop depts.). The idea there is that the order you add items to your list is probably not the same as the order you put things in your basket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To compose a new list:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose the meals you plan to cook over the next few days. A list of items that you need is then created in storage location order. The list contains all of the items you will need to prepare the chosen meals.&lt;/li&gt;
&lt;li&gt;A list of the stock items (that you have marked as favourite) is then created automatically, again in storage location order.&lt;/li&gt;
&lt;li&gt;Those separate lists are then combined and presented to you with tick boxes so you can do the tour of your cupboards and tick the items that you need to buy. (You might have some meal ingredients already in stock. eg. the list might tell you that you need garlic for the curry that you chose in step 1 but when you look in the fridge you see that you have plenty of garlic so you don't tick it)&lt;/li&gt;
&lt;li&gt;Your final list is presented to you in shop department order (you having previously adjusted the order of departments according to your supermarket). As you put an item in your basket, tap its entry on the list and it is greyed-out to show you've got it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing touches
&lt;/h2&gt;

&lt;p&gt;I deployed the app, via Git Hub, to Python Anywhere, the service used by the Django Girls in their blog tutorial. They have a free 'beginner' account which supports SQLite databases. Heroku was another hosting option but it looks like they somehow purge their file system daily so SQLite, being a file-based database, is not an option there.&lt;/p&gt;

&lt;p&gt;I did a small amount of CSS styling. (If you look at the app you will see that visual design is not my strongest suit! Haha!) It's designed to work on mobile/tablet first and foremost; not many people take their laptop shopping. &lt;/p&gt;

&lt;p&gt;I also wrote a single Javascript function to handle the 'greying-out' of list items when tapped. I would like to make the storage location/shop department re-ordering a drag-and-drop activity but that's a Javascript task for another day - mostly because I don't know how!&lt;/p&gt;

&lt;p&gt;So, next steps for this app are: drag and drop, as mentioned in the last paragraph, and adding user accounts so that it can be personalised for different users.&lt;/p&gt;

&lt;p&gt;After almost giving up mid-project, I'm very pleased with the result of my first tango with Django. I would welcome any feedback; even if it's good feedback! You can see the finished article at: &lt;a href="http://shoplist.pythonanywhere.com"&gt;http://shoplist.pythonanywhere.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@dinakaran19?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Dina karan&lt;/a&gt; on &lt;a href="https://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>showdev</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Virtual environments - first things first</title>
      <dc:creator>Matt Edwards</dc:creator>
      <pubDate>Thu, 13 Aug 2020 07:05:14 +0000</pubDate>
      <link>https://dev.to/mattedwards/virtual-environments-first-things-first-21nc</link>
      <guid>https://dev.to/mattedwards/virtual-environments-first-things-first-21nc</guid>
      <description>&lt;p&gt;If you are aware of the idea of a virtual environment but you don't really understand the concept or you can't see how it applies to your circumstances, then this article is for you. Until quite recently, I was the same: the whole concept confused me and, to be honest, I didn't see the point.&lt;/p&gt;

&lt;p&gt;I have played around with various forms of computer code in an amateur fashion for more years than I care to remember. A few months ago - during the early stages of the coronavirus lockdown in the UK - I rediscovered my love of programming, reaquainted myself with Python and made the decision that programming in one form or another needed to become a much bigger part of my life.&lt;/p&gt;

&lt;p&gt;My Python journey led me to Django and it was only then that I fully appreciated the concept of a virtual environment and understood why every programming project should start with the creation of a one.&lt;/p&gt;

&lt;p&gt;By writing this article I hope to demystify the concept for programmers who either don't get it or can't see the point because I've been that person too!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So, what is a virtual environment?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;My son is 7 and he has just finished year 2 at school. The official end of term was just 2 or 3 weeks ago but he has spent only 2 days actually in school since March (4 months ago) because the school effectively closed due to covid-19. Those 2 days that he did spend in school were only possible because of a "bubble" system that the school introduced.&lt;/p&gt;

&lt;p&gt;My son was in a "bubble" with 14 other pupils from his class. During those 2 days at school he only interracted with people in his bubble. They sat together in class, ate together, and had break time together, all without interracting with anybody who was not in their bubble. The bubble system aimed to remove the risk of cross-infection between bubbles.&lt;/p&gt;

&lt;p&gt;Virtual environments are similar to the bubbles in my son's school. Your application only interracts with software packages in its own bubble. It is insulated from packages in any other bubble or, perhaps more importantly, packages at the system level.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is the issue that virtual environments address?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you wrote a Python program some time ago, before you ever heard of virtual environments. To build the program you used a couple of third-party Python packages that you installed specifically to do the job. When you installed those packages you will have installed them at the &lt;strong&gt;system level&lt;/strong&gt;. That's not a problem at that stage. You finished the program, it worked really well and you were pretty chuffed!&lt;/p&gt;

&lt;p&gt;A little while later you start work on another Python program on the same machine. Your new program uses one of the packages that you used in the first program and you have read about some whizz-bang new features that the latest version of that package offers. You like to live on the cutting edge of technology so you update that package to the latest version. You implement those awesome new features into your new Python program and you are doubly chuffed when it's finished. Well done you!&lt;/p&gt;

&lt;p&gt;A few days later you try to run your first Python program only to discover that the new version of that package that you installed (at the system level remember) no longer supports some of the features that your first program uses. Your first Python program no longer runs. How frustrating!&lt;/p&gt;

&lt;p&gt;Imagine if that was a live application with multiple users who might even have paid you money for the privilege of using it. Somewhat more than frustrating!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So how do virtual environments help?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine a different scenario where the first thing you did when you started work on Amazing-Python-Project-1 was to set up a &lt;strong&gt;virtual environment&lt;/strong&gt; - a figurative bubble in which your application could live, insulated from other applications and installations on your machine - and you then installed the two python packages &lt;em&gt;into that same virtual environment&lt;/em&gt; and not at a system level.&lt;/p&gt;

&lt;p&gt;When you move on to Amazing-Python-Project-2, in this scenario you would again set up a dedicated virtual environment for that program, and install the packages, &lt;em&gt;including the latest whizz bang version&lt;/em&gt;, into that virtual environment. Those package installatons &lt;em&gt;have no effect on the first virtual environment&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The two programs, and all of the packages required to run them, co-exist independently in their respective bubbles. The updated packages in the second virtual environment have no impact whatsoever on the program and associated packages in the first virtual environment. &lt;/p&gt;

&lt;p&gt;Amazing-Python-Project-1 &amp;amp; Amazing-Python-Project-2 can both still run on the same machine. Your users will be unaffected, remain blissfully happy and continue to hand over their cash.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;That sounds pretty amazing! But ... how do I do it??&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Confusion reigned once again for me at the point of implementaton. I had read a few different books and tutorials offering advice on setting up your Django application - remember, it was Django that got me embroiled in the whole virtual environment thing in the first place - and it turns out that there's more than one way to skin the virtual environment cat. The author of each book or tutorial seemed to favour a different method.&lt;/p&gt;

&lt;p&gt;There are third-party "virtual environment management" packages that you have to install on your machine, counterintuitively at system level, or there is a package which has been part of the standard Python library since version 3.4.&lt;/p&gt;

&lt;p&gt;After playing around with the different methods ... and getting thoroughly confused and frustrated in the process ... I decided if something is part of the standard Python library then it makes sense to use it. &lt;/p&gt;

&lt;p&gt;It's a package called &lt;code&gt;venv&lt;/code&gt;. If you too are new to virtual environments, my advice is just stick with that: why introduce further complication when all the functionality you need is already there within Python? You can always try out different methodologies when you're comfortable with the whole concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Implementation steps&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ok, we've decided on the tool for the lob: &lt;code&gt;venv&lt;/code&gt;. The following steps show how to set up a Python virtual environment on a Mac or Linux in readiness for the start of a new Django project which will live in a new directory called &lt;code&gt;my_project&lt;/code&gt;. The virtual environment also needs a name (because it gets its own dedicated directory); let's call it &lt;code&gt;myvenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a Terminal window, navigate to the directory where you plan to put the  &lt;code&gt;my_project&lt;/code&gt; directory and type...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir my_project
$ cd my_project
$ python3 -m venv myvenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first 2 lines should be familiar Bash commands. A little explanation is probably in order for line 3. Firstly, I have to add the number 3 to the end of Python because I am using a 6 year old Macbook came with Python version 2 installed as default. I always have to explicitly tell the Python interpreter that I want to use version 3.&lt;/p&gt;

&lt;p&gt;If you habitually type &lt;code&gt;python3&lt;/code&gt; to activate the Python shell then you need to add the 3 here too. If not, then simply &lt;code&gt;python -m venv myvenv&lt;/code&gt; will work.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;venv&lt;/code&gt; is the name of the virtual environment package that comes pre-installed with Python since version 3.4 and &lt;code&gt;myvenv&lt;/code&gt; is the name that we chose to give to the virtual environment.&lt;/p&gt;

&lt;p&gt;When the command on line 3 above is run, a new directory called &lt;code&gt;myvenv&lt;/code&gt; is created within the &lt;code&gt;my_project&lt;/code&gt; directory. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;myvenv&lt;/code&gt; directory contains files that &lt;code&gt;venv&lt;/code&gt; will use to manage your virtual environment and which will contain files for the packages that are subsequently installed into the virtual environment. That is how those packages are kept within the "bubble". Ingenious eh?&lt;/p&gt;

&lt;p&gt;So that has created your new virtual environment called &lt;code&gt;myvenv&lt;/code&gt;. Before we install any packages we have to activate it with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ source myvenv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will now notice that your Terminal prompt is prefixed with the name of your virtual environment in parentheses.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;We can now install Django into our virtual environment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ pip install Django~=3.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running that command look in &lt;code&gt;myvenv/lib/python3.8/site-packages&lt;/code&gt; and you will see that the latest incarnation of Django version 3 has been installed. And it's safe within your virtual environment untouched by anything outside that "bubble".&lt;/p&gt;

&lt;p&gt;You can also run the &lt;code&gt;pip list&lt;/code&gt; command in the Terminal...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ pip list
Package    Version
---------- -------
asgiref    3.2.10 
Django     3.1    
pip        19.2.3 
pytz       2020.1 
setuptools 41.2.0 
sqlparse   0.3.1  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example shown here, I have installed Django 3.1 and there are some other default packages already installed. &lt;/p&gt;

&lt;p&gt;Any other packages that you need for your program should be installed the same way - &lt;code&gt;pip install...&lt;/code&gt; into the active virtual environment.&lt;/p&gt;

&lt;p&gt;And now for the cherry on the top: &lt;code&gt;requirements.txt&lt;/code&gt;. This is a simple text file that keeps track of the various packages that you have installed into the virtual environment. When your program is complete, &lt;code&gt;requirements.txt&lt;/code&gt; will contain a blueprint of the specific versions of all of the packages that your program needs in order to run. You can therefore run your program in a virtual environment on another machine by passing that virtual environment a copy of &lt;code&gt;requirements.txt&lt;/code&gt; to use as a recipe to concoct the precise mix of packages required. More on that later.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So how do you create the requirements.txt file?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is another &lt;code&gt;pip&lt;/code&gt; command: &lt;code&gt;pip freeze&lt;/code&gt;. Running that in the Terminal will list all third-party packages and version numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ pip freeze
asgiref==3.2.10
Django==3.1
pytz==2020.1
sqlparse==0.3.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All you need to do now is add a suffix to that same command to instruct it to write the list to file instead of to screen...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now have a file called requirements.txt that contains precisely the same list as was printed to screen by the &lt;code&gt;pip freeze&lt;/code&gt; command without the suffix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt; When you run that command you need to make sure you are in the project root directory - i.e. &lt;code&gt;my_project&lt;/code&gt; in this example - because &lt;code&gt;requirements.txt&lt;/code&gt; will be created in whatever directory the &lt;code&gt;pip freeze&lt;/code&gt; command was run in.&lt;/p&gt;

&lt;p&gt;So there you have it. All of the packages that your program needs, safe within their own bubble which has a list of contents on the outside in the form of &lt;code&gt;requirements.txt&lt;/code&gt;. Every time you install a new package to use in your program, run the command &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt; and your "contents list" is updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Take your new requirements.txt file for a test drive&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can really get a feel for how useful &lt;code&gt;requirements.txt&lt;/code&gt; is by creating a new virtual environment in a new diectory - say &lt;code&gt;my_other_project&lt;/code&gt; - by following the instructions above. You can give the virtual environment the same name - &lt;code&gt;myvenv&lt;/code&gt; - if you like; that's up to you.&lt;/p&gt;

&lt;p&gt;Then, before installing anything, copy the &lt;code&gt;requirements.txt&lt;/code&gt; file that we created above in &lt;code&gt;my_project&lt;/code&gt; into your &lt;code&gt;my_other_project&lt;/code&gt; directory. It needs to sit in that root directory, alongside &lt;code&gt;myvenv&lt;/code&gt; (assuming you chose the same virtual environment name).&lt;/p&gt;

&lt;p&gt;Then, with your Terminal in the &lt;code&gt;my_other_project&lt;/code&gt; directory, and the virtual environment activated, run...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait for the magic to happen, then run &lt;code&gt;pip freeze&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;(myvenv)$ pip freeze
asgiref==3.2.10
Django==3.1
pytz==2020.1
sqlparse==0.3.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you get exactly the same output as in the other project. The environment has been precisely recreated.&lt;/p&gt;

&lt;p&gt;Magic eh?&lt;/p&gt;

&lt;p&gt;When you have finished working on your project, use the command &lt;code&gt;deactivate&lt;/code&gt; in the Terminal to ... well ... deactivate the virtual environment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(myvenv)$ deactivate
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After reading this, I hope that you now have a better understanding of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;virtual environments in general;&lt;/li&gt;
&lt;li&gt;the reason they are a good idea;&lt;/li&gt;
&lt;li&gt;and how to set one up for your next Python project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Image credit: &lt;span&gt;Photo by &lt;a href="https://unsplash.com/@lanju_fotografie?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Lanju Fotografie&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/bubble?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
