MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / bfs

Function bfs

graphs/possible-bipartition.js:27–49  ·  view source on GitHub ↗
(source, adjList, colors)

Source from the content-addressed store, hash-verified

25module.exports = possibleBipartition;
26
27function bfs(source, adjList, colors) {
28 const queue = [source];
29 colors[source - 1] = 0;
30
31 while (queue.length > 0) {
32 const node = queue.shift();
33
34 if (adjList[node] !== undefined) {
35 for (const neighbor of adjList[node]) {
36 // if there is a conflict return false.
37 if (colors[neighbor - 1] === colors[node - 1]) {
38 return false;
39 }
40 if (colors[neighbor - 1] === -1) {
41 colors[neighbor - 1] = 1 - colors[node - 1];
42 queue.unshift(neighbor);
43 }
44 }
45 }
46 }
47
48 return true;
49}
50
51function createAdjacencyList(edges) {
52 const list = {};

Callers 1

possibleBipartitionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected