mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-11 11:07:14 +08:00
deploy
This commit is contained in:
@@ -3665,12 +3665,12 @@ E & = \{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \} \newline
|
||||
G & = \{ V, E \} \newline
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
<p>If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown below, <strong>compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom</strong>.</p>
|
||||
<p>If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown in Figure 9-1, <strong>compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom</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="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 Relationship between linked lists, trees, and graphs </p>
|
||||
|
||||
<h2 id="911-common-types-of-graphs">9.1.1 Common types of graphs<a class="headerlink" href="#911-common-types-of-graphs" title="Permanent link">¶</a></h2>
|
||||
<p>Based on whether edges have direction, graphs can be divided into "undirected graphs" and "directed graphs", as shown below.</p>
|
||||
<p>Based on whether edges have direction, graphs can be divided into "undirected graphs" and "directed graphs", 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>
|
||||
@@ -3678,7 +3678,7 @@ G & = \{ V, E \} \newline
|
||||
<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 Directed and undirected graphs </p>
|
||||
|
||||
<p>Based on whether all vertices are connected, graphs can be divided into "connected graphs" and "disconnected graphs", as shown below.</p>
|
||||
<p>Based on whether all vertices are connected, graphs can be divided into "connected graphs" and "disconnected graphs", 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>
|
||||
@@ -3686,21 +3686,21 @@ G & = \{ V, E \} \newline
|
||||
<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 Connected and disconnected graphs </p>
|
||||
|
||||
<p>We can also add a "weight" variable to edges, resulting in "weighted graphs" as shown below. 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 "weighted graphs" 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><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 Weighted and unweighted graphs </p>
|
||||
|
||||
<p>Graph data structures include the following commonly used terms.</p>
|
||||
<ul>
|
||||
<li>"Adjacency": When there is an edge connecting two vertices, these two vertices are said to be "adjacent". In the above figure, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.</li>
|
||||
<li>"Path": The sequence of edges passed from vertex A to vertex B is called a "path" from A to B. In the above figure, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.</li>
|
||||
<li>"Adjacency": When there is an edge connecting two vertices, these two vertices are said to be "adjacent". In Figure 9-4, the adjacent vertices of vertex 1 are vertices 2, 3, and 5.</li>
|
||||
<li>"Path": The sequence of edges passed from vertex A to vertex B is called a "path" from A to B. In Figure 9-4, the edge sequence 1-5-2-4 is a path from vertex 1 to vertex 4.</li>
|
||||
<li>"Degree": The number of edges a vertex has. For directed graphs, "in-degree" refers to how many edges point to the vertex, and "out-degree" refers to how many edges point out from the vertex.</li>
|
||||
</ul>
|
||||
<h2 id="912-representation-of-graphs">9.1.2 Representation of graphs<a class="headerlink" href="#912-representation-of-graphs" title="Permanent link">¶</a></h2>
|
||||
<p>Common representations of graphs include "adjacency matrices" and "adjacency lists". The following examples use undirected graphs.</p>
|
||||
<h3 id="1-adjacency-matrix">1. Adjacency matrix<a class="headerlink" href="#1-adjacency-matrix" title="Permanent link">¶</a></h3>
|
||||
<p>Let the number of vertices in the graph be <span class="arithmatex">\(n\)</span>, the "adjacency matrix" 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 below, 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>
|
||||
<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>
|
||||
<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="Representation of a graph with an adjacency matrix" class="animation-figure" src="../graph.assets/adjacency_matrix.png" /></a></p>
|
||||
<p align="center"> Figure 9-5 Representation of a graph with an adjacency matrix </p>
|
||||
|
||||
@@ -3712,14 +3712,14 @@ G & = \{ V, E \} \newline
|
||||
</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>
|
||||
<h3 id="2-adjacency-list">2. Adjacency list<a class="headerlink" href="#2-adjacency-list" title="Permanent link">¶</a></h3>
|
||||
<p>The "adjacency list" 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). The Figure 9-6 shows an example of a graph stored using an adjacency list.</p>
|
||||
<p>The "adjacency list" 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>
|
||||
<p align="center"> Figure 9-6 Representation of a graph with an adjacency list </p>
|
||||
|
||||
<p>The adjacency list only stores actual edges, and the total number of edges is often much less than <span class="arithmatex">\(n^2\)</span>, making it more space-efficient. However, finding edges in the adjacency list requires traversing the linked list, so its time efficiency is not as good as that of the adjacency matrix.</p>
|
||||
<p>Observing the above figure, <strong>the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency</strong>. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from <span class="arithmatex">\(O(n)\)</span> to <span class="arithmatex">\(O(\log n)\)</span>; the linked list can also be transformed into a hash table, thus reducing the time complexity to <span class="arithmatex">\(O(1)\)</span>.</p>
|
||||
<p>Observing Figure 9-6, <strong>the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency</strong>. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from <span class="arithmatex">\(O(n)\)</span> to <span class="arithmatex">\(O(\log n)\)</span>; the linked list can also be transformed into a hash table, thus reducing the time complexity to <span class="arithmatex">\(O(1)\)</span>.</p>
|
||||
<h2 id="913-common-applications-of-graphs">9.1.3 Common applications of graphs<a class="headerlink" href="#913-common-applications-of-graphs" title="Permanent link">¶</a></h2>
|
||||
<p>As shown in the Table 9-1 , many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.</p>
|
||||
<p>As shown in Table 9-1, many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.</p>
|
||||
<p align="center"> Table 9-1 Common graphs in real life </p>
|
||||
|
||||
<div class="center-table">
|
||||
|
||||
Reference in New Issue
Block a user