Skip to content

Commit a6be0ff

Browse files
authored
Update graph_traversal.md (krahets#1334)
The implementation uses hash set to store all visited vertices instead of hash table. Change the description text from "hash table" to "hash set"
1 parent ee67d3e commit a6be0ff

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

en/docs/chapter_graph/graph_traversal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BFS is usually implemented with the help of a queue, as shown in the code below.
1818
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.
1919
3. Repeat step `2.` until all vertices have been visited.
2020

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.
2222

2323
```src
2424
[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
6767

6868
**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.
6969

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.
7171

7272
## Depth-first search
7373

@@ -77,7 +77,7 @@ The code is relatively abstract, it is suggested to compare with the following f
7777

7878
### Algorithm implementation
7979

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.
8181

8282
```src
8383
[file]{graph_dfs}-[class]{}-[func]{graph_dfs}
@@ -133,4 +133,4 @@ To deepen the understanding, it is suggested to combine the following figure wit
133133

134134
**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.
135135

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

Comments
 (0)