2024-04-02 19:00:01 +08:00
---
comments: true
---
# 9.3 Graph traversal
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** .
2024-05-02 01:46:14 +08:00
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 > .
2024-04-02 19:00:01 +08:00
## 9.3.1 Breadth-first search
2024-05-01 07:30:10 +08:00
**Breadth-first search is a near-to-far traversal method, starting from a certain node, always prioritizing the visit to the nearest vertices and expanding outwards layer by layer**. As shown in Figure 9-9, starting from the top left vertex, first traverse all adjacent vertices of that vertex, then traverse all adjacent vertices of the next vertex, and so on, until all vertices have been visited.
2024-04-02 19:00:01 +08:00
![Breadth-first traversal of a graph ](graph_traversal.assets/graph_bfs.png ){ class="animation-figure" }
< p align = "center" > Figure 9-9 Breadth-first traversal of a graph < / p >
### 1. Algorithm implementation
BFS is usually implemented with the help of a queue, as shown in the code below. The queue has a "first in, first out" property, which aligns with the BFS idea of traversing "from near to far".
1. Add the starting vertex `startVet` to the queue and start the loop.
2. In each iteration of the loop, pop the vertex at the front of the queue and record it as visited, then add all adjacent vertices of that vertex to the back of the queue.
3. Repeat step `2.` until all vertices have been visited.
2024-05-04 19:57:03 +08:00
To prevent revisiting vertices, we use a hash set `visited` to record which nodes have been visited.
2024-04-02 19:00:01 +08:00
=== "Python"
```python title="graph_bfs.py"
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
2024-05-06 05:27:10 +08:00
"""Breadth-first traversal"""
# Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
# Vertex traversal sequence
2024-04-02 19:00:01 +08:00
res = []
2024-05-06 05:27:10 +08:00
# Hash set, used to record visited vertices
2024-04-02 19:00:01 +08:00
visited = set[Vertex]([start_vet])
2024-05-06 05:27:10 +08:00
# Queue used to implement BFS
2024-04-02 19:00:01 +08:00
que = deque[Vertex]([start_vet])
2024-05-06 05:27:10 +08:00
# Starting from vertex vet, loop until all vertices are visited
2024-04-02 19:00:01 +08:00
while len(que) > 0:
2024-05-06 05:27:10 +08:00
vet = que.popleft() # Dequeue the vertex at the head of the queue
res.append(vet) # Record visited vertex
# Traverse all adjacent vertices of that vertex
2024-04-02 19:00:01 +08:00
for adj_vet in graph.adj_list[vet]:
if adj_vet in visited:
2024-05-06 05:27:10 +08:00
continue # Skip already visited vertices
que.append(adj_vet) # Only enqueue unvisited vertices
visited.add(adj_vet) # Mark the vertex as visited
# Return the vertex traversal sequence
2024-04-02 19:00:01 +08:00
return res
```
=== "C++"
```cpp title="graph_bfs.cpp"
2024-05-06 14:40:36 +08:00
/* Breadth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
vector< Vertex * > graphBFS(GraphAdjList & graph, Vertex *startVet) {
// Vertex traversal sequence
vector< Vertex * > res;
// Hash set, used to record visited vertices
unordered_set< Vertex * > visited = {startVet};
// Queue used to implement BFS
queue< Vertex * > que;
que.push(startVet);
// Starting from vertex vet, loop until all vertices are visited
while (!que.empty()) {
Vertex *vet = que.front();
que.pop(); // Dequeue the vertex at the head of the queue
res.push_back(vet); // Record visited vertex
// Traverse all adjacent vertices of that vertex
for (auto adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // Skip already visited vertices
que.push(adjVet); // Only enqueue unvisited vertices
visited.emplace(adjVet); // Mark the vertex as visited
}
}
// Return the vertex traversal sequence
return res;
}
2024-04-02 19:00:01 +08:00
```
=== "Java"
```java title="graph_bfs.java"
2024-05-06 05:27:10 +08:00
/* Breadth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
2024-04-02 19:00:01 +08:00
List< Vertex > graphBFS(GraphAdjList graph, Vertex startVet) {
2024-05-06 05:27:10 +08:00
// Vertex traversal sequence
2024-04-02 19:00:01 +08:00
List< Vertex > res = new ArrayList< >();
2024-05-06 05:27:10 +08:00
// Hash set, used to record visited vertices
2024-04-02 19:00:01 +08:00
Set< Vertex > visited = new HashSet< >();
visited.add(startVet);
2024-05-06 05:27:10 +08:00
// Queue used to implement BFS
2024-04-02 19:00:01 +08:00
Queue< Vertex > que = new LinkedList< >();
que.offer(startVet);
2024-05-06 05:27:10 +08:00
// Starting from vertex vet, loop until all vertices are visited
2024-04-02 19:00:01 +08:00
while (!que.isEmpty()) {
2024-05-06 05:27:10 +08:00
Vertex vet = que.poll(); // Dequeue the vertex at the head of the queue
res.add(vet); // Record visited vertex
// Traverse all adjacent vertices of that vertex
2024-04-02 19:00:01 +08:00
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
2024-05-06 05:27:10 +08:00
continue; // Skip already visited vertices
que.offer(adjVet); // Only enqueue unvisited vertices
visited.add(adjVet); // Mark the vertex as visited
2024-04-02 19:00:01 +08:00
}
}
2024-05-06 05:27:10 +08:00
// Return the vertex traversal sequence
2024-04-02 19:00:01 +08:00
return res;
}
```
=== "C#"
```csharp title="graph_bfs.cs"
2024-05-06 05:27:10 +08:00
[class]{graph_bfs}-[func]{GraphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Go"
```go title="graph_bfs.go"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Swift"
```swift title="graph_bfs.swift"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "JS"
```javascript title="graph_bfs.js"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "TS"
```typescript title="graph_bfs.ts"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Dart"
```dart title="graph_bfs.dart"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Rust"
```rust title="graph_bfs.rs"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graph_bfs}
2024-04-02 19:00:01 +08:00
```
=== "C"
```c title="graph_bfs.c"
2024-05-06 05:27:10 +08:00
[class]{Queue}-[func]{}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{isVisited}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Kotlin"
```kotlin title="graph_bfs.kt"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphBFS}
2024-04-02 19:00:01 +08:00
```
=== "Ruby"
```ruby title="graph_bfs.rb"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graph_bfs}
2024-04-02 19:00:01 +08:00
```
=== "Zig"
```zig title="graph_bfs.zig"
[class]{}-[func]{graphBFS}
```
2024-05-01 07:30:10 +08:00
The code is relatively abstract, it is suggested to compare with Figure 9-10 to deepen the understanding.
2024-04-02 19:00:01 +08:00
=== "< 1 > "
![Steps of breadth-first search of a graph ](graph_traversal.assets/graph_bfs_step1.png ){ class="animation-figure" }
=== "< 2 > "
![graph_bfs_step2 ](graph_traversal.assets/graph_bfs_step2.png ){ class="animation-figure" }
=== "< 3 > "
![graph_bfs_step3 ](graph_traversal.assets/graph_bfs_step3.png ){ class="animation-figure" }
=== "< 4 > "
![graph_bfs_step4 ](graph_traversal.assets/graph_bfs_step4.png ){ class="animation-figure" }
=== "< 5 > "
![graph_bfs_step5 ](graph_traversal.assets/graph_bfs_step5.png ){ class="animation-figure" }
=== "< 6 > "
![graph_bfs_step6 ](graph_traversal.assets/graph_bfs_step6.png ){ class="animation-figure" }
=== "< 7 > "
![graph_bfs_step7 ](graph_traversal.assets/graph_bfs_step7.png ){ class="animation-figure" }
=== "< 8 > "
![graph_bfs_step8 ](graph_traversal.assets/graph_bfs_step8.png ){ class="animation-figure" }
=== "< 9 > "
![graph_bfs_step9 ](graph_traversal.assets/graph_bfs_step9.png ){ class="animation-figure" }
=== "< 10 > "
![graph_bfs_step10 ](graph_traversal.assets/graph_bfs_step10.png ){ class="animation-figure" }
=== "< 11 > "
![graph_bfs_step11 ](graph_traversal.assets/graph_bfs_step11.png ){ class="animation-figure" }
< p align = "center" > Figure 9-10 Steps of breadth-first search of a graph < / p >
!!! question "Is the sequence of breadth-first traversal unique?"
2024-05-01 07:30:10 +08:00
Not unique. Breadth-first traversal only requires traversing in a "from near to far" order, **and the traversal order of multiple vertices at the same distance can be arbitrarily shuffled** . For example, in Figure 9-10, the visitation order of vertices $1$ and $3$ can be switched, as can the order of vertices $2$, $4$, and $6$.
2024-04-02 19:00:01 +08:00
### 2. Complexity analysis
**Time complexity**: All vertices will be enqueued and dequeued once, using $O(|V|)$ time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited $2$ times, using $O(2|E|)$ time; overall using $O(|V| + |E|)$ time.
2024-05-04 19:57:03 +08:00
**Space complexity**: The maximum number of vertices in list `res` , hash set `visited` , and queue `que` is $|V|$, using $O(|V|)$ space.
2024-04-02 19:00:01 +08:00
## 9.3.2 Depth-first search
2024-05-01 07:30:10 +08:00
**Depth-first search is a traversal method that prioritizes going as far as possible and then backtracks when no further paths are available**. As shown in Figure 9-11, starting from the top left vertex, visit some adjacent vertex of the current vertex until no further path is available, then return and continue until all vertices are traversed.
2024-04-02 19:00:01 +08:00
![Depth-first traversal of a graph ](graph_traversal.assets/graph_dfs.png ){ class="animation-figure" }
< p align = "center" > Figure 9-11 Depth-first traversal of a graph < / p >
### 1. Algorithm implementation
2024-05-04 19:57:03 +08:00
This "go as far as possible and then return" algorithm paradigm is usually implemented based on recursion. Similar to breadth-first search, in depth-first search, we also need the help of a hash set `visited` to record the visited vertices to avoid revisiting.
2024-04-02 19:00:01 +08:00
=== "Python"
```python title="graph_dfs.py"
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):
2024-05-06 05:27:10 +08:00
"""Depth-first traversal helper function"""
res.append(vet) # Record visited vertex
visited.add(vet) # Mark the vertex as visited
# Traverse all adjacent vertices of that vertex
2024-04-02 19:00:01 +08:00
for adjVet in graph.adj_list[vet]:
if adjVet in visited:
2024-05-06 05:27:10 +08:00
continue # Skip already visited vertices
# Recursively visit adjacent vertices
2024-04-02 19:00:01 +08:00
dfs(graph, visited, res, adjVet)
def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
2024-05-06 05:27:10 +08:00
"""Depth-first traversal"""
# Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
# Vertex traversal sequence
2024-04-02 19:00:01 +08:00
res = []
2024-05-06 05:27:10 +08:00
# Hash set, used to record visited vertices
2024-04-02 19:00:01 +08:00
visited = set[Vertex]()
dfs(graph, visited, res, start_vet)
return res
```
=== "C++"
```cpp title="graph_dfs.cpp"
2024-05-06 14:40:36 +08:00
/* Depth-first traversal helper function */
void dfs(GraphAdjList & graph, unordered_set< Vertex *> &visited, vector<Vertex * > & res, Vertex *vet) {
res.push_back(vet); // Record visited vertex
visited.emplace(vet); // Mark the vertex as visited
// Traverse all adjacent vertices of that vertex
for (Vertex *adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // Skip already visited vertices
// Recursively visit adjacent vertices
dfs(graph, visited, res, adjVet);
}
}
2024-04-02 19:00:01 +08:00
2024-05-06 14:40:36 +08:00
/* Depth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
vector< Vertex * > graphDFS(GraphAdjList & graph, Vertex *startVet) {
// Vertex traversal sequence
vector< Vertex * > res;
// Hash set, used to record visited vertices
unordered_set< Vertex * > visited;
dfs(graph, visited, res, startVet);
return res;
}
2024-04-02 19:00:01 +08:00
```
=== "Java"
```java title="graph_dfs.java"
2024-05-06 05:27:10 +08:00
/* Depth-first traversal helper function */
2024-04-02 19:00:01 +08:00
void dfs(GraphAdjList graph, Set< Vertex > visited, List< Vertex > res, Vertex vet) {
2024-05-06 05:27:10 +08:00
res.add(vet); // Record visited vertex
visited.add(vet); // Mark the vertex as visited
// Traverse all adjacent vertices of that vertex
2024-04-02 19:00:01 +08:00
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
2024-05-06 05:27:10 +08:00
continue; // Skip already visited vertices
// Recursively visit adjacent vertices
2024-04-02 19:00:01 +08:00
dfs(graph, visited, res, adjVet);
}
}
2024-05-06 05:27:10 +08:00
/* Depth-first traversal */
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
2024-04-02 19:00:01 +08:00
List< Vertex > graphDFS(GraphAdjList graph, Vertex startVet) {
2024-05-06 05:27:10 +08:00
// Vertex traversal sequence
2024-04-02 19:00:01 +08:00
List< Vertex > res = new ArrayList< >();
2024-05-06 05:27:10 +08:00
// Hash set, used to record visited vertices
2024-04-02 19:00:01 +08:00
Set< Vertex > visited = new HashSet< >();
dfs(graph, visited, res, startVet);
return res;
}
```
=== "C#"
```csharp title="graph_dfs.cs"
2024-05-06 05:27:10 +08:00
[class]{graph_dfs}-[func]{DFS}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{graph_dfs}-[func]{GraphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Go"
```go title="graph_dfs.go"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Swift"
```swift title="graph_dfs.swift"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "JS"
```javascript title="graph_dfs.js"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "TS"
```typescript title="graph_dfs.ts"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Dart"
```dart title="graph_dfs.dart"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Rust"
```rust title="graph_dfs.rs"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graph_dfs}
2024-04-02 19:00:01 +08:00
```
=== "C"
```c title="graph_dfs.c"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{isVisited}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Kotlin"
```kotlin title="graph_dfs.kt"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
2024-04-02 19:00:01 +08:00
2024-05-06 05:27:10 +08:00
[class]{}-[func]{graphDFS}
2024-04-02 19:00:01 +08:00
```
=== "Ruby"
```ruby title="graph_dfs.rb"
2024-05-06 05:27:10 +08:00
[class]{}-[func]{dfs}
[class]{}-[func]{graph_dfs}
2024-04-02 19:00:01 +08:00
```
=== "Zig"
```zig title="graph_dfs.zig"
[class]{}-[func]{dfs}
[class]{}-[func]{graphDFS}
```
2024-05-01 07:30:10 +08:00
The algorithm process of depth-first search is shown in Figure 9-12.
2024-04-02 19:00:01 +08:00
- **Dashed lines represent downward recursion**, indicating that a new recursive method has been initiated to visit a new vertex.
- **Curved dashed lines represent upward backtracking**, indicating that this recursive method has returned to the position where this method was initiated.
2024-05-01 07:30:10 +08:00
To deepen the understanding, it is suggested to combine Figure 9-12 with the code to simulate (or draw) the entire DFS process in your mind, including when each recursive method is initiated and when it returns.
2024-04-02 19:00:01 +08:00
=== "< 1 > "
![Steps of depth-first search of a graph ](graph_traversal.assets/graph_dfs_step1.png ){ class="animation-figure" }
=== "< 2 > "
![graph_dfs_step2 ](graph_traversal.assets/graph_dfs_step2.png ){ class="animation-figure" }
=== "< 3 > "
![graph_dfs_step3 ](graph_traversal.assets/graph_dfs_step3.png ){ class="animation-figure" }
=== "< 4 > "
![graph_dfs_step4 ](graph_traversal.assets/graph_dfs_step4.png ){ class="animation-figure" }
=== "< 5 > "
![graph_dfs_step5 ](graph_traversal.assets/graph_dfs_step5.png ){ class="animation-figure" }
=== "< 6 > "
![graph_dfs_step6 ](graph_traversal.assets/graph_dfs_step6.png ){ class="animation-figure" }
=== "< 7 > "
![graph_dfs_step7 ](graph_traversal.assets/graph_dfs_step7.png ){ class="animation-figure" }
=== "< 8 > "
![graph_dfs_step8 ](graph_traversal.assets/graph_dfs_step8.png ){ class="animation-figure" }
=== "< 9 > "
![graph_dfs_step9 ](graph_traversal.assets/graph_dfs_step9.png ){ class="animation-figure" }
=== "< 10 > "
![graph_dfs_step10 ](graph_traversal.assets/graph_dfs_step10.png ){ class="animation-figure" }
=== "< 11 > "
![graph_dfs_step11 ](graph_traversal.assets/graph_dfs_step11.png ){ class="animation-figure" }
< p align = "center" > Figure 9-12 Steps of depth-first search of a graph < / p >
!!! question "Is the sequence of depth-first traversal unique?"
Similar to breadth-first traversal, the order of the depth-first traversal sequence is also not unique. Given a certain vertex, exploring in any direction first is possible, that is, the order of adjacent vertices can be arbitrarily shuffled, all being part of depth-first traversal.
2024-05-07 16:35:22 +08:00
Taking tree traversal as an example, "root $\rightarrow$ left $\rightarrow$ right", "left $\rightarrow$ root $\rightarrow$ right", "left $\rightarrow$ right $\rightarrow$ root" correspond to pre-order, in-order, and post-order traversals, respectively. They showcase three types of traversal priorities, yet all three are considered depth-first traversal.
2024-04-02 19:00:01 +08:00
### 2. Complexity analysis
**Time complexity**: All vertices will be visited once, using $O(|V|)$ time; all edges will be visited twice, using $O(2|E|)$ time; overall using $O(|V| + |E|)$ time.
2024-05-04 19:57:03 +08:00
**Space complexity**: The maximum number of vertices in list `res` , hash set `visited` is $|V|$, and the maximum recursion depth is $|V|$, therefore using $O(|V|)$ space.