DFS with C++

IS MY DFS CODE ALRIGHT??? CAN anyone plzzzz check????????? will all the output be OK with my CODE??and one more question- is the forloop necessary here before these line??
{
if(color[2]==white)
dfsvisit(2,n_v);
}

code:


#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
#define white 0
#define gray 1
#define black 2
int color[100],adjmat[100][100],parent[100];

void dfsvisit(int u,int n_v)
{
int v;
color[u]=gray;
cout<<u; //1,2,3,6
for(v=1;v<=n_v;v++)
{
if(adjmat[u][v]==1 && color[v]==white)
{ //cout<<v;
parent[v]=u;
dfsvisit(v,n_v);
}
}
color[u]=black;
//cout<<u;
}
int main()
{
//freopen("dfs1.txt","r",stdin);
int n_v,n_e,i,j,k,x,y;
cin>>n_v>>n_e;
for(i=1;i<=n_v;i++)
{
for(j=1;j<=n_v;j++)
adjmat[i][j]=-1;
}

for(k=1;k<=n_e;k++)
{
cin>>x>>y;
adjmat[x][y]=1;
adjmat[y][x]=1;
}
//dfs(n_v,n_e);
for(int u=1;u<=n_v;u++)
{color[u]=white;
parent[u]=NULL;}
for(int u=1;u<=n_v;u++)
{
if(color[2]==white)
dfsvisit(2,n_v);
}
for(int i=1;i<=n_v;i++)
cout<<"color of vertix"<<i<<"is"<<color[i]<<endl;
for(i=1;i<=n_v;i++)
cout<<"parent of vertix"<<i<<"is"<<parent[i]<<endl;
return 0;
}
Topic archived. No new replies allowed.