Skip to content

Commit cd8b9d6

Browse files
committed
merge forked with upstream
2 parents dce5251 + 945657a commit cd8b9d6

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Graphs/BreadthFirstSearch.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Queue from '../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
const visited = new Set()
1315

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

17-
while (queue.length > 0) {
20+
while (!queue.isEmpty()) {
1821
// start with the queue's first node
19-
const node = queue.shift()
22+
const node = 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 (let i = 0; i < neighbors.length; i++) {
28-
queue.push(neighbors[i])
31+
queue.enqueue(neighbors[i])
2932
}
3033
}
3134
}

Graphs/BreadthFirstShortestPath.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Queue from '../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
const initialPath = [startNode]
21-
const queue = [initialPath]
22+
const queue = new Queue()
23+
queue.enqueue(initialPath)
2224

23-
while (queue.length > 0) {
25+
while (!queue.isEmpty()) {
2426
// start with the queue's first path
25-
const path = queue.shift()
27+
const path = queue.dequeue()
2628
const node = 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)