algorithm - BFS 和 DFS 算法有什么区别?
问题描述
使用 BFS 解决算法问题时发生超时。但是,有一个问题可以用 DFS 解决。为什么会出现这种差异?
问题是通过水平、垂直或对角线移动来计算从 (1,1) 到 (N, N) 的到达数。
如果用 BFS 解决问题(最坏情况)需要 1331.0ms,而用 DFS 解决问题需要 62.0ms。(我已经创建并测试了 16 * 16 数组。)
附上问题 URL。(但请理解为韩文。) URL> https://www.acmicpc.net/problem/17070
我想听到的答案是……
- 我以为 BFS 算法会更快,但为什么 DFS 更快呢?
- BFS 会因为队列中有很多元素而变慢吗?我想知道确切的原因。
实现代码>
班级位置{
int x;
int y;
int dir;
public Location(int x, int y, int dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
}
}
公共类主要{
static int[][] map;
static int Answer;
static int N;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
map = new int[N + 1][N + 1];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= N; j++)
map[i][j] = Integer.parseInt(st.nextToken());
}
DFS(1, 2, 0);
System.out.println(Answer);
Answer = 0;
BFS(1, 2, 0);
System.out.println(Answer);
br.close();
}
static void DFS(int x, int y, int pre) {
if (x == N && y == N) {
Answer++;
return;
}
if (pre == 0) {
if (y + 1 <= N && map[x][y + 1] == 0)
DFS(x, y + 1, 0);
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
DFS(x + 1, y + 1, 1);
} else if (pre == 1) {
if (y + 1 <= N && map[x][y + 1] == 0)
DFS(x, y + 1, 0);
if (x + 1 <= N && map[x + 1][y] == 0)
DFS(x + 1, y, 2);
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
DFS(x + 1, y + 1, 1);
} else {
if (x + 1 <= N && map[x + 1][y] == 0)
DFS(x + 1, y, 2);
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
DFS(x + 1, y + 1, 1);
}
}
static void BFS(int startX, int startY, int dir) {
ArrayDeque<Location> arrayDeque = new ArrayDeque<>();
arrayDeque.add(new Location(startX, startY, dir));
Location location;
int x, y, pre;
while(!arrayDeque.isEmpty()) {
location = arrayDeque.remove();
x = location.x;
y = location.y;
pre = location.dir;
if(x == N-1 && y == N-1) {
Answer++; continue;
}
if (pre == 0) {
if (y + 1 <= N && map[x][y + 1] == 0)
arrayDeque.add(new Location(x, y + 1, 0));
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
arrayDeque.add(new Location(x + 1, y + 1, 1));
} else if (pre == 1) {
if (y + 1 <= N && map[x][y + 1] == 0)
arrayDeque.add(new Location(x, y + 1, 0));
if (x + 1 <= N && map[x + 1][y] == 0)
arrayDeque.add(new Location(x + 1, y, 2));
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
arrayDeque.add(new Location(x + 1, y + 1, 1));
} else {
if (x + 1 <= N && map[x + 1][y] == 0)
arrayDeque.add(new Location(x + 1, y, 2));
if (y + 1 <= N && map[x][y + 1] == 0 && x + 1 <= N && map[x + 1][y] == 0 && map[x + 1][y + 1] == 0)
arrayDeque.add(new Location(x + 1, y + 1, 1));
}
}
}
}
解决方案
BFS 和 DFS 都具有O(|V| + |E|)
时间复杂度,您遇到的时间差异很可能源于 BFS 实现中的错误,它破坏了循环不变性。
实现 BFS 时最常见的错误之一是多次将相同的元素添加到队列中。一个顶点应该只添加一次v
到队列中,这样您就可以确保它被删除一次。除非您这样做,否则渐近运行时间(即其复杂性)将不再是线性的。您可以根据他们引入的循环不变概念查看相关的CLRS章节来证明这一点。
换句话说,BFS 是一种遍历算法。它找出可以到达的顶点,而不是到达每个顶点的路线数v
。如果您尝试计算通过 BFS 到达每个路径的数量,则复杂性会大于线性。如果问题要求你找到,那么你的方法应该是使用记忆和动态编程,而不是 BFS。Kv
v
(1, 1)
Kv
具体来说,根据您提供的代码,您的算法似乎无法跟踪先前是否探索过顶点(即网格中的单元格)。这会导致对顶点进行多次探索,这优于使用 BFS 和 DFS 等图遍历算法的要点。使用我上面提到的术语,您将违背 BFS 的循环不变量,它指出每个顶点仅从队列中弹出一次。这会导致代码的复杂性远高于线性。
您应该研究术语memoization并找出一种方法来找到 的解决方案(N, N)
,使用您只计算一次的解决(N-1, N-1)
方案(N-1, N)
和(N, N-1)
。
推荐阅读
- sdk - 如何将一维光谱分配给图像堆栈
- java - 如何使用 javac 或 javah 命令在 JNI 头文件中生成结构定义?
- python - 如何找到具有一个常量变量的矩阵的逆矩阵(使用数组)?
- python - 在 python 项目中移植 matlab 项目时的 Element-wise 右除问题
- python - 使用 pyqt5 录制音频
- c - 在 C 中返回零和 Inf 值
- firebase - 有没有办法限制可以使用 Firebase 规则更改的值?
- c# - 有没有更有效的方法从 c# 字典中过滤旧记录
- nginx - NGINX 打开的文件太多
- date - 是否有一个查询公式来计算特定 ID 在特定日期的最新值以及所有唯一 ID 的值?