chore: use internal queue definition in BFS Shortest Path (#1230)

This commit is contained in:
Eddie Nuno
2022-10-27 06:15:33 -07:00
committed by GitHub
parent b24f1f61cb
commit 945657a98f

View File

@ -1,3 +1,4 @@
import Queue from '../Data-Structures/Queue/Queue'
/** /**
* Breadth-first approach can be applied to determine the shortest path between two nodes in an equi-weighted graph. * Breadth-first approach can be applied to determine the shortest path between two nodes in an equi-weighted graph.
* *
@ -18,11 +19,12 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
// queue contains the paths to be explored in the future // queue contains the paths to be explored in the future
const initialPath = [startNode] const initialPath = [startNode]
const queue = [initialPath] const queue = new Queue()
queue.enqueue(initialPath)
while (queue.length > 0) { while (!queue.isEmpty()) {
// start with the queue's first path // start with the queue's first path
const path = queue.shift() const path = queue.dequeue()
const node = path[path.length - 1] const node = path[path.length - 1]
// explore this node if it hasn't been visited yet // explore this node if it hasn't been visited yet
@ -42,7 +44,7 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
} }
// queue the new path // queue the new path
queue.push(newPath) queue.enqueue(newPath)
} }
} }
} }