From e7d1ce1e6ab8e209075509cda9513cc029644ecd Mon Sep 17 00:00:00 2001 From: rahulogre Date: Sat, 27 Oct 2018 02:29:06 +0530 Subject: [PATCH] Shortest Path finding using BFS in an undirected graph --- shortest_path_BFS.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 shortest_path_BFS.cpp 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; + } + +