C++ programs for the implementation of Breadth First Search(BFS) for a given graph

by Nideesh C on April 14, 2011 · 0 comments

in C++




#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
 
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
 
cout <<"enter initial vertex";
cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}

OUTPUT
 Enter no of vertices9
 Enter no of edges9
 EDGES
 1 2
 2 3
 1 5
 1 4
 4 7
 7 8
 8 9
 2 6
 5 7
 enter initial vertex1
 Visited vertices
 12 4 5 3 6 7 8 9



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ programs for the implementation of Depth-first search(DFS) for a given graph
  2. C++ programs to implement Graph Traversal Techniques – Depth First Search/Breadth First Search
  3. C++ programs – Implement the Prim’s algorithm to generate a minimum cost spanning tree
  4. C++ programs – Implement the Kruskal’s algorithm to generate a minimum cost spanning tree
  5. C++ program – Solve the single source shortest path problem Using Dijkstra’s algorithm

Leave a Comment

Previous post:

Next post: