-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_Graph_tree.cpp
More file actions
35 lines (35 loc) · 849 Bytes
/
19_Graph_tree.cpp
File metadata and controls
35 lines (35 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>>&graphs, int src,vector<int>&visited){
cout<<src<<" ";
visited[src]=1;
for(auto i:graphs[src]){
if(visited[i])continue;
dfs(graphs, i,visited);
}
}
void bfs(vector<vector<int>>&graphs, int src,vector<int>&visited){
queue<int>q;
q.push(src);
visited[src]=1;
while(!q.empty()){
int node=q.front();
q.pop();
cout<<node<<" ";
for(auto i:graphs[node]){
if(visited[i])continue;
q.push(i);
visited[i]=1;
}
}
}
int main(){
vector<vector<int>>graphs({{1,2,3},{0,4,5},{0},{0,6},{1},{1,7},{3},{5}});
int n=graphs.size();
vector<int>vis(n, 0),vis2(n,0);
cout<<"dfs print"<<endl;
dfs(graphs,0,vis);
cout<<endl<<"bfs print"<<endl;
bfs(graphs,0,vis2);
return 0;
}