|
| 1 | +# 深度优先搜索,广度优先搜索 |
| 2 | + |
| 3 | +### 深度优先搜索模板 |
| 4 | + |
| 5 | +- 先序,递归 |
| 6 | + |
| 7 | +```Python |
| 8 | +def DFS(x): |
| 9 | + visit(x) |
| 10 | + for n in neighbor(x): |
| 11 | + if not visited(n): |
| 12 | + DFS(n) |
| 13 | + return |
| 14 | +``` |
| 15 | + |
| 16 | +- 先序,迭代 |
| 17 | + |
| 18 | +```Python |
| 19 | +def DFS(x): |
| 20 | + dfs = [x] # implement by a stack |
| 21 | + while dfs: |
| 22 | + v = dfs.pop() |
| 23 | + if not visited(v): |
| 24 | + visit(v) |
| 25 | + |
| 26 | + for n in neighbor(v): |
| 27 | + if not visited(n): |
| 28 | + dfs.append(n) |
| 29 | + return |
| 30 | +``` |
| 31 | + |
| 32 | +- 后序,递归 |
| 33 | + |
| 34 | +```Python |
| 35 | +def DFS(x): # used when need to aggregate results from children |
| 36 | + discovering(x) |
| 37 | + for n in neighbor(x): |
| 38 | + if not discovering(n) and not visited(n): |
| 39 | + DFS(n) |
| 40 | + visit(x) |
| 41 | + return |
| 42 | +``` |
| 43 | + |
| 44 | +### 广度优先搜索模板 |
| 45 | + |
| 46 | +相对于 dfs 可能收敛更慢,但是可以用来找不带权的最短路径 |
| 47 | + |
| 48 | +- 以结点为单位搜索 |
| 49 | + |
| 50 | +```Python |
| 51 | +def BFS(x): |
| 52 | + bfs = collections.deque([x]) |
| 53 | + while bfs: |
| 54 | + v = bfs.popleft() |
| 55 | + if not visited(v): |
| 56 | + visit(v) |
| 57 | + for n in neighbor(v): |
| 58 | + if not visited(v): |
| 59 | + bfs.append(n) |
| 60 | + return |
| 61 | +``` |
| 62 | + |
| 63 | +- 以层为单位搜索,典型应用是找不带权的最短路径 |
| 64 | + |
| 65 | +```Python |
| 66 | +def BFS(x): |
| 67 | + bfs = collections.deque([x]) |
| 68 | + while bfs: |
| 69 | + num_level = len(bfs) |
| 70 | + for _ in range(num_level) |
| 71 | + v = bfs.popleft() |
| 72 | + if not visited(v): |
| 73 | + visit(v) |
| 74 | + for n in neighbor(v): |
| 75 | + if not visited(v): |
| 76 | + bfs.append(n) |
| 77 | + return |
| 78 | +``` |
| 79 | + |
| 80 | +## 例题 |
| 81 | + |
| 82 | +### [shortest-bridge](https://leetcode-cn.com/problems/shortest-bridge/) |
| 83 | + |
| 84 | +> 在给定的 01 矩阵 A 中,存在两座岛 (岛是由四面相连的 1 形成的一个连通分量)。现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。返回必须翻转的 0 的最小数目。 |
| 85 | +> |
| 86 | + |
| 87 | +**图森面试真题**。思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径 |
| 88 | + |
| 89 | +```Python |
| 90 | +class Solution: |
| 91 | + def shortestBridge(self, A: List[List[int]]) -> int: |
| 92 | + |
| 93 | + M, N = len(A), len(A[0]) |
| 94 | + |
| 95 | + for i in range(M): |
| 96 | + for j in range(N): |
| 97 | + if A[i][j] == 1: # start from a 1 |
| 98 | + dfs = [(i, j)] |
| 99 | + break |
| 100 | + |
| 101 | + bfs = collections.deque([]) |
| 102 | + |
| 103 | + while dfs: |
| 104 | + r, c = dfs.pop() |
| 105 | + if A[r][c] == 1: |
| 106 | + A[r][c] = -1 |
| 107 | + |
| 108 | + if r - 1 >= 0: |
| 109 | + if A[r - 1][c] == 0: # meet and edge |
| 110 | + bfs.append((r - 1, c)) |
| 111 | + elif A[r - 1][c] == 1: |
| 112 | + dfs.append((r - 1, c)) |
| 113 | + |
| 114 | + if r + 1 < M: |
| 115 | + if A[r + 1][c] == 0: |
| 116 | + bfs.append((r + 1, c)) |
| 117 | + elif A[r + 1][c] == 1: |
| 118 | + dfs.append((r + 1, c)) |
| 119 | + |
| 120 | + if c - 1 >= 0: |
| 121 | + if A[r][c - 1] == 0: |
| 122 | + bfs.append((r, c - 1)) |
| 123 | + elif A[r][c - 1] == 1: |
| 124 | + dfs.append((r, c - 1)) |
| 125 | + |
| 126 | + if c + 1 < N: |
| 127 | + if A[r][c + 1] == 0: |
| 128 | + bfs.append((r, c + 1)) |
| 129 | + elif A[r][c + 1] == 1: |
| 130 | + dfs.append((r, c + 1)) |
| 131 | + flip = 1 |
| 132 | + while bfs: |
| 133 | + num_level = len(bfs) |
| 134 | + for _ in range(num_level): |
| 135 | + r, c = bfs.popleft() |
| 136 | + if A[r][c] == 0: |
| 137 | + A[r][c] = -2 |
| 138 | + |
| 139 | + if r - 1 >= 0: |
| 140 | + if A[r - 1][c] == 0: |
| 141 | + bfs.append((r - 1, c)) |
| 142 | + elif A[r - 1][c] == 1: |
| 143 | + return flip |
| 144 | + |
| 145 | + if r + 1 < M: |
| 146 | + if A[r + 1][c] == 0: |
| 147 | + bfs.append((r + 1, c)) |
| 148 | + elif A[r + 1][c] == 1: |
| 149 | + return flip |
| 150 | + |
| 151 | + if c - 1 >= 0: |
| 152 | + if A[r][c - 1] == 0: |
| 153 | + bfs.append((r, c - 1)) |
| 154 | + elif A[r][c - 1] == 1: |
| 155 | + return flip |
| 156 | + |
| 157 | + if c + 1 < N: |
| 158 | + if A[r][c + 1] == 0: |
| 159 | + bfs.append((r, c + 1)) |
| 160 | + elif A[r][c + 1] == 1: |
| 161 | + return flip |
| 162 | + flip += 1 |
| 163 | +``` |
| 164 | + |
0 commit comments