Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Graphs/BreadthFirstShortestPath.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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
Expand All @@ -42,7 +44,7 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
}

// queue the new path
queue.push(newPath)
queue.enqueue(newPath)
}
}
}
Expand Down