Depth First Search Logical Error
Nov 11, 2012 at 3:59am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include<iostream>
using namespace std;
class dfs
{
int v,i,matrix[20][20],j,color[20];
public :
dfs();
void graph();
void traversal(int );
};
dfs::dfs()
{
for (int i=1;i<=20;i++)
color[i]=0;
}
void dfs::graph()
{
int e,s;
cout<<"Enter the number of vertices\n" ;
cin>>v;
cout<<"Enter the number of edges\n" ;
cin>>e;
cout<<"Enter the edges\n" ;
for (int k=1;k<=e;k++)
{
cin>>i>>j;
matrix[i][j]=1;
}
cout<<"Enter the source vertex\n" ;
cin>>s;
traversal(s);
}
void dfs::traversal(int s)
{
cout<<s<<" " ;
color[s]=1;
for (j=1;j<=v;j++)
if (matrix[s][j]==1&&color[j]==0)
traversal(j);
}
int main()
{
dfs a;
a.graph();
system("pause" );
return 0;
}
I am not getting the output for all inputs. Please help.
Nov 11, 2012 at 4:12am UTC
What your correct output that you should have?
Last edited on Nov 11, 2012 at 4:13am UTC
Nov 11, 2012 at 4:17am UTC
Enter the number of vertices
8
Enter the number of edges
9
Enter the edges
1 4
1 8
1 2
1 5
4 8
2 8
5 7
5 6
8 3
Enter the source vertex
1
1 2 8 3 Press any key to continue . . .
Nov 11, 2012 at 4:18am UTC
So I am not getting all the vertices visited. :(
Nov 11, 2012 at 4:20am UTC
How did your output become '1 2 8 3'?
Nov 11, 2012 at 4:25am UTC
Hey 2 and 8 are adjacent to 1 and 3 is adjacent to 8.. program does not work after that..
Nov 11, 2012 at 4:34am UTC
Help please..
Topic archived. No new replies allowed.