<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Index Zero - Medium]]></title>
        <description><![CDATA[Concepts of computer science explained with simple words and simple pictures - Medium]]></description>
        <link>https://medium.com/index-zero?source=rss----6a1e34bab720---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Index Zero - Medium</title>
            <link>https://medium.com/index-zero?source=rss----6a1e34bab720---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 23 Jun 2026 12:15:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/index-zero" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[A Simple Explanation of OOP]]></title>
            <link>https://medium.com/index-zero/a-simple-explanation-of-oop-ed30dc800e05?source=rss----6a1e34bab720---4</link>
            <guid isPermaLink="false">https://medium.com/p/ed30dc800e05</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Caleb Winston]]></dc:creator>
            <pubDate>Sat, 25 May 2019 17:26:49 GMT</pubDate>
            <atom:updated>2019-05-25T17:25:36.999Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u82GCIqF5FkOp3-qs_EP8Q.png" /><figcaption>Types</figcaption></figure><p>You already know how you can declare variables and assign values to them.</p><p>For example, you can declare a variable called x.</p><pre>int x;</pre><p>And you can assign the value 5 to the variable x .</p><pre>x = 5;</pre><p>You can also declare a variable called y.</p><pre>String y;</pre><p>And you can assign the value &quot;hello world&quot; to the variable y .</p><pre>y = &quot;hello world&quot;;</pre><p>The values you assigned to these two variables were of different types.</p><p>The variable x was assigned a value of 5 , which is of type int.</p><p>The variable y was assigned a value of hello world , which is of type String.</p><p>Both of these types are defined in the Java language.</p><p>But the Java language has a way for <em>you</em> to define types as well.</p><p>You can define the structure of values of type Car by defining a <em>class</em>.</p><pre>class Car {<br>    int mileage;<br>    String name;<br>}</pre><p>You can then declare a variable of type Car .</p><pre>Car mySedan;</pre><p>You can then assign a value to the mySedan variable.</p><pre>mySedan = new Car();</pre><p>And that’s object-oriented programming (OOP) — creating classes that define a type of values.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed30dc800e05" width="1" height="1" alt=""><hr><p><a href="https://medium.com/index-zero/a-simple-explanation-of-oop-ed30dc800e05">A Simple Explanation of OOP</a> was originally published in <a href="https://medium.com/index-zero">Index Zero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To-do Lists]]></title>
            <link>https://medium.com/index-zero/to-do-lists-1ebcf7dc7c36?source=rss----6a1e34bab720---4</link>
            <guid isPermaLink="false">https://medium.com/p/1ebcf7dc7c36</guid>
            <category><![CDATA[comics]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[index-out-of-bound]]></category>
            <dc:creator><![CDATA[Caleb Winston]]></dc:creator>
            <pubDate>Tue, 09 Oct 2018 16:01:32 GMT</pubDate>
            <atom:updated>2018-10-09T16:12:21.095Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_hg35ziDUjOAg7NB4MzQGQ.png" /></figure><p><em>Index Out of Bounds ~ October 2018 ~ More stuff by me can be found on my website at </em><a href="https://calebwin.github.io"><em>calebwin.github.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ebcf7dc7c36" width="1" height="1" alt=""><hr><p><a href="https://medium.com/index-zero/to-do-lists-1ebcf7dc7c36">To-do Lists</a> was originally published in <a href="https://medium.com/index-zero">Index Zero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Heaps]]></title>
            <link>https://medium.com/index-zero/a-guide-to-heaps-d4a347408d?source=rss----6a1e34bab720---4</link>
            <guid isPermaLink="false">https://medium.com/p/d4a347408d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[index-zero]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Caleb Winston]]></dc:creator>
            <pubDate>Wed, 03 Oct 2018 15:16:19 GMT</pubDate>
            <atom:updated>2018-10-04T18:46:57.984Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rovQ5oTLGIcjBvIX3yZMZw.png" /><figcaption>A heap of data</figcaption></figure><p>Almost every data structure ever invented was designed for doing specific things really well. For example, lists are really good for storing stuff that’s ordered. Sets are really good for storing stuff where we <em>don’t</em> care about order. Similarly, heaps are really good at storing stuff where the only thing we care about is the one with the highest <em>priority</em>.</p><p>For example, let’s say we want to store data for a bunch of tasks on a to-do list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KyXS2KEOa1Lb6JYW0ARsLg.png" /><figcaption>Each task has a priority attached to it.A to-do list</figcaption></figure><p>And we have a priority attached to each task…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IB5WHLa6ax9grmUtDSlh0w.png" /><figcaption>A to-do list with priorities</figcaption></figure><p>And we <em>really</em> want to be able to quickly get the task with highest priority at any moment… What data structure could we use?</p><p>As it turns out, there’s one data structure that is perfect for doing this — heaps.</p><h4>What heaps do</h4><p>Like sets, heaps don’t store data in a strict order. The only order they maintain is one element with the highest priority and all other elements with other priorities beneath it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KckZg7GXiI_2YRg5nlJqHg.png" /><figcaption>How heaps store data</figcaption></figure><p>And heaps really only have three main operations.</p><ul><li>Add an element to the heap with a certain priority</li><li>Get the element with highest priority</li><li>Remove the element with highest priority</li></ul><p>As you can see, none of these operations do anything with any of the elements with less than the highest priority.</p><p>So you might be wondering — what happens when we remove the highest element? and what happens to the current highest element when we add a new element with higher priority?</p><p>Well, removing the top element simply brings the next highest element to the top of the heap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3tSObp7TA6CjIlVUOkshFA.png" /><figcaption>Removing element with highest priority from a heap</figcaption></figure><p>And adding a new element with highest priority will bury the previous element with highest priority.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nlKcyMJIAcjOfeiaZmKS-A.png" /><figcaption>Adding new element with highest priority to a heap</figcaption></figure><h4>How to implement heaps (with trees)</h4><p>So how can we structure our heaps? Well, a tree-like structure turns out to work pretty well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A-VRHy09KZmOl4-6FSE61Q.png" /><figcaption>A tree we can use to store a heaps elements</figcaption></figure><p>The above tree has a a couple properties that make it perfect for implementing a heap.</p><ul><li>Every element node has a priority greater than each of its children nodes</li><li>Every row of the heap must be filled as much as possible from left to right</li></ul><p>As long as we can maintain a tree with these two properties satisfied, we can implement an efficient heap that can have elements added to and removed from.</p><p>Let’s take a look at implementing such a tree by first looking at how we would implement addition to the heap.</p><h4>How to implement addition to heaps (with trees)</h4><p>The simplest way of doing this is to add an element all the way at the end of the bottom-most row of the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WQqKUcwRuX5w6DqIz3Fx5w.png" /><figcaption>Adding a new element with priority 9 to the end of the bottom-most row</figcaption></figure><p>As you can see, it certainly satisfies the property that our tree must be filled completely on each row from left to right. If we put our new element anywhere else, this property would no longer be true.</p><p>However, notice that the other property is <em>not</em> satisfied — the property that each element node must have a greater priority than all of its children nodes. The element node with priority 7 is <em>not </em>less than its new child with priority 9.</p><p>The simple solution to this problem is that whenever we add a new element at the end of the tree, we repeatedly swap the new element with its <em>parent </em>element until its parent element does indeed have a greater priority than the new element.</p><p>So in this case, we first check if the new element with priority 9 is less than its parent node with priority 7. It’s not, so we swap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c8ZtWurEvCFx5OhBueGOoA.png" /><figcaption>Swapping the elements with priorities 9 and 7</figcaption></figure><p>Next, we check if the new element with priority 9 is less than its parent node with priority 10. It is, so we’re done — we have added a new element and satisfied both properties of our tree.</p><p>So adding new elements to our heap requires us to do two things in our tree —</p><ol><li>Add a new node with the new element at the end of the bottom-most row</li><li>Repeatedly swap the new element with its parent node as long as it has a greater priority than its parent</li></ol><h4>How to implement removal from heaps (with trees)</h4><p>Heaps have another really important operation they need to be able to do — removing the element with highest priority.</p><p>There happens to be a really simple way of removing the top-most elements from trees while preserving both of our heap properties.</p><ol><li>Swap the top-most node with the node at the end of the bottom-most row</li><li>Remove the node at the end of the bottom-most row</li><li>Repeatedly swap the new top-most node with the child with the greatest priority as long as that child has a greater priority than the node we’re swapping.</li></ol><p>Let’s look at an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c8ZtWurEvCFx5OhBueGOoA.png" /><figcaption>A tree we want to remove the top-most element from</figcaption></figure><p>Let’s say we want to remove the top-most node with priority of 10. We can first swap it with the last node on the bottom-most level, the one with priority 7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YmRxUBrmej6i_wy-magLeA.png" /><figcaption>The same tree with the top-most element swapped with the bottom-most element</figcaption></figure><p>Our element with highest priority is now at the end at the bottom, so we can easily remove the node without losing the property that every row is filled as completely as possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uxVTlYiWoVi6p7d5oGbV2Q.png" /><figcaption>The same tree with the element with highest priority removed</figcaption></figure><p>Now we have the element with highest priority removed. But as you can probably see, we <em>have</em> lost the other property that each node must have a higher priority than all of its children. The new node at the top of the tree no longer has a greater priority than <em>all </em>of its children.</p><p>We can solve this problem by repeatedly swapping the top-most element with its child with greatest priority as long as that child has a greater priority than the element we’re swapping.</p><p>So first we look at the children of the element with priority 7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H4vfJaGd5ANKDZUP8FvCNg.png" /><figcaption>The children of the new top-most element</figcaption></figure><p>Well, the child with largest priority is the one with priority 9 and that is greater than the priority of the top-most element (7). So we swap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kZ63ZOhf-7OQLf8Yzr4h5Q.png" /><figcaption>The top-most element swapped with its child with greater priority</figcaption></figure><p>Now we have swapped the top-most element with one of its children, we take a look at its new set of children at this new position in the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nytu2eWudIbHAAz5qVsVpg.png" /><figcaption>The children of the element at its new position in the tree</figcaption></figure><p>If we look at the children of the element now, we can see that none of them have a greater priority than the element. So the element is now in its correct place in the tree and both properties are satisfied.</p><p>Now lets see how we can implement this 3-step process for removal and the 2-step process for addition we looked at earlier with an array.</p><h4>How to implement heaps (with arrays)</h4><p>We’ve looked at how we could implement heaps fairly easily with trees. It so happens that we can implement these “heap trees” fairly easily with a simple array.</p><p>We can have an array that stores each element left to right and top to bottom in that order.</p><p>For example, let’s take a look at the following tree representation of a heap —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kZ63ZOhf-7OQLf8Yzr4h5Q.png" /><figcaption>A tree representation of a heap</figcaption></figure><p>We can store the data for this tree in the following array —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t-PlHXzbWZJtfPBTL-FCeg.png" /><figcaption>An array to store the data of our tree representation</figcaption></figure><p>As you can see, we simply order the elements by how they appeared in our tree from left to right, top to bottom.</p><p>Let’s see how we can realistically implement addition and removal from heaps with arrays.</p><h4>How to implement addition to heaps (with arrays)</h4><p>If we want to add a new element to a heap, the first thing we do is add the new element to the end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nAzgGaplhOubAx7MDwa1jw.png" /><figcaption>A new element added at the end with priority 8</figcaption></figure><p>But the end might not be the correct spot for this new element with priority 8 to be. To get the element to its correct spot, we should first look at its parents.</p><p>If we know the index we added the new element at (in this case it’s 6), we can quite easily find its parent element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d6Rk081s1CfA1Kw2QRG03w.png" /><figcaption>Finding the parent element of the new element we just added</figcaption></figure><p>If we take the index of the new element, subtract 1, divide by the number of children of each node (3 in this case), and truncate the answer (remove everything after the decimal point), we get the index of the parent.</p><p>All we have to do then is compare the index of the new element we just added and its parent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RYx4r8NDMipFyI-yVIZmCg.png" /><figcaption>Comparing the priority of the new element we just added with the priority of its parent element</figcaption></figure><p>Since our new element has a priority of 8 while its parent has a priority of only 7, we swap them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ef-Fd3dqxsXlfNguyDUuQw.png" /><figcaption>The new element swapped with its parent with smaller priority</figcaption></figure><p>We now look at the new element’s new parent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*INjyGqg5wIejy-1G-kLakA.png" /><figcaption>The parent of the new element at its new position</figcaption></figure><p>But now we can see that its parent actually <em>does</em> have a greater priority as it should. So we <em>don’t </em>need to swap and we leave our new element as it is.</p><p>As you can see adding new element to our “heap array” is really just the same two-step process we had discussed earlier —</p><ol><li>Add a new node with the new element at the end of the bottom-most row (now that’s just the end of the array)</li><li>Repeatedly swap the new element with its parent node (the element at position <em>(n-1)/3</em> where n is the current index of new element) as long as it has a greater priority than its parent.</li></ol><h4>How to implement removal from heaps (with arrays)</h4><p>Removing the top element from our heap is also pretty straightforward with arrays. The first thing we do is swap it with the last element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9wDB4YqxXW40gMn990cO9Q.png" /><figcaption>The top-most element swapped with the last element</figcaption></figure><p>Now we can safely remove the element from the end of the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kr1Wdp-E6q2upk3bDUh71w.png" /><figcaption>The (former) top-most element removed</figcaption></figure><p>But now we have a bit of a problem. We’ve safely removed the element with priority 9, but remember the element we swapped it with? the one with priority 7? That’s now in the wrong place.</p><p>This can be fixed by repeatedly swapping the element with its greatest priority child as long as the child has a greater priority than the element.</p><p>We first find its children using the index of the top-most element (initially it’s 0). We simply multiply the current index by the number of children of each node (in our case, that’s 3), then add 1, 2, or 3 to get the indices of the element’s children.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RywbCNlxbCmf1sC7VeTYJw.png" /><figcaption>The children of the top-most element</figcaption></figure><p>If we look at the children, we can find the one with highest priority.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KJSa6Doi6TIhYqjjcTI3dA.png" /><figcaption>The priorities of the children of the top-most element</figcaption></figure><p>The one with priority 8 is the largest and its priority is also larger than the current top-most element (7), so we need to swap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mDbj9s5ktlcBLGN0Ejk1gw.png" /><figcaption>The top-most element swapped with its largest child</figcaption></figure><p>We can now look at the children of the element in its new position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kQYekAIaIzdHDSSFgLPYYg.png" /><figcaption>The children of the top-most element in its new position</figcaption></figure><p>Since none of its children in its new position have a larger priority than it, we can stop.</p><p>Once again, we can see that the process of removing an element from a heap is not really any different from the process we had discussed earlier —</p><ol><li>Swap the top-most node with the node at the end of the bottom-most row</li><li>Remove the node at the end of the bottom-most row</li><li>Repeatedly swap the new top-most node with the child with the greatest priority as long as that child has a greater priority than the node we’re swapping.</li></ol><p>So there you have it! 👏 Heaps are a simple data structure that are really good at storing data where the only thing you care about is the element with highest priority. Now you know what they are and now you know how to implement them! 🙂</p><p><em>If you enjoyed reading this article, you can find more articles by me and projects I’m working on (such as </em><a href="https://github.com/calebwin/margin"><em>this Markov chain compiler</em></a><em>) at </em><a href="http://calebwin.github.io"><em>calebwin.github.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4a347408d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/index-zero/a-guide-to-heaps-d4a347408d">On Heaps</a> was originally published in <a href="https://medium.com/index-zero">Index Zero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Disjoint Sets]]></title>
            <link>https://medium.com/index-zero/a-guide-to-disjoint-sets-172ba7efc52b?source=rss----6a1e34bab720---4</link>
            <guid isPermaLink="false">https://medium.com/p/172ba7efc52b</guid>
            <category><![CDATA[index-zero]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Caleb Winston]]></dc:creator>
            <pubDate>Mon, 17 Sep 2018 14:44:32 GMT</pubDate>
            <atom:updated>2018-10-04T18:46:45.728Z</atom:updated>
            <content:encoded><![CDATA[<p>Maybe you’ve heard about merge-find sets, or union-find forests, or perhaps disjoint sets. Surely such a pedantic name is that of a correspondly complicated data structure… right? 🤨</p><p>Well… not really — conceiving a data structure that can store disjoint sets is not actually all that hard.</p><h4>What are sets?</h4><p>Before we take a look at disjoint sets, what exactly are <em>sets</em>? A set is really just a collection of elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E-QVaiKPKtUgq_VNbAIcLw.png" /><figcaption>A visual representation of a single set with three elements</figcaption></figure><p>As you can see from the visualization above, sets don’t maintain an order of elements. We can perform pretty much any operation you can perform on a set of data — addition, deletion, size, etc. — except for operations that require an order of the elements to be known.</p><p>For example, a few things sets can’t do are inserting at an “index”, popping, or pushing elements, etc.</p><p>Another interesting thing about sets — what happens when we add an element to a set that already exists in it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TXk5ogVcg-UMeiw6N665_g.png" /><figcaption>What should happen when we add a duplicate element</figcaption></figure><p>That’s right — we simply replace the element that was already there; we don’t do anything.</p><p>There’s a simple reason for why sets don’t store duplicates. In other data structures such as lists and dictionaries, every element was different in at least one way. Even if two entries had the same value, they had to have different indices or keys. But in a set, we’re just storing the elements by themselves in no particular order. So we can’t distinguish a 3 we add in first from a 3 we add in later.</p><h4>What are disjoint sets?</h4><p>Now that you know what sets are, a group of disjoint sets is really just a bunch of sets where each set has a representative element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pNfJC9jrICDLirA08rmEcw.png" /><figcaption>A bunch of disjoint sets and their representative elements</figcaption></figure><p>That’s really all a data structure holding disjoint sets needs to keep track of — a bunch of elements and the representative element of the set they belong to.</p><p>The operations you can perform on disjoint sets are also quite simple -</p><ul><li>Make a new set with a new representative element</li><li>Find the representative element of the set an element belongs to</li><li>Union the sets two elements belong to</li></ul><p>The first operation is making a new disjoint set and putting an element in it. This element will be its representative element. This is shown in the below visualization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mt9jiFPNPKtFYk1y9d6t4A.png" /><figcaption>Making a new set with the element 4</figcaption></figure><p>The second operation is finding the representative element of the set an element belongs to. In a previous picture, we saw that the representative element of the set with {5, 3, 2} is 5. So the representative element of 3 is 5 because 3 is in the set which has 5 as its representative element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XTl8Z1RvbWNf0RtGuqkCKw.png" /><figcaption>Finding the representative element of the set that contains 4</figcaption></figure><p>The third operation is taking the union of the sets that contain two elements. So what’s a union? A union of two sets is what we get when we take all the elements of one set and all the elements of the other set and put them together into the same set. Here’s what that looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_o91eYQHubsx8Y_Pg1_mEA.png" /><figcaption>Taking the union of two sets</figcaption></figure><p>As you can see, we pretty much just took the elements in both sets and put them together in the same set. However there are two other things you should notice.</p><ol><li>We kept only one 3 even though there was 1 in <em>both </em>sets. This is something we have to do be because, as we mentioned earlier, we can’t distinguish the 3 from the first set from the 3 from the other set. So instead, we just keep one of them.</li><li>We also had to pick a new representative element for the new set. Naturally, we picked one of two representative elements of the original set. But why did we pick the 9 and not the 5? The answer is that it doesn’t really matter that much which one we pick. Depending on how we implement our disjoint set data structure, we might choose to pick either one.</li></ol><p>Now that we’ve seen the two things we need to keep track of for disjoint sets (all the elements, the representative elements of the sets they belong to) and the three operations we need to be able to perform (make a new set, find a representative element, and union two sets). Let’s move on to how we would actually implement these sets.</p><h4>How to implement disjoint sets</h4><p>So how exactly do we implement a data structure that stores disjoint sets?</p><p>One way we can store disjoint sets is with a tree-like structure. For example we can represent the following disjoint sets as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAjbJeoIIEddB-y9n719Iw.png" /><figcaption>Two disjoint sets represented by trees.</figcaption></figure><p>If we use some sort of tree-like structure, all we have to do to make a set is create a new “tree” with the representative element of the new set at the root or the top of the tree. To find the representative element of the set an element belongs to, we simply have to follow the element up the tree till we get to the representative element at the top. To take the union of two sets we simply have to make the root of one set’s tree point to the root of other other set’s tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X6XqTOVp3kMr_z0rMtoWkQ.png" /><figcaption>Taking the union of two sets</figcaption></figure><p>So it looks like trees are a great way of representing disjoint sets. We can store everything we want to and perform the operations we want to. But what do we actually use to store the trees?</p><p>As it turns out arrays can be used quite effectively to store these “disjoint set trees”. We can have an array and each element can correspond to an index in our array and the parent of that element in the tree can correspond to the element at that index in our array. Here’s what that would look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XYnjNxqm6-C_YqN4gR_7Hw.png" /><figcaption>An array that stores the tree representation of the disjoint set(s)</figcaption></figure><p>As you can see the elements at indices 2, 3, and 7 represented the parents of elements 2, 3, and 7 in the disjoint set tree. They all had a value of 5 because that’s what their parent element is in the tree.</p><p>And you can also notice that the element at index 5 was a -1. When we are trying to find the representative element of a set an element belongs to, we can keep moving to the parent of the current element till we get to one with a value of -1. Then we can know to stop because the current element’s index <em>is </em>the representative element.</p><p>So just to summarize — here’s how we can perform our three operations on disjoint sets using an array.</p><ul><li>We can make a new set by simply adding an element to the array at the index of the value we want the set’s representative element to have.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ASUBwVhWcoSyT9MIZ9KujA.png" /><figcaption>Making a new disjoint set with a representative element of 9</figcaption></figure><ul><li>We can find the representative element of the set an element belongs to by repeatedly moving a pointer to the parent element of the original element.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-SZbV3xD3JNx0eDyu-drzg.png" /><figcaption>Finding an element in our disjoint sets using our array</figcaption></figure><ul><li>Lastly, we can also take the union of two sets by simply setting the parent value of the representative element of one to set to the value of the representative element of the other set.</li></ul><h4>How to make disjoint sets work for stuff other than number</h4><p>Up to this point, we’ve just been looking at disjoint sets where all the elements are numbers. However, it is possible for our disjoint set data structure to support any type of data. The simplest solution is to maintain a dictionary that maps data in the type we want to corresponding numbers in our disjoint sets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f4rqgFXtcF-BS1N4pmfLPA.png" /><figcaption>A dictionary that can map from strings to integer indices in our disjoint set array</figcaption></figure><h4>How to optimize our disjoint set data structure</h4><p>There are actually a few simple optimizations we can do to speed up the operations our disjoint set data structure can do. We’re going to take a look at the two most common ones.</p><p>The first is union-by-height. It turns out that even though we <em>can </em>randomly choose which element to choose as the representative element of the union of two sets, by making the element from the set with a taller tree, we can increase performance.</p><p>The second is path compression. When we try to find the representative element of an element’s set, the representative element we return in the end is technically the representative element of every element we come across. So, we can modify our find function to simply set the parent of each element we come across to the representative element we ultimately find.</p><p>So that’s the disjoint set data structures. 👏 Disjoint sets aren’t as “general-purpose” ase other data structures out there but they can be very useful in specific and now you know how to implement them! 👍</p><p><em>(If you want to take a look at a coded implementation for one, here is </em><a href="https://github.com/calebwin/disjoint"><em>one I did in sixty lines in the Lua programming language</em></a><em>)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=172ba7efc52b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/index-zero/a-guide-to-disjoint-sets-172ba7efc52b">On Disjoint Sets</a> was originally published in <a href="https://medium.com/index-zero">Index Zero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Bags]]></title>
            <link>https://medium.com/index-zero/a-guide-to-bags-f583799c2d5b?source=rss----6a1e34bab720---4</link>
            <guid isPermaLink="false">https://medium.com/p/f583799c2d5b</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[index-zero]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Caleb Winston]]></dc:creator>
            <pubDate>Mon, 10 Sep 2018 15:49:42 GMT</pubDate>
            <atom:updated>2018-10-04T18:46:26.984Z</atom:updated>
            <content:encoded><![CDATA[<h4>The data structure, that is</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xnTyJ8sXLaEvf6QJJdjVIw.png" /><figcaption>A bag found in the wild</figcaption></figure><p>There are many data structures out there with various complex implementations. Each one has its advantages and disadvantages. Of all data structures, however, there’s one that’s probably the simplest way of storing data and super easy to implement — the bag. That’s what we’ll be focusing on in this article. Let’s go 🚀</p><h4>But what’s a data structure?</h4><p>So first of all, what exactly <em>are</em> data structures. If you take a look at the Wikipedia page for data structure, this is the definition you get —</p><blockquote>A <strong>data structure</strong> is a data organization, management and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.</blockquote><p>If this seems a bit complex, here’s a slightly simpler definition that I think can work well for our purposes —</p><blockquote>A <strong>data structure</strong> is a format for structures that store data</blockquote><p>That’s really all a data structure is. A simple example of one is a string. Strings are structures that store data — more specifically, they store characters in a linear seqence. In Java, we can create a string as follows —</p><pre>String s = new String(&quot;Hello World!&quot;);<br>// or<br>String s = &quot;Hello World!&quot;;</pre><p>Let’s say we want a data structure that stores a linear sequence of <em>anything </em>— characters, integers, booleans, even strings. We call these kind of data structures <em>generic</em>. They are data structures that can store data of any type.</p><p>There are many linear data structures that store sequences of stuff, but we’re here to talk about a data structure that stores stuff in no particular sequence.</p><p>That’s right — we’re talking about bags!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aMZB7PPC04x4du4z3kZmHg.png" /><figcaption>A representation of an empty bag</figcaption></figure><h4>What are bags?</h4><p>Most implementations of the bag data structure work quite simply.</p><p>Once you create one, pretty much all you can do is add an element, check if an element is in the bag, find the size, and check if the bag is empty.</p><p>Here’s what adding would look like in our visual representation —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gvstn3wNaWYFRBmbrnu_LQ.png" /><figcaption>Adding a new element to the bag</figcaption></figure><p>Here’s what our bag might look like after several additions —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vIBKpJxrqYP1K_75l9nejg.png" /><figcaption>The same bag with more elements added</figcaption></figure><p>As you can see, these elements don’t keep any order. Unlike strings, for example, bags don’t store elements of data in a sequence.</p><p>So there are really only two things bags need to keep track of for the four operations mentioned above to be possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8melS-SZOQa1PmZxDCwH7A.png" /><figcaption>Everything a bag implementation needs to keep track of and be able to do</figcaption></figure><h4>How to implement bags</h4><p>So now that you know what bags are supposed to keep track of and what they are supposed to be able to do, let’s take a look at how we would go about implementing one.</p><p>First things first — we need to keep track of the stuff in the bag, and the size of the bag. As it turns out, a simple array will work fine for internally keeping track of what’s in our bag and we can use an integer for its size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0B52yRfq_WEeKW9-X-9lBw.png" /><figcaption>What we can use to store the stuff we need to keep track of</figcaption></figure><p>Now to finish our implementation of the bag data structure, we just have four operations we need to implement.</p><ul><li>Add an element to the bag</li><li>Check if an element is in the bag</li><li>Get the size of the bag</li><li>Check if the bag is empty</li></ul><p>At this point, the implementations of three of these operations should be pretty self-explanatory.</p><ul><li>To check if an element is in the bag, we can just iterate over our array with a for loop and check if the element matches any of the elements we iterate through.</li><li>To get the size of the bag, we simply have to return the size variable we are keeping track of.</li><li>To check if the bag is empty, all we have to do is check if the size variable is 0 or not.</li></ul><p>But how do we add an element?</p><p>It might seem that all we have to do is insert the element into the array at position size . When size is 0, we insert at position 0. When size is 1, we insert at position 1. (Note that we are assuming that indices start from 0 here; if they were to start from 1 we would insert at position size + 1 )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4wzmNPFbfXFPfRGwcXdBcQ.png" /><figcaption>4 elements inserted into our array</figcaption></figure><p>But here’s our problem with this implementation of the add operations — what happens when we try to insert three or more elements here?</p><p>The array won’t have space for them. It’s only of size 6. It can only hold data for 6 elements in the bag and no more.</p><p>But that’s not how bags are supposed to work! Bags are supposed to allow you to add in as many elements as you want — not just 6.</p><p>So how can we solve this? We can’t just have a really huge array and hope that someone using our bag doesn’t need that more space.</p><p>A simple solution is to resize our array whenever we run out of space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xpITqj9h6hpXBnXV2YvghA.png" /><figcaption>Resizing our internal array when its full</figcaption></figure><p>We can simply create a new array twice the size (or 4x size or any factor you want) and then copy over everything from the original array to our new resized array.</p><p>And that’s really all there is to the bag data structure. 👏 Bags are one of the simplest data structures out there and now you know how to implement them! 👍</p><p><em>(If you want to take a look at a coded implementation for one, here is </em><a href="https://github.com/calebwin/bag"><em>one I did in forty lines (!) in the Lua programming language</em></a><em>)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f583799c2d5b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/index-zero/a-guide-to-bags-f583799c2d5b">On Bags</a> was originally published in <a href="https://medium.com/index-zero">Index Zero</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>