Skip to content

Commit

Permalink
Create DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
msindev authored Feb 10, 2019
1 parent 6d21f20 commit 9d8486a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Depth First Search/DFS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
void dfs(int graph[][MAX],int visit[],int n,int start)
{
visit[start]=1;
int i;
printf("%d ",start);
for(i=0;i<n;i++)
{
if(graph[start][i]==1&&visit[i]==0)
{
dfs(graph,visit,n,i);
}

}
}

void bfs(int graph[][MAX], int visit[], int n, int start);

int main()
{
int i,n,j;
printf("Enter the total number of nodes:\n");
scanf("%d",&n);
int graph[MAX][MAX],visit[n],count=0;
printf("Enter the values of adjacency matrix: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&graph[i][j]);
}
}
printf("The adjacency matrix is: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",graph[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
visit[i]=0;
printf("The dfs traversal of the graph is: \n");
for(i=0;i<n;i++)
{
if(visit[i]==0)
{
printf("Traversal for component %d: ", count+1);
dfs(graph,visit,n,i);
count++;
printf("\n");
}
}
if(count==1)
{
printf("\nConnected graph\n");
}
else
printf("\nNot connected and number of components is %d\n",count);

}

0 comments on commit 9d8486a

Please sign in to comment.