Depth First Search (DFS) Program in C

In this tutorial you will learn about Depth First Search (DFS) program in C with algorithm.

Most of graph problems involve traversal of a graph. Traversal of a graph means visiting each node and visiting exactly once. There are two types of traversal in graphs i.e. Depth First Search (DFS) and Breadth First Search (BFS).
It is like tree. Traversal can start from any vertex, say Vi . Vi is visited and then all vertices adjacent to Vi are traversed recursively using DFS. Since, a graph can have cycles. We must avoid revisiting a node. To do this, when we visit a vertex V, we mark it visited. A node that has already been marked as visited should not be selected for traversal. Marking of visited vertices can be done with the help of a global array visited[ ]. Array visited[ ] is initialized to false (0).

Depth First Search (DFS) Algorithm


Depth First Search (DFS) Traversal of a Graph [Algorithm and Program]

The graph shown above is taken as input in both the programs mentioned below:

Depth First Search (DFS) Program in C [Adjacency Matrix]


C Program to implement DFS traversal on a graph represented using an adjacency matrix

Depth First Search (DFS) Program in C [Adjacency List]

C Program to implement DFS traversal on a graph represented using an adjacency list
If you found anything incorrect or have doubts regarding above Depth First Search (DFS) program in C tutorial then comment below.

29 thoughts on “Depth First Search (DFS) Program in C

  1. Varsha Patil

    void DFS(int i)
    node *p;
    In this function after while loop is terminated how the backtracking is happen?

  2. hira naz

    i am trying to work with adjacency matrix method in C# but I am unable to use NOT operator with int data type. Can you please elaborate it in C# especially the if condition used in the adjacency matrix's program dfs function

    1. Andres Rodriguez

      hira naz instead of using an array of ints you just replace it with an array of boolean types and that is it.

  3. Zaid Hameed

    I am trying to compile this program but it shows [Error] 'malloc' was not declared in this scope. any solution plz?

    1. sagar agarwal

      add stdlib.h in the header file. it should work.

  4. Ankit


    Can we find loop in the graph by using above implementation with adj matrix? Please guide about that.

  5. shubham sharma

    it is for undirected graph but replacing (0,1) with (1,0) gives different to resolve that.

    1. Narendra Kumar

      1.mark all the vertices as not visited.
      2.apply DFS for graph from any vertix.
      3.if any vertix is not visited then return false
      4.reverse the graph and mark all the vertices as not visited
      5.apply DFS for reversed graph with from same vertix as in step 2
      6.if any vertix is not visited then return false
      7.return true

  6. Andrew

    In your “Depth First Search (DFS) Program in C [Adjacency List]” code the loop on line 57 looks wrong. You initialize G[0] to NULL and then begin inserting all the edges before you finish initializing the rest of G[]. Since you use the variable ‘i’ for both loops you win not continue where you left off, which doesn’t matter since you already inserted the edges. The problem would be if you have less edges than nodes, you may actually delete some edges in your adjacency-list.

  7. abiwo

    “q=(node*)malloc(sizeof(node));” this is eror in dev c++ whatshal i

    1. Admin Post author

      Just add stdlib.h header file as we have used malloc() function in the program.

    1. Alex

      Are you sure you understand what DFS does? We start and vertex 0, and we travel on the edge to vertex 1. From vertex 1 we follow the left most edge to the last vertex of the branch which is vertex N. Once all edges leading from Vertex N that lead to an undiscovered vertex are marked. We travel back to the previous vertex which is vertex N-1 of the branch. And continue this method until we are back at vertex 0 and we are done when all edges from Vertex 0 are marked as being discovered.

    2. Alex

      I would suggest reviewing what Depth First Search is actually doing. The output is correct for Depth First Search

    3. Rahul

      I guess it should be for(j=i;j<n;j++)

      DFS(int i)
      int j;


      1. vardhan

        no ,that’s correct, if we give the the starting index as other than ‘0’ then we have to start from i=0; every time

  8. jak

    why do I feel like the resulting traversal is wrong?
    shouldnt it be 01527634 since after visiting 5,(where 5 is adjacent to 1, 2 and 7), then it will push 2 into stack because its still not visited

  9. Varun kamani

    What if there is character input data instead of an integer.
    here you took an integer so you can do this so by visited[i]=1.
    What is there is Character??

  10. Shyamkant

    Excellent minimum line code. After many years I have used some C code. I have tried it for 9 nodes entered 81 elements and at last all get disappeared. I have tried this two times and get realized that getch() is remaining. After inserting this line I could see the output. Then I have converted it to for my use. I am sure it will be very great help for me. Thanks a lot.

  11. mysterious

    why the path is different in two adjancency matrix there is one path and in adjancey list there is another


Leave a Reply

Your email address will not be published. Required fields are marked *