mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 06:10:19 +08:00
deploy
This commit is contained in:
@@ -3415,7 +3415,7 @@ G & = \{ V, E \} \newline
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
<p>如果将顶点看作节点,将边看作连接各个节点的引用(指针),我们就可以将图看作是一种从链表拓展而来的数据结构。如图 9-1 所示,<strong>相较于线性关系(链表)和分治关系(树),网络关系(图)的自由度更高</strong>,从而更为复杂。</p>
|
||||
<p><a class="glightbox" href="../graph.assets/linkedlist_tree_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="链表、树、图之间的关系" src="../graph.assets/linkedlist_tree_graph.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/linkedlist_tree_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="链表、树、图之间的关系" class="animation-figure" src="../graph.assets/linkedlist_tree_graph.png" /></a></p>
|
||||
<p align="center"> 图 9-1 链表、树、图之间的关系 </p>
|
||||
|
||||
<h2 id="911">9.1.1 图常见类型与术语<a class="headerlink" href="#911" title="Permanent link">¶</a></h2>
|
||||
@@ -3424,7 +3424,7 @@ G & = \{ V, E \} \newline
|
||||
<li>在无向图中,边表示两顶点之间的“双向”连接关系,例如微信或 QQ 中的“好友关系”。</li>
|
||||
<li>在有向图中,边具有方向性,即 <span class="arithmatex">\(A \rightarrow B\)</span> 和 <span class="arithmatex">\(A \leftarrow B\)</span> 两个方向的边是相互独立的,例如微博或抖音上的“关注”与“被关注”关系。</li>
|
||||
</ul>
|
||||
<p><a class="glightbox" href="../graph.assets/directed_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="有向图与无向图" src="../graph.assets/directed_graph.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/directed_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="有向图与无向图" class="animation-figure" src="../graph.assets/directed_graph.png" /></a></p>
|
||||
<p align="center"> 图 9-2 有向图与无向图 </p>
|
||||
|
||||
<p>根据所有顶点是否连通,可分为图 9-3 所示的「连通图 connected graph」和「非连通图 disconnected graph」。</p>
|
||||
@@ -3432,11 +3432,11 @@ G & = \{ V, E \} \newline
|
||||
<li>对于连通图,从某个顶点出发,可以到达其余任意顶点。</li>
|
||||
<li>对于非连通图,从某个顶点出发,至少有一个顶点无法到达。</li>
|
||||
</ul>
|
||||
<p><a class="glightbox" href="../graph.assets/connected_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="连通图与非连通图" src="../graph.assets/connected_graph.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/connected_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="连通图与非连通图" class="animation-figure" src="../graph.assets/connected_graph.png" /></a></p>
|
||||
<p align="center"> 图 9-3 连通图与非连通图 </p>
|
||||
|
||||
<p>我们还可以为边添加“权重”变量,从而得到图 9-4 所示的「有权图 weighted graph」。例如在王者荣耀等手游中,系统会根据共同游戏时间来计算玩家之间的“亲密度”,这种亲密度网络就可以用有权图来表示。</p>
|
||||
<p><a class="glightbox" href="../graph.assets/weighted_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="有权图与无权图" src="../graph.assets/weighted_graph.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/weighted_graph.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="有权图与无权图" class="animation-figure" src="../graph.assets/weighted_graph.png" /></a></p>
|
||||
<p align="center"> 图 9-4 有权图与无权图 </p>
|
||||
|
||||
<p>图数据结构包含以下常用术语。</p>
|
||||
@@ -3450,7 +3450,7 @@ G & = \{ V, E \} \newline
|
||||
<h3 id="1">1. 邻接矩阵<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
|
||||
<p>设图的顶点数量为 <span class="arithmatex">\(n\)</span> ,「邻接矩阵 adjacency matrix」使用一个 <span class="arithmatex">\(n \times n\)</span> 大小的矩阵来表示图,每一行(列)代表一个顶点,矩阵元素代表边,用 <span class="arithmatex">\(1\)</span> 或 <span class="arithmatex">\(0\)</span> 表示两个顶点之间是否存在边。</p>
|
||||
<p>如图 9-5 所示,设邻接矩阵为 <span class="arithmatex">\(M\)</span>、顶点列表为 <span class="arithmatex">\(V\)</span> ,那么矩阵元素 <span class="arithmatex">\(M[i, j] = 1\)</span> 表示顶点 <span class="arithmatex">\(V[i]\)</span> 到顶点 <span class="arithmatex">\(V[j]\)</span> 之间存在边,反之 <span class="arithmatex">\(M[i, j] = 0\)</span> 表示两顶点之间无边。</p>
|
||||
<p><a class="glightbox" href="../graph.assets/adjacency_matrix.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的邻接矩阵表示" src="../graph.assets/adjacency_matrix.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/adjacency_matrix.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的邻接矩阵表示" class="animation-figure" src="../graph.assets/adjacency_matrix.png" /></a></p>
|
||||
<p align="center"> 图 9-5 图的邻接矩阵表示 </p>
|
||||
|
||||
<p>邻接矩阵具有以下特性。</p>
|
||||
@@ -3462,7 +3462,7 @@ G & = \{ V, E \} \newline
|
||||
<p>使用邻接矩阵表示图时,我们可以直接访问矩阵元素以获取边,因此增删查操作的效率很高,时间复杂度均为 <span class="arithmatex">\(O(1)\)</span> 。然而,矩阵的空间复杂度为 <span class="arithmatex">\(O(n^2)\)</span> ,内存占用较多。</p>
|
||||
<h3 id="2">2. 邻接表<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
|
||||
<p>「邻接表 adjacency list」使用 <span class="arithmatex">\(n\)</span> 个链表来表示图,链表节点表示顶点。第 <span class="arithmatex">\(i\)</span> 条链表对应顶点 <span class="arithmatex">\(i\)</span> ,其中存储了该顶点的所有邻接顶点(即与该顶点相连的顶点)。图 9-6 展示了一个使用邻接表存储的图的示例。</p>
|
||||
<p><a class="glightbox" href="../graph.assets/adjacency_list.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的邻接表表示" src="../graph.assets/adjacency_list.png" /></a></p>
|
||||
<p><a class="glightbox" href="../graph.assets/adjacency_list.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="图的邻接表表示" class="animation-figure" src="../graph.assets/adjacency_list.png" /></a></p>
|
||||
<p align="center"> 图 9-6 图的邻接表表示 </p>
|
||||
|
||||
<p>邻接表仅存储实际存在的边,而边的总数通常远小于 <span class="arithmatex">\(n^2\)</span> ,因此它更加节省空间。然而,在邻接表中需要通过遍历链表来查找边,因此其时间效率不如邻接矩阵。</p>
|
||||
|
||||
Reference in New Issue
Block a user