wap to implement dfs using adjacency matrix
#include<stdio.h>
#include<conio.h>
void DFS(int);
int
G[10][10],visited[10],n;
void main()
{
clrscr();
int i,j;
printf("enter no, of nodes=");
scanf("%d",&n);
printf("\n enter adjacency matrix of
the graph=");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
getch();
}
void DFS(int i)
{
int j;
printf("\n %d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
No comments:
Post a Comment