vault backup: 2026-06-10 11:19:43
This commit is contained in:
+78
-35
@@ -21,28 +21,52 @@ DFS 沿着一条路径**尽可能深地**搜索,走不通再回退。
|
||||
|
||||
**实现方式**:使用**栈**(Stack)或**递归调用栈**
|
||||
|
||||
```plaintext
|
||||
function DFS(graph, start):
|
||||
visited = set()
|
||||
stack = [start]
|
||||
```cpp
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
while stack is not empty:
|
||||
node = stack.pop()
|
||||
if node not in visited:
|
||||
visited.add(node)
|
||||
for neighbor in graph[node]:
|
||||
if neighbor not in visited:
|
||||
stack.push(neighbor)
|
||||
// DFS(栈实现)
|
||||
// graph: 邻接表, start: 起始顶点
|
||||
void DFS(vector<vector<int>>& graph, int start) {
|
||||
int n = graph.size();
|
||||
vector<bool> visited(n, false);
|
||||
stack<int> st;
|
||||
st.push(start);
|
||||
|
||||
while (!st.empty()) {
|
||||
int node = st.top();
|
||||
st.pop();
|
||||
if (!visited[node]) {
|
||||
visited[node] = true;
|
||||
cout << node << " ";
|
||||
for (int neighbor : graph[node]) {
|
||||
if (!visited[neighbor])
|
||||
st.push(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
递归版本更直观:
|
||||
|
||||
```plaintext
|
||||
function DFS_recursive(graph, node, visited):
|
||||
visited.add(node)
|
||||
for neighbor in graph[node]:
|
||||
if neighbor not in visited:
|
||||
DFS_recursive(graph, neighbor, visited)
|
||||
```cpp
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// DFS(递归版本)
|
||||
// graph: 邻接表, node: 当前顶点, visited: 访问标记数组
|
||||
void DFS_recursive(vector<vector<int>>& graph, int node, vector<bool>& visited) {
|
||||
visited[node] = true;
|
||||
cout << node << " ";
|
||||
for (int neighbor : graph[node]) {
|
||||
if (!visited[neighbor])
|
||||
DFS_recursive(graph, neighbor, visited);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 二、BFS(广度优先搜索)
|
||||
@@ -51,18 +75,33 @@ BFS **按层遍历**,先访问所有距离为1的节点,再访问距离为2
|
||||
|
||||
**实现方式**:使用**队列**(Queue)
|
||||
|
||||
```plaintext
|
||||
function BFS(graph, start):
|
||||
visited = set()
|
||||
queue = [start]
|
||||
visited.add(start)
|
||||
```cpp
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
while queue is not empty:
|
||||
node = queue.dequeue()
|
||||
for neighbor in graph[node]:
|
||||
if neighbor not in visited:
|
||||
visited.add(neighbor)
|
||||
queue.enqueue(neighbor)
|
||||
// BFS(队列实现)
|
||||
// graph: 邻接表, start: 起始顶点
|
||||
void BFS(vector<vector<int>>& graph, int start) {
|
||||
int n = graph.size();
|
||||
vector<bool> visited(n, false);
|
||||
queue<int> q;
|
||||
q.push(start);
|
||||
visited[start] = true;
|
||||
|
||||
while (!q.empty()) {
|
||||
int node = q.front();
|
||||
q.pop();
|
||||
cout << node << " ";
|
||||
for (int neighbor : graph[node]) {
|
||||
if (!visited[neighbor]) {
|
||||
visited[neighbor] = true;
|
||||
q.push(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 三、BFS vs DFS 对比
|
||||
@@ -112,13 +151,17 @@ flowchart TD
|
||||
|
||||
**原理**:gcd(a, b) = gcd(b, a mod b),直到 b = 0 时 a 即为答案。
|
||||
|
||||
```plaintext
|
||||
function gcd(a, b):
|
||||
while b != 0:
|
||||
temp = b
|
||||
b = a mod b
|
||||
a = temp
|
||||
return a
|
||||
```cpp
|
||||
// 欧几里得算法(辗转相除法)
|
||||
// 求两个正整数的最大公约数
|
||||
int gcd(int a, int b) {
|
||||
while (b != 0) {
|
||||
int temp = b;
|
||||
b = a % b;
|
||||
a = temp;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
```
|
||||
|
||||
**示例**:gcd(2146, 8100)
|
||||
|
||||
Reference in New Issue
Block a user