mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
use set to keep track of visited nodes, corresponding adjustments to doctest
This commit is contained in:
@ -6,17 +6,17 @@ Breadth-first search is an algorithm for traversing a graph. It's discovers all
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Doctests
|
Doctests
|
||||||
> breadthFirstSearch(graph, "C")
|
> Array.from(breadthFirstSearch(graph, "C"))
|
||||||
[ 'C', 'D', 'A', 'B', 'E' ]
|
[ 'C', 'D', 'A', 'B', 'E' ]
|
||||||
> breadthFirstSearch(graph, "A")
|
> Array.from(breadthFirstSearch(graph, "A"))
|
||||||
[ 'A', 'B', 'D', 'E' ]
|
[ 'A', 'B', 'D', 'E' ]
|
||||||
> breadthFirstSearch(graph, "F")
|
> Array.from(breadthFirstSearch(graph, "F"))
|
||||||
[ 'F', 'G' ]
|
[ 'F', 'G' ]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function breadthFirstSearch (graph, startingNode) {
|
function breadthFirstSearch (graph, startingNode) {
|
||||||
// visited keeps track of all nodes visited
|
// visited keeps track of all nodes visited
|
||||||
const visited = []
|
const visited = new Set()
|
||||||
|
|
||||||
// queue contains the nodes to be explored in the future
|
// queue contains the nodes to be explored in the future
|
||||||
const queue = [startingNode]
|
const queue = [startingNode]
|
||||||
@ -25,9 +25,9 @@ function breadthFirstSearch (graph, startingNode) {
|
|||||||
// start with the queue's first node
|
// start with the queue's first node
|
||||||
const node = queue.shift()
|
const node = queue.shift()
|
||||||
|
|
||||||
if (!visited.includes(node)) {
|
if (!visited.has(node)) {
|
||||||
// mark the node as visited
|
// mark the node as visited
|
||||||
visited.push(node)
|
visited.add(node)
|
||||||
const neighbors = graph[node]
|
const neighbors = graph[node]
|
||||||
|
|
||||||
// put all its neighbors into the queue
|
// put all its neighbors into the queue
|
||||||
|
Reference in New Issue
Block a user