diff --git a/Graphs/BreadthFirstShortestPath.js b/Graphs/BreadthFirstShortestPath.js index 39a23c75be..0e29b6440a 100644 --- a/Graphs/BreadthFirstShortestPath.js +++ b/Graphs/BreadthFirstShortestPath.js @@ -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. * @@ -18,11 +19,12 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) { // queue contains the paths to be explored in the future 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 - const path = queue.shift() + const path = queue.dequeue() const node = path[path.length - 1] // 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.push(newPath) + queue.enqueue(newPath) } } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: