Skip to content

Commit b067faa

Browse files
committed
INIT
0 parents  commit b067faa

20 files changed

+1197
-0
lines changed

Bit-mask/Bitmask_Basic.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Reference -> https://www.youtube.com/watch?v=7FmL-WpTTJ4
2+
// We are using bitmask to store the numbers between 1 and 10 (in integers)
3+
// left shifting (x<<y) is equivalent to multiplying x with 2^y
4+
// right shifting (x>>y) is equivalent to dividing x with 2^y.
5+
6+
#include<bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
// If element is present it will get deleted and if its not there it will get added
11+
void erase_or_add(int n, int& subset)
12+
{
13+
subset = subset ^ 1<<(n-1);
14+
}
15+
void display(int subset)
16+
{
17+
cout<<"The subset is: ";
18+
// i is the ith bit from right to left
19+
// 0th bit represents 1
20+
for(int i=0;i<9;i++)
21+
{
22+
if(1<<i&subset)
23+
cout << i+1<<" ";
24+
}
25+
cout<<"\n";
26+
}
27+
28+
int main()
29+
{
30+
// num represents subset of numbers from 1 to 10
31+
int num = 15;
32+
display(num);
33+
// delete the d from subset num
34+
int d = 4;
35+
erase_or_add(d,num);
36+
display(num);
37+
38+
d=9;
39+
erase_or_add(d,num);
40+
display(num);
41+
}

DP/LIS.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Longest Increasing Subsequence
3+
Complexity for LIS Algo -> O(NlogN)
4+
N-number of elements
5+
*/
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
// NOTE : aux is not necesserily the LIS but its length is same as LIS
11+
int length_LIS(int arr[], int n)
12+
{
13+
vector<int> aux; //auxiliary vector stores the array elements likely to be LIS
14+
for(int i = 0; i < n; i++)
15+
{
16+
// Change lower_bound to upper_bound for non-decreasing subsequence
17+
auto itr = lower_bound(aux.begin(), aux.end(), arr[i]);
18+
if (itr == aux.end())
19+
aux.push_back(arr[i]); // No element is larger than arr[i]
20+
else
21+
*itr = arr[i]; // replace the element in aux with elemwnt just smaller than it.
22+
}
23+
return aux.size();
24+
25+
}
26+
27+
void find_LIS(int arr[],int n)
28+
{
29+
vector<int> aux(n, 0);
30+
vector<vector<int>> parent(n);
31+
32+
aux[0] = arr[0];
33+
34+
parent[0].push_back(arr[0]);
35+
int aux_size = 1;
36+
for (int i = 1; i < n; i++)
37+
{
38+
39+
auto it =lower_bound(aux.begin(), aux.begin() + aux_size, arr[i]);
40+
41+
if (it == aux.begin() + aux_size)
42+
{
43+
aux[aux_size] = arr[i];
44+
45+
parent[aux_size] = parent[aux_size - 1];
46+
parent[aux_size].push_back(arr[i]);
47+
aux_size++;
48+
}
49+
else
50+
{
51+
if (*it != arr[i]) {
52+
aux[it- aux.begin()] = arr[i];
53+
54+
parent[it - aux.begin()][parent[it - aux.begin()].size() - 1] = arr[i];
55+
}
56+
}
57+
58+
}
59+
60+
cout << "length " << aux_size << endl;
61+
62+
for (auto x : parent[aux_size - 1]) {
63+
cout << x << " ";
64+
}
65+
}
66+
67+
int main()
68+
{
69+
int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
70+
int n= sizeof(arr)/sizeof(int);
71+
72+
// Strictly increasing sub sequence
73+
cout << "Length of LIS is :" << length_LIS(arr,n) << endl;
74+
75+
// Print the LIS
76+
find_LIS(arr,n);
77+
}
78+
79+
// Intersting question :
80+
// Given arr1[N], arr2[M] 1<M<N<10^5
81+
// Find minimum elements you need to add in arr1 such that arr2 is a subsequence of arr1?
82+
// Soln: Codenation 1 april 2020 : hint(use maps, LIS)

DP/digit_dp.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Reference -> https://www.youtube.com/watch?v=heUFId6Qd1A
2+
// F(x) returns boolean value
3+
// General -> How many integers x in range [0,R] obey F(x)
4+
// Example -> Find cnt of numbers betn L and R which have a sum of digits = x
5+
// 1 <= L <= R <= 10^18
6+
// 1 <= X <= 180
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
// Max no of dights in a number be 100(Not needed)
11+
int dp[101][181][2];
12+
// boolena value tight stores checks if we can place any number at indx(R.size()-n) or we need we can place till ub
13+
int digit_dp(string R,int n,int sum,bool tight)
14+
{
15+
if(sum < 0)
16+
return 0;
17+
if(n==0)
18+
{
19+
if(sum == 0)
20+
return 1;
21+
return 0;
22+
}
23+
24+
if(dp[n][sum][tight] != -1)
25+
return dp[n][sum][tight];
26+
27+
// ub is upper bound till which digit we can use at that particular index
28+
int ub = 9,ans=0;
29+
if(tight)
30+
ub = (R[R.size()-n]-'0');
31+
for(int i=0;i<=ub;i++)
32+
{
33+
// Once a number is non tight it won't ever be tight in future
34+
// A tight will be tight only if we keep on passing the higest number possible at that indx
35+
ans += digit_dp(R,n-1,sum-i,tight&(i==ub));
36+
}
37+
return dp[n][sum][tight] = ans;
38+
}
39+
40+
int main()
41+
{
42+
string L = "";
43+
string R = "490447834749";
44+
int x = 20;
45+
memset(dp,-1,sizeof dp);
46+
cout<<digit_dp(R,R.size(),x,1);
47+
}

DP/travelling_salesman_prob.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Reference -> https://www.youtube.com/watch?v=QukpHtZMAtM&list=PLb3g_Z8nEv1icFNrtZqByO1CrWVHLlO5g&index=4
2+
// Reference -> https://www.thecrazyprogrammer.com/2017/05/travelling-salesman-problem.html
3+
// Time Complexcity -> O(n^2 * 2^n)
4+
// Space Complexcity -> O(2^n * n)
5+
// NOTE : Whenever we feel a need to pass a subset of set we can use this technique of digit dp
6+
// Que. Find minimum distance to travel if travellers wishes to visit every city atleast once and reach back to his starting location at the end
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
#define MAX 9999
11+
12+
int n=4; // Number of the places want to visit
13+
14+
//Next distan array will give Minimum distance through all the position
15+
int distan[10][10] = {
16+
{0, 10, 15, 20},
17+
{10, 0, 35, 25},
18+
{15, 35, 0, 30},
19+
{20, 25, 30, 0}
20+
};
21+
22+
int all_visited = (1<<n) -1;
23+
int dp[16][4];
24+
25+
26+
int TSP(int visited,int position)
27+
{
28+
// Initially checking whether all the places are visited or not
29+
if(visited==all_visited)
30+
return distan[position][0];
31+
32+
if(dp[visited][position]!=-1)
33+
return dp[visited][position];
34+
35+
// init
36+
int answer = MAX;
37+
38+
// chack for each city
39+
for(int city=0;city<n;city++)
40+
if((visited&(1<<city))==0) // is the city visited check
41+
answer = min(answer, distan[position][city] + TSP( visited|(1<<city),city));
42+
43+
return dp[visited][position] = answer;
44+
}
45+
46+
int main()
47+
{
48+
memset(dp,-1,sizeof dp);
49+
cout<<"Minimum Distance Travelled by traveller is "<<TSP(1,0);
50+
51+
return 0;
52+
}

Graph/BFS.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
#define LIM 1007
5+
6+
vector<int> adj[LIM];
7+
// vector<int> dist(LIM,-1);
8+
// vector<int> parent(LIM,-1);
9+
int visited[LIM];
10+
11+
void addEdge(int u, int v)
12+
{
13+
adj[u].push_back(v);
14+
adj[v].push_back(u);
15+
}
16+
17+
void bfs(int src)
18+
{
19+
queue<int> q;
20+
int d=0;
21+
// dist[src] = d;
22+
// parent[src] = -1;
23+
q.push(src);
24+
visited[src]=true;
25+
while(!q.empty())
26+
{
27+
int u = q.front();
28+
q.pop();
29+
30+
cout<<u<<" ";
31+
for(int v : adj[u])
32+
{
33+
if(!visited[v])
34+
{
35+
// dist[v] = dist[u] + 1;
36+
// parent[v] = u;
37+
38+
visited[v]=true; //NOTE : We put visited here inside the inner loop
39+
q.push(v);
40+
}
41+
42+
}
43+
}
44+
}
45+
46+
47+
int main()
48+
{
49+
int V,i,src,u,v,wt,E;
50+
51+
cout<<"Enter number of vertices and edges: ";
52+
cin>>V>>E;
53+
54+
55+
for(i=0;i<E;i++)
56+
{
57+
cout<<i+1<<". Enter vertex name u and v: ";
58+
cin>>u>>v;
59+
addEdge(u,v);
60+
}
61+
62+
63+
cout<<"Enter source node for iterative bfs: ";
64+
cin>>src;
65+
cout<<"BFS=> ";
66+
bfs(src);
67+
cout<<endl;
68+
69+
70+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Reference -> https://www.youtube.com/watch?v=FtN3BYH2Zes
3+
Minimum distance of src from every other node present in graph
4+
Complexity for Bellman Ford's Algo -> O(E*V)
5+
E-Edges V-Vertices
6+
*/
7+
#include<bits/stdc++.h>
8+
#define LIM 3000
9+
#define INF 1e5+3
10+
11+
using namespace std;
12+
13+
14+
//NOTE: Here reprentation of graph is done a bit differnetly
15+
typedef pair<int,int> pii;
16+
17+
vector<vector<int>> G(LIM,vector<int>(3));
18+
vector<int> min_dist(LIM,INF);
19+
int V,E;
20+
21+
/*
22+
IMP NOTE: If there is a cycle with total sum negative then this algo WON'T WORK
23+
We baically relax the edges V-1 times
24+
*/
25+
void bellman_ford(int src)
26+
{
27+
min_dist[src] = 0;
28+
int u,v,k;
29+
for(k=0;k<V-1;k++)
30+
{
31+
for(auto itr : G)
32+
{
33+
int u = itr[0];
34+
int v = itr[1];
35+
int wt = itr[2];
36+
37+
min_dist[v] = min(min_dist[v],min_dist[u]+wt);
38+
}
39+
}
40+
41+
// If the min min_dist is still changing even after V-1 iterstions means there is no soln.
42+
for(auto itr : G)
43+
{
44+
int u = itr[0];
45+
int v = itr[1];
46+
int wt = itr[2];
47+
48+
if(min_dist[v] > min_dist[u]+wt)
49+
{
50+
cout << "There is a negtive cycle\n";
51+
break;
52+
}
53+
}
54+
55+
}
56+
57+
int main()
58+
{
59+
int i,src=0,u,v,wt;
60+
61+
cout<<"Enter number of vertices and edges: ";
62+
cin>>V>>E;
63+
cout<<endl;
64+
65+
for(i=0;i<8;i++)
66+
{
67+
cout<<"Enter vertex name u , v and weight: ";
68+
cin>>u>>v>>wt;
69+
70+
G[i][0] = u;
71+
G[i][1] = v;
72+
G[i][2] = wt;
73+
}
74+
75+
cout<<"Enter the source node: ";
76+
cin>>src;
77+
bellman_ford(src);
78+
cout<<endl;
79+
80+
cout<<"Min distace all nodes from "<<src<<"is:\n";
81+
for(int i=0;i<V;i++)
82+
cout<<i<<" : "<<min_dist[i]<<endl;
83+
}

0 commit comments

Comments
 (0)