1 Replies - 5552 Views - Last Post: 28 April 2010 - 09:24 PM Rate Topic: -----

#1 cissee   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 66
  • Joined: 20-November 09

depth first search

Posted 27 April 2010 - 07:53 PM

This is about graph traversal.

How can i backtrack when the DFS is implemented using a stack.

pseudo code

 void dfs(vertex *root)
	{
		
	
		push root to stack
		root visited
		while(stack is not empty)
		{
			vertex *n= top of stack vertex;
		
			vertex  *child=unvisited child nodes
			if(child!=NULL)
			{
                        // go deeper into the graph
                         visit child

			}
			else
			{
				pop from stack
	
			}
		}

	}



Is This A Good Question/Topic? 0
  • +

Replies To: depth first search

#2 Crunch   User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: depth first search

Posted 28 April 2010 - 09:24 PM

The recursive depth first search is much easier and you could even see the backtracking once you put cout statements at the appropriate place :bigsmile:

With this stack implementation also it should be possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1