forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add summary for the chapters of introduction, hashing, heap, graph, s…
…orting
- Loading branch information
Showing
15 changed files
with
78 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 小结 | ||
|
||
- 图由顶点和边组成,可以表示为一组顶点和一组边构成的集合。 | ||
- 相比线性关系(链表)和分治关系(树),网络关系(图)的自由度更高,也从而更为复杂。 | ||
- 有向图的边存在方向,连通图中的任意顶点都可达,有权图的每条边都包含权重变量。 | ||
- 邻接矩阵使用方阵来表示图,每一行(列)代表一个顶点,矩阵元素代表边,使用 $1$ 或 $0$ 来表示两个顶点之间有边或无边。邻接矩阵的增删查操作效率很高,但占用空间大。 | ||
- 邻接表使用多个链表来表示图,第 $i$ 条链表对应顶点 $i$ ,其中存储了该顶点的所有邻接顶点。邻接表相对邻接矩阵更加节省空间,但由于需要通过遍历链表来查找边,因此时间效率较低。 | ||
- 当邻接表中的链表过长时,可以将其转化为红黑树或哈希表,从而提升查询效率。 | ||
- 从算法思想角度分析,邻接矩阵体现“以空间换时间”,邻接表体现“以时间换空间” | ||
- 图可以用于建模各类现实系统,例如社交网络、地铁线路等。 | ||
- 树是图的一种特例,树的遍历也是图的遍历的一种特例。 | ||
- 图的广度优先遍历是一种由近及远、层层扩张的搜索方式,常借助队列实现。 | ||
- 图的深度优先遍历是一种优先走到底、无路可走再回头的搜索方式,常基于递归来实现。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
# 小结 | ||
|
||
- 向哈希表中输入一个键 key ,查询到值 value 的时间复杂度为 $O(1)$ ,非常高效。 | ||
- 哈希表的常用操作包括查询、添加与删除键值对、遍历键值对等。 | ||
- 哈希函数将 key 映射到桶(数组)索引,从而访问到对应的值 value 。 | ||
- 两个不同的 key 经过哈希函数可能得到相同的桶索引,进而发生哈希冲突,导致查询错误。 | ||
- 缓解哈希冲突的途径有两种:哈希表扩容、优化哈希表的表示方式。 | ||
- 负载因子定义为哈希表中元素数量除以桶槽数量,体现哈希冲突的严重程度,常用作哈希表扩容的触发条件。与数组扩容的原理类似,哈希表扩容操作开销也很大。 | ||
- 链式地址考虑将单个元素转化成一个链表,将所有冲突元素都存储在一个链表中,从而解决哈希冲突。而为了提升查询效率,可以把链表转化为 AVL 树或红黑树, | ||
- 开放寻址通过多次探测来解决哈希冲突。线性探测使用固定步长,缺点是不能删除元素且容易产生聚集。多次哈希使用多个哈希函数进行探测,相对线性探测不容易产生聚集,代价是多个哈希函数增加了计算量。 | ||
- 在工业界中,Java 的 HashMap 采用链式地址、Python 的 Dict 采用开放寻址。 |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# 小结 | ||
|
||
- 堆是一棵限定条件下的完全二叉树,根据成立条件可分为大顶堆和小顶堆。大(小)顶堆的堆顶元素最大(小)。 | ||
- 优先队列定义为一种具有出队优先级的队列。堆是实现优先队列的最常用数据结构。 | ||
- 堆的常用操作和对应时间复杂度为元素入堆 $O(\log n)$ 、堆顶元素出堆 $O(\log n)$ 、访问堆顶元素 $O(1)$ 等。 | ||
- 完全二叉树非常适合用数组来表示,因此我们一般用数组来存储堆。 | ||
- 堆化操作用于修复堆的特性,在入堆和出堆操作中都会使用到。 | ||
- 输入 $n$ 个元素并建堆的时间复杂度可以被优化至 $O(n)$ ,非常高效。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 小结 | ||
|
||
- 算法在生活中随处可见,并不高深莫测。我们已经不知不觉地学习到许多“算法”,用于解决生活中大大小小的问题。 | ||
- “查字典”的原理和二分查找算法一致。二分体现分而治之的重要算法思想。 | ||
- 算法是在有限时间内解决特定问题的一组指令或操作步骤,数据结构是在计算机中组织与存储数据的方式。 | ||
- 数据结构与算法两者紧密联系。数据结构是算法的底座,算法是发挥数据结构的舞台。 | ||
- 乐高积木对应数据,积木形状和连接形式对应数据结构,拼装积木的流程步骤对应算法。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
# 小结 | ||
|
||
- 冒泡排序通过交换相邻元素来实现排序。通过增加标志位实现提前返回,我们可将冒泡排序的最佳时间复杂度优化至 $O(N)$ 。 | ||
- 插入排序每轮将待排序区间内元素插入至已排序区间的正确位置,从而实现排序。插入排序的时间复杂度虽为 $O(N^2)$ ,但因为总体操作少而很受欢迎,一般用于小数据量的排序工作。 | ||
- 快速排序基于哨兵划分操作实现排序。在哨兵划分中,有可能每次都选取到最差的基准数,从而导致时间复杂度劣化至 $O(N^2)$ ,通过引入中位数基准数或随机基准数可大大降低劣化概率。尾递归方法可以有效减小递归深度,将空间复杂度优化至 $O(\log N)$ 。 | ||
- 归并排序包含划分和合并两个阶段,是分而治之的标准体现。对于归并排序,排序数组需要借助辅助数组,空间复杂度为 $O(N)$ ;而排序链表的空间复杂度可以被优化至 $O(1)$ 。 | ||
- 下图总结对比了各个排序算法的运行效率与特性。其中,桶排序中 $k$ 为桶的数量;基数排序仅适用于正整数、字符串、特定格式的浮点数,$k$ 为最大数字的位数。 | ||
|
||
![排序算法对比](summary.assets/sorting_algorithms_comparison.png) | ||
|
||
- 总体来看,我们追求运行快、稳定、原地、正向自适应性的排序。显然,如同其它数据结构与算法一样,同时满足这些条件的排序算法并不存在,我们需要根据问题特点来选择排序算法。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
# 小结 | ||
|
||
### 二叉树 | ||
|
||
- 二叉树是一种非线性数据结构,代表着“一分为二”的分治逻辑。二叉树的结点包含「值」和两个「指针」,分别指向左子结点和右子结点。 | ||
- 选定二叉树中某结点,将其左(右)子结点以下形成的树称为左(右)子树。 | ||
- 二叉树的术语较多,包括根结点、叶结点、层、度、边、高度、深度等。 | ||
- 二叉树的初始化、结点插入、结点删除操作与链表的操作方法类似。 | ||
- 常见的二叉树类型包括完美二叉树、完全二叉树、完满二叉树、平衡二叉树。完美二叉树是理想状态,链表则是退化后的最差状态。 | ||
- 二叉树可以使用数组表示,具体做法是将结点值和空位按照层序遍历的顺序排列,并基于父结点和子结点之间的索引映射公式实现指针。 | ||
|
||
### 二叉树遍历 | ||
|
||
- 二叉树层序遍历是一种广度优先搜索,体现着“一圈一圈向外”的层进式遍历方式,通常借助队列来实现。 | ||
- 前序、中序、后序遍历是深度优先搜索,体现着“走到头、再回头继续”的回溯遍历方式,通常使用递归实现。 | ||
|
||
### 二叉搜索树 | ||
|
||
- 二叉搜索树是一种高效的元素查找数据结构,查找、插入、删除操作的时间复杂度皆为 $O(\log n)$ 。二叉搜索树退化为链表后,各项时间复杂度劣化至 $O(n)$ ,因此如何避免退化是非常重要的课题。 | ||
- AVL 树又称平衡二叉搜索树,其通过旋转操作,使得在不断插入与删除结点后,仍然可以保持二叉树的平衡(不退化)。 | ||
- AVL 树的旋转操作分为右旋、左旋、先右旋后左旋、先左旋后右旋。在插入或删除结点后,AVL 树会从底至顶地执行旋转操作,使树恢复平衡。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters