mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 15:59:42 +08:00
build
This commit is contained in:
@@ -4,7 +4,7 @@ comments: true
|
||||
|
||||
# 9.1 Graph
|
||||
|
||||
A "graph" is a type of nonlinear data structure, consisting of "vertices" and "edges". A graph $G$ can be abstractly represented as a collection of a set of vertices $V$ and a set of edges $E$. The following example shows a graph containing 5 vertices and 7 edges.
|
||||
A <u>graph</u> is a type of nonlinear data structure, consisting of <u>vertices</u> and <u>edges</u>. A graph $G$ can be abstractly represented as a collection of a set of vertices $V$ and a set of edges $E$. The following example shows a graph containing 5 vertices and 7 edges.
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
@@ -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 in Figure 9-2.
|
||||
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.
|
||||
|
||||
- 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 in Figure 9-3.
|
||||
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.
|
||||
|
||||
- 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 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.
|
||||
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.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -48,9 +48,9 @@ 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 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.
|
||||
- <u>Adjacency</u>: 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.
|
||||
- <u>Path</u>: 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.
|
||||
- <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.
|
||||
|
||||
## 9.1.2 Representation of graphs
|
||||
|
||||
@@ -58,7 +58,7 @@ Common representations of graphs include "adjacency matrices" and "adjacency lis
|
||||
|
||||
### 1. Adjacency matrix
|
||||
|
||||
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.
|
||||
Let the number of vertices in the graph be $n$, the <u>adjacency matrix</u> 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 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.
|
||||
|
||||
@@ -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). Figure 9-6 shows an example of a graph stored using an adjacency list.
|
||||
The <u>adjacency list</u> 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" }
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ comments: true
|
||||
|
||||
Trees represent a "one-to-many" relationship, while graphs have a higher degree of freedom and can represent any "many-to-many" relationship. Therefore, we can consider trees as a special case of graphs. Clearly, **tree traversal operations are also a special case of graph traversal operations**.
|
||||
|
||||
Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal can be divided into two types: "Breadth-First Search (BFS)" and "Depth-First Search (DFS)".
|
||||
Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal can be divided into two types: <u>Breadth-First Search (BFS)</u> and <u>Depth-First Search (DFS)</u>.
|
||||
|
||||
## 9.3.1 Breadth-first search
|
||||
|
||||
|
||||
Reference in New Issue
Block a user