diff --git a/chapter_dynamic_programming/unbounded_knapsack_problem.md b/chapter_dynamic_programming/unbounded_knapsack_problem.md index e9bd08450..fb29b7cce 100644 --- a/chapter_dynamic_programming/unbounded_knapsack_problem.md +++ b/chapter_dynamic_programming/unbounded_knapsack_problem.md @@ -153,7 +153,7 @@ $$ 由于当前状态是从左边和上边的状态转移而来,**因此状态压缩后应该对 $dp$ 表中的每一行采取正序遍历**,这个遍历顺序与 0-1 背包正好相反。请通过以下动画来理解为什么要改为正序遍历。 === "<1>" - ![unbounded_knapsack_dp_comp_step1](unbounded_knapsack_problem.assets/unbounded_knapsack_dp_comp_step1.png) + ![完全背包的状态压缩后的动态规划过程](unbounded_knapsack_problem.assets/unbounded_knapsack_dp_comp_step1.png) === "<2>" ![unbounded_knapsack_dp_comp_step2](unbounded_knapsack_problem.assets/unbounded_knapsack_dp_comp_step2.png) @@ -469,7 +469,7 @@ $$ 下图展示了零钱兑换的动态规划过程。 === "<1>" - ![coin_change_dp_step1](unbounded_knapsack_problem.assets/coin_change_dp_step1.png) + ![零钱兑换问题的动态规划过程](unbounded_knapsack_problem.assets/coin_change_dp_step1.png) === "<2>" ![coin_change_dp_step2](unbounded_knapsack_problem.assets/coin_change_dp_step2.png) diff --git a/chapter_graph/graph_operations.md b/chapter_graph/graph_operations.md index 57ae7bb94..77b4a9535 100644 --- a/chapter_graph/graph_operations.md +++ b/chapter_graph/graph_operations.md @@ -573,7 +573,181 @@ comments: true === "C" ```c title="graph_adjacency_matrix.c" - [class]{graphAdjMat}-[func]{} + /* 基于邻接矩阵实现的无向图类结构 */ + struct graphAdjMat { + int *vertices; + unsigned int **adjMat; + unsigned int size; + unsigned int capacity; + }; + + typedef struct graphAdjMat graphAdjMat; + + /* 添加边 */ + void addEdge(graphAdjMat *t, int i, int j) { + // 越界检查 + if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + exit(1); + } + t->adjMat[i][j] = 1; + t->adjMat[j][i] = 1; + } + + /* 删除边 */ + void removeEdge(graphAdjMat *t, int i, int j) { + // 越界检查 + if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + exit(1); + } + t->adjMat[i][j] = 0; + t->adjMat[j][i] = 0; + } + + /* 添加顶点 */ + void addVertex(graphAdjMat *t, int val) { + // 如果实际使用不大于预设空间,则直接初始化新空间 + if (t->size < t->capacity) { + t->vertices[t->size] = val; + + // 邻接矩新列阵置0 + for (int i = 0; i < t->size; i++) { + t->adjMat[i][t->size] = 0; + } + memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); + t->size++; + return; + } + + // 扩容,申请新的顶点数组 + int *temp = (int *)malloc(sizeof(int) * (t->size * 2)); + memcpy(temp, t->vertices, sizeof(int) * t->size); + temp[t->size] = val; + + // 释放原数组 + free(t->vertices); + t->vertices = temp; + + // 扩容,申请新的二维数组 + unsigned int **tempMat = (unsigned int **)malloc(sizeof(unsigned int *) * t->size * 2); + unsigned int *tempMatLine = (unsigned int *)malloc(sizeof(unsigned int) * (t->size * 2) * (t->size * 2)); + memset(tempMatLine, 0, sizeof(unsigned int) * (t->size * 2) * (t->size * 2)); + for (int k = 0; k < t->size * 2; k++) { + tempMat[k] = tempMatLine + k * (t->size * 2); + } + + // 原数据复制到新数组 + for (int i = 0; i < t->size; i++) { + memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); + } + + // 新列置0 + for (int i = 0; i < t->size; i++) { + tempMat[i][t->size] = 0; + } + memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); + + // 释放原数组 + free(t->adjMat[0]); + free(t->adjMat); + + // 扩容后,指向新地址 + t->adjMat = tempMat; + t->capacity = t->size * 2; + t->size++; + } + + /* 删除顶点 */ + void removeVertex(graphAdjMat *t, unsigned int index) { + // 越界检查 + if (index < 0 || index >= t->size) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + exit(1); + } + + // 清除删除的顶点,并将其后所有顶点前移 + for (int i = index; i < t->size - 1; i++) { + t->vertices[i] = t->vertices[i + 1]; + } + + // 将被前移的最后一个顶点置0 + t->vertices[t->size - 1] = 0; + + // 清除邻接矩阵中删除的列 + for (int i = 0; i < t->size - 1; i++) { + if (i < index) { + // 被删除列后的所有列前移 + for (int j = index; j < t->size - 1; j++) { + t->adjMat[i][j] = t->adjMat[i][j + 1]; + } + } else { + // 被删除行的下方所有行上移 + memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); + // 被删除列后的所有列前移 + for (int j = index; j < t->size; j++) { + t->adjMat[i][j] = t->adjMat[i][j + 1]; + } + } + } + t->size--; + } + + /* 打印顶点与邻接矩阵 */ + void printGraph(graphAdjMat *t) { + if (t->size == 0) { + printf("graph is empty\n"); + return; + } + printf("顶点列表 = ["); + for (int i = 0; i < t->size; i++) { + if (i != t->size - 1) { + printf("%d, ", t->vertices[i]); + } else { + printf("%d", t->vertices[i]); + } + } + printf("]\n"); + printf("邻接矩阵 =\n[\n"); + for (int i = 0; i < t->size; i++) { + printf(" ["); + for (int j = 0; j < t->size; j++) { + if (j != t->size - 1) { + printf("%u, ", t->adjMat[i][j]); + } else { + printf("%u", t->adjMat[i][j]); + } + } + printf("],\n"); + } + printf("]\n"); + } + + /* 构造函数 */ + graphAdjMat *newGraphic(unsigned int numberVertices, int *vertices, unsigned int **adjMat) { + // 函数指针 + graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); + + // 申请内存 + newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); + newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); + unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); + newGraph->size = numberVertices; + newGraph->capacity = numberVertices * 2; + + // 配置二维数组 + for (int i = 0; i < numberVertices * 2; i++) { + newGraph->adjMat[i] = temp + i * numberVertices * 2; + } + + // 赋值 + memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices); + for (int i = 0; i < numberVertices; i++) { + memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); + } + + return newGraph; + } ``` === "C#" @@ -1376,7 +1550,135 @@ comments: true === "C" ```c title="graph_adjacency_list.c" - [class]{graphAdjList}-[func]{} + /* 基于邻接链表实现的无向图类结构 */ + struct graphAdjList { + // 顶点列表 + Vertex **verticesList; + // 顶点数量 + unsigned int size; + // 当前容量 + unsigned int capacity; + }; + + typedef struct graphAdjList graphAdjList; + + /* 添加边 */ + void addEdge(graphAdjList *t, int i, int j) { + // 越界检查 + if (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + return; + } + // 查找待连接的节点 + Vertex *v1 = t->verticesList[i]; + Vertex *v2 = t->verticesList[j]; + + // 连接节点 + pushBack(v1->linked, v2); + pushBack(v2->linked, v1); + } + + /* 删除边 */ + void removeEdge(graphAdjList *t, int i, int j) { + // 越界检查 + if (i < 0 || j < 0 || i == j || i >= t->size || j >= t->size) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + return; + } + + // 查找待删除边的相关节点 + Vertex *v1 = t->verticesList[i]; + Vertex *v2 = t->verticesList[j]; + + // 移除待删除边 + removeLink(v1->linked, v2); + removeLink(v2->linked, v1); + } + + /* 添加顶点 */ + void addVertex(graphAdjList *t, int val) { + // 若大小超过容量,则扩容 + if (t->size >= t->capacity) { + Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity); + memcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size); + free(t->verticesList); + // 指向新顶点表 + t->verticesList = tempList; + t->capacity = t->capacity * 2; + } + // 申请新顶点内存并将新顶点地址存入顶点列表 + Vertex *newV = newVertex(val); + newV->pos = t->size; + newV->linked = newLinklist(newV); + t->verticesList[t->size] = newV; + t->size++; + } + + /* 删除顶点 */ + void removeVertex(graphAdjList *t, unsigned int index) { + // 越界检查 + if (index < 0 || index >= t->size) { + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + exit(1); + } + + // 查找待删节点 + Vertex *v = t->verticesList[index]; + // 若不存在该节点,则返回 + if (v == 0) { + printf("index is:%d\n", index); + printf("Out of range in %s:%d\n", __FILE__, __LINE__); + return; + } + + // 遍历待删除节点链表,将所有与待删除结点有关的边删除 + Node *temp = v->linked->head->next; + while (temp != 0) { + removeLink(temp->val->linked, v); + temp = temp->next; + } + + // 定点列表前移 + for (int i = index; i < t->size - 1; i++) { + t->verticesList[i] = t->verticesList[i + 1]; + } + t->verticesList[t->size - 1] = 0; + t->size--; + + //释放被删除顶点的内存 + freeVertex(v); + } + + /* 打印顶点与邻接矩阵 */ + void printGraph(graphAdjList *t) { + printf("邻接表 =\n"); + for (int i = 0; i < t->size; i++) { + Node *n = t->verticesList[i]->linked->head->next; + printf("%d: [", t->verticesList[i]->val); + while (n != 0) { + if (n->next != 0) { + printf("%d, ", n->val->val); + } else { + printf("%d", n->val->val); + } + n = n->next; + } + printf("]\n"); + } + } + + /* 构造函数 */ + graphAdjList *newGraphic(unsigned int verticesNumber) { + // 申请内存 + graphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList)); + // 建立顶点表并分配内存 + newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesNumber); + memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesNumber); + // 初始化大小和容量 + newGraph->size = 0; + newGraph->capacity = verticesNumber; + return newGraph; + } ``` === "C#" diff --git a/chapter_introduction/algorithms_are_everywhere.md b/chapter_introduction/algorithms_are_everywhere.md index b99b26128..bf6a6c287 100644 --- a/chapter_introduction/algorithms_are_everywhere.md +++ b/chapter_introduction/algorithms_are_everywhere.md @@ -8,18 +8,10 @@ comments: true 在正式探讨算法之前,有一个有趣的事实值得分享:**你已经在不知不觉中学会了许多算法,并习惯将它们应用到日常生活中了**。下面,我将举几个具体例子来证实这一点。 -**例一:拼装积木**。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。 +**例一:查阅字典**。在字典里,每个汉字都对应一个拼音,而字典是按照拼音的英文字母顺序排列的。假设我们需要查找一个拼音首字母为 $r$ 的字,通常会这样操作: -从数据结构与算法的角度来看,积木的各种形状和连接方式代表数据结构,而组装说明书上的一系列步骤则是算法。 - -![拼装积木](algorithms_are_everywhere.assets/assembling_blocks.jpg) - -

Fig. 拼装积木

- -**例二:查阅字典**。在字典里,每个汉字都对应一个拼音,而字典是按照拼音的英文字母顺序排列的。假设我们需要查找一个拼音首字母为 $r$ 的字,通常会这样操作: - -1. 翻开字典约一半的页数,查看该页首字母是什么(假设为 $m$ ); -2. 由于在英文字母表中 $r$ 位于 $m$ 之后,所以排除字典前半部分,查找范围缩小到后半部分; +1. 翻开字典约一半的页数,查看该页首字母是什么,假设首字母为 $m$ 。 +2. 由于在英文字母表中 $r$ 位于 $m$ 之后,所以排除字典前半部分,查找范围缩小到后半部分。 3. 不断重复步骤 1-2 ,直至找到拼音首字母为 $r$ 的页码为止。 === "<1>" @@ -39,6 +31,19 @@ comments: true 查阅字典这个小学生必备技能,实际上就是著名的「二分查找」。从数据结构的角度,我们可以把字典视为一个已排序的「数组」;从算法的角度,我们可以将上述查字典的一系列操作看作是「二分查找」算法。 +**例二:整理扑克**。我们在打斗地主时,每局都需要整理扑克牌,使其从小到大排列,实现流程如下: + +1. 将扑克牌划分为“有序”和“无序”两部分,并假设初始状态下最左 1 张扑克牌已经有序。 +2. 在无序区间抽出一张扑克牌,插入至有序区间的正确位置;完成后最左 2 张扑克已经有序。 +3. 在无序区间抽出一张扑克牌,插入至有序区间的正确位置;完成后最左 3 张扑克已经有序。 +4. 不断循环以上操作,直至所有扑克牌都有序后终止。 + +以上整理扑克牌的方法本质上就是「插入排序」,它在处理小型数据集时非常高效,因此插入排序常作为编程语言的排序库函数的重要组成部分。 + +![扑克排序步骤](algorithms_are_everywhere.assets/playing_cards_sorting.png) + +

Fig. 扑克排序步骤

+ **例三:货币找零**。假设我们在超市购买了 $69$ 元的商品,给收银员付了 $100$ 元,则收银员需要给我们找 $31$ 元。他会很自然地完成以下思考: 1. 可选项是比 $31$ 元面值更小的货币,包括 $1$ , $5$ , $10$ , $20$ 元。 @@ -49,9 +54,9 @@ comments: true 在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是「贪心算法」。 -![货币找零](algorithms_are_everywhere.assets/greedy_change.png) +![货币找零过程](algorithms_are_everywhere.assets/greedy_change.png) -

Fig. 货币找零

+

Fig. 货币找零过程

小到烹饪一道菜,大到星际航行,几乎所有问题的解决都离不开算法。计算机的出现使我们能够通过编程将数据结构存储在内存中,同时编写代码调用 CPU 和 GPU 执行算法。这样一来,我们就能把生活中的问题转移到计算机上,以更高效的方式解决各种复杂问题。 diff --git a/chapter_introduction/summary.md b/chapter_introduction/summary.md index b07cc463d..8ce4d05ef 100644 --- a/chapter_introduction/summary.md +++ b/chapter_introduction/summary.md @@ -6,6 +6,8 @@ comments: true - 算法在日常生活中无处不在,并不是遥不可及的高深知识。实际上,我们已经在不知不觉中学会了许多算法,用以解决生活中的大小问题。 - 查阅字典的原理与二分查找算法相一致。二分查找体现了分而治之的重要算法思想。 +- 整理扑克的过程与插入排序算法非常类似。插入排序适合排序小型数据集。 +- 货币找零的步骤本质上是贪心算法,每一步都采取当前看来的最好选择。 - 算法是在有限时间内解决特定问题的一组指令或操作步骤,而数据结构是计算机中组织和存储数据的方式。 - 数据结构与算法紧密相连。数据结构是算法的基石,而算法则是发挥数据结构作用的舞台。 - 乐高积木对应于数据,积木形状和连接方式代表数据结构,拼装积木的步骤则对应算法。 diff --git a/chapter_introduction/what_is_dsa.md b/chapter_introduction/what_is_dsa.md index 2433dd06e..57c2b1c43 100644 --- a/chapter_introduction/what_is_dsa.md +++ b/chapter_introduction/what_is_dsa.md @@ -34,16 +34,22 @@ comments: true

Fig. 数据结构与算法的关系

-类比「LEGO 乐高」和「数据结构与算法」,则对应关系如下表所示。 +我们可以把数据结构与算法类比为拼装积木。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。 + +![拼装积木](what_is_dsa.assets/assembling_blocks.jpg) + +

Fig. 拼装积木

+ +两者的详细对应关系如下表所示。
-| 数据结构与算法 | LEGO 乐高 | -| -------------- | ------------------------------- | -| 输入数据 | 未拼装的积木 | +| 数据结构与算法 | LEGO 乐高 | +| -------------- | ---------------------------------------- | +| 输入数据 | 未拼装的积木 | | 数据结构 | 积木组织形式,包括形状、大小、连接方式等 | -| 算法 | 把积木拼成目标形态的一系列操作步骤 | -| 输出数据 | 积木模型 | +| 算法 | 把积木拼成目标形态的一系列操作步骤 | +| 输出数据 | 积木模型 |