DFS

void DFS(long node)
{
long i;
printf(“%ld “,node);
visit[node]=1;

for(i=1;i<=n;i++)
if(visit[i]==0&&A[node][i]==1)
DFS(i);

}

visit[]={0};

A[][] = connection matrix.

n = number of nodes, from 1 to n.

source = 1;
DFS(source);

Advertisements