|
| 1 | +## Graph Traversal (DFS and BFS). |
| 2 | + |
| 3 | +## DFS |
| 4 | + |
| 5 | +Can be implemented with a stack to remember where it should go when it reaches a dead end. |
| 6 | + |
| 7 | +DFS 可以使用 Stack(栈) 来实现。栈会记录之前经过的位置,以便搜索遇到 Dead End(死路) 时,知道应该返回哪里继续搜索。 |
| 8 | + |
| 9 | +DFS goes far way from the starting point as quickly as possible and returns only if it reaches a dead end. |
| 10 | + |
| 11 | +DFS 会从起点出发,沿着一条路径尽可能不断深入;只有当这条路径无法继续时,才会返回之前的节点。 |
| 12 | + |
| 13 | +Used in simulations of games |
| 14 | + |
| 15 | +DFS 可以用于 Game Simulation(游戏模拟),例如搜索游戏中的不同操作路径或预测后续可能出现的局面。 |
| 16 | + |
| 17 | +### Rules |
| 18 | + |
| 19 | +There are 3 rules |
| 20 | + |
| 21 | +-- start with a vertex |
| 22 | + |
| 23 | +R1 a. go to any vertex adjacent to it that hasn’t yet been visited |
| 24 | + |
| 25 | +b. push it on a stack and mark it |
| 26 | + |
| 27 | +R2 if can’t follow R1, then possible pop up a vertex off the stack |
| 28 | + |
| 29 | +R3 if can’t follow R1 and R2, you’re done |
| 30 | + |
| 31 | +翻译: |
| 32 | + |
| 33 | +有 3 条规则 |
| 34 | + |
| 35 | +-- 从一个顶点开始 |
| 36 | + |
| 37 | +R1 a. 前往与该顶点相邻、且尚未被访问的任意顶点 |
| 38 | + |
| 39 | +b. 将它压入栈中并标记 |
| 40 | + |
| 41 | +R2 如果无法执行 R1,那么可以从栈中弹出一个顶点 |
| 42 | + |
| 43 | +R3 如果 R1 和 R2 都无法执行,则搜索完成 |
| 44 | + |
| 45 | +### Steps: |
| 46 | + |
| 47 | +-- start with a vertex |
| 48 | + |
| 49 | +-- visit it |
| 50 | + |
| 51 | +-- push it on a stack and mark it |
| 52 | + |
| 53 | +-- go to any vertex adjacent to it that hasn’t yet been visited |
| 54 | + |
| 55 | +-- if there are no unvisited nodes, pop a vertex off the stack till you come across a vertex that has unvisited adjacent vertices |
| 56 | + |
| 57 | +• 步骤: |
| 58 | + |
| 59 | +-- 从一个顶点开始 |
| 60 | + |
| 61 | +-- 访问它 |
| 62 | + |
| 63 | +-- 将它压入栈中,并标记为已访问 |
| 64 | + |
| 65 | +-- 前往与它相邻、且尚未被访问的任意顶点 |
| 66 | + |
| 67 | +-- 如果当前顶点没有尚未访问的相邻顶点,就从栈中不断弹出顶点,直到找到一个仍然存在未访问相邻顶点的顶点 |
| 68 | + |
| 69 | +### Requirements |
| 70 | + |
| 71 | +Can be used to attempt to visit all nodes of a graph in a systematic manner |
| 72 | + |
| 73 | +Works with directed and undirected graphs |
| 74 | + |
| 75 | +Works with weighted and unweighted graphs |
| 76 | + |
| 77 | +翻译: |
| 78 | + |
| 79 | +• 可以用于以系统化的方式,尝试访问图中的所有节点 |
| 80 | + |
| 81 | +• 适用于 Directed Graph(有向图) 和 Undirected Graph(无向图) |
| 82 | + |
| 83 | +• 适用于 Weighted Graph(带权图) 和 Unweighted Graph(无权图) |
| 84 | + |
| 85 | +## BFS |
| 86 | + |
| 87 | +Implemented with a queue. |
| 88 | + |
| 89 | +Stays as close as possible to the starting point. |
| 90 | + |
| 91 | +Visits all the vertices adjacent to the starting vertex |
| 92 | + |
| 93 | +翻译: |
| 94 | + |
| 95 | +• 使用 Queue(队列) 来实现。 |
| 96 | + |
| 97 | +• 搜索时尽可能靠近起始点。 |
| 98 | + |
| 99 | +• 先访问与起始顶点相邻的所有顶点。 |
| 100 | + |
| 101 | +### Steps: |
| 102 | + |
| 103 | +-- start with visiting a starting vertex |
| 104 | + |
| 105 | +-- visit the next unvisited adjacent vertex, mark it and insert it into the queue. |
| 106 | + |
| 107 | +-- if there are no unvisited vertices, remove a vertex from the queue and make it the current vertex. |
| 108 | + |
| 109 | +-- continue until you reach the end. |
| 110 | + |
| 111 | +翻译: |
| 112 | + |
| 113 | +-- 从访问一个起始顶点开始 |
| 114 | + |
| 115 | +-- 访问下一个尚未访问的相邻顶点,将它标记,并把它插入队列中 |
| 116 | + |
| 117 | +-- 如果没有尚未访问的相邻顶点,就从队列中移出一个顶点,并将它设为当前顶点 |
| 118 | + |
| 119 | +-- 持续执行,直到搜索结束 |
| 120 | + |
| 121 | +### Requirements: |
| 122 | + |
| 123 | +Can be used to attempt to visit all nodes of a graph in a systematic manner |
| 124 | + |
| 125 | +Works with directed and undirected graphs |
| 126 | + |
| 127 | +Works with weighted and unweighted graphs |
| 128 | + |
| 129 | +翻译: |
| 130 | + |
| 131 | +• 可以用于以系统化的方式,尝试访问图中的所有节点 |
| 132 | + |
| 133 | +• 适用于 Directed Graph(有向图) 和 Undirected Graph(无向图) |
| 134 | + |
| 135 | +• 适用于 Weighted Graph(带权图) 和 Unweighted Graph(无权图) |
0 commit comments