RSS Feed

Depth First Search Algorithm

A DFS algorithm, as the name implies, is used to search deeper in the graph, whenever possible. The edges are explored, out of the most recently discovered vertex v that still has unexplored edges leaving it. When all of v's edges have been explored, the search backtracks to explore edges leaving the vertex from which v was discovered. This process continues until we have discovered all the vertices that are reachable from the source vertex. DFS uses stack structure to maintain the order in which the vertices are to be processed.

Algorithm:

Step 1: Initialize all nodes to ready state (status = 1)

Step 2: Push the starting node in stack and change its status to the waiting state (status = 2)

Step 3: Repeat step 4 and 5 until stack is empty

Step 4: pop the top node n of stack. Process n and change the status of n to the processed state (status = 3)

Step 5: Push on to the stack, all the neighbor of n that are in ready state (status = 1), and change their status to the waiting state (status = 2) [End of the step 3 loop]

Step 6: exit

graph depth first search clip image002 Depth First Search Algorithm

graph depth first search clip image004 Depth First Search Algorithm

graph depth first search clip image006 Depth First Search Algorithm

graph depth first search clip image008 Depth First Search Algorithm

graph depth first search clip image010 Depth First Search Algorithm

Output:

graph depth first search clip image012 Depth First Search Algorithm

graph depth first search clip image014 Depth First Search Algorithm

graph depth first search clip image016 Depth First Search Algorithm

graph depth first search clip image018 Depth First Search Algorithm

Follow Daniel Memetic on Google+
Respond to “Depth First Search Algorithm”
  1. seda says:

    it is very good that alogrithem but iwant cod c++
    and steps of that alogrithem

  2. sibin says:

    very bad algorithm.
    please modify it.
    9846388009……….

  3. shamae says:

    it was a good algorithm though i wasn’t understand it enough , it should be clarified correctly and explain it words for words… please have a dev c++ and java..(can u)

Leave a Reply

Post your comments and questions below, but please follow our commenting guidelines.


Path: Home > Programming > C > Depth First Search Algorithm