diff --git a/shortest_path_BFS.cpp b/shortest_path_BFS.cpp new file mode 100644 index 0000000..3f4d390 --- /dev/null +++ b/shortest_path_BFS.cpp @@ -0,0 +1,90 @@ + //finding shortest path in an undirected graph using BFS + #include + using namespace std; + + #define pb push_back + #define MAX 100001 + + vector G[MAX]; + int parent[MAX]; + bool visited[MAX]; + + + void bfs(int s){ + + queue q; + q.push(s); + visited[s] = true; + + while(!q.empty()){ + int node = q.front(); + q.pop(); + + for(int i=0 ; i ans; + for(int i=e ; i!=-1 ; ){ + ans.pb(i); + i = parent[i]; + } + + reverse(ans.begin(), ans.end()); + + int i; + for(i=0 ; i "; + } + cout<>nodes>>edges; + + for(int i=0; i >u>>v; + + G[u].pb(v); + G[v].pb(u); + + } + + int start,end; + cin>>start>>end; + + shortest_path(start,end); + + return 0; + } + +