fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
This commit is contained in:
@@ -1,56 +1,46 @@
|
||||
---
|
||||
title: Breadth-First Search
|
||||
---
|
||||
## Breadth-First Search
|
||||
# Breadth-First Search
|
||||
|
||||
Let's first define the `Tree` class to be used for the implementation of the Breadth First Search algorithm.
|
||||
|
||||
```python
|
||||
class Tree:
|
||||
def __init__(self, x):
|
||||
self.val = x
|
||||
self.left = None
|
||||
self.right = None
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function bfs(graph, root) {
|
||||
// Distance object returned
|
||||
var nodesLen = {};
|
||||
// Set all distances to infinity
|
||||
for (var i = 0; i < graph.length; i++) {
|
||||
nodesLen[i] = Infinity;
|
||||
}
|
||||
nodesLen[root] = 0; // ...except root node
|
||||
var queue = [root]; // Keep track of nodes to visit
|
||||
var current; // Current node traversing
|
||||
// Keep on going until no more nodes to traverse
|
||||
while (queue.length !== 0) {
|
||||
current = queue.shift();
|
||||
// Get adjacent nodes from current node
|
||||
var curConnected = graph[current]; // Get layer of edges from current
|
||||
var neighborIdx = []; // List of nodes with edges
|
||||
var idx = curConnected.indexOf(1); // Get first edge connection
|
||||
while (idx !== -1) {
|
||||
neighborIdx.push(idx); // Add to list of neighbors
|
||||
idx = curConnected.indexOf(1, idx + 1); // Keep on searching
|
||||
}
|
||||
// Loop through neighbors and get lengths
|
||||
for (var j = 0; j < neighborIdx.length; j++) {
|
||||
// Increment distance for nodes traversed
|
||||
if (nodesLen[neighborIdx[j]] === Infinity) {
|
||||
nodesLen[neighborIdx[j]] = nodesLen[current] + 1;
|
||||
queue.push(neighborIdx[j]); // Add new neighbors to queue
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodesLen;
|
||||
}
|
||||
```
|
||||
|
||||
The breadth first search algorithm moves from one level to another starting from the root of the tree. We will make use of a `queue` for this.
|
||||
|
||||
```python
|
||||
|
||||
def bfs(root_node):
|
||||
queue = [root_node]
|
||||
|
||||
while queue:
|
||||
top_element = queue.pop()
|
||||
print("Node processed: ",top_element)
|
||||
|
||||
if top_element.left:
|
||||
queue.append(top_element.left)
|
||||
|
||||
if top_element.right:
|
||||
queue.append(top_element.right)
|
||||
```
|
||||
|
||||
We can easily modify the above code to print the level of each node as well.
|
||||
|
||||
```python
|
||||
|
||||
def bfs(root_node):
|
||||
queue = [(root_node, 0)]
|
||||
|
||||
while queue:
|
||||
top_element, level = queue.pop()
|
||||
print("Node processed: {} at level {}".format(top_element, level))
|
||||
|
||||
if top_element.left:
|
||||
queue.append((top_element.left, level + 1))
|
||||
|
||||
if top_element.right:
|
||||
queue.append((top_element.right, level + 1))
|
||||
```
|
||||
|
||||
|
||||
| Complexity | Time | Space |
|
||||
| ----- | ------ | ------ |
|
||||
| BFS | n | n |
|
||||
|
||||
</details>
|
Reference in New Issue
Block a user