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
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Graph traversal
2
2
3
-
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**.
3
+
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 tree as a special case of graph. Clearly, **tree traversal operations are also a special case of graph traversal operations**.
4
4
5
5
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>.
6
6
@@ -12,7 +12,7 @@ Both graphs and trees require the application of search algorithms to implement
12
12
13
13
### Algorithm implementation
14
14
15
-
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".
15
+
BFS is usually implemented with the help of a queue, as shown in the code below. The queue is "first in, first out", which aligns with the BFS idea of traversing "from near to far".
16
16
17
17
1. Add the starting vertex `startVet` to the queue and start the loop.
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.
@@ -24,7 +24,7 @@ To prevent revisiting vertices, we use a hash set `visited` to record which node
24
24
[file]{graph_bfs}-[class]{}-[func]{graph_bfs}
25
25
```
26
26
27
-
The code is relatively abstract, it is suggested to compare with the figure below to deepen the understanding.
27
+
The code is relatively abstract, you can compare it with the figure below to get a better understanding.
28
28
29
29
=== "<1>"
30
30

@@ -61,7 +61,7 @@ The code is relatively abstract, it is suggested to compare with the figure belo
61
61
62
62
!!! question "Is the sequence of breadth-first traversal unique?"
63
63
64
-
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 the figure above, the visitation order of vertices $1$ and $3$ can be switched, as can the order of vertices $2$, $4$, and $6$.
64
+
Not unique. Breadth-first traversal only requires traversing in a "near to far" order, **and the traversal order of the vertices with the same distance can be arbitrary**. For example, in the figure above, the visit order of vertices $1$ and $3$ can be swapped, as can the order of vertices $2$, $4$, and $6$.
65
65
66
66
### Complexity analysis
67
67
@@ -71,7 +71,7 @@ The code is relatively abstract, it is suggested to compare with the figure belo
71
71
72
72
## Depth-first search
73
73
74
-
**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 the figure below, 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.
74
+
**Depth-first search is a traversal method that prioritizes going as far as possible and then backtracks when no further path is available**. As shown in the figure below, 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.
75
75
76
76

0 commit comments