This commit is contained in:
krahets
2023-09-21 20:44:52 +08:00
parent 830dc9e326
commit 0ba5acd92b
111 changed files with 13161 additions and 5302 deletions

View File

@ -3510,7 +3510,32 @@
<div class="md-content" data-md-component="content">
<article class="md-content__inner md-typeset">
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Tags -->
<!-- Actions -->
<a href="https://github.com/krahets/hello-algo/tree/main/docs/chapter_graph/graph_traversal.md" title="编辑此页" class="md-content__button md-icon">
@ -3521,6 +3546,13 @@
<!--
Hack: check whether the content contains a h1 headline. If it doesn't, the
page title (or respectively site name) is used as the main headline.
-->
<!-- Page content -->
<h1 id="93">9.3 &nbsp; 图的遍历<a class="headerlink" href="#93" title="Permanent link">&para;</a></h1>
<p>树代表的是“一对多”的关系,而图则具有更高的自由度,可以表示任意的“多对多”关系。因此,我们可以把树看作是图的一种特例。显然,<strong>树的遍历操作也是图的遍历操作的一种特例</strong></p>
<p>图和树都都需要应用搜索算法来实现遍历操作。图的遍历方式可分为两种:「广度优先遍历 breadth-first traversal」和「深度优先遍历 depth-first traversal」。它们也常被称为「广度优先搜索 breadth-first search」和「深度优先搜索 depth-first search」简称 BFS 和 DFS 。</p>
@ -4313,11 +4345,73 @@
<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>
<!-- Source file information -->
<!-- Was this page helpful? -->
<h2 id="__comments">评论</h2>
<!-- Previous and next pages link -->
<nav
class="md-footer__inner md-grid"
aria-label="页脚"
>
<!-- Link to previous page -->
<a
href="../graph_operations/"
class="md-footer__link md-footer__link--prev"
aria-label="上一页: 9.2 &amp;nbsp; 图基础操作"
rel="prev"
>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.2 &nbsp; 图基础操作
</div>
</div>
</a>
<!-- Link to next page -->
<a
href="../summary/"
class="md-footer__link md-footer__link--next"
aria-label="下一页: 9.4 &amp;nbsp; 小结"
rel="next"
>
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!-- Comment system -->
<h5 align="center" id="__comments">欢迎你提出疑问或建议</h5>
<!-- Insert generated snippet here -->
<script
src="https://giscus.app/client.js"
@ -4383,48 +4477,31 @@
</main>
<footer class="md-footer">
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../graph_operations/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 9.2 &amp;nbsp; 图基础操作" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<span class="md-footer__direction">
上一页
</span>
<div class="md-ellipsis">
9.2 &nbsp; 图基础操作
</div>
</div>
</a>
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 9.4 &amp;nbsp; 小结" rel="next">
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
9.4 &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<!--
Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Footer -->
<footer class="md-footer">
<!-- Further information -->
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-copyright">
@ -4435,6 +4512,8 @@
</div>
<!-- Social links -->
<div class="md-social">