Files
hello-algo/en/docs/chapter_graph/graph.md
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

5.9 KiB

Graphs

A graph is a nonlinear data structure made up of vertices and edges. We can represent a graph G abstractly using a set of vertices V and a set of edges E. The following example describes a graph with 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 think of vertices as nodes and edges as references (pointers) connecting those nodes, a graph can be viewed as an extension of a linked list. As the figure below shows, network relationships (graphs) allow more freedom than linear relationships (linked lists) or divide-and-conquer relationships (trees), making them more complex.

Relationships between linked lists, trees, and graphs

Common graph types and terminology

Depending on whether their edges have a direction, graphs are classified as undirected graphs or directed graphs, as shown in the figure below.

  • In an undirected graph, an edge represents a two-way connection between two vertices, such as a friendship on WeChat or QQ.
  • In a directed graph, edges have a direction: the edges A \rightarrow B and A \leftarrow B are independent of each other. Examples include following and being followed on Weibo or Douyin.

Directed and undirected graphs

Depending on whether all vertices are connected, graphs are classified as connected graphs or disconnected graphs, as shown in the figure below.

  • In a connected graph, every other vertex can be reached from any starting vertex.
  • In a disconnected graph, at least one vertex cannot be reached from a given starting vertex.

Connected and disconnected graphs

We can also assign a weight to each edge to obtain a weighted graph, as shown in the figure below. For example, mobile games such as Honor of Kings calculate an affinity score between players based on the time they spend playing together. A weighted graph can represent this network of affinities.

Weighted and unweighted graphs

The following terms are commonly used when discussing graphs.

  • Adjacency: Two vertices are adjacent if an edge connects them. In the figure above, the vertices adjacent to vertex 1 are 2, 3, and 5.
  • Path: A sequence of edges followed from vertex A to vertex B is called a path from A to B. In the figure above, the edge sequence 1-5-2-4 forms a path from vertex 1 to vertex 4.
  • Degree: The number of edges incident to a vertex. In a directed graph, the in-degree is the number of edges pointing to the vertex, and the out-degree is the number of edges pointing away from it.

Representing a graph

Two common ways to represent a graph are an adjacency matrix and an adjacency list. We will use undirected graphs in the following examples.

Adjacency matrix

For a graph with n vertices, an adjacency matrix represents the graph using an n \times n matrix. Each row (or column) represents a vertex, and the matrix entries represent edges: 1 or 0 indicates whether an edge exists between two vertices.

As shown in the figure below, let M be the adjacency matrix and V the vertex list. The entry M[i, j] = 1 means that an edge connects vertices V[i] and V[j], whereas M[i, j] = 0 means that no edge connects them.

Representing a graph with an adjacency matrix

An adjacency matrix has the following properties.

  • In a simple graph, a vertex cannot connect to itself, so the entries on the main diagonal do not represent meaningful connections.
  • In an undirected graph, edges in the two directions are equivalent, so the adjacency matrix is symmetric about its main diagonal.
  • Replacing the 1 and 0 entries with weights allows the matrix to represent a weighted graph.

With an adjacency matrix, we can access a matrix entry directly to retrieve an edge. Adding, removing, looking up, and modifying edges are therefore efficient, each taking O(1) time. However, the matrix requires O(n^2) space and uses a substantial amount of memory.

Adjacency list

An adjacency list represents a graph using n linked lists whose nodes represent vertices. The $i$-th linked list corresponds to vertex i and stores all of its adjacent vertices (the vertices connected to it). The figure below shows an example of a graph stored as an adjacency list.

Representing a graph with an adjacency list

An adjacency list stores only edges that actually exist. Since the total number of edges is usually much smaller than n^2, it saves space. However, finding an edge requires traversing a linked list, so lookups are slower than with an adjacency matrix.

As the figure above shows, an adjacency list closely resembles separate chaining in a hash table, so similar techniques can improve its efficiency. For example, a long linked list can be replaced with an AVL tree or a red-black tree to improve lookup time from O(n) to O(\log n). Replacing it with a hash table can further reduce the time complexity to O(1).

Common applications of graphs

As shown in the table below, many real-world systems can be modeled as graphs, and their associated problems can be reduced to graph computation problems.

Table   Common graphs in everyday life

Vertices Edges Graph computation problem
Social network Users Friendships Recommending potential friends
Subway network Stations Connections between stations Recommending the shortest route
Solar system Celestial bodies Gravitational interactions between bodies Calculating planetary orbits