Skip to content

Commit 945657a

Browse files
authored
chore: use internal queue definition in BFS Shortest Path (TheAlgorithms#1230)
1 parent b24f1f6 commit 945657a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Graphs/BreadthFirstShortestPath.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Queue from '../Data-Structures/Queue/Queue'
12
/**
23
* Breadth-first approach can be applied to determine the shortest path between two nodes in an equi-weighted graph.
34
*
@@ -18,11 +19,12 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
1819

1920
// queue contains the paths to be explored in the future
2021
const initialPath = [startNode]
21-
const queue = [initialPath]
22+
const queue = new Queue()
23+
queue.enqueue(initialPath)
2224

23-
while (queue.length > 0) {
25+
while (!queue.isEmpty()) {
2426
// start with the queue's first path
25-
const path = queue.shift()
27+
const path = queue.dequeue()
2628
const node = path[path.length - 1]
2729

2830
// explore this node if it hasn't been visited yet
@@ -42,7 +44,7 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
4244
}
4345

4446
// queue the new path
45-
queue.push(newPath)
47+
queue.enqueue(newPath)
4648
}
4749
}
4850
}

0 commit comments

Comments
 (0)