From c6fa5ba5ec77edb830b66657bfcfae963ae88d39 Mon Sep 17 00:00:00 2001 From: Archaengel Date: Wed, 26 Oct 2022 22:07:55 -0700 Subject: [PATCH] Use repo's own Queue in BFS Shortest Path --- Graphs/BreadthFirstShortestPath.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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) } } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy