en/docs/chapter_graph/graph_operations.md
Basic operations on graphs can be divided into operations on "edges" and operations on "vertices". Under the two representation methods of "adjacency matrix" and "adjacency list", the implementation methods differ.
Given an undirected graph with $n$ vertices, the various operations are implemented as shown in the figure below.
vertices of length $n$, using $O(n)$ time; initialize an adjacency matrix adjMat of size $n \times n$, using $O(n^2)$ time.=== "Initialize adjacency matrix"
=== "Add an edge"
=== "Remove an edge"
=== "Add a vertex"
=== "Remove a vertex"
The following is the implementation code for graphs represented using an adjacency matrix:
[file]{graph_adjacency_matrix}-[class]{graph_adj_mat}-[func]{}
Given an undirected graph with a total of $n$ vertices and $m$ edges, the various operations can be implemented as shown in the figure below.
=== "Initialize adjacency list"
=== "Add an edge"
=== "Remove an edge"
=== "Add a vertex"
=== "Remove a vertex"
The following is the adjacency list code implementation. Compared to the figure above, the actual code has the following differences.
key is the vertex instance and value is the list (linked list) of adjacent vertices for that vertex.Additionally, we use the Vertex class to represent vertices in the adjacency list. The reason for this is: if we used list indices to distinguish different vertices as with adjacency matrices, then to delete the vertex at index $i$, we would need to traverse the entire adjacency list and decrement all indices greater than $i$ by $1$, which is very inefficient. However, if each vertex is a unique Vertex instance, deleting a vertex does not require modifying other vertices.
[file]{graph_adjacency_list}-[class]{graph_adj_list}-[func]{}
Assuming the graph has $n$ vertices and $m$ edges, the table below compares the time efficiency and space efficiency of adjacency matrices and adjacency lists. Note that the adjacency list (linked list) corresponds to the implementation in this text, while the adjacency list (hash table) refers specifically to the implementation where all linked lists are replaced with hash tables.
<p align="center"> Table <id> Comparison of adjacency matrix and adjacency list </p>| Adjacency matrix | Adjacency list (linked list) | Adjacency list (hash table) | |
|---|---|---|---|
| Determine adjacency | $O(1)$ | $O(n)$ | $O(1)$ |
| Add an edge | $O(1)$ | $O(1)$ | $O(1)$ |
| Remove an edge | $O(1)$ | $O(n)$ | $O(1)$ |
| Add a vertex | $O(n)$ | $O(1)$ | $O(1)$ |
| Remove a vertex | $O(n^2)$ | $O(n + m)$ | $O(n)$ |
| Memory space usage | $O(n^2)$ | $O(n + m)$ | $O(n + m)$ |
Observing the table above, it appears that the adjacency list (hash table) has the best time efficiency and space efficiency. However, in practice, operating on edges in the adjacency matrix is more efficient, requiring only a single array access or assignment operation. Overall, adjacency matrices embody the principle of "trading space for time", while adjacency lists embody "trading time for space".