mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
chore: use internal queue definition in BFS (#1228)
This commit is contained in:
@ -1,3 +1,5 @@
|
|||||||
|
import Queue from '../Data-Structures/Queue/Queue'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Breadth-first search is an algorithm for traversing a graph.
|
* Breadth-first search is an algorithm for traversing a graph.
|
||||||
*
|
*
|
||||||
@ -12,11 +14,12 @@ export function breadthFirstSearch (graph, startingNode) {
|
|||||||
const visited = new Set()
|
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 = new Queue()
|
||||||
|
queue.enqueue(startingNode)
|
||||||
|
|
||||||
while (queue.length > 0) {
|
while (!queue.isEmpty()) {
|
||||||
// start with the queue's first node
|
// start with the queue's first node
|
||||||
const node = queue.shift()
|
const node = queue.dequeue()
|
||||||
|
|
||||||
if (!visited.has(node)) {
|
if (!visited.has(node)) {
|
||||||
// mark the node as visited
|
// mark the node as visited
|
||||||
@ -25,7 +28,7 @@ export function breadthFirstSearch (graph, startingNode) {
|
|||||||
|
|
||||||
// put all its neighbors into the queue
|
// put all its neighbors into the queue
|
||||||
for (let i = 0; i < neighbors.length; i++) {
|
for (let i = 0; i < neighbors.length; i++) {
|
||||||
queue.push(neighbors[i])
|
queue.enqueue(neighbors[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user