You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/docs/chapter_graph/graph_traversal.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ BFS is usually implemented with the help of a queue, as shown in the code below.
18
18
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.
19
19
3. Repeat step `2.` until all vertices have been visited.
20
20
21
-
To prevent revisiting vertices, we use a hash table`visited` to record which nodes have been visited.
21
+
To prevent revisiting vertices, we use a hash set`visited` to record which nodes have been visited.
22
22
23
23
```src
24
24
[file]{graph_bfs}-[class]{}-[func]{graph_bfs}
@@ -67,7 +67,7 @@ The code is relatively abstract, it is suggested to compare with the following f
67
67
68
68
**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.
69
69
70
-
**Space complexity**: The maximum number of vertices in list `res`, hash table`visited`, and queue `que` is $|V|$, using $O(|V|)$ space.
70
+
**Space complexity**: The maximum number of vertices in list `res`, hash set`visited`, and queue `que` is $|V|$, using $O(|V|)$ space.
71
71
72
72
## Depth-first search
73
73
@@ -77,7 +77,7 @@ The code is relatively abstract, it is suggested to compare with the following f
77
77
78
78
### Algorithm implementation
79
79
80
-
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 table`visited` to record the visited vertices to avoid revisiting.
80
+
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.
81
81
82
82
```src
83
83
[file]{graph_dfs}-[class]{}-[func]{graph_dfs}
@@ -133,4 +133,4 @@ To deepen the understanding, it is suggested to combine the following figure wit
133
133
134
134
**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.
135
135
136
-
**Space complexity**: The maximum number of vertices in list `res`, hash table`visited` is $|V|$, and the maximum recursion depth is $|V|$, therefore using $O(|V|)$ space.
136
+
**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.
0 commit comments