diff --git a/C++14/Graphs/Basics/BFS.cpp b/C++14/Graphs/Basics/BFS.cpp new file mode 100644 index 0000000..00a37c6 --- /dev/null +++ b/C++14/Graphs/Basics/BFS.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +#define int long long int +#define endl '\n' +#define mod 1000000007 + +void bfs(vector *g, int start,int *vis) +{ + queue q; + q.push(start); // print starting node + cout<>n>>m; // n -> Number of Nodes ,, m -> Number of edges + vector g[n+1]; // An array of type vector of size N such that g[i] signifies the nodes connected to node i + int vis[n+1]={}; // An array to check which nodes are visited and which are remained + for(int i=0;i>x>>y; // defines that there is a bidirectional edge between Node x and Node y + g[x].push_back(y); // pushing y in g[x] which signifies that there is and edge from x to y + g[y].push_back(x); // pushing x in g[y] which signifies that there is and edge from y to x + } + bfs(g,1,vis); // Calling BFS from starting Node 1 + return 0; +} diff --git a/C++14/Maths/Basics/sieve-of-eratosthenes.cpp b/C++14/Maths/Basics/sieve-of-eratosthenes.cpp new file mode 100644 index 0000000..c5b3666 --- /dev/null +++ b/C++14/Maths/Basics/sieve-of-eratosthenes.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +#define int long long int +#define endl '\n' +#define mod 1000000007 + +bool prime[1000001]; +void sieve(int n) +{ + for(int i=2;i<=n;i++) + prime[i]=true; // setting all values as true + for(int i=2;i<=sqrt(n);i++) + { + if(prime[i]) + { + // Update all multiples of i greater than or equal to the square of it + // numbers which are multiple of i and are less than i^2 are already been marked. + for(int j=i*i;j<=n;j+=i) + prime[j]=false; + } + } +} +int32_t main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + #ifndef ONLINE_JUDGE + // for getting input from input.txt + freopen("input.txt", "r", stdin); + // for writing output to output.txt + freopen("output.txt", "w", stdout); + #endif + + int n; + cin>>n; + sieve(n); + for(int i=1;i<=n;i++) + { + if(prime[i]) + cout<