chore(i8n,learn): processed translations
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
15047f2d90
commit
e5c44a3ae5
@ -1,22 +1,38 @@
|
||||
---
|
||||
id: 587d825c367417b2b2512c90
|
||||
title: 广度优先搜索
|
||||
title: Breadth-First Search
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301622
|
||||
dashedName: breadth-first-search
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
到目前为止,我们已经学会了创建图表表示的不同方法。现在怎么办?一个自然的问题是图中任何两个节点之间的距离是多少?输入<dfn>图遍历算法</dfn> 。 <dfn>遍历算法</dfn>是遍历或访问图中节点的算法。一种遍历算法是广度优先搜索算法。该算法从一个节点开始,首先访问一个边缘的所有邻居,然后继续访问它们的每个邻居。在视觉上,这就是算法正在做的事情。 要实现此算法,您需要输入图形结构和要启动的节点。首先,您需要了解距起始节点的距离。这个你想要开始你所有的距离最初一些大的数字,如`Infinity` 。这为从起始节点无法访问节点的情况提供了参考。接下来,您将要从开始节点转到其邻居。这些邻居是一个边缘,此时你应该添加一个距离单位到你要跟踪的距离。最后,有助于实现广度优先搜索算法的重要数据结构是队列。这是一个数组,您可以在其中添加元素到一端并从另一端删除元素。这也称为<dfn>FIFO</dfn>或<dfn>先进先出</dfn>数据结构。
|
||||
So far, we've learned different ways of creating representations of graphs. What now? One natural question to have is what are the distances between any two nodes in the graph? Enter <dfn>graph traversal algorithms</dfn>.
|
||||
|
||||
<dfn>Traversal algorithms</dfn> are algorithms to traverse or visit nodes in a graph. One type of traversal algorithm is the breadth-first search algorithm.
|
||||
|
||||
This algorithm starts at one node and visits all its neighbors that are one edge away. It then goes on to visit each of their neighbors and so on until all nodes have been reached.
|
||||
|
||||
An important data structure that will help implement the breadth-first search algorithm is the queue. This is an array where you can add elements to one end and remove elements from the other end. This is also known as a <dfn>FIFO</dfn> or <dfn>First-In-First-Out</dfn> data structure.
|
||||
|
||||
Visually, this is what the algorithm is doing. 
|
||||
|
||||
The grey shading represents a node getting added into the queue and the black shading represents a node getting removed from the queue. See how every time a node gets removed from the queue (node turns black), all their neighbors get added into the queue (node turns grey).
|
||||
|
||||
To implement this algorithm, you'll need to input a graph structure and a node you want to start at.
|
||||
|
||||
First, you'll want to be aware of the distances from, or number of edges away from, the start node. You'll want to start all your distances with some large number, like `Infinity`. This prevents counting issues for when a node may not be reachable from your start node. Next, you'll want to go from the start node to its neighbors. These neighbors are one edge away and at this point you should add one unit of distance to the distances you're keeping track of.
|
||||
|
||||
# --instructions--
|
||||
|
||||
编写一个函数`bfs()` ,它将邻接矩阵图(二维数组)和节点标签根作为参数。节点标签只是`0`到`n - 1`之间节点的整数值,其中`n`是图中节点的总数。您的函数将输出JavaScript对象键值对与节点及其与根的距离。如果无法到达节点,则其距离应为`Infinity` 。
|
||||
Write a function `bfs()` that takes an adjacency matrix graph (a two-dimensional array) and a node label root as parameters. The node label will just be the integer value of the node between `0` and `n - 1`, where `n` is the total number of nodes in the graph.
|
||||
|
||||
Your function will output a JavaScript object key-value pairs with the node and its distance from the root. If the node could not be reached, it should have a distance of `Infinity`.
|
||||
|
||||
# --hints--
|
||||
|
||||
输入图`[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` ,起始节点为`1`应该返回`{0: 1, 1: 0, 2: 1, 3: 2}`
|
||||
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `1` should return `{0: 1, 1: 0, 2: 1, 3: 2}`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -33,7 +49,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
输入图`[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]` ,起始节点为`1`应该返回`{0: 1, 1: 0, 2: 1, 3: Infinity}`
|
||||
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]` with a start node of `1` should return `{0: 1, 1: 0, 2: 1, 3: Infinity}`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -50,7 +66,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
输入图`[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` ,起始节点为`0`应该返回`{0: 0, 1: 1, 2: 2, 3: 3}`
|
||||
The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `0` should return `{0: 0, 1: 1, 2: 2, 3: 3}`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -67,7 +83,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
起始节点为`0`的输入图`[[0, 1], [1, 0]]`应返回`{0: 0, 1: 1}`
|
||||
The input graph `[[0, 1], [1, 0]]` with a start node of `0` should return `{0: 0, 1: 1}`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
Reference in New Issue
Block a user