Skip to content

Commit 21ba2fe

Browse files
Added Some famous Algorithms
1 parent 7d71013 commit 21ba2fe

7 files changed

+345
-0
lines changed

Calc_nCr.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<iostream>
2+
#include<vector>
3+
4+
using namespace std;
5+
6+
const long long int MOD=1e9+7;
7+
long long int n,t,m,ans;
8+
9+
long long in(long long int a)
10+
{
11+
long long b=MOD-2;
12+
long long x=1,y=a;
13+
while(b > 0)
14+
{
15+
if(b%2 == 1)
16+
{
17+
x=(x*y);
18+
if(x>MOD) x%=MOD;
19+
}
20+
y = (y*y);
21+
if(y>MOD) y%=MOD;
22+
b /= 2;
23+
}
24+
return x;
25+
}
26+
27+
long long int C(int n, int r)
28+
{
29+
vector<long long> f(n+1,1);
30+
for(int i=2;i<n+1;i++)
31+
{
32+
f[i]=(f[i-1]*i)%MOD;
33+
}
34+
return((f[n]*((in(f[r])*in(f[n-r]))%MOD))%MOD);
35+
36+
}
37+
38+
int main()
39+
{
40+
cin>>t;
41+
for(int i=0;i<t;i++)
42+
{
43+
cin>>n;
44+
ans=0;
45+
int a=1;
46+
for(int j=0;j<n;j++)
47+
{
48+
cin>>m;
49+
cout<<C(n-1,j)<<endl;
50+
ans=(ans+(a*C(n-1,j)*m)%MOD)%MOD;
51+
a=a*-1;
52+
}
53+
cout<<ans<<endl;
54+
}
55+
}

Sieve_of_Eratosthenes.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<iostream>
2+
#include<bitset>
3+
using namespace std;
4+
5+
int primes[3000100]; //list of primes
6+
bitset <30000000> P; //instant prime check with boolean table
7+
8+
void generate( int N ) //sieve
9+
{
10+
P[2] = 1;
11+
primes[0] = 2; //set 2 as prime
12+
bool prime;
13+
int ct = 1, i, j;
14+
for ( i = 3; i <= N; i++ ) //range to sieve
15+
{
16+
prime = true;
17+
for ( j = 0; primes[j]*primes[j] <= i; j++ ) //loop through previous primes until root of current number to check
18+
{
19+
if ( i % primes[j] == 0 )
20+
{
21+
prime = false; //not prime
22+
break;
23+
}
24+
}
25+
if ( prime )
26+
{
27+
primes[ct] = i; //add to list of primes
28+
ct++;
29+
P[i] = true; //add to boolean table
30+
}
31+
}
32+
}
33+
34+
int main()
35+
{
36+
int N;
37+
cin>>N; //specify range
38+
generate(N);
39+
40+
while ( 1 )
41+
{
42+
cout<<"Enter 0 to end loop or N to check if N is prime:\n";
43+
cin>>N;
44+
if ( !N ) break;
45+
P[N] ? cout<<"Yes\n" : cout<<"No\n";
46+
}
47+
48+
return 0;
49+
}
50+

String Searching - KMP.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<iostream>
2+
#include<cstdio>
3+
#include<algorithm>
4+
#include<cmath>
5+
#include<cstring>
6+
#include<stack>
7+
#include<queue>
8+
#include<deque>
9+
#include<vector>
10+
#include<map>
11+
#include<set>
12+
#include<bitset>
13+
#include<ctime>
14+
#include<limits>
15+
#include<iomanip>
16+
#include<cstdlib>
17+
#include<list>
18+
#include<functional>
19+
#include<numeric>
20+
#include<utility>
21+
#include<sstream>
22+
23+
#define MAXIMUM ( 1 << 31 ) - 1
24+
#define MINIMUM ( 1 << 31 )
25+
#define pb push_back
26+
#define endl '\n'
27+
28+
typedef long long ll;
29+
using namespace std;
30+
31+
bool match[100005]; //All indices where pattern is present are 1
32+
string TEXT, PATTERN; //Text to search and pattern to search for
33+
int F[1000]; //Stores automaton states. Every index represents the largest partial match a prefix of that size has.
34+
35+
void build() //builds automaton for the pattern
36+
{
37+
int i, j;
38+
F[0] = F[1] = 0; //always true
39+
for ( i = 2; i <= PATTERN.length(); i++ )
40+
//we go an extra step because the failure function is 1-indexed
41+
//PATTERN is still 0 indexed
42+
//The 0 index in the array F represents an empty string. Every consequent element represents prefix of that length
43+
{
44+
j = F[i - 1];
45+
while ( true )
46+
{
47+
if ( PATTERN[j] == PATTERN[i-1] ) //If the char that extends partial match is same as last char of curr prefix
48+
{
49+
F[i] = j+1;
50+
break;
51+
}
52+
else if ( j == 0 ) //The best match is an empty string
53+
{
54+
F[i] = 0;
55+
break;
56+
}
57+
else j = F[j]; //Check for an even smaller partial match
58+
}
59+
}
60+
}
61+
62+
int find()
63+
{
64+
build();
65+
int i = 0, j = 0, ct = 0; //i loops over TEXT, j over PATTERN. ct is counts of patterns.
66+
while ( true )
67+
{
68+
if ( i == TEXT.length() ) break; //Full TEXT has been searched
69+
if ( TEXT[i] == PATTERN[j] ) //The state of the automata can be extended to the next one
70+
{
71+
i++;
72+
j++;
73+
if ( j == PATTERN.length() ) //Match found
74+
{
75+
match[i - PATTERN.length()] = true; //Mark indice
76+
ct++;
77+
j = F[j]; //Since a full match has been found, we can start at this index of TEXT and assume that another
78+
//smaller match has already been extended up till F[j]
79+
}
80+
}
81+
else if ( j ) j = F[j]; //If you can't extend this match, see if you can extend a smaller match
82+
else i++; //Can't extend shit on this TEXT index, try with the next one
83+
}
84+
return ct;
85+
}
86+
87+
88+
int main()
89+
{
90+
int i, j, k;
91+
cin>>TEXT>>PATTERN;
92+
cout<<find()<<endl;
93+
94+
return 0;
95+
}

bfs.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Breadth First Search
2+
* Number of Vertices: n
3+
* Source: s
4+
* Distance Array: dist[]
5+
* Adjacency List: adj[][]
6+
* Infinite Constant: inf
7+
*/
8+
9+
void bfs()
10+
{
11+
for(int i=0;i<n;++i)
12+
{
13+
dist[i] = inf;
14+
}
15+
16+
queue<int> q;
17+
q.push(s);
18+
dist[s] = 0;
19+
20+
while(!q.empty())
21+
{
22+
int u = q.front();
23+
q.pop();
24+
25+
for(int i=0;i<adj[u].size();++i)
26+
{
27+
int v = adj[u][i];
28+
if(dist[v]==inf)
29+
{
30+
dist[v] = dist[u]+1;
31+
q.push(v);
32+
}
33+
}
34+
}
35+
}

dfs.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Depth First Search
2+
* Adjacency List: adj[][]
3+
* Visited Check: vis[]
4+
*/
5+
6+
void dfs(int u)
7+
{
8+
vis[u] = 1;
9+
for(int i=0;i<adj[u].size();++i)
10+
{
11+
int v = adj[u][i];
12+
if(!vis[v])
13+
{
14+
dfs(v);
15+
}
16+
}
17+
}

dijkstra.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Dijkstra's Single Source Shortest Path Algorithm
2+
* Number of Vertices: n
3+
* Source: s
4+
* Distance Array: dist[]
5+
* Adjacency List: adj[][] tuple(vertex, weight)
6+
* Infinte Constant: inf
7+
*/
8+
9+
void dijkstra()
10+
{
11+
for(int i=0;i<n;++i)
12+
{
13+
dist[i] = inf;
14+
}
15+
16+
set< tuple<int,int> > q;
17+
dist[s] = 0;
18+
q.insert(make_tuple(0,s));
19+
20+
while(!q.empty())
21+
{
22+
int u = get<1>(*q.begin());
23+
24+
for(int i=0; i<adj[u].size(); ++i)
25+
{
26+
int v = get<0>(adj[u][i]);
27+
int w = get<1>(adj[u][i]);
28+
29+
if(dist[v] > dist[u]+w)
30+
{
31+
if(dist[v] != inf)
32+
{
33+
q.erase(q.find(make_tuple(dist[v],v)));
34+
}
35+
dist[v] = dist[u]+w;
36+
q.insert(make_tuple(dist[v],v));
37+
}
38+
}
39+
}
40+
}

kruskal.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Kruskal's Minimum Spanning Tree Algorithm
2+
* Edge List: e tuple(weight,a,b)
3+
* Number of Edges: m
4+
* Number of Vertices: n
5+
* Parent in DS: par[]
6+
* Height in DS: rank[]
7+
*/
8+
9+
int findPar(int v)
10+
{
11+
if(par[v]==v)
12+
{
13+
return v;
14+
}
15+
par[v] = findPar(par[v]);
16+
}
17+
18+
void merge(int a, int b)
19+
{
20+
if(rank[a] < rank[b])
21+
{
22+
par[a] = b;
23+
}
24+
else
25+
{
26+
par[b] = a;
27+
if(rank[a] == rank[b])
28+
{
29+
rank[a]++;
30+
}
31+
}
32+
}
33+
34+
void kruskal()
35+
{
36+
sort(e,e+m);
37+
38+
for(int i=0;i<n;++i)
39+
{
40+
rank[i] = 1;
41+
par[i] = i;
42+
}
43+
44+
for(int i=0;i<m;++i)
45+
{
46+
int a = get<1>(e), b = get<2>(e);
47+
48+
if(findPar(a) != findPar(b))
49+
{
50+
merge(findPar(a),findPar(b));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)