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
sorting Article's
30 articles in total
Favicon
Difference Between Merge Sort and Quick Sort
Favicon
Leetcode 75. Sort Colors
Favicon
Sorted Data Structures in Python
Favicon
Sorting Algorithms That Use Hash Tables
Favicon
C# Essentials: Operator Overloading and Custom Sorting Made Simple
Favicon
Recap the highlight of the sorting algorithms using JavaScript for beginners
Favicon
Merge Sort Demystified: A Beginner's Guide to Divide and Conquer Sorting
Favicon
Understanding Bubble Sort: Simple Sorting Method
Favicon
Introduction to Sorting Algorithms in JavaScript
Favicon
Understanding the SQL ORDER BY Clause
Favicon
Demystifying Sorting Algorithms: Making Order Out of Chaos
Favicon
Merge Intervals : A unique Graph-based approach
Favicon
Bubble Sort
Favicon
COMPARATOR vs COMPARABLEโ€Š-โ€ŠA Java Surprise You did inย School!
Favicon
Streamlining Data Management with Python's sorted() Function
Favicon
1 billion rows challenge in MySQL
Favicon
1 billion rows challenge in PostgreSQL and ClickHouse
Favicon
Sorting in Java โ€“ how to, and how not to do it anymore
Favicon
Reversing sort order in Rust
Favicon
Priority Queue: Creating order from chaos
Favicon
Mastering Array Sorting in PHP: usort & uasort ๐Ÿš€๐Ÿš€
Favicon
QuickSort - Time Analysis (Part2)
Favicon
Quicksort (Grokking Algorithms)
Favicon
Better Bogo Sort
Favicon
Sorting Array of Objects in Javascript
Favicon
Bubble Sort
Favicon
Understanding insertion sort algorithm
Favicon
Sorting Visualizer [ A web app to visualize sorting algorithm ]
Favicon
How to sort complex objects with custom criteria in Python
Favicon
Iterative Sorting algorithms in Javascript

Featured ones: