directed-graph-typed 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +7 -7
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +7 -7
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +7 -7
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +7 -7
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/types/types/data-structures/base/base.d.ts +1 -1
- package/dist/umd/directed-graph-typed.js +7 -7
- package/dist/umd/directed-graph-typed.js.map +1 -1
- package/dist/umd/directed-graph-typed.min.js +1 -1
- package/dist/umd/directed-graph-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +4 -2
- package/src/data-structures/binary-tree/binary-tree.ts +3 -2
- package/src/data-structures/binary-tree/bst.ts +2 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -1
- package/src/data-structures/binary-tree/tree-counter.ts +1 -1
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -1
- package/src/data-structures/graph/abstract-graph.ts +3 -3
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/types/data-structures/base/base.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var directedGraphTyped=(()=>{var T=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var q=(h,o,e)=>o in h?T(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var J=(h,o)=>{for(var e in o)T(h,e,{get:o[e],enumerable:!0})},Y=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of Q(o))!X.call(h,n)&&n!==e&&T(h,n,{get:()=>o[n],enumerable:!(t=W(o,n))||t.enumerable});return h};var Z=h=>Y(T({},"__esModule",{value:!0}),h);var b=(h,o,e)=>q(h,typeof o!="symbol"?o+"":o,e);var $={};J($,{AbstractEdge:()=>v,AbstractGraph:()=>C,AbstractVertex:()=>_,DFSOperation:()=>B,DirectedEdge:()=>D,DirectedGraph:()=>z,DirectedVertex:()=>N,Range:()=>G});var j=function(){return"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g,function(h){let o=Math.random()*16|0;return(h=="x"?o:o&3|8).toString(16)})},I=function(h,o){let e=-1,t=h?h.length:0,n=[];for(;++e<t;){let r=h[e];o(r,e,h)&&(n.push(r),Array.prototype.splice.call(h,e--,1),t--)}return n};function H(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function P(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(H(o))return o;if(typeof o=="object"&&o!==null)return P(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function U(h,o=!1){if(h==null)return!1;if(H(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=P(h);return e==null?!1:H(e)}var A=class{*[Symbol.iterator](...o){yield*this._getIterator(...o)}*entries(){for(let o of this)yield o}*keys(){for(let o of this)yield o[0]}*values(){for(let o of this)yield o[1]}every(o,e){let t=0;for(let n of this)if(!o.call(e,n[0],n[1],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[0],n[1],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[r,s]=n;o.call(e,r,s,t++,this)}}find(o,e){let t=0;for(let n of this){let[r,s]=n;if(o.call(e,r,s,t++,this))return n}}has(o){for(let e of this){let[t]=e;if(t===o)return!0}return!1}hasValue(o){for(let[,e]of this)if(e===o)return!0;return!1}get(o){for(let e of this){let[t,n]=e;if(t===o)return n}}reduce(o,e){let t=e,n=0;for(let r of this){let[s,i]=r;t=o(t,i,s,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var w=class{constructor(o){b(this,"_toElementFn");if(o){let{toElementFn:e}=o;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...o){yield*this._getIterator(...o)}*values(){for(let o of this)yield o}every(o,e){let t=0;for(let n of this)if(e===void 0){if(!o(n,t++,this))return!1}else if(!o.call(e,n,t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return!0}else if(o.call(e,n,t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this)e===void 0?o(n,t++,this):o.call(e,n,t++,this)}find(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return n}else if(o.call(e,n,t++,this))return n}has(o){for(let e of this)if(e===o)return!0;return!1}reduce(o,e){let t=0,n=this[Symbol.iterator](),r;if(arguments.length>=2)r=e;else{let s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");r=s.value,t=1}for(let s of n)r=o(r,s,t++,this);return r}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var L=class h extends w{constructor(e=[],t){super(t);b(this,"_equals",Object.is);b(this,"_elements",[]);b(this,"_DEFAULT_COMPARATOR",(e,t)=>{if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object types, define a custom comparator in options.");return e>t?1:e<t?-1:0});b(this,"_comparator",this._DEFAULT_COMPARATOR);if(t){let{comparator:n}=t;n&&(this._comparator=n)}this.addMany(e)}get elements(){return this._elements}get size(){return this.elements.length}get leaf(){var e;return(e=this.elements[this.size-1])!=null?e:void 0}static from(e,t){return new this(e,t)}static heapify(e,t){return new h(e,t)}add(e){return this._elements.push(e),this._bubbleUp(this.elements.length-1)}addMany(e){let t=[];for(let n of e)if(this.toElementFn){let r=this.add(this.toElementFn(n));t.push(r)}else{let r=this.add(n);t.push(r)}return t}poll(){if(this.elements.length===0)return;let e=this.elements[0],t=this.elements.pop();return this.elements.length&&(this.elements[0]=t,this._sinkDown(0,this.elements.length>>1)),e}peek(){return this.elements[0]}isEmpty(){return this.size===0}clear(){this._elements=[]}refill(e){return this._elements=Array.from(e),this.fix()}has(e){for(let t of this.elements)if(this._equals(t,e))return!0;return!1}delete(e){let t=-1;for(let n=0;n<this.elements.length;n++)if(this._equals(this.elements[n],e)){t=n;break}return t<0?!1:(t===0?this.poll():t===this.elements.length-1?this.elements.pop():(this.elements.splice(t,1,this.elements.pop()),this._bubbleUp(t),this._sinkDown(t,this.elements.length>>1)),!0)}deleteBy(e){let t=-1;for(let n=0;n<this.elements.length;n++)if(e(this.elements[n],n,this)){t=n;break}return t<0?!1:(t===0?this.poll():t===this.elements.length-1?this.elements.pop():(this.elements.splice(t,1,this.elements.pop()),this._bubbleUp(t),this._sinkDown(t,this.elements.length>>1)),!0)}setEquality(e){return this._equals=e,this}dfs(e="PRE"){let t=[],n=r=>{let s=2*r+1,i=s+1;r<this.size&&(e==="IN"?(n(s),t.push(this.elements[r]),n(i)):e==="PRE"?(t.push(this.elements[r]),n(s),n(i)):e==="POST"&&(n(s),n(i),t.push(this.elements[r])))};return n(0),t}fix(){let e=[];for(let t=Math.floor(this.size/2)-1;t>=0;t--)e.push(this._sinkDown(t,this.elements.length>>1));return e}sort(){let e=[],t=this._createInstance();for(let n of this.elements)t.add(n);for(;!t.isEmpty();){let n=t.poll();n!==void 0&&e.push(n)}return e}clone(){let e=this._createInstance();for(let t of this.elements)e.add(t);return e}filter(e,t){let n=this._createInstance(),r=0;for(let s of this)(t===void 0?e(s,r++,this):e.call(t,s,r++,this))?n.add(s):r++;return n}map(e,t,n){let{comparator:r,toElementFn:s,...i}=t!=null?t:{};if(!r)throw new TypeError("Heap.map requires options.comparator for EM");let a=this._createLike([],{...i,comparator:r,toElementFn:s}),l=0;for(let f of this){let c=n===void 0?e(f,l++,this):e.call(n,f,l++,this);a.add(c)}return a}mapSame(e,t){let n=this._createInstance(),r=0;for(let s of this){let i=t===void 0?e(s,r++,this):e.call(t,s,r++,this);n.add(i)}return n}get comparator(){return this._comparator}*_getIterator(){for(let e of this.elements)yield e}_bubbleUp(e){let t=this.elements[e];for(;e>0;){let n=e-1>>1,r=this.elements[n];if(this.comparator(r,t)<=0)break;this.elements[e]=r,e=n}return this.elements[e]=t,!0}_sinkDown(e,t){let n=this.elements[e];for(;e<t;){let r=e<<1|1,s=r+1,i=this.elements[r];if(s<this.elements.length&&this.comparator(i,this.elements[s])>0&&(r=s,i=this.elements[s]),this.comparator(i,n)>=0)break;this.elements[e]=i,e=r}return this.elements[e]=n,!0}_createInstance(e){let t=this.constructor;return new t([],{comparator:this.comparator,toElementFn:this.toElementFn,...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}_spawnLike(e){return this._createLike([],e)}};var F=class h extends w{constructor(e){super(e);b(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let r=this.at(n);if(r!==void 0&&e.call(t,r,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof h?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let r=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let s=0;s<t;s++){let i=this.deleteAt(e);i!==void 0&&r.push(i)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);return r}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let r=this.length-1;r>=0;r--)n=e(n,this.at(r),r,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let r=e;r<t;r++)n.push(this.at(r));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let r=t;r<n;r++)this.setAt(r,e);return this}};var S=class h extends F{constructor(e=[],t){super(t);b(this,"_elements",[]);b(this,"_offset",0);b(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new h(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let r=this._offset+e,s=this._elements.splice(r,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let i=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return i._setAutoCompactRatio(this._autoCompactRatio),i.pushMany(s),i}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let r=0;for(let s of this)e.call(t,s,r,this)&&n.push(s),r++;return n}map(e,t,n){var i,a;let r=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(i=t==null?void 0:t.maxLen)!=null?i:this._maxLen,autoCompactRatio:(a=t==null?void 0:t.autoCompactRatio)!=null?a:this._autoCompactRatio}),s=0;for(let l of this)r.push(n===void 0?e(l,s++,this):e.call(n,l,s++,this));return r}mapSame(e,t){var i;let n=this.constructor,r=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(i=r._setAutoCompactRatio)==null||i.call(r,this._autoCompactRatio);let s=0;for(let a of this){let l=t===void 0?e(a,s++,this):e.call(t,a,s++,this);r.push(l)}return r}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var _=class{constructor(o,e){b(this,"key");b(this,"value");this.key=o,this.value=e}},v=class{constructor(o,e){b(this,"value");b(this,"weight");b(this,"_hashCode");this.weight=o!==void 0?o:1,this.value=e,this._hashCode=j()}get hashCode(){return this._hashCode}},C=class extends A{constructor(e){super();b(this,"_options",{defaultEdgeWeight:1});b(this,"_vertexMap",new Map);let t=e==null?void 0:e.graph;this._options={defaultEdgeWeight:1,...t!=null?t:{}}}get options(){return this._options}get vertexMap(){return this._vertexMap}set vertexMap(e){this._vertexMap=e}get size(){return this._vertexMap.size}getVertex(e){return this._vertexMap.get(e)||void 0}hasVertex(e){return this._vertexMap.has(this._getVertexKey(e))}addVertex(e,t){if(e instanceof _)return this._addVertex(e);{let n=this.createVertex(e,t);return this._addVertex(n)}}isVertexKey(e){let t=typeof e;return t==="string"||t==="number"}removeManyVertices(e){let t=[];for(let n of e)t.push(this.deleteVertex(n));return t.length>0}hasEdge(e,t){return!!this.getEdge(e,t)}addEdge(e,t,n,r){if(e instanceof v)return this._addEdge(e);if(t instanceof _||typeof t=="string"||typeof t=="number"){if(!(this.hasVertex(e)&&this.hasVertex(t)))return!1;e instanceof _&&(e=e.key),t instanceof _&&(t=t.key);let s=this.createEdge(e,t,n,r);return this._addEdge(s)}else throw new Error("dest must be a Vertex or vertex key while srcOrEdge is an Edge")}setEdgeWeight(e,t,n){let r=this.getEdge(e,t);return r?(r.weight=n,!0):!1}getAllPathsBetween(e,t,n=1e3){let r=[],s=this._getVertex(e),i=this._getVertex(t);if(!(s&&i))return[];let a=[];for(a.push({vertex:s,path:[s]});a.length>0;){let{vertex:l,path:f}=a.pop();if(l===i&&(r.push(f),r.length>=n))return r;let c=this.getNeighbors(l);for(let d of c)if(!f.includes(d)){let g=[...f,d];a.push({vertex:d,path:g})}}return r}getPathSumWeight(e){var n;let t=0;for(let r=0;r<e.length;r++)t+=((n=this.getEdge(e[r],e[r+1]))==null?void 0:n.weight)||0;return t}getMinCostBetween(e,t,n){if(n===void 0&&(n=!1),n){let r=this.getAllPathsBetween(e,t),s=Number.MAX_SAFE_INTEGER;for(let i of r)s=Math.min(this.getPathSumWeight(i),s);return s}else{let r=this._getVertex(t),s=this._getVertex(e);if(!(s&&r))return;let i=new Map,a=new S([s]);i.set(s,!0);let l=0;for(;a.length>0;){for(let f=0,c=a.length;f<c;f++){let d=a.shift();if(d===r)return l;if(d!==void 0){let g=this.getNeighbors(d);for(let x of g)i.has(x)||(i.set(x,!0),a.push(x))}}l++}return}}getMinPathBetween(e,t,n,r=!1){var s,i;if(n===void 0&&(n=!1),n)if(r){let a=this.getAllPathsBetween(e,t,1e4),l=Number.MAX_SAFE_INTEGER,f=-1,c=0;for(let d of a){let g=this.getPathSumWeight(d);g<l&&(l=g,f=c),c++}return a[f]||void 0}else return(i=(s=this.dijkstra(e,t,!0,!0))==null?void 0:s.minPath)!=null?i:[];else{let a=[],l=this._getVertex(e),f=this._getVertex(t);if(!(l&&f))return[];let c=(d,g,x,y)=>{if(x.add(d),d===g){a=[l,...y];return}let R=this.getNeighbors(d);for(let k of R)x.has(k)||(y.push(k),c(k,g,x,y),y.pop());x.delete(d)};return c(l,f,new Set,[]),a}}dijkstraWithoutHeap(e,t=void 0,n=!1,r=!1){let s=Number.MAX_SAFE_INTEGER,i,a=[],l=[],f=this._vertexMap,c=new Map,d=new Set,g=new Map,x=this._getVertex(e),y=t?this._getVertex(t):void 0;if(!x)return;for(let E of f){let u=E[1];u instanceof _&&c.set(u,Number.MAX_SAFE_INTEGER)}c.set(x,0),g.set(x,void 0);let R=()=>{let E=Number.MAX_SAFE_INTEGER,u;for(let[p,m]of c)d.has(p)||m<E&&(E=m,u=p);return u},k=E=>{for(let u of f){let p=u[1];if(p instanceof _){let m=[p],V=g.get(p);for(;V;)m.push(V),V=g.get(V);let O=m.reverse();u[1]===E&&(a=O),l.push(O)}}};for(let E=1;E<f.size;E++){let u=R();if(u){if(d.add(u),y&&y===u)return n&&(s=c.get(y)||Number.MAX_SAFE_INTEGER),r&&k(y),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a};let p=this.getNeighbors(u);for(let m of p)if(!d.has(m)){let V=this.getEdge(u,m);if(V){let O=c.get(u),M=c.get(m);O!==void 0&&M!==void 0&&V.weight+O<M&&(c.set(m,V.weight+O),g.set(m,u))}}}}return n&&c.forEach((E,u)=>{u!==x&&E<s&&(s=E,r&&(i=u))}),r&&k(i),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a}}dijkstra(e,t=void 0,n=!1,r=!1){var E;let s=Number.MAX_SAFE_INTEGER,i,a=[],l=[],f=this._vertexMap,c=new Map,d=new Set,g=new Map,x=this._getVertex(e),y=t?this._getVertex(t):void 0;if(!x)return;for(let u of f){let p=u[1];p instanceof _&&c.set(p,Number.MAX_SAFE_INTEGER)}let R=new L([],{comparator:(u,p)=>u.key-p.key});R.add({key:0,value:x}),c.set(x,0),g.set(x,void 0);let k=u=>{for(let p of f){let m=p[1];if(m instanceof _){let V=[m],O=g.get(m);for(;O;)V.push(O),O=g.get(O);let M=V.reverse();p[1]===u&&(a=M),l.push(M)}}};for(;R.size>0;){let u=R.poll(),p=u==null?void 0:u.key,m=u==null?void 0:u.value;if(p!==void 0&&m){if(d.add(m),y&&y===m)return n&&(s=c.get(y)||Number.MAX_SAFE_INTEGER),r&&k(y),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a};let V=this.getNeighbors(m);for(let O of V)if(!d.has(O)){let M=(E=this.getEdge(m,O))==null?void 0:E.weight;if(typeof M=="number"){let K=c.get(O);K!==void 0&&p+M<K&&(R.add({key:p+M,value:O}),g.set(O,m),c.set(O,p+M))}}}}return n&&c.forEach((u,p)=>{p!==x&&u<s&&(s=u,r&&(i=p))}),r&&k(i),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a}}bellmanFord(e,t,n,r){n===void 0&&(n=!1),r===void 0&&(r=!1);let s=this._getVertex(e),i=[],a=new Map,l=new Map,f=Number.MAX_SAFE_INTEGER,c=[],d;if(t&&(d=!1),!s)return{hasNegativeCycle:d,distMap:a,preMap:l,paths:i,min:f,minPath:c};let g=this._vertexMap,x=g.size,y=this.edgeSet(),R=y.length;this._vertexMap.forEach(E=>{a.set(E,Number.MAX_SAFE_INTEGER)}),a.set(s,0);for(let E=1;E<x;++E)for(let u=0;u<R;++u){let p=this.getEndsOfEdge(y[u]);if(p){let[m,V]=p,O=y[u].weight,M=a.get(m),K=a.get(V);M!==void 0&&K!==void 0&&a.get(m)!==Number.MAX_SAFE_INTEGER&&M+O<K&&(a.set(V,M+O),r&&l.set(V,m))}}let k;if(n&&a.forEach((E,u)=>{u!==s&&E<f&&(f=E,r&&(k=u))}),r)for(let E of g){let u=E[1];if(u instanceof _){let p=[u],m=l.get(u);for(;m!==void 0;)p.push(m),m=l.get(m);let V=p.reverse();E[1]===k&&(c=V),i.push(V)}}for(let E=0;E<R;++E){let u=this.getEndsOfEdge(y[E]);if(u){let[p]=u,m=y[E].weight,V=a.get(p);V&&V!==Number.MAX_SAFE_INTEGER&&V+m<V&&(d=!0)}}return{hasNegativeCycle:d,distMap:a,preMap:l,paths:i,min:f,minPath:c}}floydWarshall(){var s;let e=[...this._vertexMap],t=e.length,n=[],r=[];for(let i=0;i<t;i++){n[i]=[],r[i]=[];for(let a=0;a<t;a++)r[i][a]=void 0}for(let i=0;i<t;i++)for(let a=0;a<t;a++)n[i][a]=((s=this.getEdge(e[i][1],e[a][1]))==null?void 0:s.weight)||Number.MAX_SAFE_INTEGER;for(let i=0;i<t;i++)for(let a=0;a<t;a++)for(let l=0;l<t;l++)n[a][l]>n[a][i]+n[i][l]&&(n[a][l]=n[a][i]+n[i][l],r[a][l]=e[i][1]);return{costs:n,predecessor:r}}getCycles(e=!1){let t=[],n=new Set,r=(i,a,l)=>{if(l.has(i)){(!e&&a.length>2||e&&a.length>=2)&&a[0]===i.key&&t.push([...a]);return}l.add(i),a.push(i.key);for(let f of this.getNeighbors(i))f&&r(f,a,l);l.delete(i),a.pop()};for(let i of this.vertexMap.values())r(i,[],n);let s=new Map;for(let i of t){let a=[...i].sort().toString();s.has(a)||s.set(a,i)}return[...s].map(i=>i[1])}filter(e,t){let n=[],r=0;for(let[s,i]of this)e.call(t,s,i,r,this)&&n.push([s,i]),r++;return this._createLike(n,this._snapshotOptions())}filterEntries(e,t){let n=[],r=0;for(let[s,i]of this)e.call(t,s,i,r,this)&&n.push([s,i]),r++;return n}map(e,t){let n=[],r=0;for(let[s,i]of this)n.push(e.call(t,s,i,r,this)),r++;return n}clone(){return this._createLike(void 0,this._snapshotOptions())}*_getIterator(){for(let e of this._vertexMap.values())yield[e.key,e.value]}_snapshotOptions(){return{graph:{...this._options}}}_createInstance(e){let t=this.constructor,n=new t,r=e==null?void 0:e.graph;return r?n._options={...n._options,...r}:n._options={...n._options,...this._options},n}_createLike(e,t){let n=this._createInstance(t);if(e)for(let[s,i]of e)n.addVertex(s,i);else for(let[s,i]of this)n.addVertex(s,i);let r=this.edgeSet();for(let s of r){let i=this.getEndsOfEdge(s);if(!i)continue;let[a,l]=i,f=a.key,c=l.key,d=n.hasVertex?n.hasVertex(f):!1,g=n.hasVertex?n.hasVertex(c):!1;if(d&&g){let x=s.weight,y=s.value,R=n.createEdge(f,c,x,y);n._addEdge(R)}}return n}_addVertex(e){return this.hasVertex(e)?!1:(this._vertexMap.set(e.key,e),!0)}_getVertex(e){let t=this._getVertexKey(e);return this._vertexMap.get(t)||void 0}_getVertexKey(e){return e instanceof _?e.key:e}};var N=class extends _{constructor(o,e){super(o,e)}},D=class extends v{constructor(e,t,n,r){super(n,r);b(this,"src");b(this,"dest");this.src=e,this.dest=t}},z=class h extends C{constructor(e){super(e);b(this,"_outEdgeMap",new Map);b(this,"_inEdgeMap",new Map)}get outEdgeMap(){return this._outEdgeMap}set outEdgeMap(e){this._outEdgeMap=e}get inEdgeMap(){return this._inEdgeMap}set inEdgeMap(e){this._inEdgeMap=e}static fromKeys(e){let t=new h({vertexValueInitializer:n=>n});for(let n of e)t.addVertex(n);return t}static fromEntries(e){let t=new h;for(let[n,r]of e)t.addVertex(n,r);return t}createVertex(e,t){return new N(e,t)}createEdge(e,t,n,r){var s;return new D(e,t,(s=n!=null?n:this.options.defaultEdgeWeight)!=null?s:1,r)}getEdge(e,t){let n=[];if(e!==void 0&&t!==void 0){let r=this._getVertex(e),s=this._getVertex(t);if(r&&s){let i=this._outEdgeMap.get(r);i&&(n=i.filter(a=>a.dest===s.key))}}return n[0]||void 0}deleteEdgeSrcToDest(e,t){let n=this._getVertex(e),r=this._getVertex(t),s;if(!n||!r)return;let i=this._outEdgeMap.get(n);i&&I(i,l=>l.dest===r.key);let a=this._inEdgeMap.get(r);return a&&(s=I(a,l=>l.src===n.key)[0]||void 0),s}deleteEdge(e,t){let n,r,s;if(this.isVertexKey(e))if(this.isVertexKey(t))r=this._getVertex(e),s=this._getVertex(t);else return;else r=this._getVertex(e.src),s=this._getVertex(e.dest);if(r&&s){let i=this._outEdgeMap.get(r);i&&i.length>0&&I(i,l=>l.src===r.key&&l.dest===(s==null?void 0:s.key));let a=this._inEdgeMap.get(s);a&&a.length>0&&(n=I(a,l=>l.src===r.key&&l.dest===s.key)[0])}return n}deleteVertex(e){let t,n;if(this.isVertexKey(e)?(n=this.getVertex(e),t=e):(n=e,t=this._getVertexKey(e)),n){let r=this.getNeighbors(n);for(let s of r)this.deleteEdgeSrcToDest(n,s);this._outEdgeMap.delete(n),this._inEdgeMap.delete(n)}return this._vertexMap.delete(t)}deleteEdgesBetween(e,t){let n=[];if(e&&t){let r=this.deleteEdgeSrcToDest(e,t),s=this.deleteEdgeSrcToDest(t,e);r&&n.push(r),s&&n.push(s)}return n}incomingEdgesOf(e){let t=this._getVertex(e);return t?this.inEdgeMap.get(t)||[]:[]}outgoingEdgesOf(e){let t=this._getVertex(e);return t?this._outEdgeMap.get(t)||[]:[]}degreeOf(e){return this.outDegreeOf(e)+this.inDegreeOf(e)}inDegreeOf(e){return this.incomingEdgesOf(e).length}outDegreeOf(e){return this.outgoingEdgesOf(e).length}edgesOf(e){return[...this.outgoingEdgesOf(e),...this.incomingEdgesOf(e)]}getEdgeSrc(e){return this._getVertex(e.src)}getEdgeDest(e){return this._getVertex(e.dest)}getDestinations(e){if(e===void 0)return[];let t=[],n=this.outgoingEdgesOf(e);for(let r of n){let s=this.getEdgeDest(r);s&&t.push(s)}return t}topologicalSort(e){e=e!=null?e:"key";let t=new Map;for(let i of this.vertexMap)t.set(i[1],0);let n=[],r=!1,s=i=>{t.set(i,1);let a=this.getDestinations(i);for(let l of a){let f=t.get(l);f===0?s(l):f===1&&(r=!0)}t.set(i,2),n.push(i)};for(let i of this.vertexMap)t.get(i[1])===0&&s(i[1]);if(!r)return e==="key"&&(n=n.map(i=>i instanceof N?i.key:i)),n.reverse()}edgeSet(){let e=[];return this._outEdgeMap.forEach(t=>{e=[...e,...t]}),e}getNeighbors(e){let t=[],n=this._getVertex(e);if(n){let r=this.outgoingEdgesOf(n);for(let s of r){let i=this._getVertex(s.dest);i&&t.push(i)}}return t}getEndsOfEdge(e){if(!this.hasEdge(e.src,e.dest))return;let t=this._getVertex(e.src),n=this._getVertex(e.dest);if(t&&n)return[t,n]}isEmpty(){return this.vertexMap.size===0&&this.inEdgeMap.size===0&&this.outEdgeMap.size===0}clear(){this._vertexMap=new Map,this._inEdgeMap=new Map,this._outEdgeMap=new Map}clone(){return super.clone()}tarjan(){let e=new Map,t=new Map,n=new Map,r=0,s=[],i=new Set,a=l=>{e.set(l,r),t.set(l,r),r++,s.push(l),i.add(l);let f=this.getNeighbors(l);for(let c of f)e.has(c)?i.has(c)&&t.set(l,Math.min(t.get(l),e.get(c))):(a(c),t.set(l,Math.min(t.get(l),t.get(c))));if(e.get(l)===t.get(l)){let c=[],d;do d=s.pop(),i.delete(d),c.push(d);while(d!==l);n.set(n.size,c)}};for(let l of this.vertexMap.values())e.has(l)||a(l);return{dfnMap:e,lowMap:t,SCCs:n}}getDFNMap(){return this.tarjan().dfnMap}getLowMap(){return this.tarjan().lowMap}getSCCs(){return this.tarjan().SCCs}_addEdge(e){if(!(this.hasVertex(e.src)&&this.hasVertex(e.dest)))return!1;let t=this._getVertex(e.src),n=this._getVertex(e.dest);if(t&&n){let r=this._outEdgeMap.get(t);r?r.push(e):this._outEdgeMap.set(t,[e]);let s=this._inEdgeMap.get(n);return s?s.push(e):this._inEdgeMap.set(n,[e]),!0}else return!1}};var B=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(B||{}),G=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(U(o)&&U(e)))throw new RangeError("low or high is not comparable");if(o>e)throw new RangeError("low must be less than or equal to high")}isInRange(o,e){let t=this.includeLow?e(o,this.low)>=0:e(o,this.low)>0,n=this.includeHigh?e(o,this.high)<=0:e(o,this.high)<0;return t&&n}};return Z($);})();
|
|
1
|
+
"use strict";var directedGraphTyped=(()=>{var T=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var q=(h,o,e)=>o in h?T(h,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[o]=e;var J=(h,o)=>{for(var e in o)T(h,e,{get:o[e],enumerable:!0})},Y=(h,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of Q(o))!X.call(h,n)&&n!==e&&T(h,n,{get:()=>o[n],enumerable:!(t=W(o,n))||t.enumerable});return h};var Z=h=>Y(T({},"__esModule",{value:!0}),h);var b=(h,o,e)=>q(h,typeof o!="symbol"?o+"":o,e);var $={};J($,{AbstractEdge:()=>v,AbstractGraph:()=>C,AbstractVertex:()=>_,DFSOperation:()=>B,DirectedEdge:()=>D,DirectedGraph:()=>z,DirectedVertex:()=>N,Range:()=>G});var j=function(){return"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g,function(h){let o=Math.random()*16|0;return(h=="x"?o:o&3|8).toString(16)})},I=function(h,o){let e=-1,t=h?h.length:0,n=[];for(;++e<t;){let r=h[e];o(r,e,h)&&(n.push(r),Array.prototype.splice.call(h,e--,1),t--)}return n};function H(h){let o=typeof h;return o==="number"?!0:o==="bigint"||o==="string"||o==="boolean"}function P(h){if(typeof h.valueOf=="function"){let o=h.valueOf();if(o!==h){if(H(o))return o;if(typeof o=="object"&&o!==null)return P(o)}}if(typeof h.toString=="function"){let o=h.toString();if(o!=="[object Object]")return o}return null}function U(h,o=!1){if(h==null)return!1;if(H(h))return!0;if(typeof h!="object")return!1;if(h instanceof Date||o)return!0;let e=P(h);return e==null?!1:H(e)}var A=class{*[Symbol.iterator](...o){yield*this._getIterator(...o)}*entries(){for(let o of this)yield o}*keys(){for(let o of this)yield o[0]}*values(){for(let o of this)yield o[1]}every(o,e){let t=0;for(let n of this)if(!o.call(e,n[1],n[0],t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(o.call(e,n[1],n[0],t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this){let[r,s]=n;o.call(e,s,r,t++,this)}}find(o,e){let t=0;for(let n of this){let[r,s]=n;if(o.call(e,s,r,t++,this))return n}}has(o){for(let e of this){let[t]=e;if(t===o)return!0}return!1}hasValue(o){for(let[,e]of this)if(e===o)return!0;return!1}get(o){for(let e of this){let[t,n]=e;if(t===o)return n}}reduce(o,e){let t=e,n=0;for(let r of this){let[s,i]=r;t=o(t,i,s,n++,this)}return t}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var w=class{constructor(o){b(this,"_toElementFn");if(o){let{toElementFn:e}=o;if(typeof e=="function")this._toElementFn=e;else if(e)throw new TypeError("toElementFn must be a function type")}}get toElementFn(){return this._toElementFn}*[Symbol.iterator](...o){yield*this._getIterator(...o)}*values(){for(let o of this)yield o}every(o,e){let t=0;for(let n of this)if(e===void 0){if(!o(n,t++,this))return!1}else if(!o.call(e,n,t++,this))return!1;return!0}some(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return!0}else if(o.call(e,n,t++,this))return!0;return!1}forEach(o,e){let t=0;for(let n of this)e===void 0?o(n,t++,this):o.call(e,n,t++,this)}find(o,e){let t=0;for(let n of this)if(e===void 0){if(o(n,t++,this))return n}else if(o.call(e,n,t++,this))return n}has(o){for(let e of this)if(e===o)return!0;return!1}reduce(o,e){let t=0,n=this[Symbol.iterator](),r;if(arguments.length>=2)r=e;else{let s=n.next();if(s.done)throw new TypeError("Reduce of empty structure with no initial value");r=s.value,t=1}for(let s of n)r=o(r,s,t++,this);return r}toArray(){return[...this]}toVisual(){return[...this]}print(){console.log(this.toVisual())}};var L=class h extends w{constructor(e=[],t){super(t);b(this,"_equals",Object.is);b(this,"_elements",[]);b(this,"_DEFAULT_COMPARATOR",(e,t)=>{if(typeof e=="object"||typeof t=="object")throw TypeError("When comparing object types, define a custom comparator in options.");return e>t?1:e<t?-1:0});b(this,"_comparator",this._DEFAULT_COMPARATOR);if(t){let{comparator:n}=t;n&&(this._comparator=n)}this.addMany(e)}get elements(){return this._elements}get size(){return this.elements.length}get leaf(){var e;return(e=this.elements[this.size-1])!=null?e:void 0}static from(e,t){return new this(e,t)}static heapify(e,t){return new h(e,t)}add(e){return this._elements.push(e),this._bubbleUp(this.elements.length-1)}addMany(e){let t=[];for(let n of e)if(this.toElementFn){let r=this.add(this.toElementFn(n));t.push(r)}else{let r=this.add(n);t.push(r)}return t}poll(){if(this.elements.length===0)return;let e=this.elements[0],t=this.elements.pop();return this.elements.length&&(this.elements[0]=t,this._sinkDown(0,this.elements.length>>1)),e}peek(){return this.elements[0]}isEmpty(){return this.size===0}clear(){this._elements=[]}refill(e){return this._elements=Array.from(e),this.fix()}has(e){for(let t of this.elements)if(this._equals(t,e))return!0;return!1}delete(e){let t=-1;for(let n=0;n<this.elements.length;n++)if(this._equals(this.elements[n],e)){t=n;break}return t<0?!1:(t===0?this.poll():t===this.elements.length-1?this.elements.pop():(this.elements.splice(t,1,this.elements.pop()),this._bubbleUp(t),this._sinkDown(t,this.elements.length>>1)),!0)}deleteBy(e){let t=-1;for(let n=0;n<this.elements.length;n++)if(e(this.elements[n],n,this)){t=n;break}return t<0?!1:(t===0?this.poll():t===this.elements.length-1?this.elements.pop():(this.elements.splice(t,1,this.elements.pop()),this._bubbleUp(t),this._sinkDown(t,this.elements.length>>1)),!0)}setEquality(e){return this._equals=e,this}dfs(e="PRE"){let t=[],n=r=>{let s=2*r+1,i=s+1;r<this.size&&(e==="IN"?(n(s),t.push(this.elements[r]),n(i)):e==="PRE"?(t.push(this.elements[r]),n(s),n(i)):e==="POST"&&(n(s),n(i),t.push(this.elements[r])))};return n(0),t}fix(){let e=[];for(let t=Math.floor(this.size/2)-1;t>=0;t--)e.push(this._sinkDown(t,this.elements.length>>1));return e}sort(){let e=[],t=this._createInstance();for(let n of this.elements)t.add(n);for(;!t.isEmpty();){let n=t.poll();n!==void 0&&e.push(n)}return e}clone(){let e=this._createInstance();for(let t of this.elements)e.add(t);return e}filter(e,t){let n=this._createInstance(),r=0;for(let s of this)(t===void 0?e(s,r++,this):e.call(t,s,r++,this))?n.add(s):r++;return n}map(e,t,n){let{comparator:r,toElementFn:s,...i}=t!=null?t:{};if(!r)throw new TypeError("Heap.map requires options.comparator for EM");let a=this._createLike([],{...i,comparator:r,toElementFn:s}),l=0;for(let f of this){let c=n===void 0?e(f,l++,this):e.call(n,f,l++,this);a.add(c)}return a}mapSame(e,t){let n=this._createInstance(),r=0;for(let s of this){let i=t===void 0?e(s,r++,this):e.call(t,s,r++,this);n.add(i)}return n}get comparator(){return this._comparator}*_getIterator(){for(let e of this.elements)yield e}_bubbleUp(e){let t=this.elements[e];for(;e>0;){let n=e-1>>1,r=this.elements[n];if(this.comparator(r,t)<=0)break;this.elements[e]=r,e=n}return this.elements[e]=t,!0}_sinkDown(e,t){let n=this.elements[e];for(;e<t;){let r=e<<1|1,s=r+1,i=this.elements[r];if(s<this.elements.length&&this.comparator(i,this.elements[s])>0&&(r=s,i=this.elements[s]),this.comparator(i,n)>=0)break;this.elements[e]=i,e=r}return this.elements[e]=n,!0}_createInstance(e){let t=this.constructor;return new t([],{comparator:this.comparator,toElementFn:this.toElementFn,...e!=null?e:{}})}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}_spawnLike(e){return this._createLike([],e)}};var F=class h extends w{constructor(e){super(e);b(this,"_maxLen",-1);if(e){let{maxLen:t}=e;typeof t=="number"&&t>0&&t%1===0&&(this._maxLen=t)}}get maxLen(){return this._maxLen}indexOf(e,t=0){if(this.length===0)return-1;t<0&&(t=this.length+t),t<0&&(t=0);for(let n=t;n<this.length;n++)if(this.at(n)===e)return n;return-1}lastIndexOf(e,t=this.length-1){if(this.length===0)return-1;t>=this.length&&(t=this.length-1),t<0&&(t=this.length+t);for(let n=t;n>=0;n--)if(this.at(n)===e)return n;return-1}findIndex(e,t){for(let n=0;n<this.length;n++){let r=this.at(n);if(r!==void 0&&e.call(t,r,n,this))return n}return-1}concat(...e){let t=this.clone();for(let n of e)n instanceof h?t.pushMany(n):t.push(n);return t}sort(e){let t=this.toArray();t.sort(e),this.clear();for(let n of t)this.push(n);return this}splice(e,t=0,...n){let r=this._createInstance();e=e<0?this.length+e:e,e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));for(let s=0;s<t;s++){let i=this.deleteAt(e);i!==void 0&&r.push(i)}for(let s=0;s<n.length;s++)this.addAt(e+s,n[s]);return r}join(e=","){return this.toArray().join(e)}toReversedArray(){let e=[];for(let t=this.length-1;t>=0;t--)e.push(this.at(t));return e}reduceRight(e,t){let n=t!=null?t:0;for(let r=this.length-1;r>=0;r--)n=e(n,this.at(r),r,this);return n}slice(e=0,t=this.length){e=e<0?this.length+e:e,t=t<0?this.length+t:t;let n=this._createInstance();for(let r=e;r<t;r++)n.push(this.at(r));return n}fill(e,t=0,n=this.length){if(t=t<0?this.length+t:t,n=n<0?this.length+n:n,t<0&&(t=0),n>this.length&&(n=this.length),t>=n)return this;for(let r=t;r<n;r++)this.setAt(r,e);return this}};var S=class h extends F{constructor(e=[],t){super(t);b(this,"_elements",[]);b(this,"_offset",0);b(this,"_autoCompactRatio",.5);if(t){let{autoCompactRatio:n=.5}=t;this._autoCompactRatio=n}this.pushMany(e)}get elements(){return this._elements}get offset(){return this._offset}get autoCompactRatio(){return this._autoCompactRatio}set autoCompactRatio(e){this._autoCompactRatio=e}get length(){return this.elements.length-this._offset}get first(){return this.length>0?this.elements[this._offset]:void 0}get last(){return this.length>0?this.elements[this.elements.length-1]:void 0}static fromArray(e){return new h(e)}isEmpty(){return this.length===0}push(e){return this.elements.push(e),this._maxLen>0&&this.length>this._maxLen&&this.shift(),!0}pushMany(e){let t=[];for(let n of e)this.toElementFn?t.push(this.push(this.toElementFn(n))):t.push(this.push(n));return t}shift(){if(this.length===0)return;let e=this.first;return this._offset+=1,this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact(),e}delete(e){for(let t=this._offset;t<this.elements.length;t++)if(Object.is(this.elements[t],e))return this.elements.splice(t,1),!0;return!1}at(e){if(!(e<0||e>=this.length))return this._elements[this._offset+e]}deleteAt(e){if(e<0||e>=this.length)return;let t=this._offset+e,[n]=this.elements.splice(t,1);return n}addAt(e,t){return e<0||e>this.length?!1:(this._elements.splice(this._offset+e,0,t),!0)}setAt(e,t){return e<0||e>=this.length?!1:(this._elements[this._offset+e]=t,!0)}reverse(){return this._elements=this.elements.slice(this._offset).reverse(),this._offset=0,this}clear(){this._elements=[],this._offset=0}compact(){return this._elements=this.elements.slice(this._offset),this._offset=0,!0}splice(e,t=0,...n){e=Math.max(0,Math.min(e,this.length)),t=Math.max(0,Math.min(t,this.length-e));let r=this._offset+e,s=this._elements.splice(r,t,...n);this.elements.length>0&&this.offset/this.elements.length>this.autoCompactRatio&&this.compact();let i=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});return i._setAutoCompactRatio(this._autoCompactRatio),i.pushMany(s),i}clone(){let e=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});e._setAutoCompactRatio(this._autoCompactRatio);for(let t=this._offset;t<this.elements.length;t++)e.push(this.elements[t]);return e}filter(e,t){let n=this._createInstance({toElementFn:this.toElementFn,maxLen:this._maxLen});n._setAutoCompactRatio(this._autoCompactRatio);let r=0;for(let s of this)e.call(t,s,r,this)&&n.push(s),r++;return n}map(e,t,n){var i,a;let r=new this.constructor([],{toElementFn:t==null?void 0:t.toElementFn,maxLen:(i=t==null?void 0:t.maxLen)!=null?i:this._maxLen,autoCompactRatio:(a=t==null?void 0:t.autoCompactRatio)!=null?a:this._autoCompactRatio}),s=0;for(let l of this)r.push(n===void 0?e(l,s++,this):e.call(n,l,s++,this));return r}mapSame(e,t){var i;let n=this.constructor,r=new n([],{toElementFn:this.toElementFn,maxLen:this._maxLen,autoCompactRatio:this._autoCompactRatio});(i=r._setAutoCompactRatio)==null||i.call(r,this._autoCompactRatio);let s=0;for(let a of this){let l=t===void 0?e(a,s++,this):e.call(t,a,s++,this);r.push(l)}return r}_setAutoCompactRatio(e){this._autoCompactRatio=e}*_getIterator(){for(let e=this._offset;e<this.elements.length;e++)yield this.elements[e]}*_getReverseIterator(){for(let e=this.length-1;e>=0;e--){let t=this.at(e);t!==void 0&&(yield t)}}_createInstance(e){let t=this.constructor;return new t([],e)}_createLike(e=[],t){let n=this.constructor;return new n(e,t)}};var _=class{constructor(o,e){b(this,"key");b(this,"value");this.key=o,this.value=e}},v=class{constructor(o,e){b(this,"value");b(this,"weight");b(this,"_hashCode");this.weight=o!==void 0?o:1,this.value=e,this._hashCode=j()}get hashCode(){return this._hashCode}},C=class extends A{constructor(e){super();b(this,"_options",{defaultEdgeWeight:1});b(this,"_vertexMap",new Map);let t=e==null?void 0:e.graph;this._options={defaultEdgeWeight:1,...t!=null?t:{}}}get options(){return this._options}get vertexMap(){return this._vertexMap}set vertexMap(e){this._vertexMap=e}get size(){return this._vertexMap.size}getVertex(e){return this._vertexMap.get(e)||void 0}hasVertex(e){return this._vertexMap.has(this._getVertexKey(e))}addVertex(e,t){if(e instanceof _)return this._addVertex(e);{let n=this.createVertex(e,t);return this._addVertex(n)}}isVertexKey(e){let t=typeof e;return t==="string"||t==="number"}removeManyVertices(e){let t=[];for(let n of e)t.push(this.deleteVertex(n));return t.length>0}hasEdge(e,t){return!!this.getEdge(e,t)}addEdge(e,t,n,r){if(e instanceof v)return this._addEdge(e);if(t instanceof _||typeof t=="string"||typeof t=="number"){if(!(this.hasVertex(e)&&this.hasVertex(t)))return!1;e instanceof _&&(e=e.key),t instanceof _&&(t=t.key);let s=this.createEdge(e,t,n,r);return this._addEdge(s)}else throw new Error("dest must be a Vertex or vertex key while srcOrEdge is an Edge")}setEdgeWeight(e,t,n){let r=this.getEdge(e,t);return r?(r.weight=n,!0):!1}getAllPathsBetween(e,t,n=1e3){let r=[],s=this._getVertex(e),i=this._getVertex(t);if(!(s&&i))return[];let a=[];for(a.push({vertex:s,path:[s]});a.length>0;){let{vertex:l,path:f}=a.pop();if(l===i&&(r.push(f),r.length>=n))return r;let c=this.getNeighbors(l);for(let d of c)if(!f.includes(d)){let g=[...f,d];a.push({vertex:d,path:g})}}return r}getPathSumWeight(e){var n;let t=0;for(let r=0;r<e.length;r++)t+=((n=this.getEdge(e[r],e[r+1]))==null?void 0:n.weight)||0;return t}getMinCostBetween(e,t,n){if(n===void 0&&(n=!1),n){let r=this.getAllPathsBetween(e,t),s=Number.MAX_SAFE_INTEGER;for(let i of r)s=Math.min(this.getPathSumWeight(i),s);return s}else{let r=this._getVertex(t),s=this._getVertex(e);if(!(s&&r))return;let i=new Map,a=new S([s]);i.set(s,!0);let l=0;for(;a.length>0;){for(let f=0,c=a.length;f<c;f++){let d=a.shift();if(d===r)return l;if(d!==void 0){let g=this.getNeighbors(d);for(let x of g)i.has(x)||(i.set(x,!0),a.push(x))}}l++}return}}getMinPathBetween(e,t,n,r=!1){var s,i;if(n===void 0&&(n=!1),n)if(r){let a=this.getAllPathsBetween(e,t,1e4),l=Number.MAX_SAFE_INTEGER,f=-1,c=0;for(let d of a){let g=this.getPathSumWeight(d);g<l&&(l=g,f=c),c++}return a[f]||void 0}else return(i=(s=this.dijkstra(e,t,!0,!0))==null?void 0:s.minPath)!=null?i:[];else{let a=[],l=this._getVertex(e),f=this._getVertex(t);if(!(l&&f))return[];let c=(d,g,x,y)=>{if(x.add(d),d===g){a=[l,...y];return}let R=this.getNeighbors(d);for(let k of R)x.has(k)||(y.push(k),c(k,g,x,y),y.pop());x.delete(d)};return c(l,f,new Set,[]),a}}dijkstraWithoutHeap(e,t=void 0,n=!1,r=!1){let s=Number.MAX_SAFE_INTEGER,i,a=[],l=[],f=this._vertexMap,c=new Map,d=new Set,g=new Map,x=this._getVertex(e),y=t?this._getVertex(t):void 0;if(!x)return;for(let E of f){let u=E[1];u instanceof _&&c.set(u,Number.MAX_SAFE_INTEGER)}c.set(x,0),g.set(x,void 0);let R=()=>{let E=Number.MAX_SAFE_INTEGER,u;for(let[p,m]of c)d.has(p)||m<E&&(E=m,u=p);return u},k=E=>{for(let u of f){let p=u[1];if(p instanceof _){let m=[p],V=g.get(p);for(;V;)m.push(V),V=g.get(V);let O=m.reverse();u[1]===E&&(a=O),l.push(O)}}};for(let E=1;E<f.size;E++){let u=R();if(u){if(d.add(u),y&&y===u)return n&&(s=c.get(y)||Number.MAX_SAFE_INTEGER),r&&k(y),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a};let p=this.getNeighbors(u);for(let m of p)if(!d.has(m)){let V=this.getEdge(u,m);if(V){let O=c.get(u),M=c.get(m);O!==void 0&&M!==void 0&&V.weight+O<M&&(c.set(m,V.weight+O),g.set(m,u))}}}}return n&&c.forEach((E,u)=>{u!==x&&E<s&&(s=E,r&&(i=u))}),r&&k(i),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a}}dijkstra(e,t=void 0,n=!1,r=!1){var E;let s=Number.MAX_SAFE_INTEGER,i,a=[],l=[],f=this._vertexMap,c=new Map,d=new Set,g=new Map,x=this._getVertex(e),y=t?this._getVertex(t):void 0;if(!x)return;for(let u of f){let p=u[1];p instanceof _&&c.set(p,Number.MAX_SAFE_INTEGER)}let R=new L([],{comparator:(u,p)=>u.key-p.key});R.add({key:0,value:x}),c.set(x,0),g.set(x,void 0);let k=u=>{for(let p of f){let m=p[1];if(m instanceof _){let V=[m],O=g.get(m);for(;O;)V.push(O),O=g.get(O);let M=V.reverse();p[1]===u&&(a=M),l.push(M)}}};for(;R.size>0;){let u=R.poll(),p=u==null?void 0:u.key,m=u==null?void 0:u.value;if(p!==void 0&&m){if(d.add(m),y&&y===m)return n&&(s=c.get(y)||Number.MAX_SAFE_INTEGER),r&&k(y),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a};let V=this.getNeighbors(m);for(let O of V)if(!d.has(O)){let M=(E=this.getEdge(m,O))==null?void 0:E.weight;if(typeof M=="number"){let K=c.get(O);K!==void 0&&p+M<K&&(R.add({key:p+M,value:O}),g.set(O,m),c.set(O,p+M))}}}}return n&&c.forEach((u,p)=>{p!==x&&u<s&&(s=u,r&&(i=p))}),r&&k(i),{distMap:c,preMap:g,seen:d,paths:l,minDist:s,minPath:a}}bellmanFord(e,t,n,r){n===void 0&&(n=!1),r===void 0&&(r=!1);let s=this._getVertex(e),i=[],a=new Map,l=new Map,f=Number.MAX_SAFE_INTEGER,c=[],d;if(t&&(d=!1),!s)return{hasNegativeCycle:d,distMap:a,preMap:l,paths:i,min:f,minPath:c};let g=this._vertexMap,x=g.size,y=this.edgeSet(),R=y.length;this._vertexMap.forEach(E=>{a.set(E,Number.MAX_SAFE_INTEGER)}),a.set(s,0);for(let E=1;E<x;++E)for(let u=0;u<R;++u){let p=this.getEndsOfEdge(y[u]);if(p){let[m,V]=p,O=y[u].weight,M=a.get(m),K=a.get(V);M!==void 0&&K!==void 0&&a.get(m)!==Number.MAX_SAFE_INTEGER&&M+O<K&&(a.set(V,M+O),r&&l.set(V,m))}}let k;if(n&&a.forEach((E,u)=>{u!==s&&E<f&&(f=E,r&&(k=u))}),r)for(let E of g){let u=E[1];if(u instanceof _){let p=[u],m=l.get(u);for(;m!==void 0;)p.push(m),m=l.get(m);let V=p.reverse();E[1]===k&&(c=V),i.push(V)}}for(let E=0;E<R;++E){let u=this.getEndsOfEdge(y[E]);if(u){let[p]=u,m=y[E].weight,V=a.get(p);V&&V!==Number.MAX_SAFE_INTEGER&&V+m<V&&(d=!0)}}return{hasNegativeCycle:d,distMap:a,preMap:l,paths:i,min:f,minPath:c}}floydWarshall(){var s;let e=[...this._vertexMap],t=e.length,n=[],r=[];for(let i=0;i<t;i++){n[i]=[],r[i]=[];for(let a=0;a<t;a++)r[i][a]=void 0}for(let i=0;i<t;i++)for(let a=0;a<t;a++)n[i][a]=((s=this.getEdge(e[i][1],e[a][1]))==null?void 0:s.weight)||Number.MAX_SAFE_INTEGER;for(let i=0;i<t;i++)for(let a=0;a<t;a++)for(let l=0;l<t;l++)n[a][l]>n[a][i]+n[i][l]&&(n[a][l]=n[a][i]+n[i][l],r[a][l]=e[i][1]);return{costs:n,predecessor:r}}getCycles(e=!1){let t=[],n=new Set,r=(i,a,l)=>{if(l.has(i)){(!e&&a.length>2||e&&a.length>=2)&&a[0]===i.key&&t.push([...a]);return}l.add(i),a.push(i.key);for(let f of this.getNeighbors(i))f&&r(f,a,l);l.delete(i),a.pop()};for(let i of this.vertexMap.values())r(i,[],n);let s=new Map;for(let i of t){let a=[...i].sort().toString();s.has(a)||s.set(a,i)}return[...s].map(i=>i[1])}filter(e,t){let n=[],r=0;for(let[s,i]of this)e.call(t,i,s,r,this)&&n.push([s,i]),r++;return this._createLike(n,this._snapshotOptions())}filterEntries(e,t){let n=[],r=0;for(let[s,i]of this)e.call(t,i,s,r,this)&&n.push([s,i]),r++;return n}map(e,t){let n=[],r=0;for(let[s,i]of this)n.push(e.call(t,i,s,r,this)),r++;return n}clone(){return this._createLike(void 0,this._snapshotOptions())}*_getIterator(){for(let e of this._vertexMap.values())yield[e.key,e.value]}_snapshotOptions(){return{graph:{...this._options}}}_createInstance(e){let t=this.constructor,n=new t,r=e==null?void 0:e.graph;return r?n._options={...n._options,...r}:n._options={...n._options,...this._options},n}_createLike(e,t){let n=this._createInstance(t);if(e)for(let[s,i]of e)n.addVertex(s,i);else for(let[s,i]of this)n.addVertex(s,i);let r=this.edgeSet();for(let s of r){let i=this.getEndsOfEdge(s);if(!i)continue;let[a,l]=i,f=a.key,c=l.key,d=n.hasVertex?n.hasVertex(f):!1,g=n.hasVertex?n.hasVertex(c):!1;if(d&&g){let x=s.weight,y=s.value,R=n.createEdge(f,c,x,y);n._addEdge(R)}}return n}_addVertex(e){return this.hasVertex(e)?!1:(this._vertexMap.set(e.key,e),!0)}_getVertex(e){let t=this._getVertexKey(e);return this._vertexMap.get(t)||void 0}_getVertexKey(e){return e instanceof _?e.key:e}};var N=class extends _{constructor(o,e){super(o,e)}},D=class extends v{constructor(e,t,n,r){super(n,r);b(this,"src");b(this,"dest");this.src=e,this.dest=t}},z=class h extends C{constructor(e){super(e);b(this,"_outEdgeMap",new Map);b(this,"_inEdgeMap",new Map)}get outEdgeMap(){return this._outEdgeMap}set outEdgeMap(e){this._outEdgeMap=e}get inEdgeMap(){return this._inEdgeMap}set inEdgeMap(e){this._inEdgeMap=e}static fromKeys(e){let t=new h({vertexValueInitializer:n=>n});for(let n of e)t.addVertex(n);return t}static fromEntries(e){let t=new h;for(let[n,r]of e)t.addVertex(n,r);return t}createVertex(e,t){return new N(e,t)}createEdge(e,t,n,r){var s;return new D(e,t,(s=n!=null?n:this.options.defaultEdgeWeight)!=null?s:1,r)}getEdge(e,t){let n=[];if(e!==void 0&&t!==void 0){let r=this._getVertex(e),s=this._getVertex(t);if(r&&s){let i=this._outEdgeMap.get(r);i&&(n=i.filter(a=>a.dest===s.key))}}return n[0]||void 0}deleteEdgeSrcToDest(e,t){let n=this._getVertex(e),r=this._getVertex(t),s;if(!n||!r)return;let i=this._outEdgeMap.get(n);i&&I(i,l=>l.dest===r.key);let a=this._inEdgeMap.get(r);return a&&(s=I(a,l=>l.src===n.key)[0]||void 0),s}deleteEdge(e,t){let n,r,s;if(this.isVertexKey(e))if(this.isVertexKey(t))r=this._getVertex(e),s=this._getVertex(t);else return;else r=this._getVertex(e.src),s=this._getVertex(e.dest);if(r&&s){let i=this._outEdgeMap.get(r);i&&i.length>0&&I(i,l=>l.src===r.key&&l.dest===(s==null?void 0:s.key));let a=this._inEdgeMap.get(s);a&&a.length>0&&(n=I(a,l=>l.src===r.key&&l.dest===s.key)[0])}return n}deleteVertex(e){let t,n;if(this.isVertexKey(e)?(n=this.getVertex(e),t=e):(n=e,t=this._getVertexKey(e)),n){let r=this.getNeighbors(n);for(let s of r)this.deleteEdgeSrcToDest(n,s);this._outEdgeMap.delete(n),this._inEdgeMap.delete(n)}return this._vertexMap.delete(t)}deleteEdgesBetween(e,t){let n=[];if(e&&t){let r=this.deleteEdgeSrcToDest(e,t),s=this.deleteEdgeSrcToDest(t,e);r&&n.push(r),s&&n.push(s)}return n}incomingEdgesOf(e){let t=this._getVertex(e);return t?this.inEdgeMap.get(t)||[]:[]}outgoingEdgesOf(e){let t=this._getVertex(e);return t?this._outEdgeMap.get(t)||[]:[]}degreeOf(e){return this.outDegreeOf(e)+this.inDegreeOf(e)}inDegreeOf(e){return this.incomingEdgesOf(e).length}outDegreeOf(e){return this.outgoingEdgesOf(e).length}edgesOf(e){return[...this.outgoingEdgesOf(e),...this.incomingEdgesOf(e)]}getEdgeSrc(e){return this._getVertex(e.src)}getEdgeDest(e){return this._getVertex(e.dest)}getDestinations(e){if(e===void 0)return[];let t=[],n=this.outgoingEdgesOf(e);for(let r of n){let s=this.getEdgeDest(r);s&&t.push(s)}return t}topologicalSort(e){e=e!=null?e:"key";let t=new Map;for(let i of this.vertexMap)t.set(i[1],0);let n=[],r=!1,s=i=>{t.set(i,1);let a=this.getDestinations(i);for(let l of a){let f=t.get(l);f===0?s(l):f===1&&(r=!0)}t.set(i,2),n.push(i)};for(let i of this.vertexMap)t.get(i[1])===0&&s(i[1]);if(!r)return e==="key"&&(n=n.map(i=>i instanceof N?i.key:i)),n.reverse()}edgeSet(){let e=[];return this._outEdgeMap.forEach(t=>{e=[...e,...t]}),e}getNeighbors(e){let t=[],n=this._getVertex(e);if(n){let r=this.outgoingEdgesOf(n);for(let s of r){let i=this._getVertex(s.dest);i&&t.push(i)}}return t}getEndsOfEdge(e){if(!this.hasEdge(e.src,e.dest))return;let t=this._getVertex(e.src),n=this._getVertex(e.dest);if(t&&n)return[t,n]}isEmpty(){return this.vertexMap.size===0&&this.inEdgeMap.size===0&&this.outEdgeMap.size===0}clear(){this._vertexMap=new Map,this._inEdgeMap=new Map,this._outEdgeMap=new Map}clone(){return super.clone()}tarjan(){let e=new Map,t=new Map,n=new Map,r=0,s=[],i=new Set,a=l=>{e.set(l,r),t.set(l,r),r++,s.push(l),i.add(l);let f=this.getNeighbors(l);for(let c of f)e.has(c)?i.has(c)&&t.set(l,Math.min(t.get(l),e.get(c))):(a(c),t.set(l,Math.min(t.get(l),t.get(c))));if(e.get(l)===t.get(l)){let c=[],d;do d=s.pop(),i.delete(d),c.push(d);while(d!==l);n.set(n.size,c)}};for(let l of this.vertexMap.values())e.has(l)||a(l);return{dfnMap:e,lowMap:t,SCCs:n}}getDFNMap(){return this.tarjan().dfnMap}getLowMap(){return this.tarjan().lowMap}getSCCs(){return this.tarjan().SCCs}_addEdge(e){if(!(this.hasVertex(e.src)&&this.hasVertex(e.dest)))return!1;let t=this._getVertex(e.src),n=this._getVertex(e.dest);if(t&&n){let r=this._outEdgeMap.get(t);r?r.push(e):this._outEdgeMap.set(t,[e]);let s=this._inEdgeMap.get(n);return s?s.push(e):this._inEdgeMap.set(n,[e]),!0}else return!1}};var B=(e=>(e[e.VISIT=0]="VISIT",e[e.PROCESS=1]="PROCESS",e))(B||{}),G=class{constructor(o,e,t=!0,n=!0){this.low=o;this.high=e;this.includeLow=t;this.includeHigh=n;if(!(U(o)&&U(e)))throw new RangeError("low or high is not comparable");if(o>e)throw new RangeError("low must be less than or equal to high")}isInRange(o,e){let t=this.includeLow?e(o,this.low)>=0:e(o,this.low)>0,n=this.includeHigh?e(o,this.high)<=0:e(o,this.high)<0;return t&&n}};return Z($);})();
|
|
2
2
|
/**
|
|
3
3
|
* data-structure-typed
|
|
4
4
|
*
|