BFS and Queue

BFS (Bridth First Search) can be implemented by a queue.

Procedure is like this: (Q is Queue)

1, Put 1 in Q : ->1 (front)

2, Read the front of Q (which is 1) and put its children in Q: ->4->3->2

3, Repeat 2

....

 

Also, DFS (Depth First Search) can be implemented by a stack.

Procedure is like this: (S is Stack)

1, Put 1 in S: 1<- (bottom)

2, Read the top of S which is 1 and put its children into S: 2<-7<-8

3, Repeat 2

....

Think about it!

posted @ 2014-09-20 04:07  zmiao  阅读(162)  评论(0编辑  收藏  举报