Skip to content

Commit ed8d5f9

Browse files
authored
added code
1 parent 2d6eb60 commit ed8d5f9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Graph 1/get path - BFS renamed to Graph 1/get path - BFS.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,47 @@ Print the path in reverse order. That is, print v2 first, then intermediate vert
1111
#include <queue>
1212
using namespace std;
1313

14+
void BFS(vector<vector<int> > &graph , vector<bool> & visited ,int v, int sv , int ev , vector<int>& path)
15+
{
16+
if( sv == ev)
17+
{ path.push_back(sv); return ;}
18+
19+
20+
queue<int> pn;
21+
unordered_map<int, int> m;
22+
pn.push(sv);
23+
visited[sv]=1;
24+
25+
26+
while(!pn.empty())
27+
{
28+
int s= pn.front ();
29+
pn.pop();
30+
31+
if(graph[s][ev])
32+
{
33+
path.push_back(ev);
34+
while(s!=sv)
35+
{
36+
path.push_back(s);
37+
s=m[s];
38+
39+
}
40+
path.push_back(sv);
41+
return;
42+
43+
}
44+
45+
for(int i=0 ; i< v ; i++)
46+
{
47+
if(!visited[i]&& graph[s][i])
48+
{
49+
m[i]=s,pn.push(i), visited[i]=1;
50+
}
51+
}
52+
}
53+
54+
}
1455

1556
void getPath(int **edges, int n, bool *visited, int v1, int v2)
1657
{

0 commit comments

Comments
 (0)