Published
Edited
May 18, 2021
1 fork
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
start = colRowToId(5, 2)
Insert cell
Insert cell
breadthFirstSearch = {
let count = 0;
let frontier = [start];
let explored = [start];
while (frontier.length > 0 && count++ < step) {
let current = frontier.shift();
for (let next of neighbors(current)) {
if (explored.indexOf(next) < 0) {
frontier.push(next);
explored.push(next);
}
}
}
return {frontier, explored};
}
Insert cell
Insert cell
function colRowToId(col, row) {
return col + row * cols;
}
Insert cell
function idToCol(id) {
return id % cols;
}
Insert cell
function idToRow(id) {
return Math.floor(id / cols);
}
Insert cell
function inGridRange(col, row) {
return 0 <= col && col < cols
&& 0 <= row && row < rows;
}
Insert cell
function neighbors(id) {
let col = idToCol(id),
row = idToRow(id);
let neighbors = [];
let candidates = [[col+1, row], [col-1, row], [col, row+1], [col, row-1]];
for (let candidate of candidates) {
if (inGridRange(candidate[0], candidate[1])) {
neighbors.push(colRowToId(candidate[0], candidate[1]));
}
}
return neighbors;
}
Insert cell
html`<h2>CSS</h2><style>
.cell { fill: hsl(60, 10%, 90%); stroke: white; stroke-width: 0.02px; }
.explored { fill: hsl(45, 20%, 70%); }
.frontier { fill: hsl(220, 50%, 70%); }
</style>
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {slider} from "@jashkenas/inputs"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more