MCPcopy Index your code
hub / github.com/loiane/javascript-datastructures-algorithms / breadthFirstSearch

Function breadthFirstSearch

src/13-graph/bfs.js:17–39  ·  view source on GitHub ↗
(graph, startVertex, callback)

Source from the content-addressed store, hash-verified

15};
16
17const breadthFirstSearch = (graph, startVertex, callback) => {
18 const vertices = graph.vertices;
19 const adjList = graph.adjList;
20 const color = initializeColor(vertices);
21 const queue = [startVertex];
22
23 while (queue.length) {
24 const visiting = queue.shift();
25 const neighbors = adjList.get(visiting);
26 color[visiting] = Colors.GREY;
27 for (let i = 0; i < neighbors.length; i++) {
28 const neighbor = neighbors[i];
29 if (color[neighbor] === Colors.WHITE) {
30 color[neighbor] = Colors.GREY;
31 queue.push(neighbor);
32 }
33 }
34 color[visiting] = Colors.BLACK;
35 if (callback) {
36 callback(visiting);
37 }
38 }
39};
40
41const bfsShortestPath = (graph, startVertex) => {
42 const vertices = graph.vertices;

Callers 3

02-using-bfs.tsFile · 0.90
graph.test.tsFile · 0.90
02-using-bfs.jsFile · 0.70

Calls 3

initializeColorFunction · 0.70
getMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected