Skip to content
This repository was archived by the owner on Nov 26, 2019. It is now read-only.

added matrix exponentiation #178

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ 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/)
- [Matrix Exponentiation](algorithms/Matrix-Expo/)
- [Disjoint Set Union](algorithms/disjoint_set_union/)
29 changes: 29 additions & 0 deletions algorithms/Knuth-Morris-Pratt/README.md
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions algorithms/Knuth-Morris-Pratt/kmpalgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//KMP Algo
#include<iostream>
#include<string>
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<sz){
if(s[l]==s[r]){
a[r++]=++l;

}
else if(l==0){
a[r++]=l;
}
else
l=a[l-1];
}
l=0;r=0;
while(l<str.length()){
if(str[l]==s[r]){
l++;r++;
}
if(r==sz){
cnt++;
r=a[r-1];
}
else if(l<str.length() && str[l]!=s[r]){
if(r==0)
l++;
else
r=a[r-1];
}
}
cout<<cnt;
// cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
return 0;
}
57 changes: 57 additions & 0 deletions algorithms/Matrix-Expo/MatrixExpo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<iostream>
#include<cstring>

using namespace std;

void mulMatrix(int a[][20],int b[][20],int n){
int c[20][20];
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
c[i][j]=0;
for(int k=0;k<n;++k){
c[i][j]+=(a[i][k]*b[k][j]);
}
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
a[i][j]=c[i][j];
}
}

void MatExpo(int a[][20],int n,int p){
int res[20][20];
memset(res,0,sizeof res);
for (int i = 0; i < n; ++i)
{
res[i][i]=1;
}

while(p>0){
if(p&1)
mulMatrix(res,a,n);
mulMatrix(a,a,n);
p>>=1;
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
a[i][j]=res[i][j];
}
}

int32_t main(){
int a[20][20],n,m;
cin>>n>>m;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
cin>>a[i][j];
}
MatExpo(a,n,m);
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)
cout<<a[i][j]<<" ";
cout<<'\n';
}
// cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
return 0;
}
32 changes: 32 additions & 0 deletions algorithms/Matrix-Expo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Matrix Exponentiation

An Algorithm For finding Exponentiation of a Matrix

### Input Format

Input:
Enter N of (NxN matrix) and Power to be calculated
Enter the matrix elements

### Output Format

Calculated Matrix

### Sample Input

```
2 2
1 1
1 0
```

### Sample Output

```
2 1
1 1
```

### Implemented in:

- [C++](MatrixExpo.cpp)
17 changes: 17 additions & 0 deletions algorithms/disjoint_set_union/README.md
Original file line number Diff line number Diff line change
@@ -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)
77 changes: 77 additions & 0 deletions algorithms/disjoint_set_union/dsu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include<iostream>
#include<map>
using namespace std;

struct node{
long data;
long rank;
node* parent;
};

void makeSet(map<long,node*> &mp,long data){
node* temp=new node;
temp->data=data;
temp->rank=0;
temp->parent=temp;
mp[data]=temp;
}

node* findRoot(map<long,node*> &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<long,node*> &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<long,node*> &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<long,node*> 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<<x->data<<" "<<y->data;
Union(mp,3,6);
x=findRoot(mp,3);
cout<<" "<<x->data;
}