Logo

dev-resources.site

for different kinds of informations.

Merge Intervals : A unique Graph-based approach

Published at
3/19/2024
Categories
dsa
leetcode
graph
sorting
Author
mishratharv10
Categories
4 categories in total
dsa
open
leetcode
open
graph
open
sorting
open
Author
13 person written this
mishratharv10
open
Merge Intervals : A unique Graph-based approach

So I was solving this leetcode question titled Merge Intervals 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.

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.

The other approach got me. It was based on the concept of Connected Components 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😭 .

Anyway here is the implementation of that approach:

class Solution {
public:
    map<vector<int>, vector<vector<int>>> graph;
    map<int, vector<vector<int>>> nodes_in_comp;
    set<vector<int>> visited;

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

    // build a graph where an undirected edge between intervals u and v exists
    // iff u and v overlap.
    void buildGraph(vector<vector<int>>& 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<int> mergeNodes(vector<vector<int>>& 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<int>& start, int comp_number) {
        stack<vector<int>> stk;
        stk.push(start);

        while (!stk.empty()) {
            vector<int> 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<vector<int>>& intervals) {
        int comp_number = 0;

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

    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        buildGraph(intervals);
        buildComponents(intervals);

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

        return merged;
    }
};

Enter fullscreen mode Exit fullscreen mode
graph Article's
30 articles in total
Favicon
Get a gist of graph data structure here...
Favicon
Find safest walk through the grid
Favicon
Number of islands
Favicon
Negative cycle with Dijskta(Possible but not optimal)
Favicon
YugabyteDB as a Graph database with PuppyGraph
Favicon
Remove Methods from project
Favicon
Visualize the preferences of cats
Favicon
How to Determine if a Graph is Not Simple Without Checking Every Edge for Loops or Parallelism
Favicon
Disjoint Set Graph Learning
Favicon
Bellman ford algorithm(Single Source Shorted Path in DAG)
Favicon
Safely restructure your codebase with Dependency Graphs
Favicon
Unveiling the Connections: A Beginner's Guide to Graph Theory
Favicon
How is Graph stored in Memory?
Favicon
Graph problems are not hard
Favicon
Como andam as suas Relações?
Favicon
Merge Intervals : A unique Graph-based approach
Favicon
Explaining LinkedIn profile, "1st," "2nd," and "3rd" values.
Favicon
Neo4j and the Power of Graph Databases in Data Science
Favicon
Exploring the Implementation of Graph Data in OceanBase
Favicon
Solving "Word Search" problem
Favicon
Powershell script to call microsoft graph and send email using azure app registration
Favicon
A developer’s introduction to graph databases
Favicon
Graph in R with Grouping Letters from the Tukey, LSD, Duncan Test, agricolae Package
Favicon
How to: Get up to speed and scale with Aerospike Graph on Google Cloud Marketplace
Favicon
Preview Geometry Nodes on web using React
Favicon
Improved Data Point Graph Widget for Cumulocity IoT
Favicon
Six Degrees of Separation Using Apache AGE
Favicon
Next Big Data System
Favicon
The Usability of Graph Data and Graph Algorithms: Unleashing the Power of Connections
Favicon
Accelerate Domain Learning: Explore Application Dependencies with RailsGraph

Featured ones: