Skip to content

Commit b24f1f6

Browse files
authored
chore: use internal queue definition in BFS (TheAlgorithms#1228)
1 parent 0529e19 commit b24f1f6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Graphs/BreadthFirstSearch.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Queue from '../Data-Structures/Queue/Queue'
2+
13
/**
24
* Breadth-first search is an algorithm for traversing a graph.
35
*
@@ -12,11 +14,12 @@ export function breadthFirstSearch (graph, startingNode) {
1214
const visited = new Set()
1315

1416
// queue contains the nodes to be explored in the future
15-
const queue = [startingNode]
17+
const queue = new Queue()
18+
queue.enqueue(startingNode)
1619

17-
while (queue.length > 0) {
20+
while (!queue.isEmpty()) {
1821
// start with the queue's first node
19-
const node = queue.shift()
22+
const node = queue.dequeue()
2023

2124
if (!visited.has(node)) {
2225
// mark the node as visited
@@ -25,7 +28,7 @@ export function breadthFirstSearch (graph, startingNode) {
2528

2629
// put all its neighbors into the queue
2730
for (let i = 0; i < neighbors.length; i++) {
28-
queue.push(neighbors[i])
31+
queue.enqueue(neighbors[i])
2932
}
3033
}
3134
}

0 commit comments

Comments
 (0)