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

Commitb24f1f6

Browse files
authored
chore: use internal queue definition in BFS (#1228)
1 parent0529e19 commitb24f1f6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

‎Graphs/BreadthFirstSearch.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importQueuefrom'../Data-Structures/Queue/Queue'
2+
13
/**
24
* Breadth-first search is an algorithm for traversing a graph.
35
*
@@ -12,11 +14,12 @@ export function breadthFirstSearch (graph, startingNode) {
1214
constvisited=newSet()
1315

1416
// queue contains the nodes to be explored in the future
15-
constqueue=[startingNode]
17+
constqueue=newQueue()
18+
queue.enqueue(startingNode)
1619

17-
while(queue.length>0){
20+
while(!queue.isEmpty()){
1821
// start with the queue's first node
19-
constnode=queue.shift()
22+
constnode=queue.dequeue()
2023

2124
if(!visited.has(node)){
2225
// mark the node as visited
@@ -25,7 +28,7 @@ export function breadthFirstSearch (graph, startingNode) {
2528

2629
// put all its neighbors into the queue
2730
for(leti=0;i<neighbors.length;i++){
28-
queue.push(neighbors[i])
31+
queue.enqueue(neighbors[i])
2932
}
3033
}
3134
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp