4 Applications for DFS and BFS

 1- Count Numbers of Connected Component :

__________________________________________________

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
bool visited[N];
int numo_f_Components=0;
int n,m;
void dfs(int node){
if(visited[node]==1)return;
visited[node]=true;
for(auto newnode:adjlist[node])
dfs(newnode);

}
int main() {
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}

for(int i=0;i<n;i++){
if(!visited[i]) {
numo_f_Components++;

dfs(i);
}
}


return 0;
}
_____________________________________________________________________________________________________

2-the vertix belongs to which component (DFS)

______________________________________________________________________

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
int visited[N];
int numo_f_Components=0;
int n,m;
void dfs(int node){
if(visited[node]==1)return;
visited[node]=numo_f_Components;
for(auto newnode:adjlist[node])
dfs(newnode);

}
int main() {
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}

for(int i=0;i<n;i++){
if(!visited[i]) {
numo_f_Components++;

dfs(i);
}
}

for(int i=0;i<n;i++)cout<<visited[i]<<" ";
return 0;
}
______________________________________________________________________________________________________

3-the vertix belongs to which component (BFS)

_____________________________________

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
int visited[N];
int numo_f_Components=0;
int n,m;
void dfs(int node){
if(visited[node]==1)return;
visited[node]=numo_f_Components;
for(auto newnode:adjlist[node])
dfs(newnode);

}

void bfs(int node){

queue<int>q;
q.push(node);
while (!q.empty()){
node=q.front();
q.pop();
if(visited[node])continue;
visited[node]=numo_f_Components;
for (auto it: adjlist[node])q.push(it);


}

}


int main() {
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}

for(int i=0;i<n;i++){
if(!visited[i]) {
numo_f_Components++;

bfs(i);
}
}

for(int i=0;i<n;i++)cout<<visited[i]<<" ";
return 0;
}
___________________________________________________________________________________________________
4-Find The Path From A To B (DFS)
__________________________________
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
int visited[N];

int n,m;

vector<int>path;
bool done = false;
int target;
void findpathDFS(int node){
if(visited[node])return;
visited[node]=1;

path.push_back(node);

if(node==target && !done){
for(auto j:path)cout<<j<<" ";
cout<<endl;
done=true;
return;
}

for(auto it:adjlist[node])findpathDFS(it);
path.pop_back();

}

int main() {
cin>>n>>m>>target;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}

findpathDFS(1);


return 0;
}
____________________________________________________________________________________________________
5-Find The Path From A To B (BFS)
__________________________________
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
int visited[N];
int n,m;
vector<int>path;

int target;

int parent[N];
void findpathBFS(int node){
queue<int>q;
q.push(node);
visited[node]=1;
while (!q.empty()){
node=q.front();
q.pop();
for(auto it:adjlist[node]){
if (!visited[it]){
parent[it]=node;
visited[it]=1;
q.push(it);

}

}


}
path.push_back(target);
int cur=parent[target];
while (cur!=0){
path.push_back(cur);
cur=parent[cur];

}
reverse(path.begin(),path.end());
for(auto it:path)cout<<it<<' ';
}




int main() {
cin>>n>>m>>target;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}

findpathBFS(1);


return 0;
}
__________________________________________________________________________________________________
6- find the smallest lexicographical path(DFS) Can't doing this with BFS
______________________________________________________________________
 
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N=5e5+4;
vector<int>adjlist[N];
int visited[N];
int n,m;


vector<int>path;
bool done = false;
int target;
void findpathDFS(int node){
if(visited[node])return;
visited[node]=1;

path.push_back(node);

if(node==target && !done){
for(auto j:path)cout<<j<<" ";
cout<<endl;
done=true;
return;
}

for(auto it:adjlist[node])findpathDFS(it);
path.pop_back();

}



int main() {
cin>>n>>m>>target;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adjlist[x].push_back(y);
adjlist[y].push_back(x);

}
for(int i=1;i<=n;i++)
sort(adjlist[i].begin(),adjlist[i].end());

findpathDFS(1);



return 0;
}

______________________________________________________________________________________________________




Comments

Popular posts from this blog

Leet code (Reverse Integer)

Graph Theory(adjacency matrix,adjacency list)

Recursion(Part 1)