Skip to content
Welcome to contribute to Chinese-to-English translation! For more details, please refer to CONTRIBUTING.md.

# 9.1 Graph

A graph is a nonlinear data structure consisting of vertices and edges. We can abstractly represent a graph \(G\) as 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} V & = \{ 1, 2, 3, 4, 5 \} \newline E & = \{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \} \newline G & = \{ V, E \} \newline \end{aligned} \]

If we view vertices as nodes and edges as references (pointers) connecting them, we can regard a graph as an extension of the linked list data structure. As shown in Figure 9-1, compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) have a higher degree of freedom and are therefore more complex.

Figure 9-1 Relationships among linked lists, trees, and graphs

## 9.1.1 Common Types and Terminology of Graphs

Graphs can be divided into undirected graphs and directed graphs based on whether edges have direction, as shown in Figure 9-2.

- In undirected graphs, edges represent a "bidirectional" connection between two vertices, such as friendships on WeChat or QQ.
- In directed graphs, edges have directionality, meaning edges \(A \rightarrow B\) and \(A \leftarrow B\) are independent of each other, such as following and follower relationships on Weibo or TikTok.

Figure 9-2 Directed and undirected graphs

Graphs can be divided into connected graphs and disconnected graphs based on whether all vertices are connected, as shown in Figure 9-3.

- For connected graphs, starting from any vertex, all other vertices can be reached.
- For disconnected graphs, starting from a certain vertex, at least one vertex cannot be reached.

Figure 9-3 Connected and disconnected graphs

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 "intimacy" between players based on how long they have played together, and such intimacy networks can be represented using weighted graphs.

Figure 9-4 Weighted and unweighted graphs

Graph data structures include the following commonly used terms.

- Adjacency: When two vertices are connected by an edge, 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 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 indicates how many edges point to the vertex, and out-degree indicates how many edges leave the vertex.

## 9.1.2 Representation of Graphs

Common representations of graphs include "adjacency matrices" and "adjacency lists". The following uses undirected graphs as examples.

### 1. Adjacency Matrix

Given a graph with \(n\) vertices, an adjacency matrix uses an \(n \times n\) matrix to represent the graph, where each row (column) represents a vertex, and matrix elements represent edges, using \(1\) or \(0\) to indicate whether an edge exists between two vertices.

As shown in Figure 9-5, let the adjacency matrix be \(M\) and the vertex list be \(V\). Then matrix element \(M[i, j] = 1\) indicates that an edge exists between vertex \(V[i]\) and vertex \(V[j]\), whereas \(M[i, j] = 0\) indicates no edge between the two vertices.

Figure 9-5 Adjacency matrix representation of a graph

Adjacency matrices have the following properties.

- In simple graphs, vertices cannot connect to themselves, so the elements on the main diagonal of the adjacency matrix are meaningless.
- For undirected graphs, edges in both directions are equivalent, so the adjacency matrix is symmetric about the main diagonal.
- Replacing the \(1\) and \(0\) entries in the adjacency matrix with weights allows it to represent weighted graphs.

When using adjacency matrices to represent graphs, we can directly access matrix elements to obtain edges, resulting in highly efficient addition, deletion, lookup, and modification operations, all with a time complexity of \(O(1)\). However, the space complexity of the matrix is \(O(n^2)\), which consumes significant memory.

### 2. Adjacency List

An adjacency list uses \(n\) linked lists to represent a graph, with linked list nodes representing vertices. The \(i\)-th linked list corresponds to vertex \(i\) and stores all adjacent vertices of that vertex (vertices connected to that vertex). Figure 9-6 shows an example of a graph stored using an adjacency list.

Figure 9-6 Adjacency list representation of a graph

Adjacency lists only store edges that actually exist, and the total number of edges is typically much less than \(n^2\), making them more space-efficient. However, finding edges in an adjacency list requires traversing the linked list, so it is less time-efficient than an adjacency matrix.

As shown in Figure 9-6, the structure of adjacency lists is very similar to separate chaining in hash tables, so we can use similar methods to improve efficiency. For example, when a linked list becomes long, it can be converted into an AVL tree or red-black tree, improving the time complexity from \(O(n)\) to \(O(\log n)\); it can also be converted into a hash table, reducing the time complexity to \(O(1)\).

## 9.1.3 Common Applications of Graphs

As shown in Table 9-1, many real-world systems can be modeled using graphs, and corresponding problems can be reduced to graph computation problems.

Table 9-1 Common graphs in real life

| Vertices | Edges | Graph Computation Problem
--- | --- | --- | ---
Social network | Users | Friend relationships | Potential friend recommendation
Subway lines | Stations | Connectivity between stations | Shortest route recommendation
Solar system | Celestial bodies | Gravitational forces between celestial bodies | Planetary orbit calculation

##### Feel free to drop your insights, questions or suggestions