From f2f6b3c1f3fe5c32d65ef2f30526c9843c14be5c Mon Sep 17 00:00:00 2001 From: Abhishek Upadhayay <52717415+mr-abhi-k@users.noreply.github.com> Date: Fri, 23 Oct 2020 09:38:12 +0530 Subject: [PATCH 1/2] added most suitable bfs for competitive programing --- .../cpp/bfs for competitive programming.cpp | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Graphs/bfs/cpp/bfs for competitive programming.cpp diff --git a/Graphs/bfs/cpp/bfs for competitive programming.cpp b/Graphs/bfs/cpp/bfs for competitive programming.cpp new file mode 100644 index 000000000..24a62f100 --- /dev/null +++ b/Graphs/bfs/cpp/bfs for competitive programming.cpp @@ -0,0 +1,134 @@ +#include //includes all libraries mainly used in CP +using namespace std; + +/* +CODE INTRO:-- +this code has 3 functions: +1st- for adding edge +2nd- for printing graph as adjacency list +3rd- bfs implementation +*/ + +/* + +addedge function: +Arguments : 1.adjacency list(adj), 2.first vertex(u), 3.second vertex(v), 4.bool value(twoWay) to check whether itis directed or undirected +then fills list as u-->v +if undirected twoWay will be true and, +v-->u will also gets implemented + +*/ + +void addedge(vector> &adj, int u, int v, bool twoWay) +{ + adj[u].push_back(v);//u-->v + if(twoWay) + adj[v].push_back(u);//v-->u +} + +/* + +printgraph function: +Arguments : 1.adjacency list, 2.no. of vertex(n) +loops through whole vertex and goes inside the list of particular vertex to print all vertices +e.g: +[1]-->3-->4-->5 + | +[2]-->1-->3-->6 + | +[3]-->4-->3 + +o/p will be : 1->3->4->5->2->6 + +*/ + +void printgraph( vector> &adj, int n) +{ + for(int v=0;v"; + for(auto x:adj[v])//loops through particular vertex + cout<<"->"<> &adj,int n, int s) +{ + //vector> adj adjacency list representation + //int n no. of nodes + //int s source vetex + queue q; + vector used(n); //for marking visited nodes + vector d(n), p(n); //for distance and parent + vectorpath; + q.push(s);//first pushes this source vertex to the queue to find all the connected vertices of s + used[s]=true;//marked visited + p[s]=-1; + d[s]=0; + while(!q.empty())//loop until queue becomes empty + { + int v=q.front();//take first vertex of queue + path.push_back(v);//push it into path vector + q.pop(); + for(int u : adj[v])//find all vertices connected with that vertex + { + //for traversing + if(!used[u])//if that vertex is not visited make it visit + { + used[u]=true; + q.push(u); + d[u]=d[v]+1;//find distance + p[u]=v;//find parent + } + } + } + cout<<"path :";//prints shortest path + for(auto x:path) + cout<>n1; + cout<<"enter no. of edges :"; + cin>>n2; + cout<<"enter vertex"; + int u,v; + vector> adj(n1+1);//adjacency list + char ch; + for(int i=0;i>u; + cin>>v; + cout<<"y: undirected -"; + cin>>ch; + if(ch=='y') + addedge(adj,u,v,true); + else + addedge(adj,u,v,false); + } + printgraph(adj,n1); + cout<<"enter the sorce vertex : "; + int s; + cin>>s; + bfs(adj,n1,s);//finds bfs +} \ No newline at end of file From ec60363525389702a53da86b16700b34bb0b83c4 Mon Sep 17 00:00:00 2001 From: Abhishek Upadhayay <52717415+mr-abhi-k@users.noreply.github.com> Date: Fri, 23 Oct 2020 09:39:35 +0530 Subject: [PATCH 2/2] Update bfs for competitive programming.cpp --- Graphs/bfs/cpp/bfs for competitive programming.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Graphs/bfs/cpp/bfs for competitive programming.cpp b/Graphs/bfs/cpp/bfs for competitive programming.cpp index 24a62f100..d9e3276bf 100644 --- a/Graphs/bfs/cpp/bfs for competitive programming.cpp +++ b/Graphs/bfs/cpp/bfs for competitive programming.cpp @@ -119,7 +119,7 @@ int main() cout<<"enter two vertex :\n"; cin>>u; cin>>v; - cout<<"y: undirected -"; + cout<<"y: undirected -";// asks for directed or undirected cin>>ch; if(ch=='y') addedge(adj,u,v,true); @@ -131,4 +131,4 @@ int main() int s; cin>>s; bfs(adj,n1,s);//finds bfs -} \ No newline at end of file +}