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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more