@cedoor/nfa
Version:
TypeScript implementation of some network flow algorithms.
9 lines • 6.3 kB
JavaScript
/**
* @module @cedoor/nfa
* @version 0.1.1
* @file TypeScript implementation of some network flow algorithms.
* @copyright Omar Desogus 2020
* @license MIT
* @see [Github]{@link https://github.com/cedoor/network-flow-algorithms}
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).nfa={})}(this,function(e){"use strict";var p=(t.prototype.addArc=function(e){if(this.hasArc(e.head))throw Error("Arc with head "+e.head+" already exists");this.arcs.set(e.head,e)},t.prototype.removeArc=function(e){if(!this.hasArc(e))throw Error("Arc with head "+e+" does not exists");this.arcs.delete(e)},t.prototype.getArc=function(e){var t=this.arcs.get(e);if(!t)throw Error("Arc with head "+e+" does not exists");return t},t.prototype.hasArc=function(e){return this.arcs.has(e)},t.prototype.getArcs=function(){return Array.from(this.arcs.values())},t.prototype.size=function(){return this.arcs.size},t);function t(e,t,r){void 0===r&&(r=[]),this.id=e,this.balance=t,this.arcs=new Map(r.map(function(e){return[e.head,e]}))}var g=function(e,t,r,o){void 0===o&&(o=0),this.head=e,this.cost=t,this.capacity=r,this.flow=o},A=(r.prototype.addNode=function(e){if(this.hasNode(e.id))throw Error("Node with id "+e.id+" already exists");this.nodes.set(e.id,e)},r.prototype.removeNode=function(e){if(!this.hasNode(e))throw Error("Node with id "+e+" does not exists");this.nodes.delete(e)},r.prototype.getNode=function(e){var t=this.nodes.get(e);if(!t)throw Error("Node with id "+e+" does not exists");return t},r.prototype.hasNode=function(e){return this.nodes.has(e)},r.prototype.getNodes=function(){return Array.from(this.nodes.values())},r.prototype.size=function(){return this.nodes.size},r.prototype.copy=function(){return new r(this.export())},r.prototype.checkIntegrity=function(){for(var e=0,t=this.getNodes();e<t.length;e++)for(var r=0,o=t[e].getArcs();r<o.length;r++){var n=o[r];if(!this.hasNode(n.head))return!1}return!0},r.prototype.export=function(){for(var e=[],t=0,r=this.getNodes();t<r.length;t++){var o=r[t],n=o.getArcs().map(function(e){return{head:e.head,cost:e.cost,capacity:e.capacity,flow:e.flow}});e.push({id:o.id,balance:o.balance,arcs:n})}return e},r);function r(e){if(this.nodes=new Map,e)for(var t=0,r=e;t<r.length;t++){var o=r[t],n=o.arcs?o.arcs.map(function(e){return new g(e.head,e.cost,e.capacity)}):[];this.addNode(new p(o.id,o.balance,n))}}var h=(o.prototype.enqueue=function(e){this.store[this.end]=e,this.end++},o.prototype.dequeue=function(){if(this.front!==this.end){var e=this.store[this.front];return delete this.store[this.front],this.front++,e}},o.prototype.size=function(){return this.end-this.front},o.prototype.peek=function(){if(0!==this.size())return this.store[this.front]},o);function o(){this.front=0,this.end=0,this.store={}}function N(e,t){for(var r=new Set([t]),o=e.get(t);-1!==o&&!r.has(o);)r.add(o),o=e.get(o);t=Array.from(r);return r.has(o)&&(t=t.slice(t.indexOf(o))),t.reverse()}function u(e){for(var t=e.copy(),r=0,o=e.getNodes();r<o.length;r++)for(var n=o[r],a=0,i=n.getArcs();a<i.length;a++){var s=i[a];0<s.flow&&t.getNode(s.head).addArc(new g(n.id,-s.cost,s.capacity,s.flow));var d=t.getNode(n.id);s.capacity>s.flow?d.getArc(s.head).flow=s.capacity-s.flow:d.removeArc(s.head)}return t}function l(e,t){for(var r=1/0,o=0;o<t.length-1;o++){var n=e.getNode(t[o]).getArc(t[o+1]);n.flow<r&&(r=n.flow)}return r}function v(e,t,r){for(var o=0;o<t.length-1;o++){var n=e.getNode(t[o]),a=n.getArc(t[o+1]),i=e.getNode(a.head);a.flow===r?n.removeArc(i.id):a.flow-=r,i.hasArc(n.id)?i.getArc(n.id).flow+=r:i.addArc(new g(n.id,-a.cost,a.capacity,r))}}function w(e){for(var t=new A,r=0,o=e.getNodes();r<o.length;r++){var n=o[r];t.addNode(new p(n.id,n.balance))}for(var a=0,i=e.getNodes();a<i.length;a++)for(var s=0,d=(n=i[a]).getArcs();s<d.length;s++){var c,h,f=d[s],u=e.getNode(f.head);f.cost<0||Object.is(f.cost,-0)?(c=t.getNode(f.head),h=new g(n.id,-f.cost,f.capacity,f.flow),c.addArc(h)):u.hasArc(n.id)||t.getNode(n.id).addArc(new g(f.head,f.cost,f.capacity))}return t}function d(e,t,r){e instanceof A||(e=new A(e));var o=new Map,n=new h;for(n.enqueue(t),o.set(t,-1);n.size();){var a=n.dequeue(),i=e.getNode(a);if("number"==typeof r&&i.hasArc(r))return o.set(r,a),N(o,r);for(var s=0,d=e.getNode(a).getArcs();s<d.length;s++){var c=d[s];o.has(c.head)||(o.set(c.head,a),n.enqueue(c.head))}}return o}function y(e){e instanceof A||(e=new A(e));for(var t=function(e){var t=e.getNodes(),r=Math.max.apply(Math,t.map(function(e){return e.id})),o=new p(r+1,0),n=new p(r+2,0);e.addNode(o),e.addNode(n);for(var a=0,i=t;a<i.length;a++){var s,d=i[a];0<d.balance?(s=new g(d.id,0,d.balance),o.addArc(s),o.balance+=d.balance):d.balance<0&&(s=new g(n.id,0,-d.balance),d.addArc(s),n.balance+=d.balance),d.balance=0}return[o.id,n.id]}(e),r=t[0],o=t[1],n=u(e),a=0,i=d(n,r,o);i&&Array.isArray(i);){var s=l(n,i);a+=s,v(n,i,s),i=d(n,r,o)}return[w(n),a,r,o]}function b(e,t){e instanceof A||(e=new A(e));for(var r=new Map,o=new Map,n=e.getNodes(),a=0,i=n;a<i.length;a++){var s=i[a];r.set(s.id,1/0)}r.set(t,0);for(var d=0;d<e.size()-1;d++)for(var c=0,h=n;c<h.length;c++)for(var f=0,u=(s=h[c]).getArcs();f<u.length;f++){var p=u[f],g=r.get(s.id);r.get(p.head)>g+p.cost&&(r.set(p.head,g+p.cost),o.set(p.head,s.id))}for(var l=0,v=n;l<v.length;l++)for(var w=0,y=(s=v[l]).getArcs();w<y.length;w++){p=y[w],g=r.get(s.id);if(r.get(p.head)>g+p.cost)return N(o,s.id)}return new Map(Array.from(o).map(function(e){return[e[0],[e[1],r.get(e[0])]]}))}e.Arc=g,e.Graph=A,e.Node=p,e.bellmanFord=b,e.bfs=d,e.cycleCanceling=function(e){e instanceof A||(e=new A(e));for(var t=y(e),r=t[0],e=t[1],o=t[3],n=u(r),a=b(n,o);a&&Array.isArray(a);)a.push(a[0]),v(n,a,l(n,a)),a=b(n,o);for(var i=0,s=0,d=n.getNodes();s<d.length;s++)for(var c=0,h=d[s].getArcs();c<h.length;c++){var f=h[c];f.cost<0&&(i-=f.cost)}return[w(n),e,i]},e.dfs=function(e,t,r){e instanceof A||(e=new A(e));var o=new Map,n=[t];for(o.set(t,-1);n.length;){var a=n.pop(),i=e.getNode(a);if("number"==typeof r&&i.hasArc(r))return o.set(r,a),N(o,r);for(var s=0,d=i.getArcs();s<d.length;s++){var c=d[s];o.has(c.head)||(o.set(c.head,a),n.push(c.head))}}return o},e.edmondsKarp=y,Object.defineProperty(e,"__esModule",{value:!0})});