Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created June 4, 2017 10:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammedeminoglu/8e630298e13f131f069d41689892da2c to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/8e630298e13f131f069d41689892da2c to your computer and use it in GitHub Desktop.
DFS C Code implementation. This code has been written recursive method. Don't forget matris.txt file. You can change it as you want.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int Array[6][6];
bool visited[6];
void DFS(int root, bool visited[])
{
int i;
visited[root] = true;
printf("%d ", root);
for( i = 0; i < 6; i++)
{
if(Array[root][i] == 1 && visited[i] == false)
{
DFS(i, visited);
}
}
}
void readMatrix()
{
int i = 0;
FILE *fp = fopen("matris.txt", "r");
while(fscanf(fp, "%d %d %d %d %d %d",
&Array[i][0],
&Array[i][1],
&Array[i][2],
&Array[i][3],
&Array[i][4],
&Array[i][5]
) != EOF)
{
i = i + 1 ;
}
}
int main()
{
readMatrix();
DFS(3, visited);
return 0;
}
0 1 1 0 0 0
1 0 0 1 0 0
1 0 0 1 0 0
0 1 1 0 1 0
0 0 0 1 0 1
0 0 0 0 1 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment