<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Atharv Mishra</title>
    <description>The latest articles on Forem by Atharv Mishra (@mishratharv10).</description>
    <link>https://forem.com/mishratharv10</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%2F1359977%2F71abd000-a029-45fa-a14a-0e214a79b4ad.png</url>
      <title>Forem: Atharv Mishra</title>
      <link>https://forem.com/mishratharv10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mishratharv10"/>
    <language>en</language>
    <item>
      <title>Merge Intervals : A unique Graph-based approach</title>
      <dc:creator>Atharv Mishra</dc:creator>
      <pubDate>Tue, 19 Mar 2024 19:04:29 +0000</pubDate>
      <link>https://forem.com/mishratharv10/merge-intervals-a-unique-graph-based-approach-hm2</link>
      <guid>https://forem.com/mishratharv10/merge-intervals-a-unique-graph-based-approach-hm2</guid>
      <description>&lt;p&gt;So I was solving this leetcode question titled &lt;a href="https://leetcode.com/problems/merge-intervals/description/"&gt;Merge Intervals&lt;/a&gt; and after trying for 30-40 mins I got an intuition that it could be solved by sorting but anyway, I was not able to implement it in one go. I missed some edge cases. &lt;/p&gt;

&lt;p&gt;But when I opened the editorial on this problem, I was shocked. Two approaches were discussed on that page, one was the sorting-based approach which solved the problem in O(NlogN) time and O(1) space. Indeed that one was the optimised one. &lt;/p&gt;

&lt;p&gt;The other approach got me. It was based on the concept of &lt;a href="https://www.geeksforgeeks.org/connected-component-definition-meaning-in-dsa/"&gt;Connected Components&lt;/a&gt; in Graphs and trust me this is the first topic in my current Data Structures course of Sem 4 in my college. I feel terrible that I could not apply what I have learned from that college course😭 . &lt;/p&gt;

&lt;p&gt;Anyway here is the implementation of that approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    map&amp;lt;vector&amp;lt;int&amp;gt;, vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;gt; graph;
    map&amp;lt;int, vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;gt; nodes_in_comp;
    set&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; visited;

    bool overlap(vector&amp;lt;int&amp;gt;&amp;amp; a, vector&amp;lt;int&amp;gt;&amp;amp; b) {
        return a[0] &amp;lt;= b[1] and b[0] &amp;lt;= a[1];
    }

    // build a graph where an undirected edge between intervals u and v exists
    // iff u and v overlap.
    void buildGraph(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; intervals) {
        for (auto interval1 : intervals) {
            for (auto interval2 : intervals) {
                if (overlap(interval1, interval2)) {
                    graph[interval1].push_back(interval2);
                    graph[interval2].push_back(interval1);
                }
            }
        }
    }

    // merges all of the nodes in this connected component into one interval.
    vector&amp;lt;int&amp;gt; mergeNodes(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; nodes) {
        int min_start = nodes[0][0];
        for (auto node : nodes) {
            min_start = min(min_start, node[0]);
        }

        int max_end = nodes[0][1];
        for (auto node : nodes) {
            max_end = max(max_end, node[1]);
        }

        return {min_start, max_end};
    }

    // use depth-first search to mark all nodes in the same connected component
    // with the same integer.
    void markComponentDFS(vector&amp;lt;int&amp;gt;&amp;amp; start, int comp_number) {
        stack&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; stk;
        stk.push(start);

        while (!stk.empty()) {
            vector&amp;lt;int&amp;gt; node = stk.top();
            stk.pop();

            // not found
            if (visited.find(node) == visited.end()) {
                visited.insert(node);

                nodes_in_comp[comp_number].push_back(node);

                for (auto child : graph[node]) {
                    stk.push(child);
                }
            }
        }
    }

    // gets the connected components of the interval overlap graph.
    void buildComponents(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; intervals) {
        int comp_number = 0;

        for (auto interval : intervals) {
            if (visited.find(interval) == visited.end()) {
                markComponentDFS(interval, comp_number);
                comp_number++;
            }
        }
    }

    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; merge(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; intervals) {
        buildGraph(intervals);
        buildComponents(intervals);

        // for each component, merge all intervals into one interval.
        vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; merged;
        for (size_t comp = 0; comp &amp;lt; nodes_in_comp.size(); comp++) {
            merged.push_back(mergeNodes(nodes_in_comp[comp]));
        }

        return merged;
    }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dsa</category>
      <category>leetcode</category>
      <category>graph</category>
      <category>sorting</category>
    </item>
  </channel>
</rss>
