mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
chore: use internal queue definition in BFS Shortest Path (#1230)
This commit is contained in:
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user