Sa se parcurga un graf graf in latime (BF)

 

 

#include<fstream.h>
#include<conio.h>
struct nod
{
int inf;
nod* adr;
};

nod* l[20];
int c[20],s[20],i,sf,n;

void citire(char fisier[10],nod*l[20],int& n)
{nod* p;
int i,j;
fstream f(fisier,ios::in);
f>>n;
for(i=1;i<=n;i++) l[i]=0;
while(f>>i>>j)
{p=new nod;
p->adr=l[i];
p->inf=j;
l[i]=p;
}
f.close();
}

void bf()
{
nod* p;
if(i<=sf)
{
p=l[c[i]];
while(p)
{
if(s[p->inf]==0)
{sf++;
c[sf]=p->inf;
s[p->inf]=1;
}
p=p->adr;
}
i++;
bf();
}
}

void main()
{
citire("graf.txt",l,n);
i=1;sf=1;c[i]=1;s[1]=1;
bf();
for(int i=1;i<=sf;i++) cout<<c[i]<<" ";
cout<<endl;
getch();
}

{joscommentenable}