Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit945657a

Browse files
authored
chore: use internal queue definition in BFS Shortest Path (#1230)
1 parentb24f1f6 commit945657a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

‎Graphs/BreadthFirstShortestPath.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
importQueuefrom'../Data-Structures/Queue/Queue'
12
/**
23
* Breadth-first approach can be applied to determine the shortest path between two nodes in an equi-weighted graph.
34
*
@@ -18,11 +19,12 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
1819

1920
// queue contains the paths to be explored in the future
2021
constinitialPath=[startNode]
21-
constqueue=[initialPath]
22+
constqueue=newQueue()
23+
queue.enqueue(initialPath)
2224

23-
while(queue.length>0){
25+
while(!queue.isEmpty()){
2426
// start with the queue's first path
25-
constpath=queue.shift()
27+
constpath=queue.dequeue()
2628
constnode=path[path.length-1]
2729

2830
// explore this node if it hasn't been visited yet
@@ -42,7 +44,7 @@ export function breadthFirstShortestPath (graph, startNode, targetNode) {
4244
}
4345

4446
// queue the new path
45-
queue.push(newPath)
47+
queue.enqueue(newPath)
4648
}
4749
}
4850
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp