Skip to content

Instantly share code, notes, and snippets.

@jcready
jcready / PriorityQueue.js
Created February 18, 2018 18:22
A priority queue using a private binary heap
const PriorityQueue = (() => {
const parent = i => ((i + 1) >>> 1) - 1
const left = i => (i << 1) + 1
const right = i => (i + 1) << 1
const privateMap = new WeakMap()
const $ = privateMap.get.bind(privateMap)
const swap = (self, i, j) => {
const h = $(self).heap
@jcready
jcready / parseGraphAndFindNodesWhichCanReachAGivenNode.js
Created February 22, 2018 22:25
Parse stringified graph (‘a->b,b->c, a->c’) and find all the nodes that can reach a given node
export const parseGraph = s => s
.split(',')
.map(edge => edge.trim().split('->'))
.reduce((adjacency, [a, b]) => {
if (!adjacency.has(a)) {
adjacency.set(a, new Set())
}
if (!adjacency.has(b)) {
adjacency.set(b, new Set())
}