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.
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 ratings0% 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.
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; }
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; }