<?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: Addy</title>
    <description>The latest articles on DEV Community by Addy (@addy118).</description>
    <link>https://dev.to/addy118</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%2F2211112%2Fd143fad8-2339-4d79-9d0e-b0c324e6a619.jpeg</url>
      <title>DEV Community: Addy</title>
      <link>https://dev.to/addy118</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/addy118"/>
    <language>en</language>
    <item>
      <title>An efficient approach to problem solving for DSA</title>
      <dc:creator>Addy</dc:creator>
      <pubDate>Mon, 09 Jun 2025 05:49:21 +0000</pubDate>
      <link>https://dev.to/addy118/an-efficient-approach-to-problem-solving-for-dsa-2j6l</link>
      <guid>https://dev.to/addy118/an-efficient-approach-to-problem-solving-for-dsa-2j6l</guid>
      <description>&lt;p&gt;Starting to solve DSA problems might seem scary to many and some might have started but failed to continue it for a significiant amount of time. So, why does this happen? What are you lacking? A structured problem solving approach.&lt;/p&gt;

&lt;p&gt;You don't read the problem statement and straight away dive to code the solution. That's not what a wise person does and its not something to be ashamed of. Before coding up the solution you first need to understand the problem and plan the algorithm beforehand and test it yourself. Then you can implement it which would not take much time then.&lt;/p&gt;

&lt;p&gt;Now comes the main question, how do you start then?&lt;/p&gt;

&lt;p&gt;First of all, you should read the question properly and should understand the question well enough, the input format, the output format needed. The core question might be layered with an extra complexity by adding some useless info. You need to extract the core question that the problem asks.&lt;br&gt;
For example, look at this leetcode question below (198. House Robber)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.&lt;/p&gt;

&lt;p&gt;Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The question added unnecessary entities like money, police, robber, house. You don't need to know them for solving the question. You just need numbers and data structures. Its mentioned in the end that you are given an array representing the amount of money, therefore the input is an &lt;code&gt;array&lt;/code&gt; of &lt;code&gt;numbers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On reading the first paragraph carefully or several times if you need to, you can come to a conclusion that at its core, it just asks you this basic thing&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Return the maximum sum of non-adjacent array elements from the given input array of numbers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This core question was coated with many things to confuse the readers and take their time. Now if you get this you can start to focus on the &lt;strong&gt;&lt;em&gt;problem&lt;/em&gt;&lt;/strong&gt; that you got now.&lt;/p&gt;

&lt;p&gt;Whenever you solve a question always make yourself clear what is the problem you're solving? Break that problem to smaller test-cases, list them and tackle them in an isolated manner by completely focusing on the sub-problem. As you solve the sub-problems, tick them in your list to boost your motivation. Once you tick all the problems, bam! you solved the main problem.&lt;/p&gt;

&lt;p&gt;For solving a problem use the PBOI approach. It is an acronym for&lt;/p&gt;

&lt;h3&gt;
  
  
  P - Pen &amp;amp; Paper
&lt;/h3&gt;

&lt;h3&gt;
  
  
  B - Bruteforce
&lt;/h3&gt;

&lt;h3&gt;
  
  
  O - Optimize the bruteforce
&lt;/h3&gt;

&lt;h3&gt;
  
  
  I - Only now, Implement
&lt;/h3&gt;

&lt;p&gt;Think of the testcases or use the given ones and solve by yourself on paper. It would be pretty easy to solve for you unless some recursion is involved. So, better start with a small testcase.&lt;/p&gt;

&lt;p&gt;Now, slow down and carefully go through the process your brain follows to solve the testcase. Translate your brain's process on paper using the steps that computers could understand and perform. This process will take time and you will improve by time and practice.&lt;/p&gt;

&lt;p&gt;Once you have the steps, congratulations! that's what you call an algorithm. The next step for you would be to turn this algorithm into pseudo-code. If your programming knowledge is good this step won't be tough.&lt;/p&gt;

&lt;p&gt;Now take a test-case, and don't solve by your brain this time, but solve using the pseudo-code that you wrote on paper. Go line by line and pass the control of the program as a real computer would do. Track the variables and data structures on paper and update them at each line. This process is called dry-running the problem. &lt;/p&gt;

&lt;p&gt;This helps you by saving time from implementing a wrong solution and then banging your head. If there was a problem in your algorithm, you'd find right away at the time of dry-run and then you can fix it there. This approach would save a lot of time and is efficient.&lt;/p&gt;

&lt;p&gt;Once you get a working algorithm, calculate the time complexity of your algorithm and try to optimize it even further on paper itself. You don't need to implement that bruteforce if it working on dry-run. It would just boost your ego. Try to optimize it and after trying enough if you were able to good, else you still have a bruteforce approach.&lt;/p&gt;

&lt;p&gt;Finally, comes the step for implementation. Now as you have an optimized algorithm and a pseudo-code for the problem, you can easily implement it in a programming language if your syntactically good in your preferred lanugage.&lt;/p&gt;

&lt;p&gt;This was a structured problem solving approach that many of the programmers use. Some knowingly and some unknowingly.&lt;/p&gt;

&lt;p&gt;Now, you might be thinking how much time should I try devising a working algorithm? I'd say if you're new to a concept, you should give 45-60 minutes and if you're familiar with the concept you should try for 30 minutes and then look for solutions. &lt;/p&gt;

&lt;p&gt;When you look for the solution, you don't have to copy paste it directly, but rather you have to dry-run the found solution to get a deep understanding of the algorithm that is used. In the implementation part too, you don't copy-paste the solution. You should type by yourself because this helps in even deeper understanding of the solution. Copy-pasting might make you feel good to see all the test-cases passing, but in a longer run you won't learn anything. Even if you try other's solution, &lt;strong&gt;NEVER COPY-PASTE&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading! Don't forget to leave a like if this blog post has helped you and follow for more such content.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>dsa</category>
      <category>learning</category>
    </item>
    <item>
      <title>Disjoint Set Union / Union Find Algorithm Simplified</title>
      <dc:creator>Addy</dc:creator>
      <pubDate>Sun, 08 Jun 2025 18:06:11 +0000</pubDate>
      <link>https://dev.to/addy118/disjoint-set-union-union-find-algorithm-simplified-1054</link>
      <guid>https://dev.to/addy118/disjoint-set-union-union-find-algorithm-simplified-1054</guid>
      <description>&lt;p&gt;Union Find algorithm is a crucial algorithm in problems related to graph data structure. It helps find the relation between vertices by using the existing edges and tells whether the two are related or not.&lt;/p&gt;

&lt;p&gt;It is very useful in real-world scenarios like for example, LinkedIn uses this algorithm to display the users information about the degree of connection with other users. People who are direct &lt;code&gt;parent&lt;/code&gt; to the user are the 1st level connection, people who are the &lt;code&gt;parent&lt;/code&gt; of the &lt;code&gt;parent&lt;/code&gt; of the user are the 2nd level connection and so on. &lt;/p&gt;

&lt;p&gt;You might be thinking what is this &lt;code&gt;parent&lt;/code&gt; thing I mentioned so much about? It's just a directed edge between the two nodes, in our case, an edge from user to another person makes and edge like &lt;code&gt;user -&amp;gt; another person&lt;/code&gt;. In real life it translates to user "follows/is a friend of" another person.&lt;/p&gt;

&lt;h3&gt;
  
  
  Union Find algorithm includes two methods:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Find&lt;/strong&gt; - to get the root representative of the set in which the current vertex belongs to. (uses Path Compression for optimization)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Union&lt;/strong&gt; - Tells whether the two vertices are in the same set or not, and if not merges them into a single set. (uses Rank to decide the resultant merged set)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Here, are some basic things we should know before diving in:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each vertex initially belongs to its own set, and therefore is the root representative of that set. It is implemented as an array generally.&lt;/li&gt;
&lt;li&gt;Each vertex initially is assigned the same rank which others have, here we assign 0. It is also implemented as an array generally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement the find() method first as it is required in the union() method later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take a vertex &lt;code&gt;i&lt;/code&gt; and the &lt;code&gt;parent&lt;/code&gt; array to find the root representative of the vertex &lt;code&gt;i&lt;/code&gt; and return it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get the parent of the &lt;code&gt;i&lt;/code&gt; from the array. Initially we would get the element itself, if there are no relations (merging) yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// parent of the vertex is not the root representative&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// parent of the vertex is the root representative&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we check if the parent of the vertex is the root of the set to which the vertex belongs to? if yes then we simply return it, as it was the main goal of our function.&lt;/p&gt;

&lt;p&gt;But, if not, then we recursively try to find the root until the parent and vertex are same and assign it in the &lt;code&gt;parent&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;Think of it like this, if the vertex and the parent of vertex are the same, then it is the root representative of the set in which it belongs. Once we find it we return it.&lt;/p&gt;

&lt;p&gt;For a more clearer visual representation of the find() function refer this image&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6rzpth1ctgyizgu6yv0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6rzpth1ctgyizgu6yv0e.png" alt="Path Compression using find()" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, as you can see the path from 7 to the root (ie. 1) was from 7 -&amp;gt; 3 -&amp;gt; 1 and after calling find(7) which in turn calls find(3) and later assigns the &lt;code&gt;parent&lt;/code&gt; of 7 as 1. It can be clearly seen from the image that the path is reduced, keeping all the vertices still in the same set. Just we get the compressed path of the root of the vertex from this function.&lt;/p&gt;

&lt;p&gt;Now, comes the union() method. After understanding find(), it is just a piece of cake.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;union&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take the two vertices to unite into a single set, the &lt;code&gt;parent&lt;/code&gt; array and the &lt;code&gt;rank&lt;/code&gt; array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;iRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// both vertices belong to the same set already&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&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="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, we just get the roots of both the vertices using the previously defined find() method.&lt;/p&gt;

&lt;p&gt;If the roots returned by both the method calls are same, then it would simply mean that they are already in the same set and we can't unite it further. That's why we return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, just compare the ranks of both vertices and merge the smaller ranking vertex to the set of the greater ranking vertex. Now you might think, if we don't modify the rank here, then how they can be compared in the first place? We modify the rank when the ranks of both vertices are found to be same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;]++;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can merge any vertex into the other vertex's set, but we have to make sure that we increase the rank of the set in which the vertex have been merged or rather "united" by 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of the if-else ladder, if we reach there, we return true. It indicates that the vertices have been "united" to a single set and the "union" operation was successful!&lt;/p&gt;

&lt;p&gt;You can implement this in any language as the syntax used here is generic and can be applied to implement in most of the languages with little to no change. &lt;/p&gt;

&lt;p&gt;I have implemented it in java and here is the full code for reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DSU&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// finds the root of a given vertex's set with path compression&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// the root is not the vertex itself&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// the root of this vertex isn't the root of the set&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
            &lt;span class="c1"&gt;// update the current vertex's parent with the root of its &lt;/span&gt;
            &lt;span class="c1"&gt;// current parent's root to compress path&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DSU&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// the root of this vertex is also the root of the set&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// checks the root of both the vertices and assign new roots &lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;union&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;iRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DSU&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DSU&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;find&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&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="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iRoot&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;jRoot&lt;/span&gt;&lt;span class="o"&gt;]++;&lt;/span&gt;
        &lt;span class="o"&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="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading! Follow me and stay tuned in for more such simplified explanations.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>dsa</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Migrating to Prisma Postgres Global DB from postgreSQL DB with Prisma ORM</title>
      <dc:creator>Addy</dc:creator>
      <pubDate>Sat, 07 Jun 2025 11:26:03 +0000</pubDate>
      <link>https://dev.to/addy118/migrating-to-prisma-postgres-global-db-from-postgresql-db-with-prisma-orm-6f1</link>
      <guid>https://dev.to/addy118/migrating-to-prisma-postgres-global-db-from-postgresql-db-with-prisma-orm-6f1</guid>
      <description>&lt;p&gt;Recently Prisma came with an update and launched its new global database called Prisma Postgres. It's a great option for our deploys as it saves the time and money for searching other platforms and providers to host our database online. It provides 5 databases with 1 GB storage for its free plan users.&lt;/p&gt;

&lt;p&gt;If you are a Prisma user with postgreSQL database then the below mentioned steps in this blogpost would help you easily migrate all your data from the old database to the new global database. So let's just begin directly!&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;Create a new database in &lt;a href="https://console.prisma.io" rel="noopener noreferrer"&gt;Prisma Console&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Go to Database &amp;gt; Setup &amp;gt; Existing Project &amp;gt; Generate database credentials&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwgqqedhzsuiyui8s3wzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwgqqedhzsuiyui8s3wzw.png" alt="New DB Details"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;On clicking the generate button, your new database details (connection url + api key) would be shown, store them in your .env file of the project.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4
&lt;/h3&gt;

&lt;p&gt;Now, open up a terminal and get your old database's connection url and enter the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_dump -Fc -v -d DATABASE_URL -n public -f db_dump.bak
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will create a &lt;code&gt;bak&lt;/code&gt; file which will have your all previous data with the name db_dump.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 5
&lt;/h3&gt;

&lt;p&gt;Set the environment variable in the terminal explicitly by running the following command&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Use the &lt;code&gt;API_KEY&lt;/code&gt; which was received in step 3.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 6
&lt;/h3&gt;

&lt;p&gt;Open up a bash terminal (if you weren't using it in earlier steps) and enter the following command to start a tunnel.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx @prisma/ppg-tunnel --host 127.0.0.1 --port 5433
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If this runs properly, you won't be able to use that terminal now further. Open up a new terminal, but don't close the previous one, as it runs our tunnel.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 7
&lt;/h3&gt;

&lt;p&gt;Now comes the final command to transfer the data, enter it and you're good to go&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PGSSLMODE=disable pg_restore -h 127.0.0.1 -p 5433 -v -d postgres ./db_dump.bak &amp;amp;&amp;amp; echo "-complete-"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A message "-complete-" would appear on successful transfer of the data. For further verification, you can run&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma studio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;with the new database connection url in your &lt;code&gt;.env&lt;/code&gt; to check whether the old data is there in your new database or not.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Reference
&lt;/h4&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.prisma.io/docs/prisma-postgres/import-from-existing-database-postgresql" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.prisma.io%2Fdocs%2Fog%2Fprisma-postgres%2Fimport-from-existing-database-postgresql%2Fimage.png" height="auto" class="m-0"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.prisma.io/docs/prisma-postgres/import-from-existing-database-postgresql" rel="noopener noreferrer" class="c-link"&gt;
            Import from existing Postgres database into Prisma Postgres | Prisma Documentation
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            Learn how to import data from an existing database into Prisma Postgres.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.prisma.io%2Fdocs%2Ffavicon.ico%3Ffavicon.f8f1f26e.ico"&gt;
          prisma.io
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
