Skip to content

Commit f338c81

Browse files
authored
Merge pull request #203 from Ankitr19/master
Sieve New
2 parents 56b74e9 + 04236d1 commit f338c81

File tree

9 files changed

+222
-2
lines changed

9 files changed

+222
-2
lines changed

CONTRIBUTORS.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ Thanks for all your contributions :heart: :octocat:
66

77

88
[prateekiiest](https://github.com/prateekiiest) : KWOC Mentor
9-
109
| Github username | Pull Request | Status |
11-
| ------------- |:-------------:| -----:|
10+
|[Ankitr19](https://github.com/Ankitr19)| [#203]https://github.com/codeIIEST/Algorithms/pull/203 |OPEN|
1211
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#46](https://github.com/codeIIEST/Algorithms/pull/46), [#66](https://github.com/codeIIEST/Algorithms/pull/66), [#88](https://github.com/codeIIEST/Algorithms/pull/88), [#89](https://github.com/codeIIEST/Algorithms/pull/89), [#90](https://github.com/codeIIEST/Algorithms/pull/90), [#93](https://github.com/codeIIEST/Algorithms/pull/93), [#159](https://github.com/codeIIEST/Algorithms/pull/159) | CLOSED |
1312
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#94](https://github.com/codeIIEST/Algorithms/pull/94), [#95](https://github.com/codeIIEST/Algorithms/pull/95), [#112](https://github.com/codeIIEST/Algorithms/pull/112), [#151](https://github.com/codeIIEST/Algorithms/pull/151), [#174](https://github.com/codeIIEST/Algorithms/pull/174) | MERGED |
1413
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#206](https://github.com/codeIIEST/Algorithms/pull/206), [#207](https://github.com/codeIIEST/Algorithms/pull/207) | OPEN |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int MaxSumIncrsSubseq(int arr[],int n)
5+
{
6+
int i,j,mx=0;
7+
int MSIS[n]; //An array of initial size as that of the original array to store the maximum sum of the increasing subsequence till that point.
8+
for(i=0;i<n;i++)
9+
{
10+
MSIS[i]=arr[i];
11+
}
12+
/* The main condition to check if the next element of the array should be considered in the maximum sum subsequence array or not ,
13+
by checking the given two conditions:-
14+
1.) arr[i]>arr[j]
15+
2.)MSIS[i]<MSIS[j] + arr[i]
16+
*/
17+
for(i=1;i<n;i++)
18+
{
19+
for(j=0;j<i;j++)
20+
{
21+
if(arr[i]>arr[j] && MSIS[i]<MSIS[j]+arr[i])
22+
MSIS[i]=MSIS[j]+arr[i];
23+
}
24+
}
25+
for(i=0;i<n;i++)
26+
if(mx<MSIS[i])
27+
mx=MSIS[i];
28+
29+
return mx; // The maximum sum of the increasing subsequence.
30+
}
31+
32+
int main()
33+
{
34+
int arr[] = {2,3,100,5,6,101,1};
35+
int n = sizeof(arr)/sizeof(arr[0]);
36+
int ans = MaxSumIncrsSubseq(arr,n);
37+
cout<<ans<<"\n";
38+
return 0;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Maximum Sum in an increasing subsequence - Dynamic Programming
2+
3+
-This algorithm is a slight variation in the Longest Increasing Subsequence.
4+
-In this algorithm we insert a case to check whether the sum of the new increasing subsequence is greater than the previous one or not.
5+
6+
Example:-
7+
8+
arr[] = {2,3,100,5,6,101,1}
9+
10+
Now, here the maximum sum increasing subsequence will be = 206.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is the algortihm for Breadth First Search in a given graph.
2+
3+
-We take a starting vertex , and push all its adjacent vertexes in a queue.
4+
-Now we pop an element from a queue and push all its vertexes in the queue , and we also mark down these vertexes as visited.
5+
-After executing the code, we will get the vertex visited in a breadth first manner.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<bool>Visited;
5+
vector< vector<int> >Graph;
6+
7+
void edge(int a,int b)
8+
{
9+
Graph[a].push_back(b); //for directed
10+
11+
//If undirected graph is there, then add the following line
12+
// Graph[b].push_back(a);
13+
}
14+
15+
void BreadthFirstSearch(int u)
16+
{
17+
queue<int>q;
18+
q.push(u);
19+
Visited[u]=true;
20+
21+
while(!q.empty())
22+
{
23+
int f=q.front();
24+
q.pop();
25+
26+
cout<<f<<" ";
27+
int ln=Graph[f].size();
28+
for(int j=0;j<ln;j++)
29+
{
30+
if(!Visited[Graph[f][j]])
31+
{
32+
q.push(Graph[f][j]);
33+
Visited[Graph[f][j]]=true;
34+
}
35+
}
36+
}
37+
}
38+
39+
int main()
40+
{
41+
int n,e;
42+
cin>>n>>e;
43+
Visited.assign(n,false);
44+
Graph.assign(n,vector<int>());
45+
46+
int a,b;
47+
for(int i=0;i<e;i++)
48+
{
49+
cin>>a>>b;
50+
edge(a,b);
51+
}
52+
53+
for(int i=0;i<n;i++)
54+
{
55+
if(!Visited[i])
56+
{
57+
BreadthFirstSearch(i);
58+
}
59+
}
60+
return 0;
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is the algortihm for Depth First Search in a given graph.
2+
3+
-We take a starting vertex , and push all its adjacent vertexes in a stack.
4+
-Now we pop an element from a stack and push all its vertexes in the stack , and we also mark down these vertexes as visited.
5+
-After executing the code, we will get the vertex visited in a depth first manner.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void addEdge(vector<int>adj[],int u,int v)
5+
{
6+
//For undirected graph
7+
adj[u].push_back(v);
8+
adj[v].push_back(u);
9+
}
10+
11+
void DFSUtil(int u,vector<int>adj[],vector<bool>&visited)
12+
{
13+
visited[u]=true;
14+
cout<<u<<" ";
15+
for(int i=0;i<adj[u].size();i++)
16+
{
17+
if(visited[adj[u][i]]==false)
18+
DFSUtil(adj[u][i],adj,visited);
19+
}
20+
}
21+
22+
void DFS(vector<int>adj[],int V)
23+
{
24+
vector<bool>visited(V,false);
25+
for(int u=0;u<V;u++)
26+
{
27+
if(visited[u]==false)
28+
{
29+
DFSUtil(u,adj,visited);
30+
}
31+
}
32+
}
33+
34+
int main()
35+
{
36+
int V = 5;
37+
vector<int> adj[V];
38+
addEdge(adj, 0, 1);
39+
addEdge(adj, 0, 4);
40+
addEdge(adj, 1, 2);
41+
addEdge(adj, 1, 3);
42+
addEdge(adj, 1, 4);
43+
addEdge(adj, 2, 3);
44+
addEdge(adj, 3, 4);
45+
DFS(adj, V);
46+
cout<<"\n";
47+
return 0;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This is the famous algorithm to find the prime numbers from '1' to 'n'.
2+
It uses a logic that if a number 'p' is prime , then the numbers 2*p, 3*p, 4*p, .... are not prime.
3+
Suppose we have p as 2, and we know 2 is a prime number, then all its multiple, which are 4,6,8,10,.... must not be prime as they have 2 as a factor other then 1 and the number itself.
4+
Similarly, for all other number we can do that, and in the end all the numbers which are marked 'True' in the boolean array will be prime.
5+
6+
The complexity of this algorithm is O(N log(logN)).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> SieveOfEratosthenes(int n)
5+
{
6+
bool prime[n+1]; //To mark those numbers which are prime, prime numbers will be marked by a 'True'
7+
memset(prime,true,sizeof(prime));
8+
vector<int>Primes;
9+
/*
10+
In this nested loop , we start from the value 2, and marked all its multiple which are less then or equal to n,
11+
similarly for 3,4,5....upto n. The logic is simple, since every number which are multiple of 2 are having 2 as a
12+
factor other then 1 and the number itself, similarly for all other numbers.
13+
*/
14+
for(int p=2;p*p<=n;p++)
15+
{
16+
if(prime[p]==true)
17+
{
18+
for(int i=p*2;i<=n;i+=p)
19+
{
20+
prime[i]=false;
21+
}
22+
}
23+
}
24+
/* Pushing all the prime numbers in one container. */
25+
for(int p=2;p<=n;p++)
26+
{
27+
if(prime[p])
28+
{
29+
Primes.push_back(p);
30+
}
31+
}
32+
return Primes;
33+
}
34+
35+
int main()
36+
{
37+
int n;
38+
cout<<"Enter the number upto which prime numbers are to be found\n";
39+
cin>>n;
40+
vector<int> result;
41+
result = SieveOfEratosthenes(n);
42+
for(int i=0;i<result.size();i++)
43+
{
44+
cout<<result[i]<<" ";
45+
}
46+
cout<<"\n";
47+
}

0 commit comments

Comments
 (0)