Descompunerea in componente conexe a unui graf neorientat, dat prin matricea de adiacenta
#include<fstream.h>
int s[20],a[20][20],n,i,j,k;
void df(int nod)
{
int k;
cout<<nod<<” “;
s[nod]=1;
for(k=1;k<=n;k++)
if((a[nod][k]=1) && (s[k]==0)) df(k);
}
void main()
{
fstream f(“graf.txt”,ios::in);
f>>n;
while(f>>i>>j) a[i][j]=1;
f.close();
k=1;
for(i=1;i<=n;i++)
if(s[i]==0)
{
cout<<“componenta “<<k<<endl;
df(i);
cout<<endl;
k++;
}
}
{joscommentenable}