0% found this document useful (0 votes)
9 views

Disjoint Set Snippet

The document contains implementations of various algorithms and data structures in C++. It includes a Disjoint Set class for union-find operations, functions to check for prime numbers and find the next prime, a permutation generator, KMP algorithm for pattern matching, efficient power calculation, and a method to find the middle node of a linked list.

Uploaded by

Shreyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Disjoint Set Snippet

The document contains implementations of various algorithms and data structures in C++. It includes a Disjoint Set class for union-find operations, functions to check for prime numbers and find the next prime, a permutation generator, KMP algorithm for pattern matching, efficient power calculation, and a method to find the middle node of a linked list.

Uploaded by

Shreyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

class DisjointSet {

vector<int> rank, parent, size;


public:
DisjointSet(int n) {
rank.resize(n + 1, 0);
parent.resize(n + 1);
size.resize(n + 1);
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}

int findUPar(int node) {


if (node == parent[node])
return node;
return parent[node] = findUPar(parent[node]);
}

void unionByRank(int u, int v) {


int ulp_u = findUPar(u);
int ulp_v = findUPar(v);
if (ulp_u == ulp_v) return;
if (rank[ulp_u] < rank[ulp_v]) {
parent[ulp_u] = ulp_v;
}
else if (rank[ulp_v] < rank[ulp_u]) {
parent[ulp_v] = ulp_u;
}
else {
parent[ulp_v] = ulp_u;
rank[ulp_u]++;
}
}

void unionBySize(int u, int v) {


int ulp_u = findUPar(u);
int ulp_v = findUPar(v);
if (ulp_u == ulp_v) return;
if (size[ulp_u] < size[ulp_v]) {
parent[ulp_u] = ulp_v;
size[ulp_v] += size[ulp_u];
}
else {
parent[ulp_v] = ulp_u;
size[ulp_u] += size[ulp_v];
}
}
};

bool isPrime(long long int n)


{
if(n<=1)
{
return false;
}
if(n<=3)
{
return true;
}
if(n%2==0 || n%3==0)
{
return false;
}
for(int i=5; i*i<=n; i=i+6)
{
if(n%i==0 || n%(i+2)==0)
{
return false;
}
}
return true;
}
long long int nextPrime(long long int N)
{
if(isPrime(N))
{
return N;
}
if(N<=1)
{
return 2;
}
long long int prime=N;
bool found=false;
while(!found)
{
if(isPrime(N))
{
found=true;
}
prime++;
if(isPrime(prime))
{
found=true;
}
}
return prime;
}

//TO FIND ALL THE PERMUTATIONS OF A GIVEN STRING


void permute(string& s, long long int l, long long int r,set<string>&ans)
{
if(l==r)
{
ans.insert(s);
}
else
{
for(int i=l; i<=r; i++)
{
swap(s[l],s[i]);
permute(s,l+1,r,ans);
swap(s[l],s[i]);
}
}
}

//HELPER FUNCTION TO BUILD THE KMP PREFIX TABLE


vector<int>buildPrefixTable(const string& s)
{
vector<int>prefixTable(s.length(),0);
int length=0;
// Build the table by comparing characters
for(int i=1; i<s.length(); i++)
{
while(length>0 && s[i]!=s[length])
{
length=prefixTable[length-1];
}
if(s[i]==s[length])
{
length++;
}
prefixTable[i]=length;
}
return prefixTable;
}
//ANOTHER METHOD
//HELPER FUNCTION TO BUILD THE KMP PREFIX TABLE
vector<int>buildPrefixTable(const string& s)
{
vector<int>prefixTable(s.length(),0);
int length=0;
int i=1;
while(i<s.length())
{
if(s[i]==s[length])
{
length++;
prefixtable[i]=length;
i++;
}
else
{
if(length>0)
{
length=prefixtable[length-1];
}
else
{
prefixtable[i]=0;
i++;
}
}
}
return prefixTable;
}

//KMP ALGORITHM FOR PATTERN MATCHING


let n=text.length(),m=pattern.length();
vector<int>LPS=buildPrefixTable(pattern);
vector<int>result;
int i=0;
int j=0;
while(i<n)
{
if(text[i]==pattern[j])
{
i++;
j++;
}
if(j==m)
{
result.push_back(i-m);
j=LPS[j-1];
}
else if(pattern[j]!=text[i])
{
if(j!=0)
{
j=LPS{j-1];
}
else
{
i++;
}
}
return result;

//TO CALCULATE POW(X,N) EFFICIENTLY


int power(int x, int n)
{
int result = 1;
while (n > 0) {
if (n & 1 == 1) // y is odd
{
result = result * x;
}
x = x * x;
n = n >> 1; // y=y/2;
}
return result;
}

//TO FIND THE MIDDLE NODE OF A GIVEN LINKED LIST


ListNode* middleNode(ListNode* head)
{
struct ListNode *slow, *fast;
slow=head;
fast=head->next;

while(slow!=NULL && fast!=NULL && fast->next!=NULL)


{
slow=slow->next;
fast=fast->next;
if(fast->next!=NULL) fast=fast->next;
}
return slow;
}

You might also like