This commit is contained in:
krahets
2024-07-30 16:54:47 +08:00
parent 8fb4fdc14c
commit 55b91eb967
140 changed files with 426 additions and 1278 deletions
+18 -18
View File
@@ -1957,9 +1957,9 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#911-common-types-of-graphs" class="md-nav__link">
<a href="#911-common-types-and-terminologies-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.1 &nbsp; Common types of graphs
9.1.1 &nbsp; Common types and terminologies of graphs
</span>
</a>
@@ -3570,9 +3570,9 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#911-common-types-of-graphs" class="md-nav__link">
<a href="#911-common-types-and-terminologies-of-graphs" class="md-nav__link">
<span class="md-ellipsis">
9.1.1 &nbsp; Common types of graphs
9.1.1 &nbsp; Common types and terminologies of graphs
</span>
</a>
@@ -3669,24 +3669,24 @@ G &amp; = \{ V, E \} \newline
<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="Relationship between linked lists, trees, and graphs" class="animation-figure" src="../graph.assets/linkedlist_tree_graph.png" /></a></p>
<p align="center"> Figure 9-1 &nbsp; Relationship between linked lists, trees, and graphs </p>
<h2 id="911-common-types-of-graphs">9.1.1 &nbsp; Common types of graphs<a class="headerlink" href="#911-common-types-of-graphs" title="Permanent link">&para;</a></h2>
<p>Based on whether edges have direction, graphs can be divided into <u>undirected graphs</u> and <u>directed graphs</u>, as shown in Figure 9-2.</p>
<h2 id="911-common-types-and-terminologies-of-graphs">9.1.1 &nbsp; Common types and terminologies of graphs<a class="headerlink" href="#911-common-types-and-terminologies-of-graphs" title="Permanent link">&para;</a></h2>
<p>Graphs can be divided into <u>undirected graphs</u> and <u>directed graphs</u> depending on whether edges have direction, as shown in Figure 9-2.</p>
<ul>
<li>In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friendship" in WeChat or QQ.</li>
<li>In directed graphs, edges have directionality, that is, the edges <span class="arithmatex">\(A \rightarrow B\)</span> and <span class="arithmatex">\(A \leftarrow B\)</span> are independent of each other, for example, the "follow" and "be followed" relationship on Weibo or TikTok.</li>
<li>In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friends" in Facebook.</li>
<li>In directed graphs, edges have directionality, that is, the edges <span class="arithmatex">\(A \rightarrow B\)</span> and <span class="arithmatex">\(A \leftarrow B\)</span> are independent of each other. For example, the "follow" and "followed" relationship on Instagram or TikTok.</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="Directed and undirected graphs" class="animation-figure" src="../graph.assets/directed_graph.png" /></a></p>
<p align="center"> Figure 9-2 &nbsp; Directed and undirected graphs </p>
<p>Based on whether all vertices are connected, graphs can be divided into <u>connected graphs</u> and <u>disconnected graphs</u>, as shown in Figure 9-3.</p>
<p>Depending on whether all vertices are connected, graphs can be divided into <u>connected graphs</u> and <u>disconnected graphs</u>, as shown in Figure 9-3.</p>
<ul>
<li>For connected graphs, it is possible to reach any other vertex starting from a certain vertex.</li>
<li>For disconnected graphs, there is at least one vertex that cannot be reached from a certain starting vertex.</li>
<li>For connected graphs, it is possible to reach any other vertex starting from an arbitrary vertex.</li>
<li>For disconnected graphs, there is at least one vertex that cannot be reached from an arbitrary starting vertex.</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="Connected and disconnected graphs" class="animation-figure" src="../graph.assets/connected_graph.png" /></a></p>
<p align="center"> Figure 9-3 &nbsp; Connected and disconnected graphs </p>
<p>We can also add a weight variable to edges, resulting in <u>weighted graphs</u> as shown in Figure 9-4. For example, in mobile games like "Honor of Kings", the system calculates the "closeness" between players based on shared gaming time, and this closeness network can be represented with a weighted graph.</p>
<p>We can also add a weight variable to edges, resulting in <u>weighted graphs</u> as shown in Figure 9-4. For example, in Instagram, the system sorts your follower and following list by the level of interaction between you and other users (likes, views, comments, etc.). Such an interaction network can be represented by a 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="Weighted and unweighted graphs" class="animation-figure" src="../graph.assets/weighted_graph.png" /></a></p>
<p align="center"> Figure 9-4 &nbsp; Weighted and unweighted graphs </p>
@@ -3697,7 +3697,7 @@ G &amp; = \{ V, E \} \newline
<li><u>Degree</u>: The number of edges a vertex has. For directed graphs, <u>in-degree</u> refers to how many edges point to the vertex, and <u>out-degree</u> refers to how many edges point out from the vertex.</li>
</ul>
<h2 id="912-representation-of-graphs">9.1.2 &nbsp; Representation of graphs<a class="headerlink" href="#912-representation-of-graphs" title="Permanent link">&para;</a></h2>
<p>Common representations of graphs include "adjacency matrices" and "adjacency lists". The following examples use undirected graphs.</p>
<p>Common representations of graphs include "adjacency matrix" and "adjacency list". The following examples use undirected graphs.</p>
<h3 id="1-adjacency-matrix">1. &nbsp; Adjacency matrix<a class="headerlink" href="#1-adjacency-matrix" title="Permanent link">&para;</a></h3>
<p>Let the number of vertices in the graph be <span class="arithmatex">\(n\)</span>, the <u>adjacency matrix</u> uses an <span class="arithmatex">\(n \times n\)</span> matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(0\)</span> indicating whether there is an edge between two vertices.</p>
<p>As shown in Figure 9-5, let the adjacency matrix be <span class="arithmatex">\(M\)</span>, and the list of vertices be <span class="arithmatex">\(V\)</span>, then the matrix element <span class="arithmatex">\(M[i, j] = 1\)</span> indicates there is an edge between vertex <span class="arithmatex">\(V[i]\)</span> and vertex <span class="arithmatex">\(V[j]\)</span>, conversely <span class="arithmatex">\(M[i, j] = 0\)</span> indicates there is no edge between the two vertices.</p>
@@ -3707,10 +3707,10 @@ G &amp; = \{ V, E \} \newline
<p>Adjacency matrices have the following characteristics.</p>
<ul>
<li>A vertex cannot be connected to itself, so the elements on the main diagonal of the adjacency matrix are meaningless.</li>
<li>For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric about the main diagonal.</li>
<li>By replacing the elements of the adjacency matrix from <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(0\)</span> to weights, it can represent weighted graphs.</li>
<li>For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric with regard to the main diagonal.</li>
<li>By replacing the elements of the adjacency matrix from <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(0\)</span> to weights, we can represent weighted graphs.</li>
</ul>
<p>When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, thus operations of addition, deletion, lookup, and modification are very efficient, all with a time complexity of <span class="arithmatex">\(O(1)\)</span>. However, the space complexity of the matrix is <span class="arithmatex">\(O(n^2)\)</span>, which consumes more memory.</p>
<p>When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, resulting in efficient operations of addition, deletion, lookup, and modification, all with a time complexity of <span class="arithmatex">\(O(1)\)</span>. However, the space complexity of the matrix is <span class="arithmatex">\(O(n^2)\)</span>, which consumes more memory.</p>
<h3 id="2-adjacency-list">2. &nbsp; Adjacency list<a class="headerlink" href="#2-adjacency-list" title="Permanent link">&para;</a></h3>
<p>The <u>adjacency list</u> uses <span class="arithmatex">\(n\)</span> linked lists to represent the graph, with each linked list node representing a vertex. The <span class="arithmatex">\(i\)</span>-th linked list corresponds to vertex <span class="arithmatex">\(i\)</span> and contains all adjacent vertices (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.</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="Representation of a graph with an adjacency list" class="animation-figure" src="../graph.assets/adjacency_list.png" /></a></p>
@@ -3736,8 +3736,8 @@ G &amp; = \{ V, E \} \newline
<tr>
<td>Social Networks</td>
<td>Users</td>
<td>Friendships</td>
<td>Potential Friend Recommendations</td>
<td>Follow / Followed</td>
<td>Potential Following Recommendations</td>
</tr>
<tr>
<td>Subway Lines</td>