/* Write C++ programs for the implementation of BFS for a given graph */
#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;
}
} |
#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
enterno of vertices9
ente 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
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.
I guess, that there must be a space between 1 and 2 in “Visited vertices
1(here should be a space?)2 4 5 3 6 7 8 9”
please give me bfs and dfs with queue from classes
my mail address is homatts@hotmail.com
Thanks a lot
Did you try it in Parallel using OpenMP or other Parallel Programming?!!
salam
mishe baraye neveshtane tashkhis derakhte poosha ba zabane c rahnamaim konid merc
the code is wrong… it is showing wrong output for some of the inputs like:
no of vertices: 4
no of edges: 3
edges:
1 3
3 2
6 2
initial vertex: 3
and the output should be: 3 1 2 6
and the output of your program is: 3 2 0 0
code is wrong it dos not work properly
i agree with mrityunjay
there showing only one error at the fourth as declaration syntax can you explain it y?
Gandu code galat ha tera…..yaki ha yaki…….
code is wrong…will not show the correct output…
Information businesses have all the time monetized news and do
higher when it’s tailor-made to their market.
plz give code for the dfs in c++