google-site-verification: googlef8aa845b858144d3.html COMPUTER SCIENCE NOTES: Basics of Depth First Search(DFS) Algorithm

Friday, February 17, 2017

Basics of Depth First Search(DFS) Algorithm

  DFS-iterative (G, s):                                   //Where G is graph and s is source vertex
      let S be stack
      S.push( s )            //Inserting s in stack 
      mark s as visited.
      while ( S is not empty):
          //Pop a vertex from stack to visit next
          v  =  S.top( )
         S.pop( )
         //Push all the neighbours of v in stack that are not visited   
        for all neighbours w of v in Graph G:
            if w is not visited :
                     S.push( w )         
                    mark w as visited


    DFS-recursive(G, s):
        mark s as visited
        for all neighbours w of s in Graph G:
            if w is not visited:
                DFS-recursive(G, w)




No comments:

Post a Comment