< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Union-Find (Dynamic Connectivity problem)

Implementation

PreviousWhat is a Union Find Data Structure?NextWhat's Binary Tree & Implementation?

Last updated 10 months ago

Was this helpful?

Documentation:

Weighted Quick-Union with Path Compression (WQUPC)

#include <iostream>
using namespace std;

class QuickUnionUF {
     int *id;
     int *size;
     int n;

     int root(int i) {
          while (i != id[i]) {
               id[i] = id[id[i]]; // Path Compression (Keeps tree almost completely flat) Improvement. only one extra line of code 
               i = id[i];
          }
          return i;
     }

public:
     QuickUnionUF(int n) : n(n) {
          id = new int[n];
          size = new int[n];
          for (int i = 0; i < n; i++) {
               id[i] = i;
               size[i] = 1;
          }
     }

     bool Connected(int p, int q) {
          return root(p) == root(q);
     }

     void Union(int p, int q) {
          int rootA = root(p);
          int rootB= root(q);
          
          if (rootA == rootB) return;

          if (size[rootA] < size[rootB]) {
               id[rootA] = rootB;
               size[rootB] += size[rootA];
          }
          else {
               id[rootB] = rootA;
               size[rootA] += size[rootB];
          }
     }

     void print() {
          for (int i = 0; i < n; i++)
               cout << id[i] << " ";
     }
};

int main()
{
     int n;
     cout << "Enter Size: ";
     cin >> n;

     QuickUnionUF *obj = new QuickUnionUF(n);
     while (1) {
          int p, q;
          cin >> p >> q;
          if (p < 0 || q >= n || q < 0 || q >= n) break;
          
          if (!obj->Connected(p, q))
          {
               obj->Union(p, q);
               cout << p << " " << q << endl;
          }
          else cout << "Already Connected!" << endl;
          
     }
     obj->print();
}
🔗
click here!