Published
Edited
Dec 8, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function isSubset (a, b) {
if (!a && b) { return true; }
return [...a].every(member => b.has(member));
}
Insert cell
instructionOrder = {
let out = [];
let unvisitedNodes = new Set(nodes);

while (unvisitedNodes.size) {
// Find nodes whose dependencies have all been visited
// From those, pick the one whose value comes earliest in the alphabet
const nextNode = [...unvisitedNodes].filter(
n => isSubset(dependencies[n], new Set(out))).reduce((a, b) => a < b ? a : b);

out.push(nextNode);
unvisitedNodes.delete(nextNode);
}

return out.join('');
}
Insert cell
Insert cell
Insert cell
Insert cell
timeElapsed = {
let completedTasks = [];
let secondsElapsed = 0;
let timers = new Array(numWorkers).fill(0);
let tasks = new Array(numWorkers).fill('');
let remainingTasks = new Set(nodes);

while (true) {
// Decrement worker timers and unload completed tasks
for (let w = 0; w < timers.length; w++) {
timers[w] = Math.max(0, timers[w] - 1);
if (timers[w] === 0 && tasks[w].length) {
completedTasks.push(tasks[w]);
tasks[w] = '';
}
}

// Find available tasks, as before
// Sort them in order of increasing time burden
let availableTasks = [...remainingTasks].filter(
n => isSubset(dependencies[n], new Set(completedTasks))).sort();
// If there's no new work to do and all the workers are idle, return the time elapsed
if (availableTasks.length === 0 && timers.every(t => t === 0)) {
return secondsElapsed;
}

// Assign new tasks to idle workers and adjust their timers
// Remove those tasks from the list of tasks available to claim
for (let w = 0; w < timers.length; w++) {
if (availableTasks.length) {
if (timers[w] === 0) {
tasks[w] = availableTasks.shift();
timers[w] = taskTimeFloor + tasks[w].charCodeAt(0) - 64;
remainingTasks.delete(tasks[w]);
}
}
}
secondsElapsed++;
}
}
Insert cell
Insert cell
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