Skip to content

Commit 0bc531f

Browse files
authored
Add files via upload
1 parent 3fe4b73 commit 0bc531f

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Node {
2+
constructor(value, neighbors = []) {
3+
this.value = value;
4+
this.neighbors = neighbors;
5+
}
6+
}
7+
8+
function bfs(start, target) {
9+
const queue = [start];
10+
const visited = new Set();
11+
12+
while (queue.length) {
13+
const node = queue.shift();
14+
if (node.value === target) return true;
15+
if (visited.has(node)) continue;
16+
visited.add(node);
17+
18+
queue.push(...node.neighbors);
19+
}
20+
21+
return false;
22+
}
23+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Problem: Implement a JavaScript function that implements a breadth-first search (BFS) algorithm to traverse a graph.

0 commit comments

Comments
 (0)