Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add glossary and description for "哈希集合" #1310

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codes/cpp/chapter_graph/graph_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// 顶点遍历序列
vector<Vertex *> res;
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
unordered_set<Vertex *> visited = {startVet};
// 队列用于实现 BFS
queue<Vertex *> que;
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_graph/graph_dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *>
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// 顶点遍历序列
vector<Vertex *> res;
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
unordered_set<Vertex *> visited;
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_graph/graph_bfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class graph_bfs {
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
HashSet<Vertex> visited = [startVet];
// 队列用于实现 BFS
Queue<Vertex> que = new();
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_graph/graph_dfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class graph_dfs {
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
HashSet<Vertex> visited = [];
DFS(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion codes/dart/chapter_graph/graph_bfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
// 顶点遍历序列
List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
Set<Vertex> visited = {};
visited.add(startVet);
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/dart/chapter_graph/graph_dfs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void dfs(
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
Set<Vertex> visited = {};
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_graph/graph_bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// 顶点遍历序列
res := make([]Vertex, 0)
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// 队列用于实现 BFS, 使用切片模拟队列
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_graph/graph_dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// 顶点遍历序列
res := make([]Vertex, 0)
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
visited := make(map[Vertex]struct{})
dfs(g, visited, &res, startVet)
// 返回顶点遍历序列
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_bfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class graph_bfs {
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new ArrayList<>();
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
Set<Vertex> visited = new HashSet<>();
visited.add(startVet);
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_dfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Verte
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new ArrayList<>();
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
Set<Vertex> visited = new HashSet<>();
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_graph/graph_bfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { Vertex } = require('../modules/Vertex');
function graphBFS(graph, startVet) {
// 顶点遍历序列
const res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
const visited = new Set();
visited.add(startVet);
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/javascript/chapter_graph/graph_dfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function dfs(graph, visited, res, vet) {
function graphDFS(graph, startVet) {
// 顶点遍历序列
const res = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
const visited = new Set();
dfs(graph, visited, res, startVet);
return res;
Expand Down
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_graph/graph_bfs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.*
fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
// 顶点遍历序列
val res = mutableListOf<Vertex?>()
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
val visited = HashSet<Vertex>()
visited.add(startVet)
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_graph/graph_dfs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun dfs(
fun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {
// 顶点遍历序列
val res = mutableListOf<Vertex?>()
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
val visited = HashSet<Vertex?>()
dfs(graph, visited, res, startVet)
return res
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_graph/graph_bfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
# 顶点遍历序列
res = []
# 哈希表,用于记录已被访问过的顶点
# 哈希集合,用于记录已被访问过的顶点
visited = set[Vertex]([start_vet])
# 队列用于实现 BFS
que = deque[Vertex]([start_vet])
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_graph/graph_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
# 顶点遍历序列
res = []
# 哈希表,用于记录已被访问过的顶点
# 哈希集合,用于记录已被访问过的顶点
visited = set[Vertex]()
dfs(graph, visited, res, start_vet)
return res
Expand Down
2 changes: 1 addition & 1 deletion codes/ruby/chapter_graph/graph_bfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def graph_bfs(graph, start_vet)
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
# 顶点遍历序列
res = []
# 哈希表,用于记录已被访问过的顶点
# 哈希集合,用于记录已被访问过的顶点
visited = Set.new([start_vet])
# 队列用于实现 BFS
que = [start_vet]
Expand Down
2 changes: 1 addition & 1 deletion codes/ruby/chapter_graph/graph_dfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def graph_dfs(graph, start_vet)
# 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
# 顶点遍历序列
res = []
# 哈希表,用于记录已被访问过的顶点
# 哈希集合,用于记录已被访问过的顶点
visited = Set.new
dfs(graph, visited, res, start_vet)
res
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_graph/graph_bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::collections::{HashSet, VecDeque};
fn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {
// 顶点遍历序列
let mut res = vec![];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
let mut visited = HashSet::new();
visited.insert(start_vet);
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_graph/graph_dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex
fn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {
// 顶点遍历序列
let mut res = vec![];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
let mut visited = HashSet::new();
dfs(&graph, &mut visited, &mut res, start_vet);

Expand Down
2 changes: 1 addition & 1 deletion codes/swift/chapter_graph/graph_bfs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import utils
func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
// 顶点遍历序列
var res: [Vertex] = []
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
var visited: Set<Vertex> = [startVet]
// 队列用于实现 BFS
var que: [Vertex] = [startVet]
Expand Down
2 changes: 1 addition & 1 deletion codes/swift/chapter_graph/graph_dfs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], v
func graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
// 顶点遍历序列
var res: [Vertex] = []
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
var visited: Set<Vertex> = []
dfs(graph: graph, visited: &visited, res: &res, vet: startVet)
return res
Expand Down
2 changes: 1 addition & 1 deletion codes/typescript/chapter_graph/graph_bfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Vertex } from '../modules/Vertex';
function graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {
// 顶点遍历序列
const res: Vertex[] = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
const visited: Set<Vertex> = new Set();
visited.add(startVet);
// 队列用于实现 BFS
Expand Down
2 changes: 1 addition & 1 deletion codes/typescript/chapter_graph/graph_dfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function dfs(
function graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {
// 顶点遍历序列
const res: Vertex[] = [];
// 哈希表,用于记录已被访问过的顶点
// 哈希集合,用于记录已被访问过的顶点
const visited: Set<Vertex> = new Set();
dfs(graph, visited, res, startVet);
return res;
Expand Down
1 change: 1 addition & 0 deletions docs/chapter_appendix/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
| front of the queue | 队首 | 佇列首 |
| rear of the queue | 队尾 | 佇列尾 |
| hash table | 哈希表 | 雜湊表 |
| hash set | 哈希集合 | 雜湊集合 |
| bucket | 桶 | 桶 |
| hash function | 哈希函数 | 雜湊函式 |
| hash collision | 哈希冲突 | 雜湊衝突 |
Expand Down
4 changes: 2 additions & 2 deletions docs/chapter_backtracking/permutations_problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

![重复排列](permutations_problem.assets/permutations_ii.png)

那么如何去除重复的排列呢?最直接地,考虑借助一个哈希表,直接对排列结果进行去重。然而这样做不够优雅,**因为生成重复排列的搜索分支没有必要,应当提前识别并剪枝**,这样可以进一步提升算法效率。
那么如何去除重复的排列呢?最直接地,考虑借助一个哈希集合,直接对排列结果进行去重。然而这样做不够优雅,**因为生成重复排列的搜索分支没有必要,应当提前识别并剪枝**,这样可以进一步提升算法效率。

### 相等元素剪枝

Expand All @@ -73,7 +73,7 @@

### 代码实现

在上一题的代码的基础上,我们考虑在每一轮选择中开启一个哈希表 `duplicated` ,用于记录该轮中已经尝试过的元素,并将重复元素剪枝:
在上一题的代码的基础上,我们考虑在每一轮选择中开启一个哈希集合 `duplicated` ,用于记录该轮中已经尝试过的元素,并将重复元素剪枝:

```src
[file]{permutations_ii}-[class]{}-[func]{permutations_ii}
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_backtracking/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- 回溯问题通常包含多个约束条件,它们可用于实现剪枝操作。剪枝可以提前结束不必要的搜索分支,大幅提升搜索效率。
- 回溯算法主要可用于解决搜索问题和约束满足问题。组合优化问题虽然可以用回溯算法解决,但往往存在效率更高或效果更好的解法。
- 全排列问题旨在搜索给定集合元素的所有可能的排列。我们借助一个数组来记录每个元素是否被选择,剪掉重复选择同一元素的搜索分支,确保每个元素只被选择一次。
- 在全排列问题中,如果集合中存在重复元素,则最终结果会出现重复排列。我们需要约束相等元素在每轮中只能被选择一次,这通常借助一个哈希表来实现
- 在全排列问题中,如果集合中存在重复元素,则最终结果会出现重复排列。我们需要约束相等元素在每轮中只能被选择一次,这通常借助一个哈希集合来实现
- 子集和问题的目标是在给定集合中找到和为目标值的所有子集。集合不区分元素顺序,而搜索过程会输出所有顺序的结果,产生重复子集。我们在回溯前将数据进行排序,并设置一个变量来指示每一轮的遍历起始点,从而将生成重复子集的搜索分支进行剪枝。
- 对于子集和问题,数组中的相等元素会产生重复集合。我们利用数组已排序的前置条件,通过判断相邻元素是否相等实现剪枝,从而确保相等元素在每轮中只能被选中一次。
- $n$ 皇后问题旨在寻找将 $n$ 个皇后放置到 $n \times n$ 尺寸棋盘上的方案,要求所有皇后两两之间无法攻击对方。该问题的约束条件有行约束、列约束、主对角线和次对角线约束。为满足行约束,我们采用按行放置的策略,保证每一行放置一个皇后。
Expand Down
12 changes: 8 additions & 4 deletions docs/chapter_graph/graph_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
2. 在循环的每轮迭代中,弹出队首顶点并记录访问,然后将该顶点的所有邻接顶点加入到队列尾部。
3. 循环步骤 `2.` ,直到所有顶点被访问完毕后结束。

为了防止重复遍历顶点,我们需要借助一个哈希表 `visited` 来记录哪些节点已被访问。
为了防止重复遍历顶点,我们需要借助一个哈希集合 `visited` 来记录哪些节点已被访问。

!!! tip

哈希集合可以看作一个只存储 `key` 而不存储 `value` 的哈希表,它可以在 $O(1)$ 时间复杂度下进行 `key` 的增删查改操作。根据 `key` 的唯一性,哈希集合通常用于数据去重等场景。

```src
[file]{graph_bfs}-[class]{}-[func]{graph_bfs}
Expand Down Expand Up @@ -67,7 +71,7 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先

**时间复杂度**:所有顶点都会入队并出队一次,使用 $O(|V|)$ 时间;在遍历邻接顶点的过程中,由于是无向图,因此所有边都会被访问 $2$ 次,使用 $O(2|E|)$ 时间;总体使用 $O(|V| + |E|)$ 时间。

**空间复杂度**:列表 `res` ,哈希表 `visited` ,队列 `que` 中的顶点数量最多为 $|V|$ ,使用 $O(|V|)$ 空间。
**空间复杂度**:列表 `res` ,哈希集合 `visited` ,队列 `que` 中的顶点数量最多为 $|V|$ ,使用 $O(|V|)$ 空间。

## 深度优先遍历

Expand All @@ -77,7 +81,7 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先

### 算法实现

这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中,我们也需要借助一个哈希表 `visited` 来记录已被访问的顶点,以避免重复访问顶点。
这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中,我们也需要借助一个哈希集合 `visited` 来记录已被访问的顶点,以避免重复访问顶点。

```src
[file]{graph_dfs}-[class]{}-[func]{graph_dfs}
Expand Down Expand Up @@ -133,4 +137,4 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先

**时间复杂度**:所有顶点都会被访问 $1$ 次,使用 $O(|V|)$ 时间;所有边都会被访问 $2$ 次,使用 $O(2|E|)$ 时间;总体使用 $O(|V| + |E|)$ 时间。

**空间复杂度**:列表 `res` ,哈希表 `visited` 顶点数量最多为 $|V|$ ,递归深度最大为 $|V|$ ,因此使用 $O(|V|)$ 空间。
**空间复杂度**:列表 `res` ,哈希集合 `visited` 顶点数量最多为 $|V|$ ,递归深度最大为 $|V|$ ,因此使用 $O(|V|)$ 空间。
Loading