Skip to content
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
52 changes: 52 additions & 0 deletions C++14/Graphs/Basics/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl '\n'
#define mod 1000000007

void bfs(vector<int> *g, int start,int *vis)
{
queue<int> q;
q.push(start); // print starting node
cout<<start<<" "; // print starting node
vis[start]++; // Mark vis[start] as visited
while(!q.empty())
{
int front=q.front(); // storing front element
q.pop(); // poping the front element
for(int i=0;i<g[front].size();i++) // Traversing all directly connected nodes to node front
{
if(!vis[g[front][i]]) // Checking if the node is not been already visited
{
vis[g[front][i]]++; // Making the node visited
cout<<g[front][i]<<" "; // printing the node
q.push(g[front][i]); // pushing the node to queue
}
}
}
}
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,m,x,y;
cin>>n>>m; // n -> Number of Nodes ,, m -> Number of edges
vector<int> 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<m;i++)
{
cin>>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;
}
44 changes: 44 additions & 0 deletions C++14/Maths/Basics/sieve-of-eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
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<<i<<" "; // printing the prime numbers till n
}
return 0;
}