Last active
December 21, 2015 15:03
-
-
Save skhokhlov/3d245bb0fe00f7bcb3df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var fs = require('fs'); | |
/** | |
* Breadth-first search | |
* @param m {Array} | |
* @param s {Number} | |
*/ | |
function BFS(m, s) { | |
let visited = new Array(m.length).fill(false), | |
q = []; | |
q.push(s || 0); | |
while(q.length) { | |
const t = q.pop(); | |
visited[t] = true; | |
for (let i = 0; i < m.length; ++i) { | |
if(m[t][i] == 1) { | |
q.unshift(i); | |
} | |
} | |
} | |
let i = 0; | |
for(; i < m.length && visited[i]; ++i); | |
return i === m.length ? true : false; | |
} | |
fs.readFile('graph.txt', 'utf-8', function (err, data) { | |
if (err) { | |
throw new Error(err); | |
} | |
data = data.split('\n'); | |
let matrix = []; | |
for (let i in data) { | |
if (data[i] != '') { | |
matrix.push(data[i].split(' ')); | |
} | |
} | |
for (let i in matrix) { | |
for (let j in matrix[i]) { | |
matrix[i][j] = Number(matrix[i][j]); | |
} | |
} | |
console.log(BFS(matrix)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment