# 队列和 BFS

### 洞悉

1. 结点的处理顺序是什么？

2. 队列的入队和出队顺序是什么？

### 模板 I

/**
* Return the length of the shortest path between root and target node.
*/
int BFS(Node root, Node target) {
Queue<Node> queue;  // store all nodes which are waiting to be processed
int step = 0;       // number of steps neeeded from root to current node
// initialize
// BFS
while (queue is not empty) {
step = step + 1;
// iterate the nodes which are already in the queue
int size = queue.size();
for (int i = 0; i < size; ++i) {
Node cur = the first node in queue;
return step if cur is target;
for (Node next : the neighbors of cur) {
}
remove the first node from queue;
}
}
return -1;          // there is no path from root to target
}

1. 如代码所示，在每一轮中，队列中的结点是等待处理的结点
2. 在每个更外一层的 while 循环之后，我们距离根结点更远一步。变量 step 指示从根结点到我们正在访问的当前结点的距离。

### 模板 II

/**
* Return the length of the shortest path between root and target node.
*/
int BFS(Node root, Node target) {
Queue<Node> queue;  // store all nodes which are waiting to be processed
Set<Node> used;     // store all the used nodes
int step = 0;       // number of steps neeeded from root to current node
// initialize
// BFS
while (queue is not empty) {
step = step + 1;
// iterate the nodes which are already in the queue
int size = queue.size();
for (int i = 0; i < size; ++i) {
Node cur = the first node in queue;
return step if cur is target;
for (Node next : the neighbors of cur) {
if (next is not in used) {
}
}
remove the first node from queue;
}
}
return -1;          // there is no path from root to target
}

1. 你完全确定没有循环，例如，在树遍历中；
2. 你确实希望多次将结点添加到队列中。

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客