(source, adjList, colors)
| 25 | module.exports = possibleBipartition; |
| 26 | |
| 27 | function 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 | |
| 51 | function createAdjacencyList(edges) { |
| 52 | const list = {}; |
no outgoing calls
no test coverage detected