Se parcurge un graf in adancime si latime plecand dintr-un acelasi
nod x. Sa se verifice daca nodurile sunt vizitate in aceeasi ordine.
#include<iostream>
#include<fstream>
using namespace std;
int n,a[100][100],m,viz[100]={0},xx,yy,t[100][100],kk,v[100];
void citire()
{ifstream f("gf");
f>>n>>m;
int x,y;
for(int i=1;i<=m;i++)
{f>>x>>y;
a[x][y]=a[y][x]=1;}
f>>xx>>yy>>kk;
}
void DF(int x)
{cout<<x<<" ";
v[x]=1;
for(int i=1;i<=n;i++)
if(a[x][i]==1 && v[i]==0)
DF(i);
}
void BF(int x, int k)
{int c[100],i,p,u,v,ii=1,jj=1;
c[1]=x;
viz[x]=1;
p=u=1;
while(p<=u)
{v=c[p];p++;jj=1;
for(i=1;i<=n;i++)
if(a[v][i]==1 && viz[i]==0)
{t[ii][jj]=i;
jj++;
u++;
c[u]=i;
viz[i]=1;}
if(t[ii][jj-1]!=0)
ii++;
}
for(i=1;i<=u;i++)
cout<<c[i]<<" ";
//cout<<endl;
}
int main()
{
citire();
cout<<"Parcurgere in adancime"<<endl;
DF(1);
cout<<endl<<"Parcurgere in latime"<<endl;
BF(1,0);
}