0% found this document useful (0 votes)
0 views4 pages

DAA Program-1

The document contains a C++ program that implements graph traversal using the Breadth First Search (BFS) algorithm. It defines a graph class with methods to prepare an adjacency list and perform BFS on the graph. The main function allows user input for vertices and edges, and outputs the BFS traversal result.

Uploaded by

gauravbajaj2706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

DAA Program-1

The document contains a C++ program that implements graph traversal using the Breadth First Search (BFS) algorithm. It defines a graph class with methods to prepare an adjacency list and perform BFS on the graph. The main function allows user input for vertices and edges, and outputs the BFS traversal result.

Uploaded by

gauravbajaj2706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM-1

Aim:- Program to implement graph traversal using Breadth First Search.


Source Code:-
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<vector>
#include<list>
#include<queue>
using namespace std;
class graph{
public:
unordered_map<char,vector<char>>adjList;
vector<char>vertices;
vector<pair<char,char>>edges;
void prepareAdjList(){
for(int i=0;i<edges.size();i++){
char u=edges[i].first;
char v=edges[i].second;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
}
void bfs(unordered_map<char,bool>&visited,vector<char>&ans,char node){
queue<char>q;
q.push(node);
visited[node]=1;
while(!q.empty()){
char frontNode=q.front();
q.pop();
ans.push_back(frontNode);
for(auto i:adjList[frontNode]){
if(!visited[i]){
q.push(i);
visited[i]=1;
}
}
}
}
vector<char>BFS(){
vector<char>ans;
unordered_map<char,bool>visited;
for(int i=0;i<vertices.size();i++){
if(!visited[vertices[i]]){
bfs(visited,ans,vertices[i]);
}
}
return ans;
}
};
int main(){
graph g;
int n;
cout<<"Enter no. of vertices: ";
cin>>n;
cout<<"Enter all vertices: ";
for(int i=0;i<n;i++){
char x;
cin>>x;
g.vertices.push_back(x);
}
int m;
cout<<"Enter no. of edges for undirected graph: ";
cin>>m;
for(int i=0;i<m;i++){
char u;
char v;
cout<<"Enter edge pair: ";
cin>>u>>v;
pair<char,char>p={u,v};
g.edges.push_back(p);
}
// g.edges={{0,4},{4,2},{4,1},{1,5},{5,3},{3,2}};
g.prepareAdjList();
vector<char>ans;
ans=g.BFS();
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" ";
}
return 0;
}
OUTPUT:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy