Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Last active April 16, 2022 17:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManiruzzamanAkash/37f9e0b247c4826bf24be1b606124ab9 to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/37f9e0b247c4826bf24be1b606124ab9 to your computer and use it in GitHub Desktop.
DFS code in C language
#include<stdio.h>
void DFS(int);
int G[1000][1000], visited[1000], n; // n is no of vertices and graph is sorted in array G[1000][1000]
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);
}
}
}
int main()
{
int i, j;
printf("Enter number of vertices:");
scanf("%d", &n); // Read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
scanf("%d", &G[i][j]);
}
}
// Visited is initialized to zero
for(i=0; i<n; i++) {
visited[i] = 0;
}
DFS(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment