mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 18:43:59 +08:00
build
This commit is contained in:
@@ -14,7 +14,7 @@ G & = \{ V, E \} \newline
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
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, **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**.
|
||||
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, **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**.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -22,7 +22,7 @@ If vertices are viewed as nodes and edges as references (pointers) connecting th
|
||||
|
||||
## 9.1.1 Common types of graphs
|
||||
|
||||
Based on whether edges have direction, graphs can be divided into "undirected graphs" and "directed graphs", as shown below.
|
||||
Based on whether edges have direction, graphs can be divided into "undirected graphs" and "directed graphs", as shown in Figure 9-2.
|
||||
|
||||
- In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friendship" in WeChat or QQ.
|
||||
- In directed graphs, edges have directionality, that is, the edges $A \rightarrow B$ and $A \leftarrow B$ are independent of each other, for example, the "follow" and "be followed" relationship on Weibo or TikTok.
|
||||
@@ -31,7 +31,7 @@ Based on whether edges have direction, graphs can be divided into "undirected gr
|
||||
|
||||
<p align="center"> Figure 9-2 Directed and undirected graphs </p>
|
||||
|
||||
Based on whether all vertices are connected, graphs can be divided into "connected graphs" and "disconnected graphs", as shown below.
|
||||
Based on whether all vertices are connected, graphs can be divided into "connected graphs" and "disconnected graphs", as shown in Figure 9-3.
|
||||
|
||||
- For connected graphs, it is possible to reach any other vertex starting from a certain vertex.
|
||||
- For disconnected graphs, there is at least one vertex that cannot be reached from a certain starting vertex.
|
||||
@@ -40,7 +40,7 @@ Based on whether all vertices are connected, graphs can be divided into "connect
|
||||
|
||||
<p align="center"> Figure 9-3 Connected and disconnected graphs </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.
|
||||
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.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -48,8 +48,8 @@ We can also add a "weight" variable to edges, resulting in "weighted graphs" as
|
||||
|
||||
Graph data structures include the following commonly used terms.
|
||||
|
||||
- "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.
|
||||
- "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.
|
||||
- "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.
|
||||
- "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.
|
||||
- "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.
|
||||
|
||||
## 9.1.2 Representation of graphs
|
||||
@@ -60,7 +60,7 @@ Common representations of graphs include "adjacency matrices" and "adjacency lis
|
||||
|
||||
Let the number of vertices in the graph be $n$, the "adjacency matrix" uses an $n \times n$ matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with $1$ or $0$ indicating whether there is an edge between two vertices.
|
||||
|
||||
As shown below, let the adjacency matrix be $M$, and the list of vertices be $V$, then the matrix element $M[i, j] = 1$ indicates there is an edge between vertex $V[i]$ and vertex $V[j]$, conversely $M[i, j] = 0$ indicates there is no edge between the two vertices.
|
||||
As shown in Figure 9-5, let the adjacency matrix be $M$, and the list of vertices be $V$, then the matrix element $M[i, j] = 1$ indicates there is an edge between vertex $V[i]$ and vertex $V[j]$, conversely $M[i, j] = 0$ indicates there is no edge between the two vertices.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -76,7 +76,7 @@ When representing graphs with adjacency matrices, it is possible to directly acc
|
||||
|
||||
### 2. Adjacency list
|
||||
|
||||
The "adjacency list" uses $n$ linked lists to represent the graph, with each linked list node representing a vertex. The $i$-th linked list corresponds to vertex $i$ 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.
|
||||
The "adjacency list" uses $n$ linked lists to represent the graph, with each linked list node representing a vertex. The $i$-th linked list corresponds to vertex $i$ and contains all adjacent vertices (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -84,11 +84,11 @@ The "adjacency list" uses $n$ linked lists to represent the graph, with each lin
|
||||
|
||||
The adjacency list only stores actual edges, and the total number of edges is often much less than $n^2$, 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.
|
||||
|
||||
Observing the above figure, **the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency**. 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 $O(n)$ to $O(\log n)$; the linked list can also be transformed into a hash table, thus reducing the time complexity to $O(1)$.
|
||||
Observing Figure 9-6, **the structure of the adjacency list is very similar to the "chaining" in hash tables, hence we can use similar methods to optimize efficiency**. 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 $O(n)$ to $O(\log n)$; the linked list can also be transformed into a hash table, thus reducing the time complexity to $O(1)$.
|
||||
|
||||
## 9.1.3 Common applications of graphs
|
||||
|
||||
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.
|
||||
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 align="center"> Table 9-1 Common graphs in real life </p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user