-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS
186 lines (147 loc) · 4.29 KB
/
BFS
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint(a,b) scanf("%d %d",&a,&b)
#define sfl(a,b) scanf("%ld %ld",&a,&b)
#define sfll(a,b) scanf("%lld %lld",&a,&b)
#define sfd(a,b) scanf("%lf %lf",&a,&b)
#define sff(a,b) scanf("%f %f",&a,&b)
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define LL long long
#define L long
#define mem(c,v) memset(c,v,sizeof(c))
#define ui unsigned int
#define cp(a) cout<<" "<<a<<" "<<endl
#define ull unsigned long long int
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define inf 1<<30 //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
using namespace std;
//..................................................................................................................
template<class T> T setbit(T n, T pos){n=n|(1<<pos); return n;}
template<class T> T checkbit(T n, T pos){n=n&(1<<pos); return n;}
template<class T> T gcd(T a, T b ) {return b==0?a:gcd(b,a%b);}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
vector<int>G[100]; // declared a graph with 2D vector
int level[100],par[100];
bool visit[100];
// in bfs actually the level is the main distance from one node to another node. So, we have to
// handle level to find out the distance node to node.And we can not visit a node more than one time also.
// bfs goes from the below::
void bfs(int node, int source)
{
memset(level,0,sizeof(level));
memset(par,0,sizeof(par));
memset(visit,false,sizeof(visit));
// just initialized the all array with 0 and false.
queue<int>Q;
// declared a queue data structure to keep the data or node value.
level[source]=0; // initially the source is in 0 mode
visit[source]=true; // initially we visit the source node for 1 time.
Q.push(source); // initially we just pushed the source value on the queue.
while(!Q.empty())
{
int u=Q.front();
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i]; // took the size value of G[u]
if(!visit[v])
{
level[v]=level[u]+1;
par[v]=u;
visit[v]=true;
Q.push(v);
}
}
Q.pop();
}
for(int i=1;i<=node;i++)
{
cout << "Distance : " << source << " to " << i << " is " << level[i] << endl;
}
}
void path(int src, int endingnode)
{
if(endingnode == src)
{
cout << src << " -> ";
}
else if(!par[endingnode])
{
cout << " no path here ..";
}
else
{
path(src,par[endingnode]);
cout << endingnode << " -> ";
}
}
int main()
{
int node,edge; // declared node and edges
cout << "Enter number of Nodes (space) number of Edges: ";
cin >> node >> edge; // read node and edges
cout << "Take Nodes: (type 'Enter' button after taking each row of node)\n";
for(int i=1;i<=edge;i++)
{
cout << "for Edge " << i << " : ";
int x,y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
// inserted the values of x and y to the graph.
}
// showed the graph and see which node is connected exactly which one.
cout << "\n graph: \n";
for(int i=1;i<=node;i++)
{
cout << "for node " << i << " is connected with : ";
int sze = G[i].size();
for(int j=0;j<G[i].size();j++)
{
cout << G[i][j] << " ";
if(sze > 1)
{
cout << "and ";
sze--;
}
}
cout << endl;
}
cout << "\n Enter your Source node : ";
int src;
cin >> src;
bfs(node,src);
cout << "\n Enter your Ending node to see the Path from your Source node : ";
int endingnode;
cin >> endingnode;
cout << "\n Path : ";
path(src,endingnode);
return 0;
}