From 945657a98f9efb951a76c74276257726dbc8a4bf Mon Sep 17 00:00:00 2001 From: Eddie Nuno Date: Thu, 27 Oct 2022 06:15:33 -0700 Subject: [PATCH] chore: use internal queue definition in BFS Shortest Path (#1230) --- Graphs/BreadthFirstShortestPath.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Graphs/BreadthFirstShortestPath.js b/Graphs/BreadthFirstShortestPath.js index 39a23c75b..0e29b6440 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) } } }