Skip to content

Instantly share code, notes, and snippets.

@yzarubin
yzarubin / mbp.js
Last active May 11, 2016 05:50
Max Bipartite Match implementation in JavaScript
// Modified Ford-Fulkerson algorithm
function mbp(matrix, isConnected) {
var N = matrix.length;
var M = matrix[0].length;
var visited = new Array(M);
var matches = new Array(M);
var res = 0;
isConnected = isConnected || function(x) {return x;};
function hasMatch(u) {
@yzarubin
yzarubin / binary-index-tree.js
Last active May 10, 2016 02:35
Binary Index Tree in JavaScript
function BinaryIndexTree(size) {
this._tree = new Array(size + 1);
this._size = size;
for (var i = 0; i <= size; ++i) {
this._tree[i] = 0;
}
}
BinaryIndexTree.prototype.update = function(idx, value) {
@yzarubin
yzarubin / X-Error Controller Example.js
Last active August 29, 2015 14:16
Creating robust promise-based controllers with X-Error
var buildCtrl = function(promiseFn){
return function(req, res) {
promiseFn(req)
.then(function(data){
res.json(200, data);
}, function(e){
res.status(e.httpCode || 500)