Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions BFSforgraphtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Author- Amar Singh */
#include<iostream>
#include<vector>
#include<queue>

using namespace std;
/* This is for undirected graph, Remove the second condition for Directed Graph */
void addEdge(vector<int>* graph, int u, int v)
{
graph[u].push_back(v);
graph[v].push_back(u); // for Undirected Graph
}

int main()
{
int n; //vertices
int e; //edges
int v, u;
cout<< "Enter Size of Vertices \n";
cin>> n;
cout<< "Enter Size of Edges \n";
cin>> e;
int copy;
copy= n;
int a[n+1]= {0}; //array for node visited or not
vector<int> graph[n+1];
for(int i=1; i<=e; i++)
{
cout<<"From ";
cin>>u;
cout<<"To ";
cin>>v;
addEdge(graph,u,v);
}
vector<int>::iterator it;
queue<int> q;
q.push(1);
a[1] = 1;
int x;
cout<<"BFS is\n";
while(!q.empty())
{
x = q.front();
q.pop();
cout<<x <<" ";
for(it=graph[x].begin(); it!=graph[x].end(); it++)
{
if(a[*it]!=1)
{
q.push(*it);
a[*it]=1;
}
}
}
return 0;
}