mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:36:29 +08:00
deploy
This commit is contained in:
parent
7803a73a99
commit
b1bd0c8f8d
43 changed files with 990 additions and 430 deletions
|
@ -3813,6 +3813,11 @@
|
|||
<td>雜湊表</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>hash set</td>
|
||||
<td>哈希集合</td>
|
||||
<td>雜湊集合</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>bucket</td>
|
||||
<td>桶</td>
|
||||
<td>桶</td>
|
||||
|
|
|
@ -5189,7 +5189,7 @@
|
|||
<h2 id="424">4.2.4 链表典型应用<a class="headerlink" href="#424" title="Permanent link">¶</a></h2>
|
||||
<p>单向链表通常用于实现栈、队列、哈希表和图等数据结构。</p>
|
||||
<ul>
|
||||
<li><strong>栈与队列</strong>:当插入和删除操作都在链表的一端进行时,它表现出先进后出的特性,对应栈;当插入操作在链表的一端进行,删除操作在链表的另一端进行,它表现出先进先出的特性,对应队列。</li>
|
||||
<li><strong>栈与队列</strong>:当插入和删除操作都在链表的一端进行时,它表现的特性为先进后出,对应栈;当插入操作在链表的一端进行,删除操作在链表的另一端进行,它表现的特性为先进先出,对应队列。</li>
|
||||
<li><strong>哈希表</strong>:链式地址是解决哈希冲突的主流方案之一,在该方案中,所有冲突的元素都会被放到一个链表中。</li>
|
||||
<li><strong>图</strong>:邻接表是表示图的一种常用方式,其中图的每个顶点都与一个链表相关联,链表中的每个元素都代表与该顶点相连的其他顶点。</li>
|
||||
</ul>
|
||||
|
|
|
@ -3748,8 +3748,8 @@
|
|||
<p><a class="glightbox" href="../ram_and_cache.assets/storage_pyramid.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="计算机存储系统" class="animation-figure" src="../ram_and_cache.assets/storage_pyramid.png" /></a></p>
|
||||
<p align="center"> 图 4-9 计算机存储系统 </p>
|
||||
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>计算机的存储层次结构体现了速度、容量和成本三者之间的精妙平衡。实际上,这种权衡普遍存在于所有工业领域,它要求我们在不同的优势和限制之间找到最佳平衡点。</p>
|
||||
</div>
|
||||
<p>总的来说,<strong>硬盘用于长期存储大量数据,内存用于临时存储程序运行中正在处理的数据,而缓存则用于存储经常访问的数据和指令</strong>,以提高程序运行效率。三者共同协作,确保计算机系统高效运行。</p>
|
||||
|
|
|
@ -4285,7 +4285,7 @@
|
|||
<p><a class="glightbox" href="../permutations_problem.assets/permutations_ii.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="重复排列" class="animation-figure" src="../permutations_problem.assets/permutations_ii.png" /></a></p>
|
||||
<p align="center"> 图 13-7 重复排列 </p>
|
||||
|
||||
<p>那么如何去除重复的排列呢?最直接地,考虑借助一个哈希表,直接对排列结果进行去重。然而这样做不够优雅,<strong>因为生成重复排列的搜索分支没有必要,应当提前识别并剪枝</strong>,这样可以进一步提升算法效率。</p>
|
||||
<p>那么如何去除重复的排列呢?最直接地,考虑借助一个哈希集合,直接对排列结果进行去重。然而这样做不够优雅,<strong>因为生成重复排列的搜索分支没有必要,应当提前识别并剪枝</strong>,这样可以进一步提升算法效率。</p>
|
||||
<h3 id="1_1">1. 相等元素剪枝<a class="headerlink" href="#1_1" title="Permanent link">¶</a></h3>
|
||||
<p>观察图 13-8 ,在第一轮中,选择 <span class="arithmatex">\(1\)</span> 或选择 <span class="arithmatex">\(\hat{1}\)</span> 是等价的,在这两个选择之下生成的所有排列都是重复的。因此应该把 <span class="arithmatex">\(\hat{1}\)</span> 剪枝。</p>
|
||||
<p>同理,在第一轮选择 <span class="arithmatex">\(2\)</span> 之后,第二轮选择中的 <span class="arithmatex">\(1\)</span> 和 <span class="arithmatex">\(\hat{1}\)</span> 也会产生重复分支,因此也应将第二轮的 <span class="arithmatex">\(\hat{1}\)</span> 剪枝。</p>
|
||||
|
@ -4294,7 +4294,7 @@
|
|||
<p align="center"> 图 13-8 重复排列剪枝 </p>
|
||||
|
||||
<h3 id="2_1">2. 代码实现<a class="headerlink" href="#2_1" title="Permanent link">¶</a></h3>
|
||||
<p>在上一题的代码的基础上,我们考虑在每一轮选择中开启一个哈希表 <code>duplicated</code> ,用于记录该轮中已经尝试过的元素,并将重复元素剪枝:</p>
|
||||
<p>在上一题的代码的基础上,我们考虑在每一轮选择中开启一个哈希集合 <code>duplicated</code> ,用于记录该轮中已经尝试过的元素,并将重复元素剪枝:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:14"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><input id="__tabbed_2_14" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label><label for="__tabbed_2_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3671,7 +3671,7 @@
|
|||
<li>回溯问题通常包含多个约束条件,它们可用于实现剪枝操作。剪枝可以提前结束不必要的搜索分支,大幅提升搜索效率。</li>
|
||||
<li>回溯算法主要可用于解决搜索问题和约束满足问题。组合优化问题虽然可以用回溯算法解决,但往往存在效率更高或效果更好的解法。</li>
|
||||
<li>全排列问题旨在搜索给定集合元素的所有可能的排列。我们借助一个数组来记录每个元素是否被选择,剪掉重复选择同一元素的搜索分支,确保每个元素只被选择一次。</li>
|
||||
<li>在全排列问题中,如果集合中存在重复元素,则最终结果会出现重复排列。我们需要约束相等元素在每轮中只能被选择一次,这通常借助一个哈希表来实现。</li>
|
||||
<li>在全排列问题中,如果集合中存在重复元素,则最终结果会出现重复排列。我们需要约束相等元素在每轮中只能被选择一次,这通常借助一个哈希集合来实现。</li>
|
||||
<li>子集和问题的目标是在给定集合中找到和为目标值的所有子集。集合不区分元素顺序,而搜索过程会输出所有顺序的结果,产生重复子集。我们在回溯前将数据进行排序,并设置一个变量来指示每一轮的遍历起始点,从而将生成重复子集的搜索分支进行剪枝。</li>
|
||||
<li>对于子集和问题,数组中的相等元素会产生重复集合。我们利用数组已排序的前置条件,通过判断相邻元素是否相等实现剪枝,从而确保相等元素在每轮中只能被选中一次。</li>
|
||||
<li><span class="arithmatex">\(n\)</span> 皇后问题旨在寻找将 <span class="arithmatex">\(n\)</span> 个皇后放置到 <span class="arithmatex">\(n \times n\)</span> 尺寸棋盘上的方案,要求所有皇后两两之间无法攻击对方。该问题的约束条件有行约束、列约束、主对角线和次对角线约束。为满足行约束,我们采用按行放置的策略,保证每一行放置一个皇后。</li>
|
||||
|
|
|
@ -4565,7 +4565,7 @@ T(n) = 3 + 2n
|
|||
<p><span class="arithmatex">\(T(n)\)</span> 是一次函数,说明其运行时间的增长趋势是线性的,因此它的时间复杂度是线性阶。</p>
|
||||
<p>我们将线性阶的时间复杂度记为 <span class="arithmatex">\(O(n)\)</span> ,这个数学符号称为<u>大 <span class="arithmatex">\(O\)</span> 记号(big-<span class="arithmatex">\(O\)</span> notation)</u>,表示函数 <span class="arithmatex">\(T(n)\)</span> 的<u>渐近上界(asymptotic upper bound)</u>。</p>
|
||||
<p>时间复杂度分析本质上是计算“操作数量 <span class="arithmatex">\(T(n)\)</span>”的渐近上界,它具有明确的数学定义。</p>
|
||||
<div class="admonition abstract">
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">函数渐近上界</p>
|
||||
<p>若存在正实数 <span class="arithmatex">\(c\)</span> 和实数 <span class="arithmatex">\(n_0\)</span> ,使得对于所有的 <span class="arithmatex">\(n > n_0\)</span> ,均有 <span class="arithmatex">\(T(n) \leq c \cdot f(n)\)</span> ,则可认为 <span class="arithmatex">\(f(n)\)</span> 给出了 <span class="arithmatex">\(T(n)\)</span> 的一个渐近上界,记为 <span class="arithmatex">\(T(n) = O(f(n))\)</span> 。</p>
|
||||
</div>
|
||||
|
|
|
@ -3664,8 +3664,8 @@
|
|||
|
||||
<!-- Page content -->
|
||||
<h1 id="33">3.3 数字编码 *<a class="headerlink" href="#33" title="Permanent link">¶</a></h1>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>在本书中,标题带有 * 符号的是选读章节。如果你时间有限或感到理解困难,可以先跳过,等学完必读章节后再单独攻克。</p>
|
||||
</div>
|
||||
<h2 id="331">3.3.1 原码、反码和补码<a class="headerlink" href="#331" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -3774,7 +3774,11 @@
|
|||
<li>在循环的每轮迭代中,弹出队首顶点并记录访问,然后将该顶点的所有邻接顶点加入到队列尾部。</li>
|
||||
<li>循环步骤 <code>2.</code> ,直到所有顶点被访问完毕后结束。</li>
|
||||
</ol>
|
||||
<p>为了防止重复遍历顶点,我们需要借助一个哈希表 <code>visited</code> 来记录哪些节点已被访问。</p>
|
||||
<p>为了防止重复遍历顶点,我们需要借助一个哈希集合 <code>visited</code> 来记录哪些节点已被访问。</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>哈希集合可以看作一个只存储 <code>key</code> 而不存储 <code>value</code> 的哈希表,它可以在 <span class="arithmatex">\(O(1)\)</span> 时间复杂度下进行 <code>key</code> 的增删查改操作。根据 <code>key</code> 的唯一性,哈希集合通常用于数据去重等场景。</p>
|
||||
</div>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:14"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><input id="__tabbed_1_14" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label><label for="__tabbed_1_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
@ -3783,7 +3787,7 @@
|
|||
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a> <span class="c1"># 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a> <span class="n">que</span> <span class="o">=</span> <span class="n">deque</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
|
@ -3807,7 +3811,7 @@
|
|||
<a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="n">startVet</span><span class="p">};</span>
|
||||
<a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="n">queue</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">que</span><span class="p">;</span>
|
||||
|
@ -3836,7 +3840,7 @@
|
|||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -3865,7 +3869,7 @@
|
|||
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">startVet</span><span class="p">];</span>
|
||||
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="n">Queue</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="p">();</span>
|
||||
|
@ -3894,7 +3898,7 @@
|
|||
<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">[</span><span class="nx">startVet</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}{}</span>
|
||||
<a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS, 使用切片模拟队列</span>
|
||||
|
@ -3928,7 +3932,7 @@
|
|||
<a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="kd">func</span> <span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a> <span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-5-7" name="__codelineno-5-7" href="#__codelineno-5-7"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
<a id="__codelineno-5-8" name="__codelineno-5-8" href="#__codelineno-5-8"></a> <span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-5-9" name="__codelineno-5-9" href="#__codelineno-5-9"></a> <span class="kd">var</span> <span class="nv">que</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
|
@ -3956,7 +3960,7 @@
|
|||
<a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-6-7" name="__codelineno-6-7" href="#__codelineno-6-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-6-8" name="__codelineno-6-8" href="#__codelineno-6-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-6-9" name="__codelineno-6-9" href="#__codelineno-6-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -3985,7 +3989,7 @@
|
|||
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -4014,7 +4018,7 @@
|
|||
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="w"> </span><span class="c1">// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -4044,7 +4048,7 @@
|
|||
<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="k">fn</span> <span class="nf">graph_bfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -4148,7 +4152,7 @@
|
|||
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex</span><span class="p">):</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-11-9" name="__codelineno-11-9" href="#__codelineno-11-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -4177,7 +4181,7 @@
|
|||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="c1"># 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span>
|
||||
|
@ -4254,14 +4258,14 @@
|
|||
</div>
|
||||
<h3 id="2">2. 复杂度分析<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
|
||||
<p><strong>时间复杂度</strong>:所有顶点都会入队并出队一次,使用 <span class="arithmatex">\(O(|V|)\)</span> 时间;在遍历邻接顶点的过程中,由于是无向图,因此所有边都会被访问 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 时间;总体使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 时间。</p>
|
||||
<p><strong>空间复杂度</strong>:列表 <code>res</code> ,哈希表 <code>visited</code> ,队列 <code>que</code> 中的顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p>
|
||||
<p><strong>空间复杂度</strong>:列表 <code>res</code> ,哈希集合 <code>visited</code> ,队列 <code>que</code> 中的顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p>
|
||||
<h2 id="932">9.3.2 深度优先遍历<a class="headerlink" href="#932" title="Permanent link">¶</a></h2>
|
||||
<p><strong>深度优先遍历是一种优先走到底、无路可走再回头的遍历方式</strong>。如图 9-11 所示,从左上角顶点出发,访问当前顶点的某个邻接顶点,直到走到尽头时返回,再继续走到尽头并返回,以此类推,直至所有顶点遍历完成。</p>
|
||||
<p><a class="glightbox" href="../graph_traversal.assets/graph_dfs.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的深度优先遍历" class="animation-figure" src="../graph_traversal.assets/graph_dfs.png" /></a></p>
|
||||
<p align="center"> 图 9-11 图的深度优先遍历 </p>
|
||||
|
||||
<h3 id="1_1">1. 算法实现<a class="headerlink" href="#1_1" title="Permanent link">¶</a></h3>
|
||||
<p>这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中,我们也需要借助一个哈希表 <code>visited</code> 来记录已被访问的顶点,以避免重复访问顶点。</p>
|
||||
<p>这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中,我们也需要借助一个哈希集合 <code>visited</code> 来记录已被访问的顶点,以避免重复访问顶点。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:14"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><input id="__tabbed_3_13" name="__tabbed_3" type="radio" /><input id="__tabbed_3_14" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label><label for="__tabbed_3_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
@ -4281,7 +4285,7 @@
|
|||
<a id="__codelineno-14-14" name="__codelineno-14-14" href="#__codelineno-14-14"></a> <span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-14-15" name="__codelineno-14-15" href="#__codelineno-14-15"></a> <span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-14-16" name="__codelineno-14-16" href="#__codelineno-14-16"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-14-18" name="__codelineno-14-18" href="#__codelineno-14-18"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]()</span>
|
||||
<a id="__codelineno-14-19" name="__codelineno-14-19" href="#__codelineno-14-19"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-14-20" name="__codelineno-14-20" href="#__codelineno-14-20"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -4306,7 +4310,7 @@
|
|||
<a id="__codelineno-15-16" name="__codelineno-15-16" href="#__codelineno-15-16"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-15-17" name="__codelineno-15-17" href="#__codelineno-15-17"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-15-18" name="__codelineno-15-18" href="#__codelineno-15-18"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-15-20" name="__codelineno-15-20" href="#__codelineno-15-20"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-21" name="__codelineno-15-21" href="#__codelineno-15-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-15-22" name="__codelineno-15-22" href="#__codelineno-15-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4332,7 +4336,7 @@
|
|||
<a id="__codelineno-16-16" name="__codelineno-16-16" href="#__codelineno-16-16"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-16-17" name="__codelineno-16-17" href="#__codelineno-16-17"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-16-18" name="__codelineno-16-18" href="#__codelineno-16-18"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-16-20" name="__codelineno-16-20" href="#__codelineno-16-20"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-21" name="__codelineno-16-21" href="#__codelineno-16-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-16-22" name="__codelineno-16-22" href="#__codelineno-16-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4359,7 +4363,7 @@
|
|||
<a id="__codelineno-17-17" name="__codelineno-17-17" href="#__codelineno-17-17"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-17-18" name="__codelineno-17-18" href="#__codelineno-17-18"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-17-19" name="__codelineno-17-19" href="#__codelineno-17-19"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-17-21" name="__codelineno-17-21" href="#__codelineno-17-21"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-22" name="__codelineno-17-22" href="#__codelineno-17-22"></a><span class="w"> </span><span class="n">DFS</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-17-23" name="__codelineno-17-23" href="#__codelineno-17-23"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4387,7 +4391,7 @@
|
|||
<a id="__codelineno-18-18" name="__codelineno-18-18" href="#__codelineno-18-18"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-18-19" name="__codelineno-18-19" href="#__codelineno-18-19"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-18-20" name="__codelineno-18-20" href="#__codelineno-18-20"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-18-22" name="__codelineno-18-22" href="#__codelineno-18-22"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-18-23" name="__codelineno-18-23" href="#__codelineno-18-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">g</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-24" name="__codelineno-18-24" href="#__codelineno-18-24"></a><span class="w"> </span><span class="c1">// 返回顶点遍历序列</span>
|
||||
|
@ -4415,7 +4419,7 @@
|
|||
<a id="__codelineno-19-17" name="__codelineno-19-17" href="#__codelineno-19-17"></a><span class="kd">func</span> <span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-19-18" name="__codelineno-19-18" href="#__codelineno-19-18"></a> <span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-19-19" name="__codelineno-19-19" href="#__codelineno-19-19"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-19-21" name="__codelineno-19-21" href="#__codelineno-19-21"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-22" name="__codelineno-19-22" href="#__codelineno-19-22"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">:</span> <span class="p">&</span><span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&</span><span class="n">res</span><span class="p">,</span> <span class="n">vet</span><span class="p">:</span> <span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-19-23" name="__codelineno-19-23" href="#__codelineno-19-23"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -4443,7 +4447,7 @@
|
|||
<a id="__codelineno-20-18" name="__codelineno-20-18" href="#__codelineno-20-18"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-19" name="__codelineno-20-19" href="#__codelineno-20-19"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-20-20" name="__codelineno-20-20" href="#__codelineno-20-20"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-20-22" name="__codelineno-20-22" href="#__codelineno-20-22"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-20-23" name="__codelineno-20-23" href="#__codelineno-20-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-24" name="__codelineno-20-24" href="#__codelineno-20-24"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -4475,7 +4479,7 @@
|
|||
<a id="__codelineno-21-22" name="__codelineno-21-22" href="#__codelineno-21-22"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-21-23" name="__codelineno-21-23" href="#__codelineno-21-23"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-21-24" name="__codelineno-21-24" href="#__codelineno-21-24"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-21-26" name="__codelineno-21-26" href="#__codelineno-21-26"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-21-27" name="__codelineno-21-27" href="#__codelineno-21-27"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-21-28" name="__codelineno-21-28" href="#__codelineno-21-28"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -4506,7 +4510,7 @@
|
|||
<a id="__codelineno-22-21" name="__codelineno-22-21" href="#__codelineno-22-21"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-22" name="__codelineno-22-22" href="#__codelineno-22-22"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-22-23" name="__codelineno-22-23" href="#__codelineno-22-23"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-22-25" name="__codelineno-22-25" href="#__codelineno-22-25"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-22-26" name="__codelineno-22-26" href="#__codelineno-22-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-22-27" name="__codelineno-22-27" href="#__codelineno-22-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4535,7 +4539,7 @@
|
|||
<a id="__codelineno-23-19" name="__codelineno-23-19" href="#__codelineno-23-19"></a><span class="k">fn</span> <span class="nf">graph_dfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-23-20" name="__codelineno-23-20" href="#__codelineno-23-20"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-23-21" name="__codelineno-23-21" href="#__codelineno-23-21"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-23-23" name="__codelineno-23-23" href="#__codelineno-23-23"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-23-24" name="__codelineno-23-24" href="#__codelineno-23-24"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-23-25" name="__codelineno-23-25" href="#__codelineno-23-25"></a>
|
||||
|
@ -4602,7 +4606,7 @@
|
|||
<a id="__codelineno-25-21" name="__codelineno-25-21" href="#__codelineno-25-21"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex?)</span><span class="p">:</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-25-22" name="__codelineno-25-22" href="#__codelineno-25-22"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-25-23" name="__codelineno-25-23" href="#__codelineno-25-23"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-25-25" name="__codelineno-25-25" href="#__codelineno-25-25"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-26" name="__codelineno-25-26" href="#__codelineno-25-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-25-27" name="__codelineno-25-27" href="#__codelineno-25-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span>
|
||||
|
@ -4627,7 +4631,7 @@
|
|||
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span>
|
||||
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-21" name="__codelineno-26-21" href="#__codelineno-26-21"></a><span class="w"> </span><span class="n">res</span>
|
||||
|
@ -4699,7 +4703,7 @@
|
|||
</div>
|
||||
<h3 id="2_1">2. 复杂度分析<a class="headerlink" href="#2_1" title="Permanent link">¶</a></h3>
|
||||
<p><strong>时间复杂度</strong>:所有顶点都会被访问 <span class="arithmatex">\(1\)</span> 次,使用 <span class="arithmatex">\(O(|V|)\)</span> 时间;所有边都会被访问 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 时间;总体使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 时间。</p>
|
||||
<p><strong>空间复杂度</strong>:列表 <code>res</code> ,哈希表 <code>visited</code> 顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,递归深度最大为 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p>
|
||||
<p><strong>空间复杂度</strong>:列表 <code>res</code> ,哈希集合 <code>visited</code> 顶点数量最多为 <span class="arithmatex">\(|V|\)</span> ,递归深度最大为 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空间。</p>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
|
|
|
@ -4401,11 +4401,20 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">left</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">right</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">parent</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 获取左子节点的索引 ###</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">left</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="c1">### 获取右子节点的索引 ###</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="k">def</span><span class="w"> </span><span class="nf">right</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="c1">### 获取父节点的索引 ###</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="k">def</span><span class="w"> </span><span class="nf">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="c1"># 向下整除</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4516,7 +4525,10 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">peek</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="c1">### 访问堆顶元素 ###</span>
|
||||
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">peek</span>
|
||||
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span>
|
||||
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4886,9 +4898,27 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">push</span><span class="p">}</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_up</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="c1">### 元素入堆 ###</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">push</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="w"> </span><span class="c1"># 添加节点</span>
|
||||
<a id="__codelineno-54-4" name="__codelineno-54-4" href="#__codelineno-54-4"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-54-5" name="__codelineno-54-5" href="#__codelineno-54-5"></a><span class="w"> </span><span class="c1"># 从底至顶堆化</span>
|
||||
<a id="__codelineno-54-6" name="__codelineno-54-6" href="#__codelineno-54-6"></a><span class="w"> </span><span class="n">sift_up</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-7" name="__codelineno-54-7" href="#__codelineno-54-7"></a><span class="k">end</span>
|
||||
<a id="__codelineno-54-8" name="__codelineno-54-8" href="#__codelineno-54-8"></a>
|
||||
<a id="__codelineno-54-9" name="__codelineno-54-9" href="#__codelineno-54-9"></a><span class="c1">### 从节点 i 开始,从底至顶堆化 ###</span>
|
||||
<a id="__codelineno-54-10" name="__codelineno-54-10" href="#__codelineno-54-10"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_up</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-11" name="__codelineno-54-11" href="#__codelineno-54-11"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-54-12" name="__codelineno-54-12" href="#__codelineno-54-12"></a><span class="w"> </span><span class="c1"># 获取节点 i 的父节点</span>
|
||||
<a id="__codelineno-54-13" name="__codelineno-54-13" href="#__codelineno-54-13"></a><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-14" name="__codelineno-54-14" href="#__codelineno-54-14"></a><span class="w"> </span><span class="c1"># 当“越过根节点”或“节点无须修复”时,结束堆化</span>
|
||||
<a id="__codelineno-54-15" name="__codelineno-54-15" href="#__codelineno-54-15"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="nb">p</span><span class="o">]</span>
|
||||
<a id="__codelineno-54-16" name="__codelineno-54-16" href="#__codelineno-54-16"></a><span class="w"> </span><span class="c1"># 交换两节点</span>
|
||||
<a id="__codelineno-54-17" name="__codelineno-54-17" href="#__codelineno-54-17"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="nb">p</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-18" name="__codelineno-54-18" href="#__codelineno-54-18"></a><span class="w"> </span><span class="c1"># 循环向上堆化</span>
|
||||
<a id="__codelineno-54-19" name="__codelineno-54-19" href="#__codelineno-54-19"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">p</span>
|
||||
<a id="__codelineno-54-20" name="__codelineno-54-20" href="#__codelineno-54-20"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-54-21" name="__codelineno-54-21" href="#__codelineno-54-21"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -5307,7 +5337,7 @@
|
|||
<a id="__codelineno-65-7" name="__codelineno-65-7" href="#__codelineno-65-7"></a><span class="w"> </span><span class="c1">// 交换根节点与最右叶节点(交换首元素与尾元素)</span>
|
||||
<a id="__codelineno-65-8" name="__codelineno-65-8" href="#__codelineno-65-8"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-9" name="__codelineno-65-9" href="#__codelineno-65-9"></a><span class="w"> </span><span class="c1">// 删除节点</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">pop</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
|
||||
<a id="__codelineno-65-11" name="__codelineno-65-11" href="#__codelineno-65-11"></a><span class="w"> </span><span class="c1">// 从顶至底堆化</span>
|
||||
<a id="__codelineno-65-12" name="__codelineno-65-12" href="#__codelineno-65-12"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-13" name="__codelineno-65-13" href="#__codelineno-65-13"></a><span class="w"> </span><span class="c1">// 返回堆顶元素</span>
|
||||
|
@ -5419,9 +5449,37 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">pop</span><span class="p">}</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_down</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="c1">### 元素出堆 ###</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">pop</span>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="w"> </span><span class="c1"># 判空处理</span>
|
||||
<a id="__codelineno-68-4" name="__codelineno-68-4" href="#__codelineno-68-4"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span><span class="p">,</span><span class="w"> </span><span class="s2">"堆为空"</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">is_empty?</span>
|
||||
<a id="__codelineno-68-5" name="__codelineno-68-5" href="#__codelineno-68-5"></a><span class="w"> </span><span class="c1"># 交换根节点与最右叶节点(交换首元素与尾元素)</span>
|
||||
<a id="__codelineno-68-6" name="__codelineno-68-6" href="#__codelineno-68-6"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-7" name="__codelineno-68-7" href="#__codelineno-68-7"></a><span class="w"> </span><span class="c1"># 删除节点</span>
|
||||
<a id="__codelineno-68-8" name="__codelineno-68-8" href="#__codelineno-68-8"></a><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">.</span><span class="n">pop</span>
|
||||
<a id="__codelineno-68-9" name="__codelineno-68-9" href="#__codelineno-68-9"></a><span class="w"> </span><span class="c1"># 从顶至底堆化</span>
|
||||
<a id="__codelineno-68-10" name="__codelineno-68-10" href="#__codelineno-68-10"></a><span class="w"> </span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-11" name="__codelineno-68-11" href="#__codelineno-68-11"></a><span class="w"> </span><span class="c1"># 返回堆顶元素</span>
|
||||
<a id="__codelineno-68-12" name="__codelineno-68-12" href="#__codelineno-68-12"></a><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-68-13" name="__codelineno-68-13" href="#__codelineno-68-13"></a><span class="k">end</span>
|
||||
<a id="__codelineno-68-14" name="__codelineno-68-14" href="#__codelineno-68-14"></a>
|
||||
<a id="__codelineno-68-15" name="__codelineno-68-15" href="#__codelineno-68-15"></a><span class="c1">### 从节点 i 开始,从顶至底堆化 ###</span>
|
||||
<a id="__codelineno-68-16" name="__codelineno-68-16" href="#__codelineno-68-16"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_down</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-17" name="__codelineno-68-17" href="#__codelineno-68-17"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-68-18" name="__codelineno-68-18" href="#__codelineno-68-18"></a><span class="w"> </span><span class="c1"># 判断节点 i, l, r 中值最大的节点,记为 ma</span>
|
||||
<a id="__codelineno-68-19" name="__codelineno-68-19" href="#__codelineno-68-19"></a><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">right</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-20" name="__codelineno-68-20" href="#__codelineno-68-20"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">l</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-21" name="__codelineno-68-21" href="#__codelineno-68-21"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">r</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-22" name="__codelineno-68-22" href="#__codelineno-68-22"></a>
|
||||
<a id="__codelineno-68-23" name="__codelineno-68-23" href="#__codelineno-68-23"></a><span class="w"> </span><span class="c1"># 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出</span>
|
||||
<a id="__codelineno-68-24" name="__codelineno-68-24" href="#__codelineno-68-24"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-25" name="__codelineno-68-25" href="#__codelineno-68-25"></a>
|
||||
<a id="__codelineno-68-26" name="__codelineno-68-26" href="#__codelineno-68-26"></a><span class="w"> </span><span class="c1"># 交换两节点</span>
|
||||
<a id="__codelineno-68-27" name="__codelineno-68-27" href="#__codelineno-68-27"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-28" name="__codelineno-68-28" href="#__codelineno-68-28"></a><span class="w"> </span><span class="c1"># 循环向下堆化</span>
|
||||
<a id="__codelineno-68-29" name="__codelineno-68-29" href="#__codelineno-68-29"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span>
|
||||
<a id="__codelineno-68-30" name="__codelineno-68-30" href="#__codelineno-68-30"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-68-31" name="__codelineno-68-31" href="#__codelineno-68-31"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -4084,7 +4084,28 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">top_k_heap</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 基于堆查找数组中最大的 k 个元素 ###</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">top_k_heap</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 初始化小顶堆</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">max_heap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">MaxHeap</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="c1"># 将数组的前 k 个元素入堆</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="n">k</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a>
|
||||
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="c1"># 从第 k+1 个元素开始,保持堆的长度为 k</span>
|
||||
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">k</span><span class="o">...</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="c1"># 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆</span>
|
||||
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">peek_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="n">pop_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a>
|
||||
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="w"> </span><span class="n">get_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -636,6 +636,17 @@
|
|||
|
||||
|
||||
|
||||
<label class="md-nav__link md-nav__link--active" for="__toc">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.3 小结
|
||||
</span>
|
||||
|
||||
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
</label>
|
||||
|
||||
<a href="./" class="md-nav__link md-nav__link--active">
|
||||
|
||||
|
||||
|
@ -646,6 +657,34 @@
|
|||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
<nav class="md-nav md-nav--secondary" aria-label="目录">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<label class="md-nav__title" for="__toc">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
目录
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-q-a" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Q & A
|
||||
</span>
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
|
@ -3555,6 +3594,23 @@
|
|||
|
||||
|
||||
|
||||
<label class="md-nav__title" for="__toc">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
目录
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-q-a" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Q & A
|
||||
</span>
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -3599,6 +3655,15 @@
|
|||
<li>数据结构与算法紧密相连。数据结构是算法的基石,而算法是数据结构发挥作用的舞台。</li>
|
||||
<li>我们可以将数据结构与算法类比为拼装积木,积木代表数据,积木的形状和连接方式等代表数据结构,拼装积木的步骤则对应算法。</li>
|
||||
</ul>
|
||||
<h3 id="1-q-a">1. Q & A<a class="headerlink" href="#1-q-a" title="Permanent link">¶</a></h3>
|
||||
<p><strong>Q</strong>:作为一名程序员,我在日常工作中从未用算法解决过问题,常用算法都被编程语言封装好了,直接用就可以了;这是否意味着我们工作中的问题还没有到达需要算法的程度?</p>
|
||||
<p>如果把具体的工作技能比作是武功的“招式”的话,那么基础科目应该更像是“内功”。</p>
|
||||
<p>我认为学算法(以及其他基础科目)的意义不是在于在工作中从零实现它,而是基于学到的知识,在解决问题时能够作出专业的反应和判断,从而提升工作的整体质量。举一个简单例子,每种编程语言都内置了排序函数:</p>
|
||||
<ul>
|
||||
<li>如果我们没有学过数据结构与算法,那么给定任何数据,我们可能都塞给这个排序函数去做了。运行顺畅、性能不错,看上去并没有什么问题。</li>
|
||||
<li>但如果学过算法,我们就会知道内置排序函数的时间复杂度是 <span class="arithmatex">\(O(n \log n)\)</span> ;而如果给定的数据是固定位数的整数(例如学号),那么我们就可以用效率更高的“基数排序”来做,将时间复杂度降为 <span class="arithmatex">\(O(nk)\)</span> ,其中 <span class="arithmatex">\(k\)</span> 为位数。当数据体量很大时,节省出来的运行时间就能创造较大价值(成本降低、体验变好等)。</li>
|
||||
</ul>
|
||||
<p>在工程领域中,大量问题是难以达到最优解的,许多问题只是被“差不多”地解决了。问题的难易程度一方面取决于问题本身的性质,另一方面也取决于观测问题的人的知识储备。人的知识越完备、经验越多,分析问题就会越深入,问题就能被解决得更优雅。</p>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
|
|
|
@ -4508,8 +4508,8 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>设平衡因子为 <span class="arithmatex">\(f\)</span> ,则一棵 AVL 树的任意节点的平衡因子皆满足 <span class="arithmatex">\(-1 \le f \le 1\)</span> 。</p>
|
||||
</div>
|
||||
<h2 id="752-avl">7.5.2 AVL 树旋转<a class="headerlink" href="#752-avl" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -4389,8 +4389,8 @@
|
|||
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=37&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=37&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div></p>
|
||||
</details>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除通常是由一套操作配合完成的,以实现有实际意义的操作。</p>
|
||||
</div>
|
||||
<h2 id="713">7.1.3 常见二叉树类型<a class="headerlink" href="#713" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -2210,8 +2210,8 @@
|
|||
<p><a class="glightbox" href="../ram_and_cache.assets/storage_pyramid.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Computer storage system" class="animation-figure" src="../ram_and_cache.assets/storage_pyramid.png" /></a></p>
|
||||
<p align="center"> Figure 4-9 Computer storage system </p>
|
||||
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>The storage hierarchy of computers reflects a delicate balance between speed, capacity, and cost. In fact, this kind of trade-off is common in all industrial fields, requiring us to find the best balance between different advantages and limitations.</p>
|
||||
</div>
|
||||
<p>Overall, <strong>hard disks are used for long-term storage of large amounts of data, memory is used for temporary storage of data being processed during program execution, and cache is used to store frequently accessed data and instructions</strong> to improve program execution efficiency. Together, they ensure the efficient operation of computer systems.</p>
|
||||
|
|
|
@ -2960,7 +2960,7 @@ T(n) = 3 + 2n
|
|||
\]</div>
|
||||
<p>Since <span class="arithmatex">\(T(n)\)</span> is a linear function, its growth trend is linear, and therefore, its time complexity is of linear order, denoted as <span class="arithmatex">\(O(n)\)</span>. This mathematical notation, known as "big-O notation," represents the "asymptotic upper bound" of the function <span class="arithmatex">\(T(n)\)</span>.</p>
|
||||
<p>In essence, time complexity analysis is about finding the asymptotic upper bound of the "number of operations <span class="arithmatex">\(T(n)\)</span>". It has a precise mathematical definition.</p>
|
||||
<div class="admonition abstract">
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Asymptotic Upper Bound</p>
|
||||
<p>If there exist positive real numbers <span class="arithmatex">\(c\)</span> and <span class="arithmatex">\(n_0\)</span> such that for all <span class="arithmatex">\(n > n_0\)</span>, <span class="arithmatex">\(T(n) \leq c \cdot f(n)\)</span>, then <span class="arithmatex">\(f(n)\)</span> is considered an asymptotic upper bound of <span class="arithmatex">\(T(n)\)</span>, denoted as <span class="arithmatex">\(T(n) = O(f(n))\)</span>.</p>
|
||||
</div>
|
||||
|
|
|
@ -2134,8 +2134,8 @@
|
|||
|
||||
<!-- Page content -->
|
||||
<h1 id="33-number-encoding">3.3 Number encoding *<a class="headerlink" href="#33-number-encoding" title="Permanent link">¶</a></h1>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.</p>
|
||||
</div>
|
||||
<h2 id="331-integer-encoding">3.3.1 Integer encoding<a class="headerlink" href="#331-integer-encoding" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -2253,7 +2253,7 @@
|
|||
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a> <span class="c1"># 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a> <span class="n">que</span> <span class="o">=</span> <span class="n">deque</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
|
@ -2277,7 +2277,7 @@
|
|||
<a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="n">startVet</span><span class="p">};</span>
|
||||
<a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="n">queue</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">que</span><span class="p">;</span>
|
||||
|
@ -2306,7 +2306,7 @@
|
|||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2335,7 +2335,7 @@
|
|||
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">startVet</span><span class="p">];</span>
|
||||
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="n">Queue</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="p">();</span>
|
||||
|
@ -2364,7 +2364,7 @@
|
|||
<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">[</span><span class="nx">startVet</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}{}</span>
|
||||
<a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS, 使用切片模拟队列</span>
|
||||
|
@ -2398,7 +2398,7 @@
|
|||
<a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="kd">func</span> <span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a> <span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-5-7" name="__codelineno-5-7" href="#__codelineno-5-7"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
<a id="__codelineno-5-8" name="__codelineno-5-8" href="#__codelineno-5-8"></a> <span class="c1">// 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-5-9" name="__codelineno-5-9" href="#__codelineno-5-9"></a> <span class="kd">var</span> <span class="nv">que</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
|
@ -2426,7 +2426,7 @@
|
|||
<a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-6-7" name="__codelineno-6-7" href="#__codelineno-6-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-6-8" name="__codelineno-6-8" href="#__codelineno-6-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-6-9" name="__codelineno-6-9" href="#__codelineno-6-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2455,7 +2455,7 @@
|
|||
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2484,7 +2484,7 @@
|
|||
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="w"> </span><span class="c1">// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2514,7 +2514,7 @@
|
|||
<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="k">fn</span> <span class="nf">graph_bfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2618,7 +2618,7 @@
|
|||
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex</span><span class="p">):</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-11-9" name="__codelineno-11-9" href="#__codelineno-11-9"></a><span class="w"> </span><span class="c1">// 队列用于实现 BFS</span>
|
||||
|
@ -2647,7 +2647,7 @@
|
|||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="c1"># 队列用于实现 BFS</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span>
|
||||
|
@ -2751,7 +2751,7 @@
|
|||
<a id="__codelineno-14-14" name="__codelineno-14-14" href="#__codelineno-14-14"></a> <span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-14-15" name="__codelineno-14-15" href="#__codelineno-14-15"></a> <span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-14-16" name="__codelineno-14-16" href="#__codelineno-14-16"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-14-18" name="__codelineno-14-18" href="#__codelineno-14-18"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]()</span>
|
||||
<a id="__codelineno-14-19" name="__codelineno-14-19" href="#__codelineno-14-19"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-14-20" name="__codelineno-14-20" href="#__codelineno-14-20"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -2776,7 +2776,7 @@
|
|||
<a id="__codelineno-15-16" name="__codelineno-15-16" href="#__codelineno-15-16"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-15-17" name="__codelineno-15-17" href="#__codelineno-15-17"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-15-18" name="__codelineno-15-18" href="#__codelineno-15-18"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-15-20" name="__codelineno-15-20" href="#__codelineno-15-20"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-21" name="__codelineno-15-21" href="#__codelineno-15-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-15-22" name="__codelineno-15-22" href="#__codelineno-15-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -2802,7 +2802,7 @@
|
|||
<a id="__codelineno-16-16" name="__codelineno-16-16" href="#__codelineno-16-16"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-16-17" name="__codelineno-16-17" href="#__codelineno-16-17"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-16-18" name="__codelineno-16-18" href="#__codelineno-16-18"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-16-20" name="__codelineno-16-20" href="#__codelineno-16-20"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-21" name="__codelineno-16-21" href="#__codelineno-16-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-16-22" name="__codelineno-16-22" href="#__codelineno-16-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -2829,7 +2829,7 @@
|
|||
<a id="__codelineno-17-17" name="__codelineno-17-17" href="#__codelineno-17-17"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-17-18" name="__codelineno-17-18" href="#__codelineno-17-18"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-17-19" name="__codelineno-17-19" href="#__codelineno-17-19"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-17-21" name="__codelineno-17-21" href="#__codelineno-17-21"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-22" name="__codelineno-17-22" href="#__codelineno-17-22"></a><span class="w"> </span><span class="n">DFS</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-17-23" name="__codelineno-17-23" href="#__codelineno-17-23"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -2857,7 +2857,7 @@
|
|||
<a id="__codelineno-18-18" name="__codelineno-18-18" href="#__codelineno-18-18"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-18-19" name="__codelineno-18-19" href="#__codelineno-18-19"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-18-20" name="__codelineno-18-20" href="#__codelineno-18-20"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-18-22" name="__codelineno-18-22" href="#__codelineno-18-22"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-18-23" name="__codelineno-18-23" href="#__codelineno-18-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">g</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-24" name="__codelineno-18-24" href="#__codelineno-18-24"></a><span class="w"> </span><span class="c1">// 返回顶点遍历序列</span>
|
||||
|
@ -2885,7 +2885,7 @@
|
|||
<a id="__codelineno-19-17" name="__codelineno-19-17" href="#__codelineno-19-17"></a><span class="kd">func</span> <span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-19-18" name="__codelineno-19-18" href="#__codelineno-19-18"></a> <span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-19-19" name="__codelineno-19-19" href="#__codelineno-19-19"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-19-21" name="__codelineno-19-21" href="#__codelineno-19-21"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-22" name="__codelineno-19-22" href="#__codelineno-19-22"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">:</span> <span class="p">&</span><span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&</span><span class="n">res</span><span class="p">,</span> <span class="n">vet</span><span class="p">:</span> <span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-19-23" name="__codelineno-19-23" href="#__codelineno-19-23"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -2913,7 +2913,7 @@
|
|||
<a id="__codelineno-20-18" name="__codelineno-20-18" href="#__codelineno-20-18"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-19" name="__codelineno-20-19" href="#__codelineno-20-19"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-20-20" name="__codelineno-20-20" href="#__codelineno-20-20"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-20-22" name="__codelineno-20-22" href="#__codelineno-20-22"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-20-23" name="__codelineno-20-23" href="#__codelineno-20-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-24" name="__codelineno-20-24" href="#__codelineno-20-24"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -2945,7 +2945,7 @@
|
|||
<a id="__codelineno-21-22" name="__codelineno-21-22" href="#__codelineno-21-22"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-21-23" name="__codelineno-21-23" href="#__codelineno-21-23"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-21-24" name="__codelineno-21-24" href="#__codelineno-21-24"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-21-26" name="__codelineno-21-26" href="#__codelineno-21-26"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-21-27" name="__codelineno-21-27" href="#__codelineno-21-27"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-21-28" name="__codelineno-21-28" href="#__codelineno-21-28"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -2976,7 +2976,7 @@
|
|||
<a id="__codelineno-22-21" name="__codelineno-22-21" href="#__codelineno-22-21"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-22" name="__codelineno-22-22" href="#__codelineno-22-22"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-22-23" name="__codelineno-22-23" href="#__codelineno-22-23"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-22-25" name="__codelineno-22-25" href="#__codelineno-22-25"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-22-26" name="__codelineno-22-26" href="#__codelineno-22-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-22-27" name="__codelineno-22-27" href="#__codelineno-22-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -3005,7 +3005,7 @@
|
|||
<a id="__codelineno-23-19" name="__codelineno-23-19" href="#__codelineno-23-19"></a><span class="k">fn</span> <span class="nf">graph_dfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-23-20" name="__codelineno-23-20" href="#__codelineno-23-20"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-23-21" name="__codelineno-23-21" href="#__codelineno-23-21"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-23-23" name="__codelineno-23-23" href="#__codelineno-23-23"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-23-24" name="__codelineno-23-24" href="#__codelineno-23-24"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-23-25" name="__codelineno-23-25" href="#__codelineno-23-25"></a>
|
||||
|
@ -3072,7 +3072,7 @@
|
|||
<a id="__codelineno-25-21" name="__codelineno-25-21" href="#__codelineno-25-21"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex?)</span><span class="p">:</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-25-22" name="__codelineno-25-22" href="#__codelineno-25-22"></a><span class="w"> </span><span class="c1">// 顶点遍历序列</span>
|
||||
<a id="__codelineno-25-23" name="__codelineno-25-23" href="#__codelineno-25-23"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-25-25" name="__codelineno-25-25" href="#__codelineno-25-25"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-26" name="__codelineno-25-26" href="#__codelineno-25-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-25-27" name="__codelineno-25-27" href="#__codelineno-25-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span>
|
||||
|
@ -3097,7 +3097,7 @@
|
|||
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="c1"># 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点</span>
|
||||
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="c1"># 顶点遍历序列</span>
|
||||
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="c1"># 哈希表,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="c1"># 哈希集合,用于记录已被访问过的顶点</span>
|
||||
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span>
|
||||
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-21" name="__codelineno-26-21" href="#__codelineno-26-21"></a><span class="w"> </span><span class="n">res</span>
|
||||
|
|
|
@ -2870,11 +2870,20 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">left</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">right</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">parent</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 获取左子节点的索引 ###</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">left</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="c1">### 获取右子节点的索引 ###</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="k">def</span><span class="w"> </span><span class="nf">right</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="c1">### 获取父节点的索引 ###</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="k">def</span><span class="w"> </span><span class="nf">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="c1"># 向下整除</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -2985,7 +2994,10 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">peek</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="c1">### 访问堆顶元素 ###</span>
|
||||
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">peek</span>
|
||||
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span>
|
||||
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -3355,9 +3367,27 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">push</span><span class="p">}</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_up</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="c1">### 元素入堆 ###</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">push</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="w"> </span><span class="c1"># 添加节点</span>
|
||||
<a id="__codelineno-54-4" name="__codelineno-54-4" href="#__codelineno-54-4"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-54-5" name="__codelineno-54-5" href="#__codelineno-54-5"></a><span class="w"> </span><span class="c1"># 从底至顶堆化</span>
|
||||
<a id="__codelineno-54-6" name="__codelineno-54-6" href="#__codelineno-54-6"></a><span class="w"> </span><span class="n">sift_up</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-7" name="__codelineno-54-7" href="#__codelineno-54-7"></a><span class="k">end</span>
|
||||
<a id="__codelineno-54-8" name="__codelineno-54-8" href="#__codelineno-54-8"></a>
|
||||
<a id="__codelineno-54-9" name="__codelineno-54-9" href="#__codelineno-54-9"></a><span class="c1">### 从节点 i 开始,从底至顶堆化 ###</span>
|
||||
<a id="__codelineno-54-10" name="__codelineno-54-10" href="#__codelineno-54-10"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_up</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-11" name="__codelineno-54-11" href="#__codelineno-54-11"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-54-12" name="__codelineno-54-12" href="#__codelineno-54-12"></a><span class="w"> </span><span class="c1"># 获取节点 i 的父节点</span>
|
||||
<a id="__codelineno-54-13" name="__codelineno-54-13" href="#__codelineno-54-13"></a><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-14" name="__codelineno-54-14" href="#__codelineno-54-14"></a><span class="w"> </span><span class="c1"># 当“越过根节点”或“节点无须修复”时,结束堆化</span>
|
||||
<a id="__codelineno-54-15" name="__codelineno-54-15" href="#__codelineno-54-15"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="nb">p</span><span class="o">]</span>
|
||||
<a id="__codelineno-54-16" name="__codelineno-54-16" href="#__codelineno-54-16"></a><span class="w"> </span><span class="c1"># 交换两节点</span>
|
||||
<a id="__codelineno-54-17" name="__codelineno-54-17" href="#__codelineno-54-17"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="nb">p</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-18" name="__codelineno-54-18" href="#__codelineno-54-18"></a><span class="w"> </span><span class="c1"># 循环向上堆化</span>
|
||||
<a id="__codelineno-54-19" name="__codelineno-54-19" href="#__codelineno-54-19"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">p</span>
|
||||
<a id="__codelineno-54-20" name="__codelineno-54-20" href="#__codelineno-54-20"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-54-21" name="__codelineno-54-21" href="#__codelineno-54-21"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -3776,7 +3806,7 @@
|
|||
<a id="__codelineno-65-7" name="__codelineno-65-7" href="#__codelineno-65-7"></a><span class="w"> </span><span class="c1">// 交换根节点与最右叶节点(交换首元素与尾元素)</span>
|
||||
<a id="__codelineno-65-8" name="__codelineno-65-8" href="#__codelineno-65-8"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-9" name="__codelineno-65-9" href="#__codelineno-65-9"></a><span class="w"> </span><span class="c1">// 删除节点</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">pop</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
|
||||
<a id="__codelineno-65-11" name="__codelineno-65-11" href="#__codelineno-65-11"></a><span class="w"> </span><span class="c1">// 从顶至底堆化</span>
|
||||
<a id="__codelineno-65-12" name="__codelineno-65-12" href="#__codelineno-65-12"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-13" name="__codelineno-65-13" href="#__codelineno-65-13"></a><span class="w"> </span><span class="c1">// 返回堆顶元素</span>
|
||||
|
@ -3888,9 +3918,37 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">pop</span><span class="p">}</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_down</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="c1">### 元素出堆 ###</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">pop</span>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="w"> </span><span class="c1"># 判空处理</span>
|
||||
<a id="__codelineno-68-4" name="__codelineno-68-4" href="#__codelineno-68-4"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span><span class="p">,</span><span class="w"> </span><span class="s2">"堆为空"</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">is_empty?</span>
|
||||
<a id="__codelineno-68-5" name="__codelineno-68-5" href="#__codelineno-68-5"></a><span class="w"> </span><span class="c1"># 交换根节点与最右叶节点(交换首元素与尾元素)</span>
|
||||
<a id="__codelineno-68-6" name="__codelineno-68-6" href="#__codelineno-68-6"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-7" name="__codelineno-68-7" href="#__codelineno-68-7"></a><span class="w"> </span><span class="c1"># 删除节点</span>
|
||||
<a id="__codelineno-68-8" name="__codelineno-68-8" href="#__codelineno-68-8"></a><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">.</span><span class="n">pop</span>
|
||||
<a id="__codelineno-68-9" name="__codelineno-68-9" href="#__codelineno-68-9"></a><span class="w"> </span><span class="c1"># 从顶至底堆化</span>
|
||||
<a id="__codelineno-68-10" name="__codelineno-68-10" href="#__codelineno-68-10"></a><span class="w"> </span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-11" name="__codelineno-68-11" href="#__codelineno-68-11"></a><span class="w"> </span><span class="c1"># 返回堆顶元素</span>
|
||||
<a id="__codelineno-68-12" name="__codelineno-68-12" href="#__codelineno-68-12"></a><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-68-13" name="__codelineno-68-13" href="#__codelineno-68-13"></a><span class="k">end</span>
|
||||
<a id="__codelineno-68-14" name="__codelineno-68-14" href="#__codelineno-68-14"></a>
|
||||
<a id="__codelineno-68-15" name="__codelineno-68-15" href="#__codelineno-68-15"></a><span class="c1">### 从节点 i 开始,从顶至底堆化 ###</span>
|
||||
<a id="__codelineno-68-16" name="__codelineno-68-16" href="#__codelineno-68-16"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_down</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-17" name="__codelineno-68-17" href="#__codelineno-68-17"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-68-18" name="__codelineno-68-18" href="#__codelineno-68-18"></a><span class="w"> </span><span class="c1"># 判断节点 i, l, r 中值最大的节点,记为 ma</span>
|
||||
<a id="__codelineno-68-19" name="__codelineno-68-19" href="#__codelineno-68-19"></a><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">right</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-20" name="__codelineno-68-20" href="#__codelineno-68-20"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">l</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-21" name="__codelineno-68-21" href="#__codelineno-68-21"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">r</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-22" name="__codelineno-68-22" href="#__codelineno-68-22"></a>
|
||||
<a id="__codelineno-68-23" name="__codelineno-68-23" href="#__codelineno-68-23"></a><span class="w"> </span><span class="c1"># 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出</span>
|
||||
<a id="__codelineno-68-24" name="__codelineno-68-24" href="#__codelineno-68-24"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-25" name="__codelineno-68-25" href="#__codelineno-68-25"></a>
|
||||
<a id="__codelineno-68-26" name="__codelineno-68-26" href="#__codelineno-68-26"></a><span class="w"> </span><span class="c1"># 交换两节点</span>
|
||||
<a id="__codelineno-68-27" name="__codelineno-68-27" href="#__codelineno-68-27"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-28" name="__codelineno-68-28" href="#__codelineno-68-28"></a><span class="w"> </span><span class="c1"># 循环向下堆化</span>
|
||||
<a id="__codelineno-68-29" name="__codelineno-68-29" href="#__codelineno-68-29"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span>
|
||||
<a id="__codelineno-68-30" name="__codelineno-68-30" href="#__codelineno-68-30"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-68-31" name="__codelineno-68-31" href="#__codelineno-68-31"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -2554,7 +2554,28 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">top_k_heap</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 基于堆查找数组中最大的 k 个元素 ###</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">top_k_heap</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 初始化小顶堆</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 请注意:我们将堆中所有元素取反,从而用大顶堆来模拟小顶堆</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">max_heap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">MaxHeap</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="c1"># 将数组的前 k 个元素入堆</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="n">k</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a>
|
||||
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="c1"># 从第 k+1 个元素开始,保持堆的长度为 k</span>
|
||||
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">k</span><span class="o">...</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="c1"># 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆</span>
|
||||
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">peek_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="n">pop_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a>
|
||||
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="w"> </span><span class="n">get_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -2967,8 +2967,8 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>Let the balance factor be <span class="arithmatex">\(f\)</span>, then the balance factor of any node in an AVL tree satisfies <span class="arithmatex">\(-1 \le f \le 1\)</span>.</p>
|
||||
</div>
|
||||
<h2 id="752-rotations-in-avl-trees">7.5.2 Rotations in AVL trees<a class="headerlink" href="#752-rotations-in-avl-trees" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -2831,8 +2831,8 @@
|
|||
<summary>Code visualization</summary>
|
||||
<p>https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false</p>
|
||||
</details>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes usually means removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful actions.</p>
|
||||
</div>
|
||||
<h2 id="713-common-types-of-binary-trees">7.1.3 Common types of binary trees<a class="headerlink" href="#713-common-types-of-binary-trees" title="Permanent link">¶</a></h2>
|
||||
|
|
File diff suppressed because one or more lines are too long
110
en/sitemap.xml
110
en/sitemap.xml
|
@ -2,277 +2,277 @@
|
|||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/ram_and_cache/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/iteration_and_recursion/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/basic_data_types/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/character_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/number_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_graph/graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_graph/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hashing/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hashing/hash_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hashing/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_heap/heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_heap/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_heap/top_k/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_hello_algo/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_introduction/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_introduction/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_preface/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_preface/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_stack_and_queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/en/chapter_tree/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
Binary file not shown.
File diff suppressed because one or more lines are too long
212
sitemap.xml
212
sitemap.xml
|
@ -2,532 +2,532 @@
|
|||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/terminology/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/ram_and_cache/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/n_queens_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/permutations_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/iteration_and_recursion/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/basic_data_types/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/character_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/number_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/greedy_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/max_capacity_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_greedy/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/top_k/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hello_algo/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_paperbook/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_edge/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_insertion/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/heap_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/radix_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/selection_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/sorting_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
|
@ -5124,7 +5124,7 @@
|
|||
<h2 id="424">4.2.4 鏈結串列典型應用<a class="headerlink" href="#424" title="Permanent link">¶</a></h2>
|
||||
<p>單向鏈結串列通常用於實現堆疊、佇列、雜湊表和圖等資料結構。</p>
|
||||
<ul>
|
||||
<li><strong>堆疊與佇列</strong>:當插入和刪除操作都在鏈結串列的一端進行時,它表現出先進後出的特性,對應堆疊;當插入操作在鏈結串列的一端進行,刪除操作在鏈結串列的另一端進行,它表現出先進先出的特性,對應佇列。</li>
|
||||
<li><strong>堆疊與佇列</strong>:當插入和刪除操作都在鏈結串列的一端進行時,它表現的特性為先進後出,對應堆疊;當插入操作在鏈結串列的一端進行,刪除操作在鏈結串列的另一端進行,它表現的特性為先進先出,對應佇列。</li>
|
||||
<li><strong>雜湊表</strong>:鏈式位址是解決雜湊衝突的主流方案之一,在該方案中,所有衝突的元素都會被放到一個鏈結串列中。</li>
|
||||
<li><strong>圖</strong>:鄰接表是表示圖的一種常用方式,其中圖的每個頂點都與一個鏈結串列相關聯,鏈結串列中的每個元素都代表與該頂點相連的其他頂點。</li>
|
||||
</ul>
|
||||
|
|
|
@ -3683,8 +3683,8 @@
|
|||
<p><a class="glightbox" href="../ram_and_cache.assets/storage_pyramid.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="計算機儲存系統" class="animation-figure" src="../ram_and_cache.assets/storage_pyramid.png" /></a></p>
|
||||
<p align="center"> 圖 4-9 計算機儲存系統 </p>
|
||||
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>計算機的儲存層次結構體現了速度、容量和成本三者之間的精妙平衡。實際上,這種權衡普遍存在於所有工業領域,它要求我們在不同的優勢和限制之間找到最佳平衡點。</p>
|
||||
</div>
|
||||
<p>總的來說,<strong>硬碟用於長期儲存大量資料,記憶體用於臨時儲存程式執行中正在處理的資料,而快取則用於儲存經常訪問的資料和指令</strong>,以提高程式執行效率。三者共同協作,確保計算機系統高效執行。</p>
|
||||
|
|
|
@ -4220,7 +4220,7 @@
|
|||
<p><a class="glightbox" href="../permutations_problem.assets/permutations_ii.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="重複排列" class="animation-figure" src="../permutations_problem.assets/permutations_ii.png" /></a></p>
|
||||
<p align="center"> 圖 13-7 重複排列 </p>
|
||||
|
||||
<p>那麼如何去除重複的排列呢?最直接地,考慮藉助一個雜湊表,直接對排列結果進行去重。然而這樣做不夠優雅,<strong>因為生成重複排列的搜尋分支沒有必要,應當提前識別並剪枝</strong>,這樣可以進一步提升演算法效率。</p>
|
||||
<p>那麼如何去除重複的排列呢?最直接地,考慮藉助一個雜湊集合,直接對排列結果進行去重。然而這樣做不夠優雅,<strong>因為生成重複排列的搜尋分支沒有必要,應當提前識別並剪枝</strong>,這樣可以進一步提升演算法效率。</p>
|
||||
<h3 id="1_1">1. 相等元素剪枝<a class="headerlink" href="#1_1" title="Permanent link">¶</a></h3>
|
||||
<p>觀察圖 13-8 ,在第一輪中,選擇 <span class="arithmatex">\(1\)</span> 或選擇 <span class="arithmatex">\(\hat{1}\)</span> 是等價的,在這兩個選擇之下生成的所有排列都是重複的。因此應該把 <span class="arithmatex">\(\hat{1}\)</span> 剪枝。</p>
|
||||
<p>同理,在第一輪選擇 <span class="arithmatex">\(2\)</span> 之後,第二輪選擇中的 <span class="arithmatex">\(1\)</span> 和 <span class="arithmatex">\(\hat{1}\)</span> 也會產生重複分支,因此也應將第二輪的 <span class="arithmatex">\(\hat{1}\)</span> 剪枝。</p>
|
||||
|
@ -4229,7 +4229,7 @@
|
|||
<p align="center"> 圖 13-8 重複排列剪枝 </p>
|
||||
|
||||
<h3 id="2_1">2. 程式碼實現<a class="headerlink" href="#2_1" title="Permanent link">¶</a></h3>
|
||||
<p>在上一題的程式碼的基礎上,我們考慮在每一輪選擇中開啟一個雜湊表 <code>duplicated</code> ,用於記錄該輪中已經嘗試過的元素,並將重複元素剪枝:</p>
|
||||
<p>在上一題的程式碼的基礎上,我們考慮在每一輪選擇中開啟一個雜湊集合 <code>duplicated</code> ,用於記錄該輪中已經嘗試過的元素,並將重複元素剪枝:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:14"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><input id="__tabbed_2_14" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label><label for="__tabbed_2_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3606,7 +3606,7 @@
|
|||
<li>回溯問題通常包含多個約束條件,它們可用於實現剪枝操作。剪枝可以提前結束不必要的搜尋分支,大幅提升搜尋效率。</li>
|
||||
<li>回溯演算法主要可用於解決搜尋問題和約束滿足問題。組合最佳化問題雖然可以用回溯演算法解決,但往往存在效率更高或效果更好的解法。</li>
|
||||
<li>全排列問題旨在搜尋給定集合元素的所有可能的排列。我們藉助一個陣列來記錄每個元素是否被選擇,剪掉重複選擇同一元素的搜尋分支,確保每個元素只被選擇一次。</li>
|
||||
<li>在全排列問題中,如果集合中存在重複元素,則最終結果會出現重複排列。我們需要約束相等元素在每輪中只能被選擇一次,這通常藉助一個雜湊表來實現。</li>
|
||||
<li>在全排列問題中,如果集合中存在重複元素,則最終結果會出現重複排列。我們需要約束相等元素在每輪中只能被選擇一次,這通常藉助一個雜湊集合來實現。</li>
|
||||
<li>子集和問題的目標是在給定集合中找到和為目標值的所有子集。集合不區分元素順序,而搜尋過程會輸出所有順序的結果,產生重複子集。我們在回溯前將資料進行排序,並設定一個變數來指示每一輪的走訪起始點,從而將生成重複子集的搜尋分支進行剪枝。</li>
|
||||
<li>對於子集和問題,陣列中的相等元素會產生重複集合。我們利用陣列已排序的前置條件,透過判斷相鄰元素是否相等實現剪枝,從而確保相等元素在每輪中只能被選中一次。</li>
|
||||
<li><span class="arithmatex">\(n\)</span> 皇后問題旨在尋找將 <span class="arithmatex">\(n\)</span> 個皇后放置到 <span class="arithmatex">\(n \times n\)</span> 尺寸棋盤上的方案,要求所有皇后兩兩之間無法攻擊對方。該問題的約束條件有行約束、列約束、主對角線和次對角線約束。為滿足行約束,我們採用按行放置的策略,保證每一行放置一個皇后。</li>
|
||||
|
|
|
@ -4500,7 +4500,7 @@ T(n) = 3 + 2n
|
|||
<p><span class="arithmatex">\(T(n)\)</span> 是一次函式,說明其執行時間的增長趨勢是線性的,因此它的時間複雜度是線性階。</p>
|
||||
<p>我們將線性階的時間複雜度記為 <span class="arithmatex">\(O(n)\)</span> ,這個數學符號稱為<u>大 <span class="arithmatex">\(O\)</span> 記號(big-<span class="arithmatex">\(O\)</span> notation)</u>,表示函式 <span class="arithmatex">\(T(n)\)</span> 的<u>漸近上界(asymptotic upper bound)</u>。</p>
|
||||
<p>時間複雜度分析本質上是計算“操作數量 <span class="arithmatex">\(T(n)\)</span>”的漸近上界,它具有明確的數學定義。</p>
|
||||
<div class="admonition abstract">
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">函式漸近上界</p>
|
||||
<p>若存在正實數 <span class="arithmatex">\(c\)</span> 和實數 <span class="arithmatex">\(n_0\)</span> ,使得對於所有的 <span class="arithmatex">\(n > n_0\)</span> ,均有 <span class="arithmatex">\(T(n) \leq c \cdot f(n)\)</span> ,則可認為 <span class="arithmatex">\(f(n)\)</span> 給出了 <span class="arithmatex">\(T(n)\)</span> 的一個漸近上界,記為 <span class="arithmatex">\(T(n) = O(f(n))\)</span> 。</p>
|
||||
</div>
|
||||
|
|
|
@ -3599,8 +3599,8 @@
|
|||
|
||||
<!-- Page content -->
|
||||
<h1 id="33">3.3 數字編碼 *<a class="headerlink" href="#33" title="Permanent link">¶</a></h1>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>在本書中,標題帶有 * 符號的是選讀章節。如果你時間有限或感到理解困難,可以先跳過,等學完必讀章節後再單獨攻克。</p>
|
||||
</div>
|
||||
<h2 id="331">3.3.1 原碼、一補數和二補數<a class="headerlink" href="#331" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -4720,7 +4720,80 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">graph_adjacency_matrix.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">GraphAdjMat</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{}</span>
|
||||
<div class="highlight"><span class="filename">graph_adjacency_matrix.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 基於鄰接矩陣實現的無向圖類別 ###</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">class</span><span class="w"> </span><span class="nc">GraphAdjMat</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">initialize</span><span class="p">(</span><span class="n">vertices</span><span class="p">,</span><span class="w"> </span><span class="n">edges</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1">### 建構子 ###</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="c1"># 頂點串列,元素代表“頂點值”,索引代表“頂點索引”</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="vi">@vertices</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="c1"># 鄰接矩陣,行列索引對應“頂點索引”</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="c1"># 新增頂點</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="n">vertices</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">val</span><span class="o">|</span><span class="w"> </span><span class="n">add_vertex</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w"> </span><span class="c1"># 新增邊</span>
|
||||
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="c1"># 請注意,edges 元素代表頂點索引,即對應 vertices 元素索引</span>
|
||||
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="n">edges</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">add_edge</span><span class="p">(</span><span class="n">e</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a>
|
||||
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="c1">### 獲取頂點數量 ###</span>
|
||||
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">size</span>
|
||||
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="vi">@vertices</span><span class="o">.</span><span class="n">length</span>
|
||||
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a>
|
||||
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="w"> </span><span class="c1">### 新增頂點 ###</span>
|
||||
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">add_vertex</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-23" name="__codelineno-12-23" href="#__codelineno-12-23"></a><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">size</span>
|
||||
<a id="__codelineno-12-24" name="__codelineno-12-24" href="#__codelineno-12-24"></a><span class="w"> </span><span class="c1"># 向頂點串列中新增新頂點的值</span>
|
||||
<a id="__codelineno-12-25" name="__codelineno-12-25" href="#__codelineno-12-25"></a><span class="w"> </span><span class="vi">@vertices</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-12-26" name="__codelineno-12-26" href="#__codelineno-12-26"></a><span class="w"> </span><span class="c1"># 在鄰接矩陣中新增一行</span>
|
||||
<a id="__codelineno-12-27" name="__codelineno-12-27" href="#__codelineno-12-27"></a><span class="w"> </span><span class="n">new_row</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-28" name="__codelineno-12-28" href="#__codelineno-12-28"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">new_row</span>
|
||||
<a id="__codelineno-12-29" name="__codelineno-12-29" href="#__codelineno-12-29"></a><span class="w"> </span><span class="c1"># 在鄰接矩陣中新增一列</span>
|
||||
<a id="__codelineno-12-30" name="__codelineno-12-30" href="#__codelineno-12-30"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">row</span><span class="o">|</span><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-12-31" name="__codelineno-12-31" href="#__codelineno-12-31"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-32" name="__codelineno-12-32" href="#__codelineno-12-32"></a>
|
||||
<a id="__codelineno-12-33" name="__codelineno-12-33" href="#__codelineno-12-33"></a><span class="w"> </span><span class="c1">### 刪除頂點 ###</span>
|
||||
<a id="__codelineno-12-34" name="__codelineno-12-34" href="#__codelineno-12-34"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">remove_vertex</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-35" name="__codelineno-12-35" href="#__codelineno-12-35"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">size</span>
|
||||
<a id="__codelineno-12-36" name="__codelineno-12-36" href="#__codelineno-12-36"></a>
|
||||
<a id="__codelineno-12-37" name="__codelineno-12-37" href="#__codelineno-12-37"></a><span class="w"> </span><span class="c1"># 在頂點串列中移除索引 index 的頂點</span>
|
||||
<a id="__codelineno-12-38" name="__codelineno-12-38" href="#__codelineno-12-38"></a><span class="w"> </span><span class="vi">@vertices</span><span class="o">.</span><span class="n">delete_at</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-39" name="__codelineno-12-39" href="#__codelineno-12-39"></a><span class="w"> </span><span class="c1"># 在鄰接矩陣中刪除索引 index 的行</span>
|
||||
<a id="__codelineno-12-40" name="__codelineno-12-40" href="#__codelineno-12-40"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">.</span><span class="n">delete_at</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-41" name="__codelineno-12-41" href="#__codelineno-12-41"></a><span class="w"> </span><span class="c1"># 在鄰接矩陣中刪除索引 index 的列</span>
|
||||
<a id="__codelineno-12-42" name="__codelineno-12-42" href="#__codelineno-12-42"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">row</span><span class="o">|</span><span class="w"> </span><span class="n">row</span><span class="o">.</span><span class="n">delete_at</span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-12-43" name="__codelineno-12-43" href="#__codelineno-12-43"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-44" name="__codelineno-12-44" href="#__codelineno-12-44"></a>
|
||||
<a id="__codelineno-12-45" name="__codelineno-12-45" href="#__codelineno-12-45"></a><span class="w"> </span><span class="c1">### 新增邊 ###</span>
|
||||
<a id="__codelineno-12-46" name="__codelineno-12-46" href="#__codelineno-12-46"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">add_edge</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-47" name="__codelineno-12-47" href="#__codelineno-12-47"></a><span class="w"> </span><span class="c1"># 參數 i, j 對應 vertices 元素索引</span>
|
||||
<a id="__codelineno-12-48" name="__codelineno-12-48" href="#__codelineno-12-48"></a><span class="w"> </span><span class="c1"># 索引越界與相等處理</span>
|
||||
<a id="__codelineno-12-49" name="__codelineno-12-49" href="#__codelineno-12-49"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">j</span>
|
||||
<a id="__codelineno-12-50" name="__codelineno-12-50" href="#__codelineno-12-50"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span>
|
||||
<a id="__codelineno-12-51" name="__codelineno-12-51" href="#__codelineno-12-51"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-52" name="__codelineno-12-52" href="#__codelineno-12-52"></a><span class="w"> </span><span class="c1"># 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i)</span>
|
||||
<a id="__codelineno-12-53" name="__codelineno-12-53" href="#__codelineno-12-53"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span>
|
||||
<a id="__codelineno-12-54" name="__codelineno-12-54" href="#__codelineno-12-54"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">[</span><span class="n">j</span><span class="o">][</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span>
|
||||
<a id="__codelineno-12-55" name="__codelineno-12-55" href="#__codelineno-12-55"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-56" name="__codelineno-12-56" href="#__codelineno-12-56"></a>
|
||||
<a id="__codelineno-12-57" name="__codelineno-12-57" href="#__codelineno-12-57"></a><span class="w"> </span><span class="c1">### 刪除邊 ###</span>
|
||||
<a id="__codelineno-12-58" name="__codelineno-12-58" href="#__codelineno-12-58"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">remove_edge</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-59" name="__codelineno-12-59" href="#__codelineno-12-59"></a><span class="w"> </span><span class="c1"># 參數 i, j 對應 vertices 元素索引</span>
|
||||
<a id="__codelineno-12-60" name="__codelineno-12-60" href="#__codelineno-12-60"></a><span class="w"> </span><span class="c1"># 索引越界與相等處理</span>
|
||||
<a id="__codelineno-12-61" name="__codelineno-12-61" href="#__codelineno-12-61"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">j</span>
|
||||
<a id="__codelineno-12-62" name="__codelineno-12-62" href="#__codelineno-12-62"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span>
|
||||
<a id="__codelineno-12-63" name="__codelineno-12-63" href="#__codelineno-12-63"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-64" name="__codelineno-12-64" href="#__codelineno-12-64"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">[</span><span class="n">i</span><span class="o">][</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span>
|
||||
<a id="__codelineno-12-65" name="__codelineno-12-65" href="#__codelineno-12-65"></a><span class="w"> </span><span class="vi">@adj_mat</span><span class="o">[</span><span class="n">j</span><span class="o">][</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span>
|
||||
<a id="__codelineno-12-66" name="__codelineno-12-66" href="#__codelineno-12-66"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-67" name="__codelineno-12-67" href="#__codelineno-12-67"></a>
|
||||
<a id="__codelineno-12-68" name="__codelineno-12-68" href="#__codelineno-12-68"></a><span class="w"> </span><span class="c1">### 列印鄰接矩陣 ###</span>
|
||||
<a id="__codelineno-12-69" name="__codelineno-12-69" href="#__codelineno-12-69"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">__print__</span>
|
||||
<a id="__codelineno-12-70" name="__codelineno-12-70" href="#__codelineno-12-70"></a><span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s2">"頂點串列 = </span><span class="si">#{</span><span class="vi">@vertices</span><span class="si">}</span><span class="s2">"</span>
|
||||
<a id="__codelineno-12-71" name="__codelineno-12-71" href="#__codelineno-12-71"></a><span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s1">'鄰接矩陣 ='</span>
|
||||
<a id="__codelineno-12-72" name="__codelineno-12-72" href="#__codelineno-12-72"></a><span class="w"> </span><span class="n">print_matrix</span><span class="p">(</span><span class="vi">@adj_mat</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-73" name="__codelineno-12-73" href="#__codelineno-12-73"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-74" name="__codelineno-12-74" href="#__codelineno-12-74"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -5801,7 +5874,73 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">graph_adjacency_list.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">GraphAdjList</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{}</span>
|
||||
<div class="highlight"><span class="filename">graph_adjacency_list.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 基於鄰接表實現的無向圖類別 ###</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">class</span><span class="w"> </span><span class="nc">GraphAdjList</span>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="kp">attr_reader</span><span class="w"> </span><span class="ss">:adj_list</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="w"> </span><span class="c1">### 建構子 ###</span>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">initialize</span><span class="p">(</span><span class="n">edges</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="c1"># 鄰接表,key:頂點,value:該頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{}</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="c1"># 新增所有頂點和邊</span>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">edge</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">edges</span>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="n">add_vertex</span><span class="p">(</span><span class="n">edge</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="n">add_vertex</span><span class="p">(</span><span class="n">edge</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="n">add_edge</span><span class="p">(</span><span class="n">edge</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">edge</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a>
|
||||
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="c1">### 獲取頂點數量 ###</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">size</span>
|
||||
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">length</span>
|
||||
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-21" name="__codelineno-26-21" href="#__codelineno-26-21"></a>
|
||||
<a id="__codelineno-26-22" name="__codelineno-26-22" href="#__codelineno-26-22"></a><span class="w"> </span><span class="c1">### 新增邊 ###</span>
|
||||
<a id="__codelineno-26-23" name="__codelineno-26-23" href="#__codelineno-26-23"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">add_edge</span><span class="p">(</span><span class="n">vet1</span><span class="p">,</span><span class="w"> </span><span class="n">vet2</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-24" name="__codelineno-26-24" href="#__codelineno-26-24"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">ArgumentError</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-25" name="__codelineno-26-25" href="#__codelineno-26-25"></a>
|
||||
<a id="__codelineno-26-26" name="__codelineno-26-26" href="#__codelineno-26-26"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vet1</span><span class="o">]</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">vet2</span>
|
||||
<a id="__codelineno-26-27" name="__codelineno-26-27" href="#__codelineno-26-27"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vet2</span><span class="o">]</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">vet1</span>
|
||||
<a id="__codelineno-26-28" name="__codelineno-26-28" href="#__codelineno-26-28"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-29" name="__codelineno-26-29" href="#__codelineno-26-29"></a>
|
||||
<a id="__codelineno-26-30" name="__codelineno-26-30" href="#__codelineno-26-30"></a><span class="w"> </span><span class="c1">### 刪除邊 ###</span>
|
||||
<a id="__codelineno-26-31" name="__codelineno-26-31" href="#__codelineno-26-31"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">remove_edge</span><span class="p">(</span><span class="n">vet1</span><span class="p">,</span><span class="w"> </span><span class="n">vet2</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-32" name="__codelineno-26-32" href="#__codelineno-26-32"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">ArgumentError</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-33" name="__codelineno-26-33" href="#__codelineno-26-33"></a>
|
||||
<a id="__codelineno-26-34" name="__codelineno-26-34" href="#__codelineno-26-34"></a><span class="w"> </span><span class="c1"># 刪除邊 vet1 - vet2</span>
|
||||
<a id="__codelineno-26-35" name="__codelineno-26-35" href="#__codelineno-26-35"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vet1</span><span class="o">].</span><span class="n">delete</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-36" name="__codelineno-26-36" href="#__codelineno-26-36"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vet2</span><span class="o">].</span><span class="n">delete</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-37" name="__codelineno-26-37" href="#__codelineno-26-37"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-38" name="__codelineno-26-38" href="#__codelineno-26-38"></a>
|
||||
<a id="__codelineno-26-39" name="__codelineno-26-39" href="#__codelineno-26-39"></a><span class="w"> </span><span class="c1">### 新增頂點 ###</span>
|
||||
<a id="__codelineno-26-40" name="__codelineno-26-40" href="#__codelineno-26-40"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">add_vertex</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-41" name="__codelineno-26-41" href="#__codelineno-26-41"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-42" name="__codelineno-26-42" href="#__codelineno-26-42"></a>
|
||||
<a id="__codelineno-26-43" name="__codelineno-26-43" href="#__codelineno-26-43"></a><span class="w"> </span><span class="c1"># 在鄰接表中新增一個新鏈結串列</span>
|
||||
<a id="__codelineno-26-44" name="__codelineno-26-44" href="#__codelineno-26-44"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vet</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-26-45" name="__codelineno-26-45" href="#__codelineno-26-45"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-46" name="__codelineno-26-46" href="#__codelineno-26-46"></a>
|
||||
<a id="__codelineno-26-47" name="__codelineno-26-47" href="#__codelineno-26-47"></a><span class="w"> </span><span class="c1">### 刪除頂點 ###</span>
|
||||
<a id="__codelineno-26-48" name="__codelineno-26-48" href="#__codelineno-26-48"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">remove_vertex</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-49" name="__codelineno-26-49" href="#__codelineno-26-49"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">ArgumentError</span><span class="w"> </span><span class="k">unless</span><span class="w"> </span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-50" name="__codelineno-26-50" href="#__codelineno-26-50"></a>
|
||||
<a id="__codelineno-26-51" name="__codelineno-26-51" href="#__codelineno-26-51"></a><span class="w"> </span><span class="c1"># 在鄰接表中刪除頂點 vet 對應的鏈結串列</span>
|
||||
<a id="__codelineno-26-52" name="__codelineno-26-52" href="#__codelineno-26-52"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-53" name="__codelineno-26-53" href="#__codelineno-26-53"></a><span class="w"> </span><span class="c1"># 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊</span>
|
||||
<a id="__codelineno-26-54" name="__codelineno-26-54" href="#__codelineno-26-54"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">vertex</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="vi">@adj_list</span>
|
||||
<a id="__codelineno-26-55" name="__codelineno-26-55" href="#__codelineno-26-55"></a><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vertex</span><span class="o">.</span><span class="n">first</span><span class="o">].</span><span class="n">delete</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vertex</span><span class="o">.</span><span class="n">first</span><span class="o">].</span><span class="n">include?</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-56" name="__codelineno-26-56" href="#__codelineno-26-56"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-57" name="__codelineno-26-57" href="#__codelineno-26-57"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-58" name="__codelineno-26-58" href="#__codelineno-26-58"></a>
|
||||
<a id="__codelineno-26-59" name="__codelineno-26-59" href="#__codelineno-26-59"></a><span class="w"> </span><span class="c1">### 列印鄰接表 ###</span>
|
||||
<a id="__codelineno-26-60" name="__codelineno-26-60" href="#__codelineno-26-60"></a><span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">__print__</span>
|
||||
<a id="__codelineno-26-61" name="__codelineno-26-61" href="#__codelineno-26-61"></a><span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s1">'鄰接表 ='</span>
|
||||
<a id="__codelineno-26-62" name="__codelineno-26-62" href="#__codelineno-26-62"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">vertex</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="vi">@adj_list</span>
|
||||
<a id="__codelineno-26-63" name="__codelineno-26-63" href="#__codelineno-26-63"></a><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vi">@adj_list</span><span class="o">[</span><span class="n">vertex</span><span class="o">.</span><span class="n">first</span><span class="o">].</span><span class="n">map</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">v</span><span class="o">|</span><span class="w"> </span><span class="n">v</span><span class="o">.</span><span class="n">val</span><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-26-64" name="__codelineno-26-64" href="#__codelineno-26-64"></a><span class="w"> </span><span class="nb">puts</span><span class="w"> </span><span class="s2">"</span><span class="si">#{</span><span class="n">vertex</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">val</span><span class="si">}</span><span class="s2">: </span><span class="si">#{</span><span class="n">tmp</span><span class="si">}</span><span class="s2">,"</span>
|
||||
<a id="__codelineno-26-65" name="__codelineno-26-65" href="#__codelineno-26-65"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-66" name="__codelineno-26-66" href="#__codelineno-26-66"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-67" name="__codelineno-26-67" href="#__codelineno-26-67"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -3709,7 +3709,11 @@
|
|||
<li>在迴圈的每輪迭代中,彈出佇列首頂點並記錄訪問,然後將該頂點的所有鄰接頂點加入到佇列尾部。</li>
|
||||
<li>迴圈步驟 <code>2.</code> ,直到所有頂點被訪問完畢後結束。</li>
|
||||
</ol>
|
||||
<p>為了防止重複走訪頂點,我們需要藉助一個雜湊表 <code>visited</code> 來記錄哪些節點已被訪問。</p>
|
||||
<p>為了防止重複走訪頂點,我們需要藉助一個雜湊集合 <code>visited</code> 來記錄哪些節點已被訪問。</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>雜湊集合可以看作一個只儲存 <code>key</code> 而不儲存 <code>value</code> 的雜湊表,它可以在 <span class="arithmatex">\(O(1)\)</span> 時間複雜度下進行 <code>key</code> 的增刪查改操作。根據 <code>key</code> 的唯一性,雜湊集合通常用於資料去重等場景。</p>
|
||||
</div>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:14"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><input id="__tabbed_1_14" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label><label for="__tabbed_1_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
@ -3718,7 +3722,7 @@
|
|||
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="c1"># 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="c1"># 頂點走訪序列</span>
|
||||
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a> <span class="c1"># 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a> <span class="c1"># 佇列用於實現 BFS</span>
|
||||
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a> <span class="n">que</span> <span class="o">=</span> <span class="n">deque</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]([</span><span class="n">start_vet</span><span class="p">])</span>
|
||||
|
@ -3742,7 +3746,7 @@
|
|||
<a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="n">startVet</span><span class="p">};</span>
|
||||
<a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
<a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="n">queue</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">que</span><span class="p">;</span>
|
||||
|
@ -3771,7 +3775,7 @@
|
|||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -3800,7 +3804,7 @@
|
|||
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphBFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">startVet</span><span class="p">];</span>
|
||||
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="n">Queue</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="p">();</span>
|
||||
|
@ -3829,7 +3833,7 @@
|
|||
<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">[</span><span class="nx">startVet</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}{}</span>
|
||||
<a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS, 使用切片模擬佇列</span>
|
||||
|
@ -3863,7 +3867,7 @@
|
|||
<a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="kd">func</span> <span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a> <span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a> <span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-5-7" name="__codelineno-5-7" href="#__codelineno-5-7"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
<a id="__codelineno-5-8" name="__codelineno-5-8" href="#__codelineno-5-8"></a> <span class="c1">// 佇列用於實現 BFS</span>
|
||||
<a id="__codelineno-5-9" name="__codelineno-5-9" href="#__codelineno-5-9"></a> <span class="kd">var</span> <span class="nv">que</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[</span><span class="n">startVet</span><span class="p">]</span>
|
||||
|
@ -3891,7 +3895,7 @@
|
|||
<a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-6-7" name="__codelineno-6-7" href="#__codelineno-6-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-6-8" name="__codelineno-6-8" href="#__codelineno-6-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-6-9" name="__codelineno-6-9" href="#__codelineno-6-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -3920,7 +3924,7 @@
|
|||
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphBFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="nx">visited</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -3949,7 +3953,7 @@
|
|||
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="w"> </span><span class="c1">// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -3979,7 +3983,7 @@
|
|||
<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="k">fn</span> <span class="nf">graph_bfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -4083,7 +4087,7 @@
|
|||
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphBFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex</span><span class="p">):</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="p">()</span>
|
||||
<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="w"> </span><span class="n">visited</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-11-9" name="__codelineno-11-9" href="#__codelineno-11-9"></a><span class="w"> </span><span class="c1">// 佇列用於實現 BFS</span>
|
||||
|
@ -4107,7 +4111,29 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">graph_bfs.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">graph_bfs</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">graph_bfs.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 廣度優先走訪 ###</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">graph_bfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 頂點走訪序列</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a><span class="w"> </span><span class="c1"># 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="c1"># 佇列用於實現 BFS</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n">start_vet</span><span class="o">]</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="c1"># 以頂點 vet 為起點,迴圈直至訪問完所有頂點</span>
|
||||
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">que</span><span class="o">.</span><span class="n">length</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span>
|
||||
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="n">vet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">que</span><span class="o">.</span><span class="n">shift</span><span class="w"> </span><span class="c1"># 佇列首頂點出隊</span>
|
||||
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">vet</span><span class="w"> </span><span class="c1"># 記錄訪問頂點</span>
|
||||
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="c1"># 走訪該頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">adj_vet</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">graph</span><span class="o">.</span><span class="n">adj_list</span><span class="o">[</span><span class="n">vet</span><span class="o">]</span>
|
||||
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">visited</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">adj_vet</span><span class="p">)</span><span class="w"> </span><span class="c1"># 跳過已被訪問的頂點</span>
|
||||
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="n">que</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">adj_vet</span><span class="w"> </span><span class="c1"># 只入列未訪問的頂點</span>
|
||||
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="n">visited</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">adj_vet</span><span class="p">)</span><span class="w"> </span><span class="c1"># 標記該頂點已被訪問</span>
|
||||
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="w"> </span><span class="c1"># 返回頂點走訪序列</span>
|
||||
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="w"> </span><span class="n">res</span>
|
||||
<a id="__codelineno-12-23" name="__codelineno-12-23" href="#__codelineno-12-23"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4167,14 +4193,14 @@
|
|||
</div>
|
||||
<h3 id="2">2. 複雜度分析<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
|
||||
<p><strong>時間複雜度</strong>:所有頂點都會入列並出隊一次,使用 <span class="arithmatex">\(O(|V|)\)</span> 時間;在走訪鄰接頂點的過程中,由於是無向圖,因此所有邊都會被訪問 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 時間;總體使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 時間。</p>
|
||||
<p><strong>空間複雜度</strong>:串列 <code>res</code> ,雜湊表 <code>visited</code> ,佇列 <code>que</code> 中的頂點數量最多為 <span class="arithmatex">\(|V|\)</span> ,使用 <span class="arithmatex">\(O(|V|)\)</span> 空間。</p>
|
||||
<p><strong>空間複雜度</strong>:串列 <code>res</code> ,雜湊集合 <code>visited</code> ,佇列 <code>que</code> 中的頂點數量最多為 <span class="arithmatex">\(|V|\)</span> ,使用 <span class="arithmatex">\(O(|V|)\)</span> 空間。</p>
|
||||
<h2 id="932">9.3.2 深度優先走訪<a class="headerlink" href="#932" title="Permanent link">¶</a></h2>
|
||||
<p><strong>深度優先走訪是一種優先走到底、無路可走再回頭的走訪方式</strong>。如圖 9-11 所示,從左上角頂點出發,訪問當前頂點的某個鄰接頂點,直到走到盡頭時返回,再繼續走到盡頭並返回,以此類推,直至所有頂點走訪完成。</p>
|
||||
<p><a class="glightbox" href="../graph_traversal.assets/graph_dfs.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="圖的深度優先走訪" class="animation-figure" src="../graph_traversal.assets/graph_dfs.png" /></a></p>
|
||||
<p align="center"> 圖 9-11 圖的深度優先走訪 </p>
|
||||
|
||||
<h3 id="1_1">1. 演算法實現<a class="headerlink" href="#1_1" title="Permanent link">¶</a></h3>
|
||||
<p>這種“走到盡頭再返回”的演算法範式通常基於遞迴來實現。與廣度優先走訪類似,在深度優先走訪中,我們也需要藉助一個雜湊表 <code>visited</code> 來記錄已被訪問的頂點,以避免重複訪問頂點。</p>
|
||||
<p>這種“走到盡頭再返回”的演算法範式通常基於遞迴來實現。與廣度優先走訪類似,在深度優先走訪中,我們也需要藉助一個雜湊集合 <code>visited</code> 來記錄已被訪問的頂點,以避免重複訪問頂點。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:14"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><input id="__tabbed_3_13" name="__tabbed_3" type="radio" /><input id="__tabbed_3_14" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label><label for="__tabbed_3_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
@ -4194,7 +4220,7 @@
|
|||
<a id="__codelineno-14-14" name="__codelineno-14-14" href="#__codelineno-14-14"></a> <span class="c1"># 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-14-15" name="__codelineno-14-15" href="#__codelineno-14-15"></a> <span class="c1"># 頂點走訪序列</span>
|
||||
<a id="__codelineno-14-16" name="__codelineno-14-16" href="#__codelineno-14-16"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-14-17" name="__codelineno-14-17" href="#__codelineno-14-17"></a> <span class="c1"># 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-14-18" name="__codelineno-14-18" href="#__codelineno-14-18"></a> <span class="n">visited</span> <span class="o">=</span> <span class="nb">set</span><span class="p">[</span><span class="n">Vertex</span><span class="p">]()</span>
|
||||
<a id="__codelineno-14-19" name="__codelineno-14-19" href="#__codelineno-14-19"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-14-20" name="__codelineno-14-20" href="#__codelineno-14-20"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -4219,7 +4245,7 @@
|
|||
<a id="__codelineno-15-16" name="__codelineno-15-16" href="#__codelineno-15-16"></a><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="o">*</span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-15-17" name="__codelineno-15-17" href="#__codelineno-15-17"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-15-18" name="__codelineno-15-18" href="#__codelineno-15-18"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-15-19" name="__codelineno-15-19" href="#__codelineno-15-19"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-15-20" name="__codelineno-15-20" href="#__codelineno-15-20"></a><span class="w"> </span><span class="n">unordered_set</span><span class="o"><</span><span class="n">Vertex</span><span class="w"> </span><span class="o">*></span><span class="w"> </span><span class="n">visited</span><span class="p">;</span>
|
||||
<a id="__codelineno-15-21" name="__codelineno-15-21" href="#__codelineno-15-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-15-22" name="__codelineno-15-22" href="#__codelineno-15-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4245,7 +4271,7 @@
|
|||
<a id="__codelineno-16-16" name="__codelineno-16-16" href="#__codelineno-16-16"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-16-17" name="__codelineno-16-17" href="#__codelineno-16-17"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-16-18" name="__codelineno-16-18" href="#__codelineno-16-18"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-16-19" name="__codelineno-16-19" href="#__codelineno-16-19"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-16-20" name="__codelineno-16-20" href="#__codelineno-16-20"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><></span><span class="p">();</span>
|
||||
<a id="__codelineno-16-21" name="__codelineno-16-21" href="#__codelineno-16-21"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-16-22" name="__codelineno-16-22" href="#__codelineno-16-22"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4272,7 +4298,7 @@
|
|||
<a id="__codelineno-17-17" name="__codelineno-17-17" href="#__codelineno-17-17"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">GraphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-17-18" name="__codelineno-17-18" href="#__codelineno-17-18"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-17-19" name="__codelineno-17-19" href="#__codelineno-17-19"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-17-20" name="__codelineno-17-20" href="#__codelineno-17-20"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-17-21" name="__codelineno-17-21" href="#__codelineno-17-21"></a><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-17-22" name="__codelineno-17-22" href="#__codelineno-17-22"></a><span class="w"> </span><span class="n">DFS</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-17-23" name="__codelineno-17-23" href="#__codelineno-17-23"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4300,7 +4326,7 @@
|
|||
<a id="__codelineno-18-18" name="__codelineno-18-18" href="#__codelineno-18-18"></a><span class="kd">func</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">g</span><span class="w"> </span><span class="o">*</span><span class="nx">graphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="nx">Vertex</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-18-19" name="__codelineno-18-19" href="#__codelineno-18-19"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-18-20" name="__codelineno-18-20" href="#__codelineno-18-20"></a><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-18-22" name="__codelineno-18-22" href="#__codelineno-18-22"></a><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="nx">Vertex</span><span class="p">]</span><span class="kd">struct</span><span class="p">{})</span>
|
||||
<a id="__codelineno-18-23" name="__codelineno-18-23" href="#__codelineno-18-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">g</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-18-24" name="__codelineno-18-24" href="#__codelineno-18-24"></a><span class="w"> </span><span class="c1">// 返回頂點走訪序列</span>
|
||||
|
@ -4328,7 +4354,7 @@
|
|||
<a id="__codelineno-19-17" name="__codelineno-19-17" href="#__codelineno-19-17"></a><span class="kd">func</span> <span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">GraphAdjList</span><span class="p">,</span> <span class="n">startVet</span><span class="p">:</span> <span class="n">Vertex</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">{</span>
|
||||
<a id="__codelineno-19-18" name="__codelineno-19-18" href="#__codelineno-19-18"></a> <span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-19-19" name="__codelineno-19-19" href="#__codelineno-19-19"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-19-21" name="__codelineno-19-21" href="#__codelineno-19-21"></a> <span class="kd">var</span> <span class="nv">visited</span><span class="p">:</span> <span class="n">Set</span><span class="p"><</span><span class="n">Vertex</span><span class="p">></span> <span class="p">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-19-22" name="__codelineno-19-22" href="#__codelineno-19-22"></a> <span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span> <span class="n">graph</span><span class="p">,</span> <span class="n">visited</span><span class="p">:</span> <span class="p">&</span><span class="n">visited</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&</span><span class="n">res</span><span class="p">,</span> <span class="n">vet</span><span class="p">:</span> <span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-19-23" name="__codelineno-19-23" href="#__codelineno-19-23"></a> <span class="k">return</span> <span class="n">res</span>
|
||||
|
@ -4356,7 +4382,7 @@
|
|||
<a id="__codelineno-20-18" name="__codelineno-20-18" href="#__codelineno-20-18"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-20-19" name="__codelineno-20-19" href="#__codelineno-20-19"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-20-20" name="__codelineno-20-20" href="#__codelineno-20-20"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-20-21" name="__codelineno-20-21" href="#__codelineno-20-21"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-20-22" name="__codelineno-20-22" href="#__codelineno-20-22"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-20-23" name="__codelineno-20-23" href="#__codelineno-20-23"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-20-24" name="__codelineno-20-24" href="#__codelineno-20-24"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -4388,7 +4414,7 @@
|
|||
<a id="__codelineno-21-22" name="__codelineno-21-22" href="#__codelineno-21-22"></a><span class="kd">function</span><span class="w"> </span><span class="nx">graphDFS</span><span class="p">(</span><span class="nx">graph</span><span class="o">:</span><span class="w"> </span><span class="kt">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-21-23" name="__codelineno-21-23" href="#__codelineno-21-23"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-21-24" name="__codelineno-21-24" href="#__codelineno-21-24"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">res</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-21-25" name="__codelineno-21-25" href="#__codelineno-21-25"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-21-26" name="__codelineno-21-26" href="#__codelineno-21-26"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">visited</span><span class="o">:</span><span class="w"> </span><span class="kt">Set</span><span class="o"><</span><span class="nx">Vertex</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">();</span>
|
||||
<a id="__codelineno-21-27" name="__codelineno-21-27" href="#__codelineno-21-27"></a><span class="w"> </span><span class="nx">dfs</span><span class="p">(</span><span class="nx">graph</span><span class="p">,</span><span class="w"> </span><span class="nx">visited</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-21-28" name="__codelineno-21-28" href="#__codelineno-21-28"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">;</span>
|
||||
|
@ -4419,7 +4445,7 @@
|
|||
<a id="__codelineno-22-21" name="__codelineno-22-21" href="#__codelineno-22-21"></a><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">graphDFS</span><span class="p">(</span><span class="n">GraphAdjList</span><span class="w"> </span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">Vertex</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-22" name="__codelineno-22-22" href="#__codelineno-22-22"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-22-23" name="__codelineno-22-23" href="#__codelineno-22-23"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-22-24" name="__codelineno-22-24" href="#__codelineno-22-24"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-22-25" name="__codelineno-22-25" href="#__codelineno-22-25"></a><span class="w"> </span><span class="n">Set</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
|
||||
<a id="__codelineno-22-26" name="__codelineno-22-26" href="#__codelineno-22-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">);</span>
|
||||
<a id="__codelineno-22-27" name="__codelineno-22-27" href="#__codelineno-22-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
|
||||
|
@ -4448,7 +4474,7 @@
|
|||
<a id="__codelineno-23-19" name="__codelineno-23-19" href="#__codelineno-23-19"></a><span class="k">fn</span> <span class="nf">graph_dfs</span><span class="p">(</span><span class="n">graph</span>: <span class="nc">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span>: <span class="nc">Vertex</span><span class="p">)</span><span class="w"> </span>-> <span class="nb">Vec</span><span class="o"><</span><span class="n">Vertex</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-23-20" name="__codelineno-23-20" href="#__codelineno-23-20"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-23-21" name="__codelineno-23-21" href="#__codelineno-23-21"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[];</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-23-22" name="__codelineno-23-22" href="#__codelineno-23-22"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-23-23" name="__codelineno-23-23" href="#__codelineno-23-23"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span>::<span class="n">new</span><span class="p">();</span>
|
||||
<a id="__codelineno-23-24" name="__codelineno-23-24" href="#__codelineno-23-24"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="o">&</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">);</span>
|
||||
<a id="__codelineno-23-25" name="__codelineno-23-25" href="#__codelineno-23-25"></a>
|
||||
|
@ -4515,7 +4541,7 @@
|
|||
<a id="__codelineno-25-21" name="__codelineno-25-21" href="#__codelineno-25-21"></a><span class="kd">fun</span><span class="w"> </span><span class="nf">graphDFS</span><span class="p">(</span><span class="n">graph</span><span class="p">:</span><span class="w"> </span><span class="n">GraphAdjList</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">:</span><span class="w"> </span><span class="n">Vertex?)</span><span class="p">:</span><span class="w"> </span><span class="n">MutableList</span><span class="o"><</span><span class="n">Vertex?></span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-25-22" name="__codelineno-25-22" href="#__codelineno-25-22"></a><span class="w"> </span><span class="c1">// 頂點走訪序列</span>
|
||||
<a id="__codelineno-25-23" name="__codelineno-25-23" href="#__codelineno-25-23"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutableListOf</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 雜湊表,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-25-24" name="__codelineno-25-24" href="#__codelineno-25-24"></a><span class="w"> </span><span class="c1">// 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-25-25" name="__codelineno-25-25" href="#__codelineno-25-25"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashSet</span><span class="o"><</span><span class="n">Vertex?></span><span class="p">()</span>
|
||||
<a id="__codelineno-25-26" name="__codelineno-25-26" href="#__codelineno-25-26"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">startVet</span><span class="p">)</span>
|
||||
<a id="__codelineno-25-27" name="__codelineno-25-27" href="#__codelineno-25-27"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">res</span>
|
||||
|
@ -4523,9 +4549,28 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">graph_dfs.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">dfs</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">graph_dfs</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">graph_dfs.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 深度優先走訪輔助函式 ###</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">vet</span><span class="w"> </span><span class="c1"># 記錄訪問頂點</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="w"> </span><span class="n">visited</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span><span class="w"> </span><span class="c1"># 標記該頂點已被訪問</span>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="w"> </span><span class="c1"># 走訪該頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">adj_vet</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">graph</span><span class="o">.</span><span class="n">adj_list</span><span class="o">[</span><span class="n">vet</span><span class="o">]</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="w"> </span><span class="k">next</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">visited</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">adj_vet</span><span class="p">)</span><span class="w"> </span><span class="c1"># 跳過已被訪問的頂點</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="c1"># 遞迴訪問鄰接頂點</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">adj_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="c1">### 深度優先走訪 ###</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="k">def</span><span class="w"> </span><span class="nf">graph_dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="c1"># 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點</span>
|
||||
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="c1"># 頂點走訪序列</span>
|
||||
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span>
|
||||
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="w"> </span><span class="c1"># 雜湊集合,用於記錄已被訪問過的頂點</span>
|
||||
<a id="__codelineno-26-19" name="__codelineno-26-19" href="#__codelineno-26-19"></a><span class="w"> </span><span class="n">visited</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">Set</span><span class="o">.</span><span class="n">new</span>
|
||||
<a id="__codelineno-26-20" name="__codelineno-26-20" href="#__codelineno-26-20"></a><span class="w"> </span><span class="n">dfs</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span><span class="w"> </span><span class="n">visited</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">start_vet</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-21" name="__codelineno-26-21" href="#__codelineno-26-21"></a><span class="w"> </span><span class="n">res</span>
|
||||
<a id="__codelineno-26-22" name="__codelineno-26-22" href="#__codelineno-26-22"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4593,7 +4638,7 @@
|
|||
</div>
|
||||
<h3 id="2_1">2. 複雜度分析<a class="headerlink" href="#2_1" title="Permanent link">¶</a></h3>
|
||||
<p><strong>時間複雜度</strong>:所有頂點都會被訪問 <span class="arithmatex">\(1\)</span> 次,使用 <span class="arithmatex">\(O(|V|)\)</span> 時間;所有邊都會被訪問 <span class="arithmatex">\(2\)</span> 次,使用 <span class="arithmatex">\(O(2|E|)\)</span> 時間;總體使用 <span class="arithmatex">\(O(|V| + |E|)\)</span> 時間。</p>
|
||||
<p><strong>空間複雜度</strong>:串列 <code>res</code> ,雜湊表 <code>visited</code> 頂點數量最多為 <span class="arithmatex">\(|V|\)</span> ,遞迴深度最大為 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空間。</p>
|
||||
<p><strong>空間複雜度</strong>:串列 <code>res</code> ,雜湊集合 <code>visited</code> 頂點數量最多為 <span class="arithmatex">\(|V|\)</span> ,遞迴深度最大為 <span class="arithmatex">\(|V|\)</span> ,因此使用 <span class="arithmatex">\(O(|V|)\)</span> 空間。</p>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
|
|
|
@ -4336,11 +4336,20 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">left</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">right</span><span class="p">}</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">parent</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-26-1" name="__codelineno-26-1" href="#__codelineno-26-1"></a><span class="c1">### 獲取左子節點的索引 ###</span>
|
||||
<a id="__codelineno-26-2" name="__codelineno-26-2" href="#__codelineno-26-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">left</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="c1">### 獲取右子節點的索引 ###</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a><span class="k">def</span><span class="w"> </span><span class="nf">right</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="k">end</span>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="c1">### 獲取父節點的索引 ###</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="k">def</span><span class="w"> </span><span class="nf">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="c1"># 向下整除</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4451,7 +4460,10 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">peek</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="c1">### 訪問堆積頂元素 ###</span>
|
||||
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">peek</span>
|
||||
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span>
|
||||
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -4821,9 +4833,27 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">push</span><span class="p">}</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_up</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-54-1" name="__codelineno-54-1" href="#__codelineno-54-1"></a><span class="c1">### 元素入堆積 ###</span>
|
||||
<a id="__codelineno-54-2" name="__codelineno-54-2" href="#__codelineno-54-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">push</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-3" name="__codelineno-54-3" href="#__codelineno-54-3"></a><span class="w"> </span><span class="c1"># 新增節點</span>
|
||||
<a id="__codelineno-54-4" name="__codelineno-54-4" href="#__codelineno-54-4"></a><span class="w"> </span><span class="vi">@max_heap</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-54-5" name="__codelineno-54-5" href="#__codelineno-54-5"></a><span class="w"> </span><span class="c1"># 從底至頂堆積化</span>
|
||||
<a id="__codelineno-54-6" name="__codelineno-54-6" href="#__codelineno-54-6"></a><span class="w"> </span><span class="n">sift_up</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-7" name="__codelineno-54-7" href="#__codelineno-54-7"></a><span class="k">end</span>
|
||||
<a id="__codelineno-54-8" name="__codelineno-54-8" href="#__codelineno-54-8"></a>
|
||||
<a id="__codelineno-54-9" name="__codelineno-54-9" href="#__codelineno-54-9"></a><span class="c1">### 從節點 i 開始,從底至頂堆積化 ###</span>
|
||||
<a id="__codelineno-54-10" name="__codelineno-54-10" href="#__codelineno-54-10"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_up</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-11" name="__codelineno-54-11" href="#__codelineno-54-11"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-54-12" name="__codelineno-54-12" href="#__codelineno-54-12"></a><span class="w"> </span><span class="c1"># 獲取節點 i 的父節點</span>
|
||||
<a id="__codelineno-54-13" name="__codelineno-54-13" href="#__codelineno-54-13"></a><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parent</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-14" name="__codelineno-54-14" href="#__codelineno-54-14"></a><span class="w"> </span><span class="c1"># 當“越過根節點”或“節點無須修復”時,結束堆積化</span>
|
||||
<a id="__codelineno-54-15" name="__codelineno-54-15" href="#__codelineno-54-15"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">p</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="nb">p</span><span class="o">]</span>
|
||||
<a id="__codelineno-54-16" name="__codelineno-54-16" href="#__codelineno-54-16"></a><span class="w"> </span><span class="c1"># 交換兩節點</span>
|
||||
<a id="__codelineno-54-17" name="__codelineno-54-17" href="#__codelineno-54-17"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="nb">p</span><span class="p">)</span>
|
||||
<a id="__codelineno-54-18" name="__codelineno-54-18" href="#__codelineno-54-18"></a><span class="w"> </span><span class="c1"># 迴圈向上堆積化</span>
|
||||
<a id="__codelineno-54-19" name="__codelineno-54-19" href="#__codelineno-54-19"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">p</span>
|
||||
<a id="__codelineno-54-20" name="__codelineno-54-20" href="#__codelineno-54-20"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-54-21" name="__codelineno-54-21" href="#__codelineno-54-21"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -5242,7 +5272,7 @@
|
|||
<a id="__codelineno-65-7" name="__codelineno-65-7" href="#__codelineno-65-7"></a><span class="w"> </span><span class="c1">// 交換根節點與最右葉節點(交換首元素與尾元素)</span>
|
||||
<a id="__codelineno-65-8" name="__codelineno-65-8" href="#__codelineno-65-8"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-9" name="__codelineno-65-9" href="#__codelineno-65-9"></a><span class="w"> </span><span class="c1">// 刪除節點</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-10" name="__codelineno-65-10" href="#__codelineno-65-10"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">max_heap</span><span class="p">.</span><span class="n">pop</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span>
|
||||
<a id="__codelineno-65-11" name="__codelineno-65-11" href="#__codelineno-65-11"></a><span class="w"> </span><span class="c1">// 從頂至底堆積化</span>
|
||||
<a id="__codelineno-65-12" name="__codelineno-65-12" href="#__codelineno-65-12"></a><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||
<a id="__codelineno-65-13" name="__codelineno-65-13" href="#__codelineno-65-13"></a><span class="w"> </span><span class="c1">// 返回堆積頂元素</span>
|
||||
|
@ -5354,9 +5384,37 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">pop</span><span class="p">}</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{</span><span class="no">MaxHeap</span><span class="p">}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">sift_down</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">my_heap.rb</span><pre><span></span><code><a id="__codelineno-68-1" name="__codelineno-68-1" href="#__codelineno-68-1"></a><span class="c1">### 元素出堆積 ###</span>
|
||||
<a id="__codelineno-68-2" name="__codelineno-68-2" href="#__codelineno-68-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">pop</span>
|
||||
<a id="__codelineno-68-3" name="__codelineno-68-3" href="#__codelineno-68-3"></a><span class="w"> </span><span class="c1"># 判空處理</span>
|
||||
<a id="__codelineno-68-4" name="__codelineno-68-4" href="#__codelineno-68-4"></a><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="no">IndexError</span><span class="p">,</span><span class="w"> </span><span class="s2">"堆積為空"</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">is_empty?</span>
|
||||
<a id="__codelineno-68-5" name="__codelineno-68-5" href="#__codelineno-68-5"></a><span class="w"> </span><span class="c1"># 交換根節點與最右葉節點(交換首元素與尾元素)</span>
|
||||
<a id="__codelineno-68-6" name="__codelineno-68-6" href="#__codelineno-68-6"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-7" name="__codelineno-68-7" href="#__codelineno-68-7"></a><span class="w"> </span><span class="c1"># 刪除節點</span>
|
||||
<a id="__codelineno-68-8" name="__codelineno-68-8" href="#__codelineno-68-8"></a><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">.</span><span class="n">pop</span>
|
||||
<a id="__codelineno-68-9" name="__codelineno-68-9" href="#__codelineno-68-9"></a><span class="w"> </span><span class="c1"># 從頂至底堆積化</span>
|
||||
<a id="__codelineno-68-10" name="__codelineno-68-10" href="#__codelineno-68-10"></a><span class="w"> </span><span class="n">sift_down</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-11" name="__codelineno-68-11" href="#__codelineno-68-11"></a><span class="w"> </span><span class="c1"># 返回堆積頂元素</span>
|
||||
<a id="__codelineno-68-12" name="__codelineno-68-12" href="#__codelineno-68-12"></a><span class="w"> </span><span class="n">val</span>
|
||||
<a id="__codelineno-68-13" name="__codelineno-68-13" href="#__codelineno-68-13"></a><span class="k">end</span>
|
||||
<a id="__codelineno-68-14" name="__codelineno-68-14" href="#__codelineno-68-14"></a>
|
||||
<a id="__codelineno-68-15" name="__codelineno-68-15" href="#__codelineno-68-15"></a><span class="c1">### 從節點 i 開始,從頂至底堆積化 ###</span>
|
||||
<a id="__codelineno-68-16" name="__codelineno-68-16" href="#__codelineno-68-16"></a><span class="k">def</span><span class="w"> </span><span class="nf">sift_down</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-17" name="__codelineno-68-17" href="#__codelineno-68-17"></a><span class="w"> </span><span class="kp">loop</span><span class="w"> </span><span class="k">do</span>
|
||||
<a id="__codelineno-68-18" name="__codelineno-68-18" href="#__codelineno-68-18"></a><span class="w"> </span><span class="c1"># 判斷節點 i, l, r 中值最大的節點,記為 ma</span>
|
||||
<a id="__codelineno-68-19" name="__codelineno-68-19" href="#__codelineno-68-19"></a><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">right</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-20" name="__codelineno-68-20" href="#__codelineno-68-20"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">l</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-21" name="__codelineno-68-21" href="#__codelineno-68-21"></a><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">r</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vi">@max_heap</span><span class="o">[</span><span class="n">ma</span><span class="o">]</span>
|
||||
<a id="__codelineno-68-22" name="__codelineno-68-22" href="#__codelineno-68-22"></a>
|
||||
<a id="__codelineno-68-23" name="__codelineno-68-23" href="#__codelineno-68-23"></a><span class="w"> </span><span class="c1"># 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出</span>
|
||||
<a id="__codelineno-68-24" name="__codelineno-68-24" href="#__codelineno-68-24"></a><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">ma</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">i</span>
|
||||
<a id="__codelineno-68-25" name="__codelineno-68-25" href="#__codelineno-68-25"></a>
|
||||
<a id="__codelineno-68-26" name="__codelineno-68-26" href="#__codelineno-68-26"></a><span class="w"> </span><span class="c1"># 交換兩節點</span>
|
||||
<a id="__codelineno-68-27" name="__codelineno-68-27" href="#__codelineno-68-27"></a><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">ma</span><span class="p">)</span>
|
||||
<a id="__codelineno-68-28" name="__codelineno-68-28" href="#__codelineno-68-28"></a><span class="w"> </span><span class="c1"># 迴圈向下堆積化</span>
|
||||
<a id="__codelineno-68-29" name="__codelineno-68-29" href="#__codelineno-68-29"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ma</span>
|
||||
<a id="__codelineno-68-30" name="__codelineno-68-30" href="#__codelineno-68-30"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-68-31" name="__codelineno-68-31" href="#__codelineno-68-31"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -4019,7 +4019,28 @@
|
|||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="o">[</span><span class="n">class</span><span class="o">]</span><span class="p">{}</span><span class="o">-[</span><span class="n">func</span><span class="o">]</span><span class="p">{</span><span class="n">top_k_heap</span><span class="p">}</span>
|
||||
<div class="highlight"><span class="filename">top_k.rb</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">### 基於堆積查詢陣列中最大的 k 個元素 ###</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="k">def</span><span class="w"> </span><span class="nf">top_k_heap</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="w"> </span><span class="c1"># 初始化小頂堆積</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="w"> </span><span class="c1"># 請注意:我們將堆積中所有元素取反,從而用大頂堆積來模擬小頂堆積</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="w"> </span><span class="n">max_heap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="no">MaxHeap</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a><span class="w"> </span><span class="c1"># 將陣列的前 k 個元素入堆積</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="n">k</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-11" name="__codelineno-12-11" href="#__codelineno-12-11"></a>
|
||||
<a id="__codelineno-12-12" name="__codelineno-12-12" href="#__codelineno-12-12"></a><span class="w"> </span><span class="c1"># 從第 k+1 個元素開始,保持堆積的長度為 k</span>
|
||||
<a id="__codelineno-12-13" name="__codelineno-12-13" href="#__codelineno-12-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">k</span><span class="o">...</span><span class="n">nums</span><span class="o">.</span><span class="n">length</span>
|
||||
<a id="__codelineno-12-14" name="__codelineno-12-14" href="#__codelineno-12-14"></a><span class="w"> </span><span class="c1"># 若當前元素大於堆積頂元素,則將堆積頂元素出堆積、當前元素入堆積</span>
|
||||
<a id="__codelineno-12-15" name="__codelineno-12-15" href="#__codelineno-12-15"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">peek_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-16" name="__codelineno-12-16" href="#__codelineno-12-16"></a><span class="w"> </span><span class="n">pop_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-17" name="__codelineno-12-17" href="#__codelineno-12-17"></a><span class="w"> </span><span class="n">push_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">,</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-18" name="__codelineno-12-18" href="#__codelineno-12-18"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-19" name="__codelineno-12-19" href="#__codelineno-12-19"></a><span class="w"> </span><span class="k">end</span>
|
||||
<a id="__codelineno-12-20" name="__codelineno-12-20" href="#__codelineno-12-20"></a>
|
||||
<a id="__codelineno-12-21" name="__codelineno-12-21" href="#__codelineno-12-21"></a><span class="w"> </span><span class="n">get_min_heap</span><span class="p">(</span><span class="n">max_heap</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-22" name="__codelineno-12-22" href="#__codelineno-12-22"></a><span class="k">end</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -636,6 +636,17 @@
|
|||
|
||||
|
||||
|
||||
<label class="md-nav__link md-nav__link--active" for="__toc">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.3 小結
|
||||
</span>
|
||||
|
||||
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
</label>
|
||||
|
||||
<a href="./" class="md-nav__link md-nav__link--active">
|
||||
|
||||
|
||||
|
@ -646,6 +657,34 @@
|
|||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
<nav class="md-nav md-nav--secondary" aria-label="目錄">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<label class="md-nav__title" for="__toc">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
目錄
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-q-a" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Q & A
|
||||
</span>
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
|
@ -3490,6 +3529,23 @@
|
|||
|
||||
|
||||
|
||||
<label class="md-nav__title" for="__toc">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
目錄
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-q-a" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Q & A
|
||||
</span>
|
||||
</a>
|
||||
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -3534,6 +3590,15 @@
|
|||
<li>資料結構與演算法緊密相連。資料結構是演算法的基石,而演算法是資料結構發揮作用的舞臺。</li>
|
||||
<li>我們可以將資料結構與演算法類比為拼裝積木,積木代表資料,積木的形狀和連線方式等代表資料結構,拼裝積木的步驟則對應演算法。</li>
|
||||
</ul>
|
||||
<h3 id="1-q-a">1. Q & A<a class="headerlink" href="#1-q-a" title="Permanent link">¶</a></h3>
|
||||
<p><strong>Q</strong>:作為一名程式設計師,我在日常工作中從未用演算法解決過問題,常用演算法都被程式語言封裝好了,直接用就可以了;這是否意味著我們工作中的問題還沒有到達需要演算法的程度?</p>
|
||||
<p>如果把具體的工作技能比作是武功的“招式”的話,那麼基礎科目應該更像是“內功”。</p>
|
||||
<p>我認為學演算法(以及其他基礎科目)的意義不是在於在工作中從零實現它,而是基於學到的知識,在解決問題時能夠作出專業的反應和判斷,從而提升工作的整體質量。舉一個簡單例子,每種程式語言都內建了排序函式:</p>
|
||||
<ul>
|
||||
<li>如果我們沒有學過資料結構與演算法,那麼給定任何資料,我們可能都塞給這個排序函式去做了。執行順暢、效能不錯,看上去並沒有什麼問題。</li>
|
||||
<li>但如果學過演算法,我們就會知道內建排序函式的時間複雜度是 <span class="arithmatex">\(O(n \log n)\)</span> ;而如果給定的資料是固定位數的整數(例如學號),那麼我們就可以用效率更高的“基數排序”來做,將時間複雜度降為 <span class="arithmatex">\(O(nk)\)</span> ,其中 <span class="arithmatex">\(k\)</span> 為位數。當資料體量很大時,節省出來的執行時間就能創造較大價值(成本降低、體驗變好等)。</li>
|
||||
</ul>
|
||||
<p>在工程領域中,大量問題是難以達到最優解的,許多問題只是被“差不多”地解決了。問題的難易程度一方面取決於問題本身的性質,另一方面也取決於觀測問題的人的知識儲備。人的知識越完備、經驗越多,分析問題就會越深入,問題就能被解決得更優雅。</p>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
|
|
|
@ -4443,8 +4443,8 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>設平衡因子為 <span class="arithmatex">\(f\)</span> ,則一棵 AVL 樹的任意節點的平衡因子皆滿足 <span class="arithmatex">\(-1 \le f \le 1\)</span> 。</p>
|
||||
</div>
|
||||
<h2 id="752-avl">7.5.2 AVL 樹旋轉<a class="headerlink" href="#752-avl" title="Permanent link">¶</a></h2>
|
||||
|
|
|
@ -4324,8 +4324,8 @@
|
|||
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%85%83%E6%A8%B9%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%85%83%E6%A8%B9%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E7%AF%80%E9%BB%9E%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E6%A8%99%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E8%88%87%E5%88%AA%E9%99%A4%E7%AF%80%E9%BB%9E%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%96%93%E6%8F%92%E5%85%A5%E7%AF%80%E9%BB%9E%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%AA%E9%99%A4%E7%AF%80%E9%BB%9E%20P%0A%20%20%20%20n1.left%20%3D%20n2&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=37&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%85%83%E6%A8%B9%E7%AF%80%E9%BB%9E%E9%A1%9E%E5%88%A5%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E7%AF%80%E9%BB%9E%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E7%AF%80%E9%BB%9E%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%85%83%E6%A8%B9%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E7%AF%80%E9%BB%9E%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%A7%8B%E5%BB%BA%E7%AF%80%E9%BB%9E%E4%B9%8B%E9%96%93%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E6%A8%99%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E8%88%87%E5%88%AA%E9%99%A4%E7%AF%80%E9%BB%9E%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%96%93%E6%8F%92%E5%85%A5%E7%AF%80%E9%BB%9E%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%AA%E9%99%A4%E7%AF%80%E9%BB%9E%20P%0A%20%20%20%20n1.left%20%3D%20n2&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=37&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全螢幕觀看 ></a></div></p>
|
||||
</details>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>需要注意的是,插入節點可能會改變二元樹的原有邏輯結構,而刪除節點通常意味著刪除該節點及其所有子樹。因此,在二元樹中,插入與刪除通常是由一套操作配合完成的,以實現有實際意義的操作。</p>
|
||||
</div>
|
||||
<h2 id="713">7.1.3 常見二元樹型別<a class="headerlink" href="#713" title="Permanent link">¶</a></h2>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -2,527 +2,527 @@
|
|||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_appendix/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_appendix/contribution/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_appendix/installation/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_appendix/terminology/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/ram_and_cache/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/backtracking_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/n_queens_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/permutations_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/subset_sum_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_backtracking/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/iteration_and_recursion/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/basic_data_types/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/character_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/number_encoding/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/binary_search_recur/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/build_binary_tree_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/divide_and_conquer/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/hanota_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_divide_and_conquer/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/dp_problem_features/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/dp_solution_pipeline/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/edit_distance_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_dynamic_programming/unbounded_knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_graph/graph/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_graph/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/fractional_knapsack_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/greedy_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/max_capacity_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/max_product_cutting_problem/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_greedy/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hashing/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hashing/hash_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hashing/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_heap/heap/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_heap/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_heap/top_k/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_hello_algo/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_introduction/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_introduction/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_preface/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_preface/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_reference/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/binary_search/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/binary_search_edge/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/binary_search_insertion/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/replace_linear_by_hashing/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/searching_algorithm_revisited/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_searching/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/bubble_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/bucket_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/counting_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/heap_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/insertion_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/merge_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/quick_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/radix_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/selection_sort/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/sorting_algorithm/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_sorting/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_stack_and_queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/array_representation_of_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/zh-hant/chapter_tree/summary/</loc>
|
||||
<lastmod>2024-04-26</lastmod>
|
||||
<lastmod>2024-04-28</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
Binary file not shown.
Loading…
Reference in a new issue