From d2975986a9bc1ed379855a3b2433ab9769db8d2b Mon Sep 17 00:00:00 2001 From: SWAPNIL-SAGAR <85029234+SwapnilSagar@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:57:09 +0530 Subject: [PATCH] Created graph.cpp All traversal in graph --- C++/graph.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 C++/graph.cpp diff --git a/C++/graph.cpp b/C++/graph.cpp new file mode 100644 index 0000000..6478190 --- /dev/null +++ b/C++/graph.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +void graph(int n, int m, vector adj[]) +{ + for(int i=0;i>x>>y; + if(x<=n && y<=n) + { + adj[x].push_back(y); + adj[y].push_back(x); + } + } +} + +void traversalbfs(int n, int vis[],queue q, vector&bfs, vector adj[]){ + while(!q.empty()){ + int node=q.front(); + q.pop(); + bfs.push_back(node); + for(auto it:adj[node]){ + if(!vis[it]) + { + vis[it]=1; + q.push(it); + } + } + } +} + +void gprint(int n, vector adj[]){ + for(int i=1;i<=n;i++){ + cout< "; + for(int j=0;j&v){ + for(int i=0;i>n; + int m; + cout<<"No of edges"<>m; + cout<<"Enter nodes b/w edges"< adj[n+1]; + graph(n,m,adj); + int vis[n+1]={0}; + vector bfs; + queue q; + q.push(1); + vis[1]=1; + traversalbfs(n,vis,q,bfs,adj); + print(bfs); + return 0; +} \ No newline at end of file