<?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: Relja Jovicevic</title>
    <description>The latest articles on DEV Community by Relja Jovicevic (@vosse).</description>
    <link>https://dev.to/vosse</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%2F602411%2F351dfcf5-2597-4103-baf1-d8127538243e.png</url>
      <title>DEV Community: Relja Jovicevic</title>
      <link>https://dev.to/vosse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vosse"/>
    <language>en</language>
    <item>
      <title>Open addressing</title>
      <dc:creator>Relja Jovicevic</dc:creator>
      <pubDate>Wed, 20 Oct 2021 23:38:11 +0000</pubDate>
      <link>https://dev.to/vosse/open-addressing-13g9</link>
      <guid>https://dev.to/vosse/open-addressing-13g9</guid>
      <description>&lt;p&gt;If you ever wondered how collisions are handled in hash tables, chances are you've heard about open addressing.&lt;/p&gt;

&lt;p&gt;The goal of a hash table is to construct a mapping from a set of keys to a set of values.&lt;br&gt;
Hash table uses a hash function to compute an index (hash) that we store in an array.&lt;/p&gt;

&lt;p&gt;When two keys have the same hash value hashf(k1) = hashf(k2)&lt;br&gt;
collision happens. Open addressing is one of ways to avoid it.&lt;br&gt;
As opposed to &lt;a href="https://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=&amp;amp;ved=2ahUKEwjIuqzp8NnzAhWFuosKHTebAJ0Q0gIoAXoECCQQAg&amp;amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHash_table%23Collision_resolution&amp;amp;usg=AOvVaw35HhLIk5wS4LY8oElN6VI7" rel="noopener noreferrer"&gt;separate chaining&lt;/a&gt; where we use some sort of a list for entries with the same index, in open addressing we keep all the key-value pairs in the array itself.&lt;br&gt;
That means that we have to pay attention on the size of the array in order to have enough space for all entries.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9b7pw791yj57xncsj2kc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9b7pw791yj57xncsj2kc.png" alt="load factor equation"&gt;&lt;/a&gt;&lt;br&gt;
Personally, I would keep the load factor ( α ) less than 0.5 and once we reach the threshold, simply double the array size.&lt;/p&gt;

&lt;p&gt;Now let's see how open addressing works. Firstly, when a new entry has to be inserted, we check if the corresponding slot is empty, if it is we insert the element. If not, we proceed with a probing sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 1
keyHash = H(k)
index = keyHash

while table[index] != null:
    index = (keyHash + P(k, x)) mod N
    x = x + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;N -&amp;gt; table size&lt;br&gt;
H -&amp;gt; hash function&lt;br&gt;
P -&amp;gt; Probing function&lt;/p&gt;

&lt;p&gt;Be wary when choosing a probing sequence since some of them may produce cycle shorter than N and as a result you'll get stuck in an &lt;em&gt;infinite loop&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Open Addressing techniques
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Linear Probing
&lt;/h4&gt;

&lt;p&gt;When collision occurs, we linearly probe for the next bucket.&lt;br&gt;
We keep probing until an empty bucket is found.&lt;br&gt;
Linear probing function could look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P(x) = a * x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we had a table of size N = 9 and probing function P(x) = a * x, where a = 6 we wouldn't be able to reach all the buckets in the array and as a consequence, would be stuck in an infinite loop.&lt;br&gt;
In order to avoid this, &lt;strong&gt;N&lt;/strong&gt; and &lt;strong&gt;a&lt;/strong&gt; should be relatively prime (Their global common denominator should be equal to 1).&lt;/p&gt;
&lt;h4&gt;
  
  
  Quadratic Probing
&lt;/h4&gt;

&lt;p&gt;Similar as linear probing but instead of a linear function we use a quadratic one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P(x) = a * x² + b * x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few ways to avoid infinite loop&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;P(x) = x² =&amp;gt; keep the table size a prime number and &amp;gt; 3 and keep α &amp;lt;= 1/2&lt;/li&gt;
&lt;li&gt;P(x) = (x² + x) / 2 =&amp;gt; keep the table size a power of two.&lt;/li&gt;
&lt;li&gt;P(x) = (-1^x) * x² =&amp;gt; keep the table size a prime N where N ≡ 3 mod 4&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Double Hashing
&lt;/h4&gt;

&lt;p&gt;Double hashing uses the idea of applying a second hash function to key when a collision occurs.&lt;br&gt;
Probing method in double hashing could look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P(k, x) = x * secondHashFunc(k)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To avoid infinite loop when using double hashes make sure that the table size N is a prime number and calculate the delta&lt;br&gt;
δ = secondHashFunc(k) mod N.&lt;br&gt;
If δ happens to be 0, set it to 1.&lt;/p&gt;

&lt;p&gt;There are many variations of open addressing such as &lt;a href="https://programming.guide/coalesced-hashing.html" rel="noopener noreferrer"&gt;Coalesced Hashing&lt;/a&gt;, &lt;a href="https://programming.guide/cuckoo-hashing.html" rel="noopener noreferrer"&gt;Cuckoo Hashing&lt;/a&gt;, &lt;a href="https://programming.guide/robin-hood-hashing.html" rel="noopener noreferrer"&gt;Robin Hood Hashing&lt;/a&gt;... but they are beyond the scope of this post. I recommend that you explore them on your own.&lt;/p&gt;

&lt;p&gt;Key points to take from this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure the load factor (α) is below a certain threshold&lt;/li&gt;
&lt;li&gt;Avoid infinite loops&lt;/li&gt;
&lt;li&gt;Keep learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;reach me at &lt;a href="mailto:relja.jovicevic@gmail.com"&gt;relja.jovicevic@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>datastructure</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>SQL vs NoSQL</title>
      <dc:creator>Relja Jovicevic</dc:creator>
      <pubDate>Fri, 15 Oct 2021 22:25:27 +0000</pubDate>
      <link>https://dev.to/vosse/sql-vs-nosql-534p</link>
      <guid>https://dev.to/vosse/sql-vs-nosql-534p</guid>
      <description>&lt;p&gt;If you are a beginner wondering what are use cases for SQL or an experienced developer trying to make a choice between SQL or NoSQL for your next project, this article might help you steer your decision the right way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros vs cons
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Everything comes at a cost.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While SQL is &lt;strong&gt;ACID&lt;/strong&gt; ( Atomicity, Consistency, Isolation, Durability) compliant which ensures that performed transactions are always consistent, it might be an overkill for your use case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Atomicity - ensures that each database transaction is a single unit that adopts an "all or nothing" approach to execution. If any statement in the transaction fails, the entire transaction is rolled back&lt;/li&gt;
&lt;li&gt;Consistency – A processed transaction will never endanger the structural integrity of the database.&lt;/li&gt;
&lt;li&gt;Isolation - Transactions cannot compromise the integrity of other transactions by interacting with them while they are still in progress&lt;/li&gt;
&lt;li&gt;Durability - The data related to the completed transaction will persist even in the cases of network or power outages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead you might find &lt;strong&gt;BASE&lt;/strong&gt; ( Basically available, Soft state, Eventual Consistency ) NoSQL databases to be more of a fit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basically available - The NoSQL database approach focuses on the availability of data even in the presence of multiple failures. It achieves this by using a highly distributed approach to database management. Instead of maintaining a single large data store and focusing on the fault tolerance of that store, NoSQL databases spread data across many storage systems with a high degree of replication. In the unlikely event that a failure disrupts access to a segment of data, this does not necessarily result in a complete database outage.&lt;/li&gt;
&lt;li&gt;Soft state - Due to the lack of immediate consistency, data values may change over time. The BASE model breaks off with the concept of a database which enforces its own consistency, delegating that responsibility to developers.&lt;/li&gt;
&lt;li&gt;Eventual Consistency - The only requirement that NoSQL databases have regarding consistency is to require that at some point in the future, data will converge to a consistent state. No guarantees are made, however, about when this will occur.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if you're dealing with transaction processing where consistency and durability are must-have features then you'll be better off with an ACID compliant DB.&lt;br&gt;
On the other hand, social network feeds do not require such a strict compliance so it's acceptable to trade-off ACID in favour of more flexible, easier to scale, approach.&lt;/p&gt;

&lt;p&gt;This article does not contain the answer which is better, SQL or NoSQL because as always, it &lt;em&gt;depends&lt;/em&gt; on your use case.&lt;/p&gt;

&lt;p&gt;Having said that, I highly advise &lt;em&gt;against&lt;/em&gt; following the latest trends blindly and flavour of the month technologies. Instead try to define your use case and make a decision based on your needs.&lt;/p&gt;

&lt;p&gt;reach out &amp;gt; &lt;a href="mailto:relja.jovicevic@gmail.com"&gt;relja.jovicevic@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
    </item>
  </channel>
</rss>
