FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Graph/BFS.cpp at master · kothariji/competitive-programming · GitHub
kothariji
/
competitive-programming
Public
Notifications
You must be signed in to change notification settings
Fork
500
Star
704
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
Graph
/
BFS.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (60 loc) · 1.34 KB
Breadcrumbs
competitive-programming
/
Graph
/
BFS.cpp
Copy path
File metadata and controls
62 lines (60 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
include
<
bits/stdc++.h
>
using
namespace
std
;
void
printdata
(vector<
int
> grph[],
int
v){
for
(
int
i=
0
;i<v;i++){
cout<<i;
for
(
int
p:grph[i]){
cout<<
"
->
"
<<p;
}
cout<<endl;
}
}
void
addata
( vector<
int
> grph[],
int
a,
int
b){
grph[a].
push_back
(b);
grph[b].
push_back
(a);
}
void
BFS
(vector<
int
> grph[],
bool
visited[],
int
s){
visited[s]=
true
;
queue<
int
> q;
q.
push
(s);
while
(!q.
empty
()){
int
k=q.
front
();
cout<<q.
front
()<<
"
"
;
for
(
int
p:grph[k]){
if
(visited[p]==
false
){
visited[p]=
true
;
q.
push
(p);
}
}
q.
pop
();
}
}
void
BFSdistinct
(vector<
int
> grph[],
int
v){
bool
visited[v];
int
cnt=
0
;
for
(
int
i=
0
;i<v;i++){
visited[i]=
false
;
}
for
(
int
i=
0
;i<v;i++){
if
(visited[i]==
false
){
BFS
(grph,visited,i);
cnt++;
}
}
cout<<endl;
cout<<cnt<<endl;
}
int
main
(){
int
v,e,a,b;
cout<<
"
Enter the number of vertices of in undirected graphs
"
<<endl;
cin>>v;
vector<
int
> grph[v];
cout<<
"
Enter the number of edges in graph
"
<<endl;
cin>>e;
for
(
int
i=
0
;i<e;i++){
cin>>a>>b;
addata
(grph,a,b);
}
printdata
(grph,v);
BFSdistinct
(grph,v);
}
Back
|
FazBrowse Home
|
New Git URL