From c6aa9df7f566284da04fd9d25d7d6afffd3f457e Mon Sep 17 00:00:00 2001 From: hrisbh10 Date: Tue, 1 Oct 2019 16:26:33 +0530 Subject: [PATCH 1/3] Added KMP Algorithm --- README.md | 1 + algorithms/Knuth-Morris-Pratt/README.md | 29 +++++++++++++++ algorithms/Knuth-Morris-Pratt/kmpalgo.cpp | 43 +++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 algorithms/Knuth-Morris-Pratt/README.md create mode 100644 algorithms/Knuth-Morris-Pratt/kmpalgo.cpp diff --git a/README.md b/README.md index c346d4f..6f267be 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,4 @@ Happy Open Sourcing! - [Caesar's Cipher](algorithms/Caesar's_cipher) - [Array Rotation By 1](algorithms/array_rotation_by_1) - [Magic Number](algorithms/magic_no/) +- [KMP Algorithm](algorithms/Knuth-Morris-Pratt/) diff --git a/algorithms/Knuth-Morris-Pratt/README.md b/algorithms/Knuth-Morris-Pratt/README.md new file mode 100644 index 0000000..8a4abcd --- /dev/null +++ b/algorithms/Knuth-Morris-Pratt/README.md @@ -0,0 +1,29 @@ +# Knuth-Morris-Pratt + +An Algorithm For searching a string pattern in a different string + +### Input Format + +Input: First String + Second String + +### Output Format + +Output: (Number Of Occurences) + +### Sample Input + +``` +abcde +abcdeaaaaabcdehlkj +``` + +### Sample Output + +``` +2 +``` + +### Implemented in: + +- [C++](kmpalgo.cpp) \ No newline at end of file diff --git a/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp b/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp new file mode 100644 index 0000000..ed7971d --- /dev/null +++ b/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp @@ -0,0 +1,43 @@ +//KMP Algo +#include +#include +using namespace std; + +int main(){ + string s,str; + cin>>s>>str; + long sz=s.length(),cnt=0; + long a[sz]; + a[0]=0; + long l=0,r=1; + while(r Date: Tue, 1 Oct 2019 16:42:23 +0530 Subject: [PATCH 2/3] Added Matrix Exponentiation --- README.md | 1 + algorithms/Matrix-Expo/MatrixExpo.cpp | 57 +++++++++++++++++++++++++++ algorithms/Matrix-Expo/README.md | 32 +++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 algorithms/Matrix-Expo/MatrixExpo.cpp create mode 100644 algorithms/Matrix-Expo/README.md diff --git a/README.md b/README.md index 6f267be..91f15ed 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,4 @@ Happy Open Sourcing! - [Array Rotation By 1](algorithms/array_rotation_by_1) - [Magic Number](algorithms/magic_no/) - [KMP Algorithm](algorithms/Knuth-Morris-Pratt/) +- [Matrix Exponentiation](algorithms/Matrix-Expo/) diff --git a/algorithms/Matrix-Expo/MatrixExpo.cpp b/algorithms/Matrix-Expo/MatrixExpo.cpp new file mode 100644 index 0000000..2bc1c04 --- /dev/null +++ b/algorithms/Matrix-Expo/MatrixExpo.cpp @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +void mulMatrix(int a[][20],int b[][20],int n){ + int c[20][20]; + for(int i=0;i0){ + if(p&1) + mulMatrix(res,a,n); + mulMatrix(a,a,n); + p>>=1; + } + for(int i=0;i>n>>m; + for(int i=0;i>a[i][j]; + } + MatExpo(a,n,m); + for(int i=0;i Date: Tue, 1 Oct 2019 16:53:51 +0530 Subject: [PATCH 3/3] Added Union-Find Data Structure --- README.md | 1 + algorithms/disjoint_set_union/README.md | 17 ++++++ algorithms/disjoint_set_union/dsu.cpp | 77 +++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 algorithms/disjoint_set_union/README.md create mode 100644 algorithms/disjoint_set_union/dsu.cpp diff --git a/README.md b/README.md index 91f15ed..e2fafde 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,4 @@ Happy Open Sourcing! - [Magic Number](algorithms/magic_no/) - [KMP Algorithm](algorithms/Knuth-Morris-Pratt/) - [Matrix Exponentiation](algorithms/Matrix-Expo/) +- [Disjoint Set Union](algorithms/disjoint_set_union/) diff --git a/algorithms/disjoint_set_union/README.md b/algorithms/disjoint_set_union/README.md new file mode 100644 index 0000000..3d34027 --- /dev/null +++ b/algorithms/disjoint_set_union/README.md @@ -0,0 +1,17 @@ +# Disjoint Set Union + +An Algorithm For finding connected components to a node + +### Description + +A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on such a data structure: + +Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset. + +Union: Join two subsets into a single subset. + +Implemented using dynamic memory allocation + +### Implemented in: + +- [C++](dsu.cpp) \ No newline at end of file diff --git a/algorithms/disjoint_set_union/dsu.cpp b/algorithms/disjoint_set_union/dsu.cpp new file mode 100644 index 0000000..86084bc --- /dev/null +++ b/algorithms/disjoint_set_union/dsu.cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + +struct node{ + long data; + long rank; + node* parent; +}; + +void makeSet(map &mp,long data){ + node* temp=new node; + temp->data=data; + temp->rank=0; + temp->parent=temp; + mp[data]=temp; +} + +node* findRoot(map &mp,long data){ + auto it=mp.find(data); + if(it==mp.end()) + return NULL; + node* temp=it->second; + if(temp->parent==temp) + return temp; + temp->parent=findRoot(mp,temp->parent->data); + return temp->parent; +} + +void Union(map &mp,long data1,long data2){ + auto it1=mp.find(data1); + auto it2=mp.find(data2); + if(it1==mp.end() || it2==mp.end()) + return; + node* a=findRoot(mp,data1); + node* b=findRoot(mp,data2); + if(a==b) + return; + if(a->rank==b->rank){ + b->parent=a; + a->rank++; + } + else if(a->rank>b->rank) + b->parent=a; + else + a->parent=b; +} + +bool Find(map &mp,long data1,long data2){ + auto it1=mp.find(data1); + auto it2=mp.find(data2); + if(it1==mp.end() || it2==mp.end()) + return 0; + node* a=findRoot(mp,data1); + node* b=findRoot(mp,data2); + if(a==b) + return 1; + return 0; +} + +int main(){ + map mp; + for(long i=1;i<=10;++i){ + makeSet(mp,i); + } + Union(mp,1,2); + Union(mp,2,3); + Union(mp,1,3); + Union(mp,8,5); + Union(mp,5,6); + node* x=findRoot(mp,3); + node* y=findRoot(mp,5); + cout<data<<" "<data; + Union(mp,3,6); + x=findRoot(mp,3); + cout<<" "<data; +} \ No newline at end of file