Breadth-first search, starting at node B


Given: an empty queue Q of node numbers
and an empty edge list L

B.visited = true
Visit B 
Add B to Q
while( Q is not empty )
{
	V = Q.front
	Remove front item from Q

	for each unvisited neighbor W of V
		(in increasing numerical order)
	{
		W.visited = true
		Visit W
		Add W to Q
		Add V-W to L
	}
}



Depth-first search, starting at node B


Given: an empty stack S of node numbers
and an empty edge list L

B.visited = true
Visit B 
Push B onto S

while( S is not empty )
{
	V = S.top

	if( V has any unvisited neighbors )
	{
		Let W be the unvisited neighbor of V with
			the smallest number
		W.visited = true
		Visit W
		Push W onto S
		Add V-W to L
	}
	else
		S.pop
}