twokeys 2.2.0 → 3.0.0

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/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var E=class extends Error{code;constructor(n,t){super(t),this.name="GraphAlgorithmError",this.code=n;}},C=class{heap=[];get size(){return this.heap.length}push(n,t){this.heap.push({priority:t,value:n}),this.siftUp(this.heap.length-1);}pop(){if(this.heap.length===0)return;let n=this.heap[0],t=this.heap.pop();return this.heap.length>0&&t&&(this.heap[0]=t,this.siftDown(0)),n}siftUp(n){let t=n;for(;t>0;){let e=Math.floor((t-1)/2),o=this.heap[t],r=this.heap[e];if(!o||!r||r.priority<=o.priority)return;this.heap[e]=o,this.heap[t]=r,t=e;}}siftDown(n){let t=n;for(;;){let e=t*2+1,o=t*2+2,r=t,s=this.heap[r],c=this.heap[e],i=this.heap[o];s&&c&&c.priority<s.priority&&(r=e);let d=this.heap[r];if(d&&i&&i.priority<d.priority&&(r=o),r===t)return;let a=this.heap[t],h=this.heap[r];if(!a||!h)return;this.heap[t]=h,this.heap[r]=a,t=r;}}};function P(u,n){return u.localeCompare(n)}function V(u,n){return P(u,n)<=0?{from:u,to:n}:{from:n,to:u}}function F(u,n){let t=V(u,n);return `${t.from}\0${t.to}`}function Pe(u){return typeof u=="string"?u:u.id}var Y=class{parent=new Map;rank=new Map;constructor(n){for(let t of n)this.parent.set(t,t),this.rank.set(t,0);}find(n){let t=this.parent.get(n);if(!t||t===n)return n;let e=this.find(t);return this.parent.set(n,e),e}union(n,t){let e=this.find(n),o=this.find(t);if(e===o)return false;let r=this.rank.get(e)??0,s=this.rank.get(o)??0;return r<s?(this.parent.set(e,o),true):r>s?(this.parent.set(o,e),true):(this.parent.set(o,e),this.rank.set(e,r+1),true)}};function q(u,n){let t=new Set,e=[];for(let o of u){let r=Pe(o);t.has(r)||(t.add(r),e.push(r));}for(let o of n)t.has(o.from)||(t.add(o.from),e.push(o.from)),t.has(o.to)||(t.add(o.to),e.push(o.to));return e}function Ae(u){if(u===void 0)return 1;if(!Number.isFinite(u))throw new E("INVALID_ARGUMENT",`Graph edge weight must be finite, received ${u}`);return u}function ve(u){let n=new Map;for(let t of u)n.set(t,new Map);return n}function se(u,n,t,e){let o=u.get(n);if(!o)return;let r=o.get(t);(r===void 0||e<r)&&o.set(t,e);}function ke(u,n,t){let e=ve(u);for(let o of n){if(o.from===o.to)continue;let r=Ae(o.weight);!e.has(o.from)||!e.has(o.to)||(se(e,o.from,o.to,r),t||se(e,o.to,o.from,r));}return e}function Ge(u){let n=new Map;for(let[t,e]of u.entries()){let o=[];for(let[r,s]of e.entries())o.push({id:r,weight:s});n.set(t,o);}return n}function Te(u,n){let t=new Map;for(let e of u)t.set(e,[]);for(let[e,o]of n.entries())for(let r of o){let s=t.get(r.id);s&&s.push({id:e,weight:r.weight});}return t}function Oe(u){let n=[];for(let[t,e]of u.entries())for(let o of e)n.push({from:t,to:o.id,weight:o.weight});return n}function G(u){for(let n of u.edges)if(n.weight<0)return true;return false}function $(u,n,t){if(u===n)return [u];let e=[n],o=n;for(;o&&o!==u;){let r=t.get(o);if(!r)return [];e.push(r),o=r;}return e.reverse(),e[0]===u?e:[]}function ae(u){return u.reduce((n,t)=>n+t.weight,0)}function Ce(u){let n=new Map;for(let t of u.edges){let e=V(t.from,t.to),o=F(e.from,e.to),r=n.get(o);(!r||t.weight<r.weight-1e-12)&&n.set(o,{from:e.from,to:e.to,weight:t.weight});}return Array.from(n.values())}function Fe(u,n){let t=Ce(u),e=0;for(let i of t)e+=i.weight;if(!Number.isFinite(e)||e<=0)return 0;let o=new Map;for(let i of u.nodes)o.set(i,ae(u.neighborsByNode.get(i)??[]));let r=new Map,s=new Map;for(let i of u.nodes){let d=n.get(i);if(d===void 0)continue;let a=o.get(i)??0;r.set(d,(r.get(d)??0)+a);}for(let i of t){let d=n.get(i.from),a=n.get(i.to);d===void 0||a===void 0||d===a&&s.set(d,(s.get(d)??0)+i.weight);}let c=0;for(let[i,d]of r.entries()){let a=s.get(i)??0;c+=a/e-Math.pow(d/(2*e),2);}return c}function U(u,n,t,e,o){let r=new Map;for(let i of u.nodes){let d=n.get(i);if(d===void 0)continue;let a=r.get(d)??[];a.push(i),r.set(d,a);}let s=Array.from(r.values()).map(i=>[...i].sort(P));s.sort((i,d)=>P(i[0]??"",d[0]??""));let c=new Map;for(let i=0;i<s.length;i+=1)for(let d of s[i]??[])c.set(d,i);return {communities:s,communityByNode:c,iterations:t,converged:e,algorithm:o,modularity:Fe(u,c)}}function H(u){let n=new Map;for(let t of u.nodes)n.set(t,new Set);for(let[t,e]of u.neighborsByNode.entries()){let o=n.get(t);if(o)for(let r of e)o.add(r.id);}return n}function L(u,n,t,e){let o=t.get(u)??new Set,r=t.get(n)??new Set,s=o.size,c=r.size,i=s<=c?o:r,d=i===o?r:o,a=0,h=0,l=0;for(let N of i){if(!d.has(N))continue;a+=1;let b=t.get(N)?.size??0;b>1&&(h+=1/Math.log(b)),b>0&&(l+=1/b);}if(e==="common-neighbors")return a;if(e==="preferential-attachment")return s*c;if(e==="adamic-adar")return h;if(e==="resource-allocation")return l;if(e==="jaccard"){let N=s+c-a;return N>0?a/N:0}if(e==="cosine")return s>0&&c>0?a/Math.sqrt(s*c):0;let m=Math.min(s,c);return m>0?a/m:0}function Be(u,n){if(u.length<=1)return 0;let t=0;for(let e=1;e<u.length;e+=1){let o=u[e-1],r=u[e];if(!o||!r)return Number.POSITIVE_INFINITY;let s=n.neighborsByNode.get(o)?.find(c=>c.id===r)?.weight;if(s===void 0||!Number.isFinite(s))return Number.POSITIVE_INFINITY;t+=s;}return t}function _(u){return u.join("\0")}function De(u,n){if(n.length>u.length)return false;for(let t=0;t<n.length;t+=1)if(u[t]!==n[t])return false;return true}function ce(u,n){return u==="auto"?n?"bellman-ford":"dijkstra":u}function _e(u,n){if(G(u))throw new E("NEGATIVE_WEIGHT","Dijkstra cannot run on negative graph weights.");let t=new Map,e=new Map,o=new Set,r=new C;for(let s of u.nodes)t.set(s,Number.POSITIVE_INFINITY);for(t.set(n,0),r.push(n,0);r.size>0;){let s=r.pop();if(!s)break;let c=s.value;if(o.has(c))continue;o.add(c);let i=t.get(c);if(i===void 0||!Number.isFinite(i))continue;let d=u.neighborsByNode.get(c)??[];for(let a of d){let h=i+a.weight,l=t.get(a.id);(l===void 0||h<l-1e-12)&&(t.set(a.id,h),e.set(a.id,c),r.push(a.id,h));}}return {distanceByNode:t,previousByNode:e,explored:o.size,negativeCycleNodes:new Set}}function Le(u,n){let t=new Map,e=new Map;for(let d of u.nodes)t.set(d,Number.POSITIVE_INFINITY);t.set(n,0);let o=u.nodes.length;for(let d=0;d<o-1;d+=1){let a=false;for(let h of u.edges){let l=t.get(h.from);if(l===void 0||!Number.isFinite(l))continue;let m=l+h.weight,N=t.get(h.to);(N===void 0||m<N-1e-12)&&(t.set(h.to,m),e.set(h.to,h.from),a=true);}if(!a)break}let r=new Set,s=[];for(let d of u.edges){let a=t.get(d.from),h=t.get(d.to);a!==void 0&&Number.isFinite(a)&&h!==void 0&&a+d.weight<h-1e-12&&(r.has(d.to)||(r.add(d.to),s.push(d.to)),r.has(d.from)||(r.add(d.from),s.push(d.from)));}let c=0;for(;c<s.length;){let d=s[c];if(c+=1,!!d)for(let a of u.neighborsByNode.get(d)??[])r.has(a.id)||(r.add(a.id),s.push(a.id));}if(r.size>0)for(let d of r)t.set(d,Number.NEGATIVE_INFINITY),e.delete(d);let i=0;for(let d of u.nodes){let a=t.get(d);a!==void 0&&Number.isFinite(a)&&(i+=1);}return {distanceByNode:t,previousByNode:e,explored:i,negativeCycleNodes:r}}function z(u,n,t){let e=G(u),o=ce(t,e);return o==="dijkstra"?{algorithm:o,result:_e(u,n),hasNegativeWeights:e}:{algorithm:o,result:Le(u,n),hasNegativeWeights:e}}function ue(u){if(u===void 0||!Number.isFinite(u))return Math.random;let n=Math.floor(u)>>>0||1;return ()=>(n=n*1664525+1013904223>>>0,n/4294967296)}function O(u,n){let t=0;for(let e=0;e<u.length;e+=1){let o=(u[e]??0)-(n[e]??0);t+=o*o;}return t}function de(u,n){return Math.sqrt(O(u,n))}function je(u,n){let t=u[0]?.length??0;if(n==="none")return {normalize:i=>[...i],denormalize:i=>[...i]};let e=new Array(t).fill(0),o=new Array(t).fill(Number.POSITIVE_INFINITY),r=new Array(t).fill(Number.NEGATIVE_INFINITY);for(let i of u)for(let d=0;d<t;d+=1){let a=i[d]??0;e[d]=(e[d]??0)+a,o[d]=Math.min(o[d]??a,a),r[d]=Math.max(r[d]??a,a);}for(let i=0;i<t;i+=1)e[i]=(e[i]??0)/u.length;if(n==="minmax"){let i=r.map((d,a)=>{let h=o[a]??0;return d-h});return {normalize:d=>d.map((a,h)=>{let l=o[h]??0,m=i[h]??0;return Math.abs(m)<1e-12?0:(a-l)/m}),denormalize:d=>d.map((a,h)=>{let l=o[h]??0,m=i[h]??0;return a*m+l})}}let s=new Array(t).fill(0);for(let i of u)for(let d=0;d<t;d+=1){let a=(i[d]??0)-(e[d]??0);s[d]=(s[d]??0)+a*a;}let c=s.map(i=>Math.sqrt(i/u.length));return {normalize:i=>i.map((d,a)=>{let h=c[a]??0;return h<1e-12?0:(d-(e[a]??0))/h}),denormalize:i=>i.map((d,a)=>{let h=c[a]??0;return d*h+(e[a]??0)})}}function Ve(u,n,t,e){if(u.length===0)return [];if(!e){let c=[],i=new Set;for(;c.length<n;){let d=Math.floor(t()*u.length);i.has(d)||(i.add(d),c.push([...u[d]??u[0]??[]]));}return c}let o=[],r=new Set,s=Math.floor(t()*u.length);for(r.add(s),o.push([...u[s]??u[0]??[]]);o.length<n;){let c=new Array(u.length).fill(0),i=0;for(let l=0;l<u.length;l+=1){if(r.has(l))continue;let m=u[l];if(!m)continue;let N=Number.POSITIVE_INFINITY;for(let b of o)N=Math.min(N,O(m,b));c[l]=N,i+=N;}if(!Number.isFinite(i)||i<=0){for(let l=0;l<u.length;l+=1)if(!r.has(l)){r.add(l),o.push([...u[l]??u[0]??[]]);break}continue}let d=t()*i,a=0,h=-1;for(let l=0;l<u.length;l+=1)if(!r.has(l)&&(a+=c[l]??0,a>=d)){h=l;break}if(h<0){for(let l=0;l<u.length;l+=1)if(!r.has(l)){h=l;break}}if(h<0)break;r.add(h),o.push([...u[h]??u[0]??[]]);}return o}function qe(u,n,t,e,o,r){let s=u[0]?.length??0,c=Ve(u,n,t,r);if(c.length!==n)throw new E("INVALID_ARGUMENT","Unable to initialize centroids for k-means.");let i=new Array(u.length).fill(-1),d=0,a=false,h=Number.POSITIVE_INFINITY;for(;d<e;){d+=1;let l=false;h=0;for(let p=0;p<u.length;p+=1){let g=u[p];if(!g)continue;let f=0,I=Number.POSITIVE_INFINITY;for(let x=0;x<c.length;x+=1){let M=c[x];if(!M)continue;let w=O(g,M);w<I&&(I=w,f=x);}h+=I,i[p]!==f&&(i[p]=f,l=true);}let m=new Array(n),N=new Array(n).fill(0);for(let p=0;p<n;p+=1)m[p]=new Array(s).fill(0);for(let p=0;p<u.length;p+=1){let g=i[p];if(g<0)continue;let f=u[p],I=m[g];if(!(!f||!I)){N[g]=(N[g]??0)+1;for(let x=0;x<s;x+=1)I[x]=(I[x]??0)+(f[x]??0);}}for(let p=0;p<n;p+=1){let g=N[p]??0;if(g>0){let f=m[p];if(!f)continue;for(let I=0;I<s;I+=1)f[I]=(f[I]??0)/g;}else {let f=0,I=-1;for(let x=0;x<u.length;x+=1){let M=i[x],w=c[M],y=u[x];if(!w||!y)continue;let R=O(y,w);R>I&&(I=R,f=x);}m[p]=[...u[f]??u[0]??[]];}}let b=0;for(let p=0;p<n;p+=1){let g=c[p],f=m[p];!g||!f||(b=Math.max(b,Math.sqrt(O(g,f))));}if(c=m,!l||b<=o){a=true;break}}return {assignments:i,centroidsNormalized:c,iterations:d,inertia:h,converged:a}}function ze(u,n,t){if(u.length<=1||t<=1)return null;let e=Array.from({length:t},()=>[]);for(let s=0;s<n.length;s+=1){let c=n[s]??-1;if(c>=0&&c<t){let i=e[c];i&&i.push(s);}}let o=0,r=0;for(let s=0;s<u.length;s+=1){let c=u[s],i=n[s]??-1;if(!c||i<0||i>=t)continue;let d=e[i]??[];if(d.length<=1)continue;let a=0;for(let m of d){if(m===s)continue;let N=u[m];N&&(a+=de(c,N));}a/=d.length-1;let h=Number.POSITIVE_INFINITY;for(let m=0;m<t;m+=1){if(m===i)continue;let N=e[m]??[];if(N.length===0)continue;let b=0;for(let p of N){let g=u[p];g&&(b+=de(c,g));}b/=N.length,h=Math.min(h,b);}if(!Number.isFinite(h))continue;let l=Math.max(a,h);l<=0||(o+=(h-a)/l,r+=1);}return r===0?null:o/r}function W(u,n){let t=0;for(let e=1;e<u.length;e+=1){let o=u[e-1],r=u[e];if(!o||!r)return Number.POSITIVE_INFINITY;let s=n.get(o)?.get(r);if(s===void 0||!Number.isFinite(s))return Number.POSITIVE_INFINITY;t+=s;}return t}function Ke(u,n,t){if(u.length<4||t<=0)return u;let e=[...u],o=W(e,n);if(!Number.isFinite(o))return e;for(let r=0;r<t;r+=1){let s=false;for(let c=1;c<e.length-2;c+=1)for(let i=c+1;i<e.length-1;i+=1){let d=e.slice(0,c),a=e.slice(c,i+1).reverse(),h=e.slice(i+1),l=d.concat(a,h),m=W(l,n);m+1e-9<o&&(e=l,o=m,s=true);}if(!s)break}return e}function Ye(u,n){let t=[],e=new Set;n.start&&u.includes(n.start)&&(t.push(n.start),e.add(n.start));for(let c of n.startCandidates??[])u.includes(c)&&!e.has(c)&&(t.push(c),e.add(c));let o=ue(n.seed),r=u.filter(c=>!e.has(c));for(let c=r.length-1;c>0;c-=1){let i=Math.floor(o()*(c+1)),d=r[c],a=r[i];d!==void 0&&a!==void 0&&(r[c]=a,r[i]=d);}let s=Math.max(1,Math.min(u.length,n.multiStartCount??Math.min(8,u.length)));for(let c of r){if(t.length>=s)break;t.push(c);}return t.length===0&&u.length>0&&t.push(u[0]),t}function Ue(u,n,t,e){let o=[u],r=new Set(n.filter(c=>c!==u)),s=u;for(;r.size>0;){let c=null,i=Number.POSITIVE_INFINITY,d=t.get(s);for(let a of r){let h=d?.get(a)??Number.POSITIVE_INFINITY;h<i&&(i=h,c=a);}if(!c||!Number.isFinite(i))break;o.push(c),r.delete(c),s=c;}return e&&o.length>1&&o.push(u),o}function We(u,n){if(u.length<=1)return 0;let t=new Set;t.add(u[0]);let e=0;for(;t.size<u.length;){let o=Number.POSITIVE_INFINITY,r=null;for(let s of t){let c=n.get(s);for(let i of u){if(t.has(i))continue;let d=c?.get(i)??Number.POSITIVE_INFINITY;d<o&&(o=d,r=i);}}if(!r||!Number.isFinite(o))return Number.POSITIVE_INFINITY;t.add(r),e+=o;}return e}function v(u,n,t={}){let e=t.directed??true,o=q(u,n),r=ke(o,n,e),s=Ge(r),c=Te(o,s);return {nodes:o,neighborsByNode:s,incomingByNode:c,edges:Oe(s),directed:e}}function le(u,n,t={}){let e=v(u,n,t),o=[];if(e.directed){let s=0,c=new Map,i=new Map,d=[],a=new Set,h=l=>{c.set(l,s),i.set(l,s),s+=1,d.push(l),a.add(l);for(let m of e.neighborsByNode.get(l)??[])if(c.has(m.id)){if(a.has(m.id)){let N=i.get(l)??0,b=c.get(m.id)??0;i.set(l,Math.min(N,b));}}else {h(m.id);let N=i.get(l)??0,b=i.get(m.id)??0;i.set(l,Math.min(N,b));}if((i.get(l)??-1)===(c.get(l)??-2)){let m=[];for(;d.length>0;){let N=d.pop();if(!N||(a.delete(N),m.push(N),N===l))break}m.sort(P),o.push(m);}};for(let l of e.nodes)c.has(l)||h(l);}else {let s=new Set;for(let c of e.nodes){if(s.has(c))continue;let i=[c];s.add(c);let d=[],a=0;for(;a<i.length;){let h=i[a];if(a+=1,!!h){d.push(h);for(let l of e.neighborsByNode.get(h)??[])s.has(l.id)||(s.add(l.id),i.push(l.id));}}d.sort(P),o.push(d);}}o.sort((s,c)=>{let i=s[0]??"",d=c[0]??"";return P(i,d)});let r=new Map;for(let s=0;s<o.length;s+=1)for(let c of o[s]??[])r.set(c,s);return {components:o,componentByNode:r}}function $e(u,n){let t=v(u,n,{directed:false}),e=[],o=new Map,r=new Set;for(let s of t.nodes){if(r.has(s))continue;let c=[s];r.add(s);let i=[],d=0;for(;d<c.length;){let h=c[d];if(d+=1,!!h){i.push(h);for(let l of t.neighborsByNode.get(h)??[])r.has(l.id)||(r.add(l.id),c.push(l.id));}}i.sort(P);let a=e.length;e.push(i);for(let h of i)o.set(h,a);}e.sort((s,c)=>{let i=s[0]??"",d=c[0]??"";return P(i,d)}),o.clear();for(let s=0;s<e.length;s+=1){let c=e[s]??[];for(let i of c)o.set(i,s);}return {components:e,componentByNode:o}}function nt(u,n,t={}){let e=q(u,n),o=t.tieBreaker??((g,f)=>P(g,f)),r=t.priorityByNode,s=t.priority??(g=>r instanceof Map?r.get(g)??0:r?r[g]??0:0),c=(g,f)=>{let I=s(f)-s(g);return Math.abs(I)>1e-12?I:o(g,f)},i=new Map,d=new Map;for(let g of e)i.set(g,new Set),d.set(g,0);for(let g of n){if(g.from===g.to||!i.has(g.from)||!i.has(g.to))continue;let f=i.get(g.from);!f||f.has(g.to)||(f.add(g.to),d.set(g.to,(d.get(g.to)??0)+1));}let a=e.filter(g=>(d.get(g)??0)===0).sort(c),h=[];for(;a.length>0;){let g=a.shift();if(!g)continue;h.push(g);let f=Array.from(i.get(g)??[]).sort(c);for(let I of f){let x=(d.get(I)??0)-1;d.set(I,x),x===0&&a.push(I);}a.sort(c);}let l=new Set(h),m=e.filter(g=>!l.has(g));m.sort(c);let N=le(e,n,{directed:true}),b=new Set(n.filter(g=>g.from===g.to).map(g=>g.from)),p=new Set;for(let g of N.components)if(g.length>1)for(let f of g)p.add(f);else if(g.length===1){let f=g[0];f&&b.has(f)&&p.add(f);}for(let g of m)p.add(g);for(let g of m)l.has(g)||(h.push(g),l.add(g));return {order:h,cycleNodes:Array.from(p).sort(c),isDag:p.size===0}}function He(u,n,t={}){let e=v(u,n,t),o=e.nodes.length,r=o<=1?1:e.directed?2*(o-1):Math.max(1,o-1),s=new Map;for(let c of e.nodes){let i=e.neighborsByNode.get(c)?.length??0,d=e.incomingByNode.get(c)?.length??0,a=e.directed?d+i:i;s.set(c,{inDegree:d,outDegree:i,degree:a,normalized:a/r});}return s}function Xe(u,n,t={}){let e=v(u,n,t),o=e.nodes.length,r=Math.max(0,o-1),s=t.mode??"harmonic",c=new Map;for(let i of e.nodes){let d=z(e,i,t.shortestPathAlgorithm??"auto"),a=d.result.distanceByNode,h=d.result.negativeCycleNodes.size>0,l=0,m=0,N=0;for(let g of e.nodes){if(g===i)continue;let f=a.get(g);f===void 0||!Number.isFinite(f)||f<=0||(l+=1,m+=f,N+=1/f);}let b=0,p=0;h||(s==="classic"?(b=l>0&&m>0?l/m:0,p=r>0?b*(l/r):0):(b=N,p=r>0?N/r:0)),c.set(i,{reachableCount:l,distanceSum:m,score:b,normalized:p,mode:s,negativeCycle:h});}return c}function Je(u,n,t={}){let e=v(u,n,t);if(G(e))throw new E("NEGATIVE_WEIGHT","Betweenness centrality requires non-negative graph weights.");let o=new Map;for(let i of e.nodes)o.set(i,0);for(let i of e.nodes){let d=[],a=new Map,h=new Map,l=new Map,m=new C,N=new Set;for(let p of e.nodes)a.set(p,[]),h.set(p,0),l.set(p,Number.POSITIVE_INFINITY);for(h.set(i,1),l.set(i,0),m.push(i,0);m.size>0;){let p=m.pop();if(!p)break;let g=p.value;if(N.has(g))continue;N.add(g),d.push(g);let f=l.get(g);if(f===void 0||!Number.isFinite(f))continue;let I=h.get(g)??0;for(let x of e.neighborsByNode.get(g)??[]){let M=f+x.weight,w=l.get(x.id);w===void 0||M<w-1e-12?(l.set(x.id,M),m.push(x.id,M),h.set(x.id,I),a.set(x.id,[g])):w!==void 0&&Math.abs(M-w)<=1e-12&&(h.set(x.id,(h.get(x.id)??0)+I),a.get(x.id)?.push(g));}}let b=new Map;for(let p of e.nodes)b.set(p,0);for(;d.length>0;){let p=d.pop();if(!p)continue;let g=h.get(p)??0,f=b.get(p)??0;for(let I of a.get(p)??[]){let x=h.get(I)??0;if(g<=0)continue;let M=x/g*(1+f);b.set(I,(b.get(I)??0)+M);}p!==i&&o.set(p,(o.get(p)??0)+f);}}if(!e.directed)for(let i of e.nodes)o.set(i,(o.get(i)??0)/2);let r=e.nodes.length,s=r<=2?Number.POSITIVE_INFINITY:e.directed?(r-1)*(r-2):(r-1)*(r-2)/2,c=new Map;for(let i of e.nodes){let d=o.get(i)??0;c.set(i,{raw:d,normalized:Number.isFinite(s)&&s>0?d/s:0});}return c}function X(u,n,t={}){let e=v(u,n,t);if(G(e))throw new E("NEGATIVE_WEIGHT","PageRank requires non-negative graph weights.");let o=e.nodes.length;if(o===0)return {byNode:new Map,order:[],iterations:0,converged:true,dampingFactor:t.dampingFactor??.85};let r=t.dampingFactor??.85;if(!Number.isFinite(r)||r<0||r>=1)throw new E("INVALID_ARGUMENT",`PageRank dampingFactor must be in [0, 1), received ${r}.`);let s=Math.max(0,t.tolerance??1e-9),c=Math.max(1,Math.floor(t.maxIterations??200)),i=1/o,d=new Map;for(let g of e.nodes){let f=(e.neighborsByNode.get(g)??[]).reduce((I,x)=>I+x.weight,0);d.set(g,f);}let a=new Map;for(let g of e.nodes)a.set(g,i);let h=0,l=false;for(let g=0;g<c;g+=1){h=g+1;let f=new Map,I=(1-r)/o,x=0;for(let y of e.nodes)(d.get(y)??0)<=1e-12&&(x+=a.get(y)??0),f.set(y,I);let M=r*x/o;if(M>0)for(let y of e.nodes)f.set(y,(f.get(y)??0)+M);for(let y of e.nodes){let R=d.get(y)??0;if(R<=1e-12)continue;let S=a.get(y)??0;for(let A of e.neighborsByNode.get(y)??[]){let k=r*S*A.weight/R;f.set(A.id,(f.get(A.id)??0)+k);}}let w=0;for(let y of e.nodes){let R=a.get(y)??0,S=f.get(y)??0;w+=Math.abs(R-S);}if(a=f,w<=s){l=true;break}}let m=0;for(let g of e.nodes)m+=a.get(g)??0;if(!Number.isFinite(m)||m<=0){a=new Map;for(let g of e.nodes)a.set(g,i);m=1;}else if(Math.abs(m-1)>1e-12)for(let g of e.nodes)a.set(g,(a.get(g)??0)/m);let N=[...e.nodes].sort((g,f)=>{let I=(a.get(f)??0)-(a.get(g)??0);return Math.abs(I)>1e-12?I:P(g,f)}),b=N.length>0?a.get(N[0])??0:0,p=new Map;for(let g=0;g<N.length;g+=1){let f=N[g];if(!f)continue;let I=a.get(f)??0;p.set(f,{score:I,normalized:b>0?I/b:0,rank:g+1});}return {byNode:p,order:N,iterations:h,converged:l,dampingFactor:r}}function ot(u,n,t={}){let e=v(u,n,{directed:false}),o=t.tieBreaker??((l,m)=>P(l,m)),r=new Map;for(let l of e.edges){if(l.from===l.to)continue;let m=V(l.from,l.to),N=F(m.from,m.to),b=r.get(N);(!b||l.weight<b.weight-1e-12)&&r.set(N,{from:m.from,to:m.to,weight:l.weight});}let s=Array.from(r.values());s.sort((l,m)=>{let N=l.weight-m.weight;if(Math.abs(N)>1e-12)return N;let b=o(l.from,m.from);return b!==0?b:o(l.to,m.to)});let c=new Y(e.nodes),i=[],d=0;for(let l of s)c.union(l.from,l.to)&&(i.push(l),d+=l.weight);let a=new Set;for(let l of e.nodes)a.add(c.find(l));let h=a.size;return {edges:i,totalWeight:d,componentCount:h,spanning:e.nodes.length<=1||h===1&&i.length===e.nodes.length-1}}function Qe(u,n){let t=v(u,n,{directed:false}),e=new Map,o=new Map,r=new Map,s=new Set,c=new Map,i=0,d=h=>{e.set(h,i),o.set(h,i),i+=1;let l=0,m=r.get(h)??null;for(let N of t.neighborsByNode.get(h)??[]){let b=e.get(N.id);if(b===void 0){r.set(N.id,h),l+=1,d(N.id);let p=o.get(h)??0,g=o.get(N.id)??0;o.set(h,Math.min(p,g));let f=e.get(h)??0;if(m===null&&l>1&&s.add(h),m!==null&&g>=f&&s.add(h),g>f){let I=V(h,N.id),x=F(I.from,I.to),M=c.get(x);(!M||N.weight<M.weight-1e-12)&&c.set(x,{from:I.from,to:I.to,weight:N.weight});}}else if(N.id!==m){let p=o.get(h)??0;o.set(h,Math.min(p,b));}}};for(let h of t.nodes)e.has(h)||(r.set(h,null),d(h));let a=Array.from(c.values());return a.sort((h,l)=>{let m=P(h.from,l.from);if(m!==0)return m;let N=P(h.to,l.to);return N!==0?N:h.weight-l.weight}),{articulationPoints:Array.from(s).sort(P),bridges:a}}function rt(u,n,t={}){let e=le(u,n,t),o=$e(u,n),r=Qe(u,n),s=q(u,n),c=[...s].sort(P),i=new Map,d=new Map;try{i=Je(u,n,t);}catch(a){if(!(a instanceof E)||a.code!=="NEGATIVE_WEIGHT")throw a;i=new Map;for(let h of c)i.set(h,{raw:0,normalized:0});}try{d=X(u,n,{directed:t.directed??!0,...t.pageRankOptions??{}}).byNode;}catch(a){if(!(a instanceof E)||a.code!=="NEGATIVE_WEIGHT")throw a;let h=s.length>0?1/s.length:0;d=new Map;for(let l=0;l<c.length;l+=1){let m=c[l];m&&d.set(m,{score:h,normalized:1,rank:l+1});}}return {degree:He(u,n,t),closeness:Xe(u,n,{...t,mode:t.closenessMode??"harmonic",shortestPathAlgorithm:t.shortestPathAlgorithm}),betweenness:i,pageRank:d,stronglyConnectedComponents:e.components,weaklyConnectedComponents:o.components,articulationPoints:r.articulationPoints,bridges:r.bridges}}function j(u,n,t,e,o={}){let r=v(u,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,algorithm:o.algorithm??"auto",hasNegativeWeights:G(r),negativeCycle:false};let s=z(r,t,o.algorithm??"auto");if(s.result.negativeCycleNodes.has(e)){if(o.failOnNegativeCycle??false)throw new E("NEGATIVE_CYCLE",`Negative cycle reaches target node "${e}".`);return {source:t,target:e,path:[],distance:Number.NEGATIVE_INFINITY,reachable:false,explored:s.result.explored,algorithm:s.algorithm,hasNegativeWeights:s.hasNegativeWeights,negativeCycle:true}}let c=s.result.distanceByNode.get(e)??Number.POSITIVE_INFINITY,i=Number.isFinite(c),d=i?$(t,e,s.result.previousByNode):[];return {source:t,target:e,path:d,distance:c,reachable:i&&d.length>0,explored:s.result.explored,algorithm:s.algorithm,hasNegativeWeights:s.hasNegativeWeights,negativeCycle:s.result.negativeCycleNodes.size>0}}function he(u,n,t={}){let e=v(u,n,{directed:t.directed??false});if(G(e))throw new E("NEGATIVE_WEIGHT","Label propagation requires non-negative graph weights.");let o=t.tieBreaker??((a,h)=>P(a,h)),r=[...e.nodes].sort(o),s=new Map;for(let a=0;a<r.length;a+=1){let h=r[a];h&&s.set(h,a);}let c=Math.max(1,t.maxIterations??100),i=0,d=false;for(let a=0;a<c;a+=1){i=a+1;let h=false;for(let l of r){let m=e.neighborsByNode.get(l)??[];if(m.length===0)continue;let N=new Map;for(let f of m){let I=s.get(f.id);I!==void 0&&N.set(I,(N.get(I)??0)+f.weight);}let b=s.get(l);if(b===void 0||N.size===0)continue;let p=b,g=Number.NEGATIVE_INFINITY;for(let[f,I]of N.entries()){if(I>g+1e-12){g=I,p=f;continue}Math.abs(I-g)<=1e-12&&f<p&&(p=f);}p!==b&&(s.set(l,p),h=true);}if(!h){d=true;break}}return U(e,s,i,d,"label-propagation")}function me(u,n,t={}){let e=v(u,n,{directed:t.directed??false});if(G(e))throw new E("NEGATIVE_WEIGHT","Louvain requires non-negative graph weights.");let o=t.tieBreaker??((N,b)=>P(N,b)),r=[...e.nodes].sort(o),s=Math.max(1,t.maxPasses??32),c=Math.max(0,t.tolerance??1e-9),i=new Map;for(let N of e.nodes)i.set(N,ae(e.neighborsByNode.get(N)??[]));let d=0;for(let N of i.values())d+=N;let a=new Map,h=new Map;for(let N=0;N<r.length;N+=1){let b=r[N];if(!b)continue;a.set(b,N);let p=i.get(b)??0;h.set(N,p);}if(d<=1e-12)return U(e,a,0,true,"louvain");let l=0,m=false;for(let N=0;N<s;N+=1){l=N+1;let b=false;for(let p of r){let g=a.get(p);if(g===void 0)continue;let f=i.get(p)??0;h.set(g,(h.get(g)??0)-f);let I=new Map;for(let w of e.neighborsByNode.get(p)??[]){let y=a.get(w.id);y!==void 0&&I.set(y,(I.get(y)??0)+w.weight);}let x=g,M=0;for(let[w,y]of I.entries()){let R=h.get(w)??0,S=y-R*f/d;if(S>M+c){M=S,x=w;continue}Math.abs(S-M)<=c&&w<x&&(x=w);}x!==g&&M>c?(a.set(p,x),h.set(x,(h.get(x)??0)+f),b=true):h.set(g,(h.get(g)??0)+f);}if(!b){m=true;break}}return U(e,a,l,m,"louvain")}function ge(u,n,t={}){let e=v(u,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,s=H(e),c=[...e.nodes].sort(P),i=[];for(let d=0;d<c.length;d+=1){let a=c[d];if(a)for(let h=d+1;h<c.length;h+=1){let l=c[h];if(!l)continue;let m=L(a,l,s,o);m<r-1e-12||i.push({left:a,right:l,score:m,metric:o});}}return i.sort((d,a)=>{let h=a.score-d.score;if(Math.abs(h)>1e-12)return h;let l=P(d.left,a.left);return l!==0?l:P(d.right,a.right)}),{metric:o,pairs:i.map((d,a)=>({...d,rank:a+1}))}}function fe(u,n,t={}){let e=v(u,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,s=Math.max(1,Math.floor(t.k??5)),c=H(e),i=[...e.nodes].sort(P),d=new Map;for(let a of i){let h=[];for(let l of i){if(a===l)continue;let m=L(a,l,c,o);m<r-1e-12||h.push({nodeId:l,score:m});}h.sort((l,m)=>{let N=m.score-l.score;return Math.abs(N)>1e-12?N:P(l.nodeId,m.nodeId)}),d.set(a,h.slice(0,s));}return {metric:o,k:s,neighborsByNode:d}}function J(u,n,t={}){let e=t.directed??false,o=v(u,n,{directed:e}),r=t.metric??"jaccard",s=t.minScore??0,c=Math.max(1,Math.floor(t.limit??20)),i=t.allowExistingEdges??false,d=H(o),a=[...o.nodes].sort(P),h=t.sourceFilter?new Set(t.sourceFilter):null,l=t.targetFilter?new Set(t.targetFilter):null,m=new Set;for(let b of o.edges)e?m.add(`${b.from}\0${b.to}`):m.add(F(b.from,b.to));let N=[];if(e){for(let b of a)if(!(h&&!h.has(b)))for(let p of a){if(b===p||l&&!l.has(p)||!i&&m.has(`${b}\0${p}`))continue;let g=L(b,p,d,r);g<s-1e-12||N.push({from:b,to:p,score:g,metric:r});}}else for(let b=0;b<a.length;b+=1){let p=a[b];if(p)for(let g=b+1;g<a.length;g+=1){let f=a[g];if(!f||h&&!h.has(p)&&!h.has(f)||l&&!l.has(p)&&!l.has(f)||!i&&m.has(F(p,f)))continue;let I=L(p,f,d,r);I<s-1e-12||N.push({from:p,to:f,score:I,metric:r});}}return N.sort((b,p)=>{let g=p.score-b.score;if(Math.abs(g)>1e-12)return g;let f=P(b.from,p.from);return f!==0?f:P(b.to,p.to)}),{metric:r,predictions:N.slice(0,c).map((b,p)=>({...b,rank:p+1}))}}function it(u,n,t={}){return J(u,n,t)}function Ie(u,n,t,e,o={}){let r=v(u,n,o);if(G(r))throw new E("NEGATIVE_WEIGHT","A* requires non-negative graph weights.");if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,estimatedDistance:Number.POSITIVE_INFINITY};let s=o.heuristic??((p,g)=>0),c=new C,i=new Map,d=new Map,a=new Set;for(let p of r.nodes)i.set(p,Number.POSITIVE_INFINITY);i.set(t,0);let h=s(t,e);if(!Number.isFinite(h))throw new E("INVALID_ARGUMENT","A* heuristic must return finite values.");c.push(t,h);let l=0;for(;c.size>0;){let p=c.pop();if(!p)break;let g=p.value;if(a.has(g))continue;if(a.add(g),l+=1,g===e)break;let f=i.get(g);if(!(f===void 0||!Number.isFinite(f)))for(let I of r.neighborsByNode.get(g)??[]){let x=f+I.weight,M=i.get(I.id)??Number.POSITIVE_INFINITY;if(x>=M-1e-12)continue;let w=s(I.id,e);if(!Number.isFinite(w))throw new E("INVALID_ARGUMENT","A* heuristic must return finite values.");i.set(I.id,x),d.set(I.id,g),c.push(I.id,x+w);}}let m=i.get(e)??Number.POSITIVE_INFINITY,N=Number.isFinite(m),b=N?$(t,e,d):[];return {source:t,target:e,path:b,distance:m,reachable:N&&b.length>0,explored:l,estimatedDistance:m}}function Ne(u,n,t={}){let e=v(u,n,t),o=t.algorithm??"auto",r=ce(o,G(e)),s=new Map,c=new Map,i=false;for(let d of e.nodes){let a=z(e,d,r);if(a.result.negativeCycleNodes.size>0&&(i=true,t.failOnNegativeCycle??false))throw new E("NEGATIVE_CYCLE",`Negative cycle reaches source node "${d}".`);let h=new Map,l=new Map;for(let m of e.nodes){h.set(m,a.result.distanceByNode.get(m)??Number.POSITIVE_INFINITY);let N=a.result.previousByNode.get(m);N&&l.set(m,N);}s.set(d,h),c.set(d,l);}return {nodes:e.nodes,distanceBySource:s,previousBySource:c,algorithm:r,hasNegativeWeights:G(e),negativeCycle:i}}function pe(u,n,t,e,o={}){let r=o.directed??true,s=Math.max(1,Math.floor(o.k??3)),c=o.shortestPathAlgorithm??"auto",i=v(u,n,{directed:r});if(!i.neighborsByNode.has(t)||!i.neighborsByNode.has(e))return {source:t,target:e,paths:[],complete:false};let d=j(i.nodes,i.edges,t,e,{directed:r,algorithm:c});if(!d.reachable||d.path.length===0)return {source:t,target:e,paths:[],complete:false};let a=[{path:d.path,distance:d.distance}],h=new Map;for(let l=1;l<s;l+=1){let m=a[l-1]?.path??[];if(m.length<2)break;for(let b=0;b<m.length-1;b+=1){let p=m[b];if(!p)continue;let g=m.slice(0,b+1),f=new Set(g.slice(0,-1)),I=new Set;for(let S of a)if(S.path.length>b+1&&De(S.path,g)){let A=S.path[b],k=S.path[b+1];A&&k&&I.add(`${A}\0${k}`);}let x=i.edges.filter(S=>!(I.has(`${S.from}\0${S.to}`)||f.has(S.from)||f.has(S.to))),M=j(i.nodes,x,p,e,{directed:r,algorithm:c});if(!M.reachable||M.path.length===0)continue;let w=g.slice(0,-1).concat(M.path),y=Be(w,i);if(!Number.isFinite(y))continue;let R=_(w);h.has(R)||h.set(R,{path:w,distance:y});}if(h.size===0)break;let N=Array.from(h.values()).sort((b,p)=>{let g=b.distance-p.distance;return Math.abs(g)>1e-12?g:P(_(b.path),_(p.path))})[0];if(!N)break;a.push(N),h.delete(_(N.path));}return {source:t,target:e,paths:a,complete:a.length>=s}}function be(u,n,t,e,o={}){let r=v(u,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,sink:e,maxFlow:0,augmentations:0,flowByEdge:[],sourcePartition:[],sinkPartition:[],cutEdges:[]};let s=[];for(let I of r.edges){if(I.weight<0)throw new E("NEGATIVE_WEIGHT","Maximum flow requires non-negative capacities.");s.push(I);}let c=new Map;for(let I of r.nodes)c.set(I,[]);let i=(I,x,M,w)=>{let y=c.get(I),R=c.get(x);if(!y||!R)return;let S=y.length,A=R.length;y.push({to:x,reverseIndex:A,capacity:M,originalIndex:w}),R.push({to:I,reverseIndex:S,capacity:0,originalIndex:null});};for(let I=0;I<s.length;I+=1){let x=s[I];x&&i(x.from,x.to,x.weight,I);}let d=0,a=0,h=new Array(s.length).fill(0);for(;;){let I=new Map,x=[t];I.set(t,{from:t,edgeIndex:-1});let M=0;for(;M<x.length&&!I.has(e);){let R=x[M];if(M+=1,!R)continue;let S=c.get(R)??[];for(let A=0;A<S.length;A+=1){let k=S[A];!k||k.capacity<=1e-12||I.has(k.to)||(I.set(k.to,{from:R,edgeIndex:A}),x.push(k.to));}}if(!I.has(e))break;let w=Number.POSITIVE_INFINITY,y=e;for(;y!==t;){let R=I.get(y);if(!R){w=0;break}let S=c.get(R.from)?.[R.edgeIndex];if(!S){w=0;break}w=Math.min(w,S.capacity),y=R.from;}if(!Number.isFinite(w)||w<=1e-12)break;for(y=e;y!==t;){let R=I.get(y);if(!R)break;let S=c.get(R.from)?.[R.edgeIndex],A=S?c.get(S.to)?.[S.reverseIndex]:void 0;if(!S||!A)break;S.capacity-=w,A.capacity+=w,S.originalIndex!==null?h[S.originalIndex]=(h[S.originalIndex]??0)+w:A.originalIndex!==null&&(h[A.originalIndex]=(h[A.originalIndex]??0)-w),y=R.from;}d+=w,a+=1;}let l=new Set,m=[t];l.add(t);let N=0;for(;N<m.length;){let I=m[N];if(N+=1,!!I)for(let x of c.get(I)??[])x.capacity<=1e-12||l.has(x.to)||(l.add(x.to),m.push(x.to));}let b=r.nodes.filter(I=>l.has(I)).sort(P),p=r.nodes.filter(I=>!l.has(I)).sort(P),g=s.filter(I=>l.has(I.from)&&!l.has(I.to)).sort((I,x)=>{let M=P(I.from,x.from);return M!==0?M:P(I.to,x.to)}),f=s.map((I,x)=>({from:I.from,to:I.to,flow:Math.max(0,h[x]??0),capacity:I.weight})).sort((I,x)=>{let M=P(I.from,x.from);return M!==0?M:P(I.to,x.to)});return {source:t,sink:e,maxFlow:d,augmentations:a,flowByEdge:f,sourcePartition:b,sinkPartition:p,cutEdges:g}}function xe(u,n,t,e,o={}){let r=n.map(f=>({from:f.from,to:f.to,weight:f.capacity})),s=q(u,r),c=o.directed??true;if(!s.includes(t)||!s.includes(e))return {source:t,sink:e,flow:0,cost:0,complete:false,augmentations:0,flowByEdge:[]};let i=[];for(let f of n){if(!Number.isFinite(f.capacity)||f.capacity<0)throw new E("INVALID_ARGUMENT",`Flow edge capacity must be non-negative and finite, received ${f.capacity}.`);let I=f.cost??0;if(!Number.isFinite(I))throw new E("INVALID_ARGUMENT",`Flow edge cost must be finite, received ${I}.`);i.push({from:f.from,to:f.to,capacity:f.capacity,cost:I}),c||i.push({from:f.to,to:f.from,capacity:f.capacity,cost:I});}let d=new Map;for(let f of s)d.set(f,[]);let a=(f,I,x,M,w)=>{let y=d.get(f),R=d.get(I);if(!y||!R)return;let S=y.length,A=R.length;y.push({to:I,reverseIndex:A,capacity:x,cost:M,originalIndex:w}),R.push({to:f,reverseIndex:S,capacity:0,cost:-M,originalIndex:null});};for(let f=0;f<i.length;f+=1){let I=i[f];I&&a(I.from,I.to,I.capacity,I.cost,f);}let h=o.targetFlow,l=h===void 0?Number.POSITIVE_INFINITY:h;if(!Number.isFinite(l)&&l!==Number.POSITIVE_INFINITY)throw new E("INVALID_ARGUMENT",`targetFlow must be finite or omitted, received ${l}.`);if(l<0)throw new E("INVALID_ARGUMENT",`targetFlow must be non-negative, received ${l}.`);let m=0,N=0,b=0,p=new Array(i.length).fill(0);for(;m<l-1e-12;){let f=new Map,I=new Map;for(let y of s)f.set(y,Number.POSITIVE_INFINITY);f.set(t,0);for(let y=0;y<s.length-1;y+=1){let R=false;for(let S of s){let A=f.get(S);if(A===void 0||!Number.isFinite(A))continue;let k=d.get(S)??[];for(let D=0;D<k.length;D+=1){let T=k[D];if(!T||T.capacity<=1e-12)continue;let ie=A+T.cost,Ee=f.get(T.to)??Number.POSITIVE_INFINITY;ie<Ee-1e-12&&(f.set(T.to,ie),I.set(T.to,{from:S,edgeIndex:D}),R=true);}}if(!R)break}let x=f.get(e)??Number.POSITIVE_INFINITY;if(!Number.isFinite(x))break;let M=Math.min(l-m,Number.POSITIVE_INFINITY),w=e;for(;w!==t;){let y=I.get(w);if(!y){M=0;break}let R=d.get(y.from)?.[y.edgeIndex];if(!R){M=0;break}M=Math.min(M,R.capacity),w=y.from;}if(!Number.isFinite(M)||M<=1e-12)break;for(w=e;w!==t;){let y=I.get(w);if(!y)break;let R=d.get(y.from)?.[y.edgeIndex],S=R?d.get(R.to)?.[R.reverseIndex]:void 0;if(!R||!S)break;R.capacity-=M,S.capacity+=M,R.originalIndex!==null?p[R.originalIndex]=(p[R.originalIndex]??0)+M:S.originalIndex!==null&&(p[S.originalIndex]=(p[S.originalIndex]??0)-M),w=y.from;}m+=M,N+=M*x,b+=1;}let g=i.map((f,I)=>({from:f.from,to:f.to,flow:Math.max(0,p[I]??0),capacity:f.capacity,cost:f.cost})).sort((f,I)=>{let x=P(f.from,I.from);return x!==0?x:P(f.to,I.to)});return {source:t,sink:e,flow:m,cost:N,complete:l===Number.POSITIVE_INFINITY?true:m>=l-1e-12,augmentations:b,flowByEdge:g}}function Ze(u,n,t={}){if(!Array.isArray(u)||u.length===0)throw new E("INVALID_ARGUMENT","kMeansClustering requires at least one point.");let e=u[0]?.length??0;if(e===0)throw new E("INVALID_ARGUMENT","kMeansClustering requires points with at least one axis.");for(let g of u)if(g.length!==e)throw new E("INVALID_ARGUMENT","kMeansClustering requires all points to share dimensionality.");let o=Math.max(1,Math.min(u.length,Math.floor(n))),r=Math.max(1,t.maxIterations??120),s=Math.max(0,t.tolerance??1e-5),c=Math.max(1,t.nInit??8),i=t.normalization??"zscore",d=t.useKMeansPlusPlus??true,a=t.seed===void 0||!Number.isFinite(t.seed)?Math.floor(Math.random()*4294967295):Math.floor(t.seed),h=je(u,i),l=u.map(g=>h.normalize(g)),m=null,N=a;for(let g=0;g<c;g+=1){let f=a+g*2654435761>>>0,I=ue(f),x=qe(l,o,I,r,s,d);(!m||x.inertia<m.inertia-1e-9)&&(m=x,N=f);}if(!m)throw new E("INVALID_ARGUMENT","kMeansClustering failed to produce a valid run.");let b=[];for(let g=0;g<o;g+=1){let f=[];for(let I=0;I<m.assignments.length;I+=1)m.assignments[I]===g&&f.push(I);b.push({centroid:h.denormalize(m.centroidsNormalized[g]??[]),indices:f});}let p=ze(l,m.assignments,o);return {assignments:m.assignments,clusters:b,iterations:m.iterations,inertia:m.inertia,converged:m.converged,silhouette:p,selectedSeed:N}}function st(u,n={}){if(!Array.isArray(u)||u.length===0)throw new E("INVALID_ARGUMENT","kMeansAuto requires at least one point.");let t=Math.max(2,n.kMin??2),e=Math.max(t,Math.min(u.length,n.kMax??Math.min(10,Math.ceil(Math.sqrt(u.length))+2))),o=null,r=t,s=[];for(let c=t;c<=e;c+=1){let i=Ze(u,c,{...n,seed:n.seed===void 0||!Number.isFinite(n.seed)?void 0:n.seed+c*997});if(s.push({k:c,silhouette:i.silhouette,inertia:i.inertia}),!o){o=i,r=c;continue}let d=i.silhouette??Number.NEGATIVE_INFINITY,a=o.silhouette??Number.NEGATIVE_INFINITY;if(d>a+1e-9){o=i,r=c;continue}Math.abs(d-a)<=1e-9&&i.inertia<o.inertia-1e-9&&(o=i,r=c);}if(!o)throw new E("INVALID_ARGUMENT","kMeansAuto failed to choose a cluster configuration.");return {...o,selectedK:r,candidates:s}}function dt(u,n,t={}){let e=v(u,n,t);if(e.nodes.length===0)return {order:[],distance:0,segments:[],visitedCount:0,complete:true,unreachableNodes:[],lowerBound:0,optimalityGap:0};if(G(e))throw new E("NEGATIVE_WEIGHT","TSP approximation requires non-negative graph weights.");let o=t.returnToStart??true,r=Math.max(0,t.twoOptPasses??3),s=Ye(e.nodes,t),c=new Map,i=new Map;for(let f of e.nodes){let I=z(e,f,t.shortestPathAlgorithm??"auto");if(I.result.negativeCycleNodes.size>0)throw new E("NEGATIVE_CYCLE","TSP approximation cannot run when negative cycles are reachable.");c.set(f,I.result.distanceByNode),i.set(f,I.result.previousByNode);}let d=[],a=Number.POSITIVE_INFINITY;for(let f of s){let x=Ue(f,e.nodes,c,o);x.length>2&&(x=Ke(x,c,r));let M=W(x,c);M<a&&(a=M,d=x);}let h=new Set(d),l=e.nodes.filter(f=>!h.has(f)),m=l.length===0&&Number.isFinite(a),N=[],b=0;for(let f=1;f<d.length;f+=1){let I=d[f-1],x=d[f];if(!I||!x)continue;let M=c.get(I)?.get(x)??Number.POSITIVE_INFINITY,w=i.get(I)??new Map,y=$(I,x,w);!Number.isFinite(M)||y.length===0||(b+=M,N.push({from:I,to:x,distance:M,path:y}));}m||(b=Number.POSITIVE_INFINITY);let p=We(e.nodes,c),g=m&&Number.isFinite(b)&&Number.isFinite(p)&&p>0?(b-p)/p:null;return {order:d,distance:b,segments:N,visitedCount:h.size,complete:m,unreachableNodes:l,lowerBound:p,optimalityGap:g}}var Q=class{projections=new Map;project(n,t,e,o={}){let r=n.trim();if(!r)throw new E("INVALID_ARGUMENT","Projection name must be non-empty.");if(this.projections.has(r)&&!(o.replace??false))throw new E("INVALID_ARGUMENT",`Projection "${r}" already exists.`);let s=o.directed??true,c=v(t,e,{directed:s}),i={name:r,nodes:c.nodes,edges:c.edges,directed:s,createdAt:Date.now(),metadata:o.metadata??{}};return this.projections.set(r,i),i}drop(n){return this.projections.delete(n)}clear(){this.projections.clear();}exists(n){return this.projections.has(n)}get(n){return this.projections.get(n)??null}list(){return Array.from(this.projections.values()).map(n=>({name:n.name,nodeCount:n.nodes.length,edgeCount:n.edges.length,directed:n.directed,createdAt:n.createdAt,metadata:n.metadata})).sort((n,t)=>n.name.localeCompare(t.name))}pageRank(n,t={}){let e=this.requireProjection(n);return X(e.nodes,e.edges,{directed:e.directed,...t})}louvain(n,t={}){let e=this.requireProjection(n);return me(e.nodes,e.edges,{directed:e.directed,...t})}labelPropagation(n,t={}){let e=this.requireProjection(n);return he(e.nodes,e.edges,{directed:e.directed,...t})}similarity(n,t={}){let e=this.requireProjection(n);return ge(e.nodes,e.edges,{directed:e.directed,...t})}knn(n,t={}){let e=this.requireProjection(n);return fe(e.nodes,e.edges,{directed:e.directed,...t})}linkPrediction(n,t={}){let e=this.requireProjection(n);return J(e.nodes,e.edges,{directed:e.directed,...t})}shortestPath(n,t,e,o={}){let r=this.requireProjection(n);return j(r.nodes,r.edges,t,e,{directed:r.directed,...o})}aStar(n,t,e,o={}){let r=this.requireProjection(n);return Ie(r.nodes,r.edges,t,e,{directed:r.directed,...o})}yen(n,t,e,o={}){let r=this.requireProjection(n);return pe(r.nodes,r.edges,t,e,{directed:r.directed,...o})}allPairs(n,t={}){let e=this.requireProjection(n);return Ne(e.nodes,e.edges,{directed:e.directed,...t})}maxFlow(n,t,e,o={}){let r=this.requireProjection(n);return be(r.nodes,r.edges,t,e,{directed:r.directed,...o})}minCostMaxFlow(n,t,e,o=null,r={}){let s=this.requireProjection(n),c=o??s.edges.map(i=>({from:i.from,to:i.to,capacity:i.weight,cost:i.weight}));return xe(s.nodes,c,t,e,{directed:s.directed,...r})}runPipeline(n,t){let e=new Map;for(let o of t){let r=o.id.trim();if(!r)throw new E("INVALID_ARGUMENT","Pipeline step id must be non-empty.");if(e.has(r))throw new E("INVALID_ARGUMENT",`Duplicate pipeline step id "${r}".`);if(o.kind==="page-rank"){e.set(r,this.pageRank(n,o.options));continue}if(o.kind==="louvain"){e.set(r,this.louvain(n,o.options));continue}if(o.kind==="label-propagation"){e.set(r,this.labelPropagation(n,o.options));continue}if(o.kind==="similarity"){e.set(r,this.similarity(n,o.options));continue}if(o.kind==="knn"){e.set(r,this.knn(n,o.options));continue}if(o.kind==="link-prediction"){e.set(r,this.linkPrediction(n,o.options));continue}if(o.kind==="shortest-path"){e.set(r,this.shortestPath(n,o.source,o.target,o.options));continue}if(o.kind==="a-star"){e.set(r,this.aStar(n,o.source,o.target,o.options));continue}if(o.kind==="yen-k-shortest-paths"){e.set(r,this.yen(n,o.source,o.target,o.options));continue}if(o.kind==="max-flow"){e.set(r,this.maxFlow(n,o.source,o.sink,o.options));continue}if(o.kind==="min-cost-max-flow"){e.set(r,this.minCostMaxFlow(n,o.source,o.sink,o.edges??null,o.options));continue}if(o.kind==="all-pairs-shortest-paths"){e.set(r,this.allPairs(n,o.options));continue}let s=o;throw new E("INVALID_ARGUMENT",`Unsupported pipeline step kind "${String(s)}".`)}return e}requireProjection(n){let t=this.projections.get(n);if(!t)throw new E("INVALID_ARGUMENT",`Projection "${n}" does not exist.`);return t}};function et(){return new Q}var ut=et();var B=100,tt=0,re=1e3,Z=1.5,Me=1,ye=2,K=2;function we(u=B){return Math.floor(Math.random()*u)}function Re(u=re,n=B){let t=[];for(let e=0;e<u;e++)t.push(we(n));return t}function Se(u=K,n=B){let t=[];for(let e=0;e<u;e++)t.push(Math.floor(Math.random()*(n/10)%n));return t}function ee(u=re,n=K,t=B){let e=[];for(let o=0;o<u;o++)e.push(Se(n,t));return e}var te=class{data;constructor(n={}){this.data={original:n.data??Re()};}sorted(){return this.data.sorted||(this.data.sorted=this.getSorted(this.data.original)),this.data.sorted}getSorted(n){return [...n].sort((t,e)=>t>e?1:t===e?0:-1)}median(){return this.sorted(),this.data.median===void 0&&(this.data.median=this.getMedian(this.data.sorted)),this.data.medianDepth===void 0&&(this.data.medianDepth=this.getMedianDepth(this.data.sorted)),{datum:this.data.median,depth:this.data.medianDepth}}getMedianDepth(n,t=0){return n.length?t+(n.length+1)/2:NaN}getMedian(n){let t=n.length;if(!t)return NaN;if(t===1)return n[0];let e=Math.floor(t/2);return t%2===0?(n[e-1]+n[e])/2:n[e]}mean(){return this.data.mean===void 0&&(this.data.mean=this.getMean(this.data.original)),this.data.mean}getMean(n){if(!n.length)return NaN;let t=0;for(let e of n)t+=e;return t/n.length}mode(){return this.data.mode||(this.sorted(),this.data.mode=this.getMode(this.data.sorted)),this.data.mode}getMode(n){if(!n.length)return {count:0,data:[]};let t={},e=0;for(let r of n)t[r]=(t[r]||0)+1,t[r]>e&&(e=t[r]);let o=[];for(let[r,s]of Object.entries(t))s===e&&o.push(Number(r));return {count:e,data:o.sort((r,s)=>r-s)}}extremes(){return this.data.extremes||(this.sorted(),this.data.extremes=this.getExtremes(this.data.sorted)),this.data.extremes}getExtremes(n){return n.length?[n[0],n[n.length-1]]:[]}counts(){return this.data.counts||(this.sorted(),this.data.counts=this.getCounts(this.data.sorted)),this.data.counts}getCounts(n){let t=new Map;for(let o of n)t.set(o,(t.get(o)||0)+1);let e=[];for(let[o,r]of t)e.push([o,r]);return e.sort((o,r)=>o[0]-r[0])}hinges(){return this.data.hinges||(this.sorted(),this.data.hinges=this.getHinges(this.data.sorted)),this.data.hinges}getHinges(n,t=2,e=[]){let o=[...n],r=o.length,s=t;if(s%2!==0&&s++,r<=s||s<=0)return e;let c=Math.floor(r/s),i=Math.floor(r/c)-1;for(let d=0;d<=i;d++){let a=o.slice(d*c,d*c+c);e.push({datum:this.getMedian(a),depth:this.getMedianDepth(a,d*c)});}return e}iqr(){return this.data.iqr===void 0&&(this.hinges(),this.data.iqr=this.getIQR(this.data.hinges)),this.data.iqr}getIQR(n){let t=n[0]?.datum,e=n[1]?.datum;return t===void 0||e===void 0?NaN:Math.abs(t-e)}fences(){return this.data.fences||(this.median(),this.iqr(),this.data.fences=this.getFences()),this.data.fences}getFences(n=Z){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=e*n;return [t-o,t+o]}outer(){return this.data.outer||(this.median(),this.iqr(),this.data.outer=this.getOuter()),this.data.outer}getOuter(n=Z){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=2*e*n;return [t-o,t+o]}outside(){return this.data.outside||(this.outer(),this.data.outside=this.getOutside()),this.data.outside}getOutside(){let n=[],t=this.data.sorted,e=this.data.outer;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)(s>r||s<o)&&n.push(s);return n}inside(){return this.data.inside||(this.fences(),this.data.inside=this.getInside()),this.data.inside}getInside(){let n=[],t=this.data.sorted,e=this.data.fences;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)s<r&&s>o&&n.push(s);return n}outliers(){return this.data.outliers||(this.fences(),this.data.outliers=this.getOutliers()),this.data.outliers}getOutliers(){let n=[],t=this.data.sorted,e=this.data.fences;if(e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let s of t)(s>r||s<o)&&n.push(s);return n}ranked(){return this.data.ranked||(this.sorted(),this.data.ranked=this.getRanked(this.data.sorted)),this.data.ranked}getRanked(n,t=true){let e={},o={},r=n.length,s=[],c=NaN,i=[],d=()=>{c=NaN,i=[];};for(let h=0;h<n.length;h++){let l=n[h];if(!t)e[l]={rank:h+1,peers:0},o[l]={rank:r-h,peers:0};else {let m=h+1,N=h-1;l===n[N]?(!isNaN(c)&&i.length===0?(i.push(l),s.push(i),d()):(i.push(l),c=N),l!==n[m]&&(s.push(i),d())):l!==n[m]?i.length>0?(s.push(i),d()):s.push(l):i.push(l);}}let a=0;for(let h=0;h<s.length;h++){let l=s[h];if(typeof l=="number")o[l]={rank:h+1+a,peers:0},e[l]={rank:r-h-a,peers:0};else if(Array.isArray(l)){a+=l.length;let m=l[0];o[m]={rank:h+1+a,peers:l.length},e[m]={rank:r-h-a,peers:l.length};}else a+=1;}return {up:e,down:o,groups:{down:[...s],up:[...s].reverse()}}}adjacent(){return this.data.adjacent||(this.fences(),this.data.adjacent=this.getAdjacent(this.data.sorted,this.data.fences)),this.data.adjacent}getAdjacent(n,t){if(t.length===0)return [];let e=t[0],o=[],r=t[1],s=[];for(let c of n)c>e&&o.push(c),c<r&&s.push(c);return o.sort((c,i)=>c-i),s.sort((c,i)=>c-i),[o[0],s[s.length-1]]}binned(n=NaN){return this.data.binned||(this.sorted(),this.extremes(),this.data.binned=this.getBinned(this.data.sorted,n)),this.data.binned}getBinned(n,t=10,e=NaN,o=true){let r={},s=n.length,c=o?0:1;if(s===0)return {bins:0,width:NaN,binned:{}};let i=this.data.extremes,d=e;if(i&&isNaN(d)&&i.length===2){d=(i[1]-i[0])/(Math.log(n.length)/Math.LN2),d=Math.floor(d);let h=true;for(let l of n)if(l%1!==0){h=false;break}h&&(d=Math.floor(d));}let a=Math.floor(i[1]/d)+1;(!a||a<1)&&(a=1);for(let h of n){let l=Math.floor((h-c)/d);r[l]||(r[l]={from:l*d+c,to:(l+1)*d+c-1,data:[]}),r[l].data.push(h);}return {bins:a,width:d,binned:r}}logs(){return this.data.logs||(this.data.logs=this.getLogs(this.data.original)),this.data.logs}getLogs(n){return n.map(t=>Math.log(t))}roots(){return this.data.roots||(this.data.roots=this.getRoots(this.data.original)),this.data.roots}getRoots(n){return n.map(t=>Math.sqrt(t))}inverse(){return this.data.inverse||(this.data.inverse=this.getInverse(this.data.original)),this.data.inverse}getInverse(n){return n.map(t=>1/t)}hanning(){return this.data.hanning||(this.data.hanning=this.getSkipMeans(this.data.original)),this.data.hanning}getSkipMeans(n){let t=[];for(let e=0;e<n.length;e++)e!==0&&e!==n.length-1&&t.push((n[e]+n[e+1])/2);return t.unshift(n[0]),t.push(n[n.length-1]),t}smooth(){return this.data.smooth||(this.sorted(),this.data.smooth=this.getSmooth(this.data.original)),this.data.rough=this.getRough(this.data.original,this.data.smooth),this.data.smooth}getRough(n,t){let e=[];for(let o=0;o<n.length;o++)e.push(n[o]-t[o]);return e}getSmooth(n,t=3){let e=[...n];return e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothSplit(e,2),e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothMedian(e,t),e}smoothExtremes(n,t=1,e=0,o="both"){let r=n.length;if(r<=2)return [...n];let s=[...n];for(let c=e;c<t||t===-1;c++){let i=false;if(o==="both"||o==="head"){let d=s[0],a=s[1],h=s[2],l=a-2*(h-a),m=d<=a?a<=l?a:d<=l?l:d:d<=l?d:a<=l?l:a;s[0]!==m&&(s[0]=m,i=true);}if(o==="both"||o==="tail"){let d=s[r-3],a=s[r-2],h=s[r-1],l=a-2*(d-a),m=h<=a?a<=l?a:h<=l?l:h:h<=l?h:a<=l?l:a;s[r-1]!==m&&(s[r-1]=m,i=true);}if(t===-1&&!i)break}return s}smoothSplit(n,t=ye,e=0){let o=[...n],r=n.length;for(let s=e;s<t||t===-1;s++){let c=false;for(let i=2;i<r-1;i++){let d=o[i],a=o[i-1],h=o[i-2],l=o[i+1];if(d===a&&(a>h&&d>l||a<h&&d<l)){let m=this.smoothExtremes(o.slice(0,i)),N=this.smoothExtremes(o.slice(i));o=m.concat(N),c=true;}}if(t===-1&&!c)return o}return o}smoothMedian(n,t=1,e=0){let o=n,r=n.length;if(r<=2)return [...n];for(let s=e;s<t||t===-1;s++){let c=new Array(r);c[0]=o[0],c[r-1]=o[r-1];let i=false;for(let d=1;d<r-1;d++){let a=o[d],h=Math.min(Math.max(o[d-1],a),o[d+1]);c[d]=h,h!==a&&(i=true);}if(t===-1&&!i)return o;o=c;}return o}jitter(n,t=1,e=NaN,o=Me,r=NaN,s=0){let c=s+1,i=[...n];if(c<=t){let d=[];for(let a of i){let h=r;!h&&!isNaN(h)&&(h=(1+Math.floor(a/10))*(Math.random()>.5?1:-1));let l=a+Math.floor(Math.random()*o*h);!isNaN(e)&&l<e&&(l=e),d.push(l);}return this.jitter(d,t,e,o,r,c)}return i}trimean(){let n=this.median(),t=this.hinges();return t.length<2?n.datum:(t[0].datum+2*n.datum+t[1].datum)/4}letterValues(){this.sorted();let n=this.data.sorted.length;if(n<2)return [];let t=["M","F","E","D","C","B","A","Z","Y","X","W","V","U","T","S"],e=[],o=(n+1)/2,r=this.median().datum;e.push({letter:"M",depth:o,lower:r,upper:r,mid:r,spread:0});let s=o,c=1;for(;s>1&&c<t.length&&(s=Math.floor((Math.floor(s)+1)/2),!(s<1));){let i=Math.ceil(s)-1,d=n-Math.ceil(s);if(i<0||d>=n||i>=d)break;let a=this.data.sorted[i],h=this.data.sorted[d],l=(a+h)/2,m=h-a;e.push({letter:t[c],depth:s,lower:a,upper:h,mid:l,spread:m}),c++;}return e}rough(){return this.data.rough||this.smooth(),this.data.rough||[]}stemLeaf(n=1){this.sorted();let t=this.data.sorted;if(!t.length)return {stems:[],leaves:{},display:[]};let e=Math.pow(10,n),o=new Map;for(let d of t){let a=Math.floor(d/e),h=Math.abs(Math.round(d%e));o.has(a)||o.set(a,[]),o.get(a).push(h);}let r=Array.from(o.keys()).sort((d,a)=>d-a),s=[],c={},i=[];for(let d of r){let a=String(d);s.push(a);let h=o.get(d).sort((l,m)=>l-m).map(String);c[a]=h,i.push(`${a.padStart(4)} | ${h.join(" ")}`);}return {stems:s,leaves:c,display:i}}midSummaries(){return this.letterValues().map(({depth:t,mid:e,spread:o})=>({depth:t,mid:e,spread:o}))}describe(){return this.data.description={original:this.data.original,summary:{median:this.median(),mean:this.mean(),mode:this.mode(),hinges:this.hinges(),adjacent:this.adjacent(),outliers:this.outliers(),outer:this.outer(),outside:this.outside(),inside:this.inside(),extremes:this.extremes(),iqr:this.iqr(),fences:this.fences()},smooths:{smooth:this.smooth(),hanning:this.hanning()},transforms:{logs:this.logs(),roots:this.roots(),inverse:this.inverse()},counts:this.counts(),sorted:this.sorted(),ranked:this.ranked(),binned:this.binned()},this.data.description}},ne=class{data;dimension;count;constructor(n={}){typeof n=="number"?(this.count=n,this.dimension=K,this.data={original:ee(this.count,this.dimension)}):(this.dimension=n.dimensionality??2,this.count=n.count??100,this.data={original:n.data??ee(this.count,this.dimension)});}describe(){return this.data.description={original:this.data.original},this.data.description}},oe=class{smoothed=false;static DEFAULT_MAX_RANDOM_INTEGER=B;static DEFAULT_MIN_RANDOM_INTEGER=tt;static DEFAULT_RANDOM_SERIES_COUNT=re;static DEFAULT_OUTLIER_MULTIPLE=Z;static DEFAULT_JITTER_MULTIPLIER=Me;static DEFAULT_SPLIT_PASSES=ye;static DEFAULT_MAX_RANDOM_DIMENSIONALITY=K;static Series=te;static Points=ne;static randomInteger=we;static randomSeries=Re;static randomPoint=Se;static randomPoints=ee},ht=oe;export{E as GraphAlgorithmError,Q as GraphCatalog,ne as Points,te as Series,oe as Twokeys,Ie as aStarShortestPath,Ne as allPairsShortestPaths,rt as analyzeGraph,Qe as articulationPointsAndBridges,Je as betweennessCentrality,v as buildGraphAdjacency,Xe as closenessCentrality,et as createGraphCatalog,ht as default,He as degreeCentrality,ut as gds,st as kMeansAuto,Ze as kMeansClustering,fe as kNearestNeighbors,he as labelPropagationCommunities,it as linkPrediction,me as louvainCommunities,be as maximumFlow,xe as minCostMaxFlow,ot as minimumSpanningTree,ge as nodeSimilarity,X as pageRank,J as predictLinks,j as shortestPath,le as stronglyConnectedComponents,nt as topologicalSort,dt as travelingSalesmanApprox,$e as weaklyConnectedComponents,pe as yenKShortestPaths};//# sourceMappingURL=index.js.map
1
+ function se(c,n){let t=Math.min(c.length,n.length);if(t===0)return 0;let e=0,o=0,r=0;for(let a=0;a<t;a+=1){let s=c[a]??0,d=n[a]??0;e+=s*d,o+=s*s,r+=d*d;}let i=Math.sqrt(o)*Math.sqrt(r);return i===0?0:e/i}function V(c,n){let t=0,e=Math.max(c.length,n.length);for(let o=0;o<e;o+=1){let r=(c[o]??0)-(n[o]??0);t+=r*r;}return t}function U(c,n){return Math.sqrt(V(c,n))}function ie(c,n){let t=0,e=Math.max(c.length,n.length);for(let o=0;o<e;o+=1)t+=Math.abs((c[o]??0)-(n[o]??0));return t}function It(c,n,t,e=1e-8){let o=Math.min(c.length,n.length,t.length);if(o===0)return 0;let r=0;for(let i=0;i<o;i+=1){let a=(c[i]??0)-(n[i]??0),s=Math.max(t[i]??0,e);r+=a*a/s;}return Math.sqrt(r)}function pt(c){let n=0;for(let e=0;e<c.length;e+=1){let o=c[e]??0;n+=o*o;}let t=Math.sqrt(n);return t===0?c.map(()=>0):c.map(e=>(e??0)/t)}function bt(c,n){if(c.size===0||n.size===0)return 0;let t=0,e=0,o=0,[r,i]=c.size<=n.size?[c,n]:[n,c];for(let[s,d]of r){let u=i.get(s);u!==void 0&&(t+=d*u),e+=d*d;}for(let s of i.values())o+=s*s;let a=Math.sqrt(e)*Math.sqrt(o);return a===0?0:t/a}function xt(c,n){if(c.size===0||n.size===0)return 0;let t=0,[e,o]=c.size<=n.size?[c,n]:[n,c];for(let i of e)o.has(i)&&(t+=1);let r=c.size+n.size-t;return r<=0?0:t/r}function Mt(c,n){if(c.size===0||n.size===0)return 0;let t=0,[e,o]=c.size<=n.size?[c,n]:[n,c];for(let i of e)o.has(i)&&(t+=1);let r=Math.min(c.size,n.size);return r<=0?0:t/r}var R=class extends Error{code;constructor(n,t){super(t),this.name="GraphAlgorithmError",this.code=n;}},W=class{heap=[];get size(){return this.heap.length}push(n,t){this.heap.push({priority:t,value:n}),this.siftUp(this.heap.length-1);}pop(){if(this.heap.length===0)return;let n=this.heap[0],t=this.heap.pop();return this.heap.length>0&&t&&(this.heap[0]=t,this.siftDown(0)),n}siftUp(n){let t=n;for(;t>0;){let e=Math.floor((t-1)/2),o=this.heap[t],r=this.heap[e];if(!o||!r||r.priority<=o.priority)return;this.heap[e]=o,this.heap[t]=r,t=e;}}siftDown(n){let t=n;for(;;){let e=t*2+1,o=t*2+2,r=t,i=this.heap[r],a=this.heap[e],s=this.heap[o];i&&a&&a.priority<i.priority&&(r=e);let d=this.heap[r];if(d&&s&&s.priority<d.priority&&(r=o),r===t)return;let u=this.heap[t],h=this.heap[r];if(!u||!h)return;this.heap[t]=h,this.heap[r]=u,t=r;}}};function A(c,n){return c.localeCompare(n)}function te(c,n){return A(c,n)<=0?{from:c,to:n}:{from:n,to:c}}function $(c,n){let t=te(c,n);return `${t.from}\0${t.to}`}function Ve(c){return typeof c=="string"?c:c.id}var de=class{parent=new Map;rank=new Map;constructor(n){for(let t of n)this.parent.set(t,t),this.rank.set(t,0);}find(n){let t=this.parent.get(n);if(!t||t===n)return n;let e=this.find(t);return this.parent.set(n,e),e}union(n,t){let e=this.find(n),o=this.find(t);if(e===o)return false;let r=this.rank.get(e)??0,i=this.rank.get(o)??0;return r<i?(this.parent.set(e,o),true):r>i?(this.parent.set(o,e),true):(this.parent.set(o,e),this.rank.set(e,r+1),true)}};function ne(c,n){let t=new Set,e=[];for(let o of c){let r=Ve(o);t.has(r)||(t.add(r),e.push(r));}for(let o of n)t.has(o.from)||(t.add(o.from),e.push(o.from)),t.has(o.to)||(t.add(o.to),e.push(o.to));return e}function qe(c){if(c===void 0)return 1;if(!Number.isFinite(c))throw new R("INVALID_ARGUMENT",`Graph edge weight must be finite, received ${c}`);return c}function Ke(c){let n=new Map;for(let t of c)n.set(t,new Map);return n}function Me(c,n,t,e){let o=c.get(n);if(!o)return;let r=o.get(t);(r===void 0||e<r)&&o.set(t,e);}function Ye(c,n,t){let e=Ke(c);for(let o of n){if(o.from===o.to)continue;let r=qe(o.weight);!e.has(o.from)||!e.has(o.to)||(Me(e,o.from,o.to,r),t||Me(e,o.to,o.from,r));}return e}function Ue(c){let n=new Map;for(let[t,e]of c.entries()){let o=[];for(let[r,i]of e.entries())o.push({id:r,weight:i});n.set(t,o);}return n}function We(c,n){let t=new Map;for(let e of c)t.set(e,[]);for(let[e,o]of n.entries())for(let r of o){let i=t.get(r.id);i&&i.push({id:e,weight:r.weight});}return t}function $e(c){let n=[];for(let[t,e]of c.entries())for(let o of e)n.push({from:t,to:o.id,weight:o.weight});return n}function D(c){for(let n of c.edges)if(n.weight<0)return true;return false}function ue(c,n,t){if(c===n)return [c];let e=[n],o=n;for(;o&&o!==c;){let r=t.get(o);if(!r)return [];e.push(r),o=r;}return e.reverse(),e[0]===c?e:[]}function ye(c){return c.reduce((n,t)=>n+t.weight,0)}function He(c){let n=new Map;for(let t of c.edges){let e=te(t.from,t.to),o=$(e.from,e.to),r=n.get(o);(!r||t.weight<r.weight-1e-12)&&n.set(o,{from:e.from,to:e.to,weight:t.weight});}return Array.from(n.values())}function Xe(c,n){let t=He(c),e=0;for(let s of t)e+=s.weight;if(!Number.isFinite(e)||e<=0)return 0;let o=new Map;for(let s of c.nodes)o.set(s,ye(c.neighborsByNode.get(s)??[]));let r=new Map,i=new Map;for(let s of c.nodes){let d=n.get(s);if(d===void 0)continue;let u=o.get(s)??0;r.set(d,(r.get(d)??0)+u);}for(let s of t){let d=n.get(s.from),u=n.get(s.to);d===void 0||u===void 0||d===u&&i.set(d,(i.get(d)??0)+s.weight);}let a=0;for(let[s,d]of r.entries()){let u=i.get(s)??0;a+=u/e-Math.pow(d/(2*e),2);}return a}function ae(c,n,t,e,o){let r=new Map;for(let s of c.nodes){let d=n.get(s);if(d===void 0)continue;let u=r.get(d)??[];u.push(s),r.set(d,u);}let i=Array.from(r.values()).map(s=>[...s].sort(A));i.sort((s,d)=>A(s[0]??"",d[0]??""));let a=new Map;for(let s=0;s<i.length;s+=1)for(let d of i[s]??[])a.set(d,s);return {communities:i,communityByNode:a,iterations:t,converged:e,algorithm:o,modularity:Xe(c,a)}}function le(c){let n=new Map;for(let t of c.nodes)n.set(t,new Set);for(let[t,e]of c.neighborsByNode.entries()){let o=n.get(t);if(o)for(let r of e)o.add(r.id);}return n}function Z(c,n,t,e){let o=t.get(c)??new Set,r=t.get(n)??new Set,i=o.size,a=r.size,s=i<=a?o:r,d=s===o?r:o,u=0,h=0,l=0;for(let I of s){if(!d.has(I))continue;u+=1;let b=t.get(I)?.size??0;b>1&&(h+=1/Math.log(b)),b>0&&(l+=1/b);}if(e==="common-neighbors")return u;if(e==="preferential-attachment")return i*a;if(e==="adamic-adar")return h;if(e==="resource-allocation")return l;if(e==="jaccard"){let I=i+a-u;return I>0?u/I:0}if(e==="cosine")return i>0&&a>0?u/Math.sqrt(i*a):0;let f=Math.min(i,a);return f>0?u/f:0}function Je(c,n){if(c.length<=1)return 0;let t=0;for(let e=1;e<c.length;e+=1){let o=c[e-1],r=c[e];if(!o||!r)return Number.POSITIVE_INFINITY;let i=n.neighborsByNode.get(o)?.find(a=>a.id===r)?.weight;if(i===void 0||!Number.isFinite(i))return Number.POSITIVE_INFINITY;t+=i;}return t}function Q(c){return c.join("\0")}function Qe(c,n){if(n.length>c.length)return false;for(let t=0;t<n.length;t+=1)if(c[t]!==n[t])return false;return true}function we(c,n){return c==="auto"?n?"bellman-ford":"dijkstra":c}function Ze(c,n){if(D(c))throw new R("NEGATIVE_WEIGHT","Dijkstra cannot run on negative graph weights.");let t=new Map,e=new Map,o=new Set,r=new W;for(let i of c.nodes)t.set(i,Number.POSITIVE_INFINITY);for(t.set(n,0),r.push(n,0);r.size>0;){let i=r.pop();if(!i)break;let a=i.value;if(o.has(a))continue;o.add(a);let s=t.get(a);if(s===void 0||!Number.isFinite(s))continue;let d=c.neighborsByNode.get(a)??[];for(let u of d){let h=s+u.weight,l=t.get(u.id);(l===void 0||h<l-1e-12)&&(t.set(u.id,h),e.set(u.id,a),r.push(u.id,h));}}return {distanceByNode:t,previousByNode:e,explored:o.size,negativeCycleNodes:new Set}}function et(c,n){let t=new Map,e=new Map;for(let d of c.nodes)t.set(d,Number.POSITIVE_INFINITY);t.set(n,0);let o=c.nodes.length;for(let d=0;d<o-1;d+=1){let u=false;for(let h of c.edges){let l=t.get(h.from);if(l===void 0||!Number.isFinite(l))continue;let f=l+h.weight,I=t.get(h.to);(I===void 0||f<I-1e-12)&&(t.set(h.to,f),e.set(h.to,h.from),u=true);}if(!u)break}let r=new Set,i=[];for(let d of c.edges){let u=t.get(d.from),h=t.get(d.to);u!==void 0&&Number.isFinite(u)&&h!==void 0&&u+d.weight<h-1e-12&&(r.has(d.to)||(r.add(d.to),i.push(d.to)),r.has(d.from)||(r.add(d.from),i.push(d.from)));}let a=0;for(;a<i.length;){let d=i[a];if(a+=1,!!d)for(let u of c.neighborsByNode.get(d)??[])r.has(u.id)||(r.add(u.id),i.push(u.id));}if(r.size>0)for(let d of r)t.set(d,Number.NEGATIVE_INFINITY),e.delete(d);let s=0;for(let d of c.nodes){let u=t.get(d);u!==void 0&&Number.isFinite(u)&&(s+=1);}return {distanceByNode:t,previousByNode:e,explored:s,negativeCycleNodes:r}}function oe(c,n,t){let e=D(c),o=we(t,e);return o==="dijkstra"?{algorithm:o,result:Ze(c,n),hasNegativeWeights:e}:{algorithm:o,result:et(c,n),hasNegativeWeights:e}}function Se(c){if(c===void 0||!Number.isFinite(c))return Math.random;let n=Math.floor(c)>>>0||1;return ()=>(n=n*1664525+1013904223>>>0,n/4294967296)}function tt(c,n){let t=c[0]?.length??0;if(n==="none")return {normalize:s=>[...s],denormalize:s=>[...s]};let e=new Array(t).fill(0),o=new Array(t).fill(Number.POSITIVE_INFINITY),r=new Array(t).fill(Number.NEGATIVE_INFINITY);for(let s of c)for(let d=0;d<t;d+=1){let u=s[d]??0;e[d]=(e[d]??0)+u,o[d]=Math.min(o[d]??u,u),r[d]=Math.max(r[d]??u,u);}for(let s=0;s<t;s+=1)e[s]=(e[s]??0)/c.length;if(n==="minmax"){let s=r.map((d,u)=>{let h=o[u]??0;return d-h});return {normalize:d=>d.map((u,h)=>{let l=o[h]??0,f=s[h]??0;return Math.abs(f)<1e-12?0:(u-l)/f}),denormalize:d=>d.map((u,h)=>{let l=o[h]??0,f=s[h]??0;return u*f+l})}}let i=new Array(t).fill(0);for(let s of c)for(let d=0;d<t;d+=1){let u=(s[d]??0)-(e[d]??0);i[d]=(i[d]??0)+u*u;}let a=i.map(s=>Math.sqrt(s/c.length));return {normalize:s=>s.map((d,u)=>{let h=a[u]??0;return h<1e-12?0:(d-(e[u]??0))/h}),denormalize:s=>s.map((d,u)=>{let h=a[u]??0;return d*h+(e[u]??0)})}}function nt(c,n,t,e){if(c.length===0)return [];if(!e){let a=[],s=new Set;for(;a.length<n;){let d=Math.floor(t()*c.length);s.has(d)||(s.add(d),a.push([...c[d]??c[0]??[]]));}return a}let o=[],r=new Set,i=Math.floor(t()*c.length);for(r.add(i),o.push([...c[i]??c[0]??[]]);o.length<n;){let a=new Array(c.length).fill(0),s=0;for(let l=0;l<c.length;l+=1){if(r.has(l))continue;let f=c[l];if(!f)continue;let I=Number.POSITIVE_INFINITY;for(let b of o)I=Math.min(I,V(f,b));a[l]=I,s+=I;}if(!Number.isFinite(s)||s<=0){for(let l=0;l<c.length;l+=1)if(!r.has(l)){r.add(l),o.push([...c[l]??c[0]??[]]);break}continue}let d=t()*s,u=0,h=-1;for(let l=0;l<c.length;l+=1)if(!r.has(l)&&(u+=a[l]??0,u>=d)){h=l;break}if(h<0){for(let l=0;l<c.length;l+=1)if(!r.has(l)){h=l;break}}if(h<0)break;r.add(h),o.push([...c[h]??c[0]??[]]);}return o}function ot(c,n,t,e,o,r){let i=c[0]?.length??0,a=nt(c,n,t,r);if(a.length!==n)throw new R("INVALID_ARGUMENT","Unable to initialize centroids for k-means.");let s=new Array(c.length).fill(-1),d=0,u=false,h=Number.POSITIVE_INFINITY;for(;d<e;){d+=1;let l=false;h=0;for(let p=0;p<c.length;p+=1){let N=c[p];if(!N)continue;let m=0,g=Number.POSITIVE_INFINITY;for(let x=0;x<a.length;x+=1){let w=a[x];if(!w)continue;let M=V(N,w);M<g&&(g=M,m=x);}h+=g,s[p]!==m&&(s[p]=m,l=true);}let f=new Array(n),I=new Array(n).fill(0);for(let p=0;p<n;p+=1)f[p]=new Array(i).fill(0);for(let p=0;p<c.length;p+=1){let N=s[p];if(N<0)continue;let m=c[p],g=f[N];if(!(!m||!g)){I[N]=(I[N]??0)+1;for(let x=0;x<i;x+=1)g[x]=(g[x]??0)+(m[x]??0);}}for(let p=0;p<n;p+=1){let N=I[p]??0;if(N>0){let m=f[p];if(!m)continue;for(let g=0;g<i;g+=1)m[g]=(m[g]??0)/N;}else {let m=0,g=-1;for(let x=0;x<c.length;x+=1){let w=s[x],M=a[w],y=c[x];if(!M||!y)continue;let S=V(y,M);S>g&&(g=S,m=x);}f[p]=[...c[m]??c[0]??[]];}}let b=0;for(let p=0;p<n;p+=1){let N=a[p],m=f[p];!N||!m||(b=Math.max(b,Math.sqrt(V(N,m))));}if(a=f,!l||b<=o){u=true;break}}return {assignments:s,centroidsNormalized:a,iterations:d,inertia:h,converged:u}}function ve(c,n,t){if(c.length<=1||t<=1)return null;let e=Array.from({length:t},()=>[]);for(let i=0;i<n.length;i+=1){let a=n[i]??-1;if(a>=0&&a<t){let s=e[a];s&&s.push(i);}}let o=0,r=0;for(let i=0;i<c.length;i+=1){let a=c[i],s=n[i]??-1;if(!a||s<0||s>=t)continue;let d=e[s]??[];if(d.length<=1)continue;let u=0;for(let f of d){if(f===i)continue;let I=c[f];I&&(u+=U(a,I));}u/=d.length-1;let h=Number.POSITIVE_INFINITY;for(let f=0;f<t;f+=1){if(f===s)continue;let I=e[f]??[];if(I.length===0)continue;let b=0;for(let p of I){let N=c[p];N&&(b+=U(a,N));}b/=I.length,h=Math.min(h,b);}if(!Number.isFinite(h))continue;let l=Math.max(u,h);l<=0||(o+=(h-u)/l,r+=1);}return r===0?null:o/r}function ce(c,n){let t=0;for(let e=1;e<c.length;e+=1){let o=c[e-1],r=c[e];if(!o||!r)return Number.POSITIVE_INFINITY;let i=n.get(o)?.get(r);if(i===void 0||!Number.isFinite(i))return Number.POSITIVE_INFINITY;t+=i;}return t}function rt(c,n,t){if(c.length<4||t<=0)return c;let e=[...c],o=ce(e,n);if(!Number.isFinite(o))return e;for(let r=0;r<t;r+=1){let i=false;for(let a=1;a<e.length-2;a+=1)for(let s=a+1;s<e.length-1;s+=1){let d=e.slice(0,a),u=e.slice(a,s+1).reverse(),h=e.slice(s+1),l=d.concat(u,h),f=ce(l,n);f+1e-9<o&&(e=l,o=f,i=true);}if(!i)break}return e}function st(c,n){let t=[],e=new Set;n.start&&c.includes(n.start)&&(t.push(n.start),e.add(n.start));for(let a of n.startCandidates??[])c.includes(a)&&!e.has(a)&&(t.push(a),e.add(a));let o=Se(n.seed),r=c.filter(a=>!e.has(a));for(let a=r.length-1;a>0;a-=1){let s=Math.floor(o()*(a+1)),d=r[a],u=r[s];d!==void 0&&u!==void 0&&(r[a]=u,r[s]=d);}let i=Math.max(1,Math.min(c.length,n.multiStartCount??Math.min(8,c.length)));for(let a of r){if(t.length>=i)break;t.push(a);}return t.length===0&&c.length>0&&t.push(c[0]),t}function it(c,n,t,e){let o=[c],r=new Set(n.filter(a=>a!==c)),i=c;for(;r.size>0;){let a=null,s=Number.POSITIVE_INFINITY,d=t.get(i);for(let u of r){let h=d?.get(u)??Number.POSITIVE_INFINITY;h<s&&(s=h,a=u);}if(!a||!Number.isFinite(s))break;o.push(a),r.delete(a),i=a;}return e&&o.length>1&&o.push(c),o}function dt(c,n){if(c.length<=1)return 0;let t=new Set;t.add(c[0]);let e=0;for(;t.size<c.length;){let o=Number.POSITIVE_INFINITY,r=null;for(let i of t){let a=n.get(i);for(let s of c){if(t.has(s))continue;let d=a?.get(s)??Number.POSITIVE_INFINITY;d<o&&(o=d,r=s);}}if(!r||!Number.isFinite(o))return Number.POSITIVE_INFINITY;t.add(r),e+=o;}return e}function k(c,n,t={}){let e=t.directed??true,o=ne(c,n),r=Ye(o,n,e),i=Ue(r),a=We(o,i);return {nodes:o,neighborsByNode:i,incomingByNode:a,edges:$e(i),directed:e}}function Re(c,n,t={}){let e=k(c,n,t),o=[];if(e.directed){let i=0,a=new Map,s=new Map,d=[],u=new Set,h=l=>{a.set(l,i),s.set(l,i),i+=1,d.push(l),u.add(l);for(let f of e.neighborsByNode.get(l)??[])if(a.has(f.id)){if(u.has(f.id)){let I=s.get(l)??0,b=a.get(f.id)??0;s.set(l,Math.min(I,b));}}else {h(f.id);let I=s.get(l)??0,b=s.get(f.id)??0;s.set(l,Math.min(I,b));}if((s.get(l)??-1)===(a.get(l)??-2)){let f=[];for(;d.length>0;){let I=d.pop();if(!I||(u.delete(I),f.push(I),I===l))break}f.sort(A),o.push(f);}};for(let l of e.nodes)a.has(l)||h(l);}else {let i=new Set;for(let a of e.nodes){if(i.has(a))continue;let s=[a];i.add(a);let d=[],u=0;for(;u<s.length;){let h=s[u];if(u+=1,!!h){d.push(h);for(let l of e.neighborsByNode.get(h)??[])i.has(l.id)||(i.add(l.id),s.push(l.id));}}d.sort(A),o.push(d);}}o.sort((i,a)=>{let s=i[0]??"",d=a[0]??"";return A(s,d)});let r=new Map;for(let i=0;i<o.length;i+=1)for(let a of o[i]??[])r.set(a,i);return {components:o,componentByNode:r}}function at(c,n){let t=k(c,n,{directed:false}),e=[],o=new Map,r=new Set;for(let i of t.nodes){if(r.has(i))continue;let a=[i];r.add(i);let s=[],d=0;for(;d<a.length;){let h=a[d];if(d+=1,!!h){s.push(h);for(let l of t.neighborsByNode.get(h)??[])r.has(l.id)||(r.add(l.id),a.push(l.id));}}s.sort(A);let u=e.length;e.push(s);for(let h of s)o.set(h,u);}e.sort((i,a)=>{let s=i[0]??"",d=a[0]??"";return A(s,d)}),o.clear();for(let i=0;i<e.length;i+=1){let a=e[i]??[];for(let s of a)o.set(s,i);}return {components:e,componentByNode:o}}function St(c,n,t={}){let e=ne(c,n),o=t.tieBreaker??((N,m)=>A(N,m)),r=t.priorityByNode,i=t.priority??(N=>r instanceof Map?r.get(N)??0:r?r[N]??0:0),a=(N,m)=>{let g=i(m)-i(N);return Math.abs(g)>1e-12?g:o(N,m)},s=new Map,d=new Map;for(let N of e)s.set(N,new Set),d.set(N,0);for(let N of n){if(N.from===N.to||!s.has(N.from)||!s.has(N.to))continue;let m=s.get(N.from);!m||m.has(N.to)||(m.add(N.to),d.set(N.to,(d.get(N.to)??0)+1));}let u=e.filter(N=>(d.get(N)??0)===0).sort(a),h=[];for(;u.length>0;){let N=u.shift();if(!N)continue;h.push(N);let m=Array.from(s.get(N)??[]).sort(a);for(let g of m){let x=(d.get(g)??0)-1;d.set(g,x),x===0&&u.push(g);}u.sort(a);}let l=new Set(h),f=e.filter(N=>!l.has(N));f.sort(a);let I=Re(e,n,{directed:true}),b=new Set(n.filter(N=>N.from===N.to).map(N=>N.from)),p=new Set;for(let N of I.components)if(N.length>1)for(let m of N)p.add(m);else if(N.length===1){let m=N[0];m&&b.has(m)&&p.add(m);}for(let N of f)p.add(N);for(let N of f)l.has(N)||(h.push(N),l.add(N));return {order:h,cycleNodes:Array.from(p).sort(a),isDag:p.size===0}}function ct(c,n,t={}){let e=k(c,n,t),o=e.nodes.length,r=o<=1?1:e.directed?2*(o-1):Math.max(1,o-1),i=new Map;for(let a of e.nodes){let s=e.neighborsByNode.get(a)?.length??0,d=e.incomingByNode.get(a)?.length??0,u=e.directed?d+s:s;i.set(a,{inDegree:d,outDegree:s,degree:u,normalized:u/r});}return i}function ut(c,n,t={}){let e=k(c,n,t),o=e.nodes.length,r=Math.max(0,o-1),i=t.mode??"harmonic",a=new Map;for(let s of e.nodes){let d=oe(e,s,t.shortestPathAlgorithm??"auto"),u=d.result.distanceByNode,h=d.result.negativeCycleNodes.size>0,l=0,f=0,I=0;for(let N of e.nodes){if(N===s)continue;let m=u.get(N);m===void 0||!Number.isFinite(m)||m<=0||(l+=1,f+=m,I+=1/m);}let b=0,p=0;h||(i==="classic"?(b=l>0&&f>0?l/f:0,p=r>0?b*(l/r):0):(b=I,p=r>0?I/r:0)),a.set(s,{reachableCount:l,distanceSum:f,score:b,normalized:p,mode:i,negativeCycle:h});}return a}function lt(c,n,t={}){let e=k(c,n,t);if(D(e))throw new R("NEGATIVE_WEIGHT","Betweenness centrality requires non-negative graph weights.");let o=new Map;for(let s of e.nodes)o.set(s,0);for(let s of e.nodes){let d=[],u=new Map,h=new Map,l=new Map,f=new W,I=new Set;for(let p of e.nodes)u.set(p,[]),h.set(p,0),l.set(p,Number.POSITIVE_INFINITY);for(h.set(s,1),l.set(s,0),f.push(s,0);f.size>0;){let p=f.pop();if(!p)break;let N=p.value;if(I.has(N))continue;I.add(N),d.push(N);let m=l.get(N);if(m===void 0||!Number.isFinite(m))continue;let g=h.get(N)??0;for(let x of e.neighborsByNode.get(N)??[]){let w=m+x.weight,M=l.get(x.id);M===void 0||w<M-1e-12?(l.set(x.id,w),f.push(x.id,w),h.set(x.id,g),u.set(x.id,[N])):M!==void 0&&Math.abs(w-M)<=1e-12&&(h.set(x.id,(h.get(x.id)??0)+g),u.get(x.id)?.push(N));}}let b=new Map;for(let p of e.nodes)b.set(p,0);for(;d.length>0;){let p=d.pop();if(!p)continue;let N=h.get(p)??0,m=b.get(p)??0;for(let g of u.get(p)??[]){let x=h.get(g)??0;if(N<=0)continue;let w=x/N*(1+m);b.set(g,(b.get(g)??0)+w);}p!==s&&o.set(p,(o.get(p)??0)+m);}}if(!e.directed)for(let s of e.nodes)o.set(s,(o.get(s)??0)/2);let r=e.nodes.length,i=r<=2?Number.POSITIVE_INFINITY:e.directed?(r-1)*(r-2):(r-1)*(r-2)/2,a=new Map;for(let s of e.nodes){let d=o.get(s)??0;a.set(s,{raw:d,normalized:Number.isFinite(i)&&i>0?d/i:0});}return a}function he(c,n,t={}){let e=k(c,n,t);if(D(e))throw new R("NEGATIVE_WEIGHT","PageRank requires non-negative graph weights.");let o=e.nodes.length;if(o===0)return {byNode:new Map,order:[],iterations:0,converged:true,dampingFactor:t.dampingFactor??.85};let r=t.dampingFactor??.85;if(!Number.isFinite(r)||r<0||r>=1)throw new R("INVALID_ARGUMENT",`PageRank dampingFactor must be in [0, 1), received ${r}.`);let i=Math.max(0,t.tolerance??1e-9),a=Math.max(1,Math.floor(t.maxIterations??200)),s=1/o,d=new Map;for(let N of e.nodes){let m=(e.neighborsByNode.get(N)??[]).reduce((g,x)=>g+x.weight,0);d.set(N,m);}let u=new Map;for(let N of e.nodes)u.set(N,s);let h=0,l=false;for(let N=0;N<a;N+=1){h=N+1;let m=new Map,g=(1-r)/o,x=0;for(let y of e.nodes)(d.get(y)??0)<=1e-12&&(x+=u.get(y)??0),m.set(y,g);let w=r*x/o;if(w>0)for(let y of e.nodes)m.set(y,(m.get(y)??0)+w);for(let y of e.nodes){let S=d.get(y)??0;if(S<=1e-12)continue;let v=u.get(y)??0;for(let E of e.neighborsByNode.get(y)??[]){let G=r*v*E.weight/S;m.set(E.id,(m.get(E.id)??0)+G);}}let M=0;for(let y of e.nodes){let S=u.get(y)??0,v=m.get(y)??0;M+=Math.abs(S-v);}if(u=m,M<=i){l=true;break}}let f=0;for(let N of e.nodes)f+=u.get(N)??0;if(!Number.isFinite(f)||f<=0){u=new Map;for(let N of e.nodes)u.set(N,s);f=1;}else if(Math.abs(f-1)>1e-12)for(let N of e.nodes)u.set(N,(u.get(N)??0)/f);let I=[...e.nodes].sort((N,m)=>{let g=(u.get(m)??0)-(u.get(N)??0);return Math.abs(g)>1e-12?g:A(N,m)}),b=I.length>0?u.get(I[0])??0:0,p=new Map;for(let N=0;N<I.length;N+=1){let m=I[N];if(!m)continue;let g=u.get(m)??0;p.set(m,{score:g,normalized:b>0?g/b:0,rank:N+1});}return {byNode:p,order:I,iterations:h,converged:l,dampingFactor:r}}function vt(c,n,t={}){let e=k(c,n,{directed:false}),o=t.tieBreaker??((l,f)=>A(l,f)),r=new Map;for(let l of e.edges){if(l.from===l.to)continue;let f=te(l.from,l.to),I=$(f.from,f.to),b=r.get(I);(!b||l.weight<b.weight-1e-12)&&r.set(I,{from:f.from,to:f.to,weight:l.weight});}let i=Array.from(r.values());i.sort((l,f)=>{let I=l.weight-f.weight;if(Math.abs(I)>1e-12)return I;let b=o(l.from,f.from);return b!==0?b:o(l.to,f.to)});let a=new de(e.nodes),s=[],d=0;for(let l of i)a.union(l.from,l.to)&&(s.push(l),d+=l.weight);let u=new Set;for(let l of e.nodes)u.add(a.find(l));let h=u.size;return {edges:s,totalWeight:d,componentCount:h,spanning:e.nodes.length<=1||h===1&&s.length===e.nodes.length-1}}function ht(c,n){let t=k(c,n,{directed:false}),e=new Map,o=new Map,r=new Map,i=new Set,a=new Map,s=0,d=h=>{e.set(h,s),o.set(h,s),s+=1;let l=0,f=r.get(h)??null;for(let I of t.neighborsByNode.get(h)??[]){let b=e.get(I.id);if(b===void 0){r.set(I.id,h),l+=1,d(I.id);let p=o.get(h)??0,N=o.get(I.id)??0;o.set(h,Math.min(p,N));let m=e.get(h)??0;if(f===null&&l>1&&i.add(h),f!==null&&N>=m&&i.add(h),N>m){let g=te(h,I.id),x=$(g.from,g.to),w=a.get(x);(!w||I.weight<w.weight-1e-12)&&a.set(x,{from:g.from,to:g.to,weight:I.weight});}}else if(I.id!==f){let p=o.get(h)??0;o.set(h,Math.min(p,b));}}};for(let h of t.nodes)e.has(h)||(r.set(h,null),d(h));let u=Array.from(a.values());return u.sort((h,l)=>{let f=A(h.from,l.from);if(f!==0)return f;let I=A(h.to,l.to);return I!==0?I:h.weight-l.weight}),{articulationPoints:Array.from(i).sort(A),bridges:u}}function Rt(c,n,t={}){let e=Re(c,n,t),o=at(c,n),r=ht(c,n),i=ne(c,n),a=[...i].sort(A),s=new Map,d=new Map;try{s=lt(c,n,t);}catch(u){if(!(u instanceof R)||u.code!=="NEGATIVE_WEIGHT")throw u;s=new Map;for(let h of a)s.set(h,{raw:0,normalized:0});}try{d=he(c,n,{directed:t.directed??!0,...t.pageRankOptions??{}}).byNode;}catch(u){if(!(u instanceof R)||u.code!=="NEGATIVE_WEIGHT")throw u;let h=i.length>0?1/i.length:0;d=new Map;for(let l=0;l<a.length;l+=1){let f=a[l];f&&d.set(f,{score:h,normalized:1,rank:l+1});}}return {degree:ct(c,n,t),closeness:ut(c,n,{...t,mode:t.closenessMode??"harmonic",shortestPathAlgorithm:t.shortestPathAlgorithm}),betweenness:s,pageRank:d,stronglyConnectedComponents:e.components,weaklyConnectedComponents:o.components,articulationPoints:r.articulationPoints,bridges:r.bridges}}function ee(c,n,t,e,o={}){let r=k(c,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,algorithm:o.algorithm??"auto",hasNegativeWeights:D(r),negativeCycle:false};let i=oe(r,t,o.algorithm??"auto");if(i.result.negativeCycleNodes.has(e)){if(o.failOnNegativeCycle??false)throw new R("NEGATIVE_CYCLE",`Negative cycle reaches target node "${e}".`);return {source:t,target:e,path:[],distance:Number.NEGATIVE_INFINITY,reachable:false,explored:i.result.explored,algorithm:i.algorithm,hasNegativeWeights:i.hasNegativeWeights,negativeCycle:true}}let a=i.result.distanceByNode.get(e)??Number.POSITIVE_INFINITY,s=Number.isFinite(a),d=s?ue(t,e,i.result.previousByNode):[];return {source:t,target:e,path:d,distance:a,reachable:s&&d.length>0,explored:i.result.explored,algorithm:i.algorithm,hasNegativeWeights:i.hasNegativeWeights,negativeCycle:i.result.negativeCycleNodes.size>0}}function Ee(c,n,t={}){let e=k(c,n,{directed:t.directed??false});if(D(e))throw new R("NEGATIVE_WEIGHT","Label propagation requires non-negative graph weights.");let o=t.tieBreaker??((u,h)=>A(u,h)),r=[...e.nodes].sort(o),i=new Map;for(let u=0;u<r.length;u+=1){let h=r[u];h&&i.set(h,u);}let a=Math.max(1,t.maxIterations??100),s=0,d=false;for(let u=0;u<a;u+=1){s=u+1;let h=false;for(let l of r){let f=e.neighborsByNode.get(l)??[];if(f.length===0)continue;let I=new Map;for(let m of f){let g=i.get(m.id);g!==void 0&&I.set(g,(I.get(g)??0)+m.weight);}let b=i.get(l);if(b===void 0||I.size===0)continue;let p=b,N=Number.NEGATIVE_INFINITY;for(let[m,g]of I.entries()){if(g>N+1e-12){N=g,p=m;continue}Math.abs(g-N)<=1e-12&&m<p&&(p=m);}p!==b&&(i.set(l,p),h=true);}if(!h){d=true;break}}return ae(e,i,s,d,"label-propagation")}function Ae(c,n,t={}){let e=k(c,n,{directed:t.directed??false});if(D(e))throw new R("NEGATIVE_WEIGHT","Louvain requires non-negative graph weights.");let o=t.tieBreaker??((I,b)=>A(I,b)),r=[...e.nodes].sort(o),i=Math.max(1,t.maxPasses??32),a=Math.max(0,t.tolerance??1e-9),s=new Map;for(let I of e.nodes)s.set(I,ye(e.neighborsByNode.get(I)??[]));let d=0;for(let I of s.values())d+=I;let u=new Map,h=new Map;for(let I=0;I<r.length;I+=1){let b=r[I];if(!b)continue;u.set(b,I);let p=s.get(b)??0;h.set(I,p);}if(d<=1e-12)return ae(e,u,0,true,"louvain");let l=0,f=false;for(let I=0;I<i;I+=1){l=I+1;let b=false;for(let p of r){let N=u.get(p);if(N===void 0)continue;let m=s.get(p)??0;h.set(N,(h.get(N)??0)-m);let g=new Map;for(let M of e.neighborsByNode.get(p)??[]){let y=u.get(M.id);y!==void 0&&g.set(y,(g.get(y)??0)+M.weight);}let x=N,w=0;for(let[M,y]of g.entries()){let S=h.get(M)??0,v=y-S*m/d;if(v>w+a){w=v,x=M;continue}Math.abs(v-w)<=a&&M<x&&(x=M);}x!==N&&w>a?(u.set(p,x),h.set(x,(h.get(x)??0)+m),b=true):h.set(N,(h.get(N)??0)+m);}if(!b){f=true;break}}return ae(e,u,l,f,"louvain")}function Pe(c,n,t={}){let e=k(c,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,i=le(e),a=[...e.nodes].sort(A),s=[];for(let d=0;d<a.length;d+=1){let u=a[d];if(u)for(let h=d+1;h<a.length;h+=1){let l=a[h];if(!l)continue;let f=Z(u,l,i,o);f<r-1e-12||s.push({left:u,right:l,score:f,metric:o});}}return s.sort((d,u)=>{let h=u.score-d.score;if(Math.abs(h)>1e-12)return h;let l=A(d.left,u.left);return l!==0?l:A(d.right,u.right)}),{metric:o,pairs:s.map((d,u)=>({...d,rank:u+1}))}}function ke(c,n,t={}){let e=k(c,n,{directed:t.directed??false}),o=t.metric??"jaccard",r=t.minScore??0,i=Math.max(1,Math.floor(t.k??5)),a=le(e),s=[...e.nodes].sort(A),d=new Map;for(let u of s){let h=[];for(let l of s){if(u===l)continue;let f=Z(u,l,a,o);f<r-1e-12||h.push({nodeId:l,score:f});}h.sort((l,f)=>{let I=f.score-l.score;return Math.abs(I)>1e-12?I:A(l.nodeId,f.nodeId)}),d.set(u,h.slice(0,i));}return {metric:o,k:i,neighborsByNode:d}}function me(c,n,t={}){let e=t.directed??false,o=k(c,n,{directed:e}),r=t.metric??"jaccard",i=t.minScore??0,a=Math.max(1,Math.floor(t.limit??20)),s=t.allowExistingEdges??false,d=le(o),u=[...o.nodes].sort(A),h=t.sourceFilter?new Set(t.sourceFilter):null,l=t.targetFilter?new Set(t.targetFilter):null,f=new Set;for(let b of o.edges)e?f.add(`${b.from}\0${b.to}`):f.add($(b.from,b.to));let I=[];if(e){for(let b of u)if(!(h&&!h.has(b)))for(let p of u){if(b===p||l&&!l.has(p)||!s&&f.has(`${b}\0${p}`))continue;let N=Z(b,p,d,r);N<i-1e-12||I.push({from:b,to:p,score:N,metric:r});}}else for(let b=0;b<u.length;b+=1){let p=u[b];if(p)for(let N=b+1;N<u.length;N+=1){let m=u[N];if(!m||h&&!h.has(p)&&!h.has(m)||l&&!l.has(p)&&!l.has(m)||!s&&f.has($(p,m)))continue;let g=Z(p,m,d,r);g<i-1e-12||I.push({from:p,to:m,score:g,metric:r});}}return I.sort((b,p)=>{let N=p.score-b.score;if(Math.abs(N)>1e-12)return N;let m=A(b.from,p.from);return m!==0?m:A(b.to,p.to)}),{metric:r,predictions:I.slice(0,a).map((b,p)=>({...b,rank:p+1}))}}function Et(c,n,t={}){return me(c,n,t)}function Ge(c,n,t,e,o={}){let r=k(c,n,o);if(D(r))throw new R("NEGATIVE_WEIGHT","A* requires non-negative graph weights.");if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,target:e,path:[],distance:Number.POSITIVE_INFINITY,reachable:false,explored:0,estimatedDistance:Number.POSITIVE_INFINITY};let i=o.heuristic??((p,N)=>0),a=new W,s=new Map,d=new Map,u=new Set;for(let p of r.nodes)s.set(p,Number.POSITIVE_INFINITY);s.set(t,0);let h=i(t,e);if(!Number.isFinite(h))throw new R("INVALID_ARGUMENT","A* heuristic must return finite values.");a.push(t,h);let l=0;for(;a.size>0;){let p=a.pop();if(!p)break;let N=p.value;if(u.has(N))continue;if(u.add(N),l+=1,N===e)break;let m=s.get(N);if(!(m===void 0||!Number.isFinite(m)))for(let g of r.neighborsByNode.get(N)??[]){let x=m+g.weight,w=s.get(g.id)??Number.POSITIVE_INFINITY;if(x>=w-1e-12)continue;let M=i(g.id,e);if(!Number.isFinite(M))throw new R("INVALID_ARGUMENT","A* heuristic must return finite values.");s.set(g.id,x),d.set(g.id,N),a.push(g.id,x+M);}}let f=s.get(e)??Number.POSITIVE_INFINITY,I=Number.isFinite(f),b=I?ue(t,e,d):[];return {source:t,target:e,path:b,distance:f,reachable:I&&b.length>0,explored:l,estimatedDistance:f}}function Ce(c,n,t={}){let e=k(c,n,t),o=t.algorithm??"auto",r=we(o,D(e)),i=new Map,a=new Map,s=false;for(let d of e.nodes){let u=oe(e,d,r);if(u.result.negativeCycleNodes.size>0&&(s=true,t.failOnNegativeCycle??false))throw new R("NEGATIVE_CYCLE",`Negative cycle reaches source node "${d}".`);let h=new Map,l=new Map;for(let f of e.nodes){h.set(f,u.result.distanceByNode.get(f)??Number.POSITIVE_INFINITY);let I=u.result.previousByNode.get(f);I&&l.set(f,I);}i.set(d,h),a.set(d,l);}return {nodes:e.nodes,distanceBySource:i,previousBySource:a,algorithm:r,hasNegativeWeights:D(e),negativeCycle:s}}function Te(c,n,t,e,o={}){let r=o.directed??true,i=Math.max(1,Math.floor(o.k??3)),a=o.shortestPathAlgorithm??"auto",s=k(c,n,{directed:r});if(!s.neighborsByNode.has(t)||!s.neighborsByNode.has(e))return {source:t,target:e,paths:[],complete:false};let d=ee(s.nodes,s.edges,t,e,{directed:r,algorithm:a});if(!d.reachable||d.path.length===0)return {source:t,target:e,paths:[],complete:false};let u=[{path:d.path,distance:d.distance}],h=new Map;for(let l=1;l<i;l+=1){let f=u[l-1]?.path??[];if(f.length<2)break;for(let b=0;b<f.length-1;b+=1){let p=f[b];if(!p)continue;let N=f.slice(0,b+1),m=new Set(N.slice(0,-1)),g=new Set;for(let v of u)if(v.path.length>b+1&&Qe(v.path,N)){let E=v.path[b],G=v.path[b+1];E&&G&&g.add(`${E}\0${G}`);}let x=s.edges.filter(v=>!(g.has(`${v.from}\0${v.to}`)||m.has(v.from)||m.has(v.to))),w=ee(s.nodes,x,p,e,{directed:r,algorithm:a});if(!w.reachable||w.path.length===0)continue;let M=N.slice(0,-1).concat(w.path),y=Je(M,s);if(!Number.isFinite(y))continue;let S=Q(M);h.has(S)||h.set(S,{path:M,distance:y});}if(h.size===0)break;let I=Array.from(h.values()).sort((b,p)=>{let N=b.distance-p.distance;return Math.abs(N)>1e-12?N:A(Q(b.path),Q(p.path))})[0];if(!I)break;u.push(I),h.delete(Q(I.path));}return {source:t,target:e,paths:u,complete:u.length>=i}}function Oe(c,n,t,e,o={}){let r=k(c,n,o);if(!r.neighborsByNode.has(t)||!r.neighborsByNode.has(e))return {source:t,sink:e,maxFlow:0,augmentations:0,flowByEdge:[],sourcePartition:[],sinkPartition:[],cutEdges:[]};let i=[];for(let g of r.edges){if(g.weight<0)throw new R("NEGATIVE_WEIGHT","Maximum flow requires non-negative capacities.");i.push(g);}let a=new Map;for(let g of r.nodes)a.set(g,[]);let s=(g,x,w,M)=>{let y=a.get(g),S=a.get(x);if(!y||!S)return;let v=y.length,E=S.length;y.push({to:x,reverseIndex:E,capacity:w,originalIndex:M}),S.push({to:g,reverseIndex:v,capacity:0,originalIndex:null});};for(let g=0;g<i.length;g+=1){let x=i[g];x&&s(x.from,x.to,x.weight,g);}let d=0,u=0,h=new Array(i.length).fill(0);for(;;){let g=new Map,x=[t];g.set(t,{from:t,edgeIndex:-1});let w=0;for(;w<x.length&&!g.has(e);){let S=x[w];if(w+=1,!S)continue;let v=a.get(S)??[];for(let E=0;E<v.length;E+=1){let G=v[E];!G||G.capacity<=1e-12||g.has(G.to)||(g.set(G.to,{from:S,edgeIndex:E}),x.push(G.to));}}if(!g.has(e))break;let M=Number.POSITIVE_INFINITY,y=e;for(;y!==t;){let S=g.get(y);if(!S){M=0;break}let v=a.get(S.from)?.[S.edgeIndex];if(!v){M=0;break}M=Math.min(M,v.capacity),y=S.from;}if(!Number.isFinite(M)||M<=1e-12)break;for(y=e;y!==t;){let S=g.get(y);if(!S)break;let v=a.get(S.from)?.[S.edgeIndex],E=v?a.get(v.to)?.[v.reverseIndex]:void 0;if(!v||!E)break;v.capacity-=M,E.capacity+=M,v.originalIndex!==null?h[v.originalIndex]=(h[v.originalIndex]??0)+M:E.originalIndex!==null&&(h[E.originalIndex]=(h[E.originalIndex]??0)-M),y=S.from;}d+=M,u+=1;}let l=new Set,f=[t];l.add(t);let I=0;for(;I<f.length;){let g=f[I];if(I+=1,!!g)for(let x of a.get(g)??[])x.capacity<=1e-12||l.has(x.to)||(l.add(x.to),f.push(x.to));}let b=r.nodes.filter(g=>l.has(g)).sort(A),p=r.nodes.filter(g=>!l.has(g)).sort(A),N=i.filter(g=>l.has(g.from)&&!l.has(g.to)).sort((g,x)=>{let w=A(g.from,x.from);return w!==0?w:A(g.to,x.to)}),m=i.map((g,x)=>({from:g.from,to:g.to,flow:Math.max(0,h[x]??0),capacity:g.weight})).sort((g,x)=>{let w=A(g.from,x.from);return w!==0?w:A(g.to,x.to)});return {source:t,sink:e,maxFlow:d,augmentations:u,flowByEdge:m,sourcePartition:b,sinkPartition:p,cutEdges:N}}function De(c,n,t,e,o={}){let r=n.map(m=>({from:m.from,to:m.to,weight:m.capacity})),i=ne(c,r),a=o.directed??true;if(!i.includes(t)||!i.includes(e))return {source:t,sink:e,flow:0,cost:0,complete:false,augmentations:0,flowByEdge:[]};let s=[];for(let m of n){if(!Number.isFinite(m.capacity)||m.capacity<0)throw new R("INVALID_ARGUMENT",`Flow edge capacity must be non-negative and finite, received ${m.capacity}.`);let g=m.cost??0;if(!Number.isFinite(g))throw new R("INVALID_ARGUMENT",`Flow edge cost must be finite, received ${g}.`);s.push({from:m.from,to:m.to,capacity:m.capacity,cost:g}),a||s.push({from:m.to,to:m.from,capacity:m.capacity,cost:g});}let d=new Map;for(let m of i)d.set(m,[]);let u=(m,g,x,w,M)=>{let y=d.get(m),S=d.get(g);if(!y||!S)return;let v=y.length,E=S.length;y.push({to:g,reverseIndex:E,capacity:x,cost:w,originalIndex:M}),S.push({to:m,reverseIndex:v,capacity:0,cost:-w,originalIndex:null});};for(let m=0;m<s.length;m+=1){let g=s[m];g&&u(g.from,g.to,g.capacity,g.cost,m);}let h=o.targetFlow,l=h===void 0?Number.POSITIVE_INFINITY:h;if(!Number.isFinite(l)&&l!==Number.POSITIVE_INFINITY)throw new R("INVALID_ARGUMENT",`targetFlow must be finite or omitted, received ${l}.`);if(l<0)throw new R("INVALID_ARGUMENT",`targetFlow must be non-negative, received ${l}.`);let f=0,I=0,b=0,p=new Array(s.length).fill(0);for(;f<l-1e-12;){let m=new Map,g=new Map;for(let y of i)m.set(y,Number.POSITIVE_INFINITY);m.set(t,0);for(let y=0;y<i.length-1;y+=1){let S=false;for(let v of i){let E=m.get(v);if(E===void 0||!Number.isFinite(E))continue;let G=d.get(v)??[];for(let z=0;z<G.length;z+=1){let B=G[z];if(!B||B.capacity<=1e-12)continue;let q=E+B.cost,P=m.get(B.to)??Number.POSITIVE_INFINITY;q<P-1e-12&&(m.set(B.to,q),g.set(B.to,{from:v,edgeIndex:z}),S=true);}}if(!S)break}let x=m.get(e)??Number.POSITIVE_INFINITY;if(!Number.isFinite(x))break;let w=Math.min(l-f,Number.POSITIVE_INFINITY),M=e;for(;M!==t;){let y=g.get(M);if(!y){w=0;break}let S=d.get(y.from)?.[y.edgeIndex];if(!S){w=0;break}w=Math.min(w,S.capacity),M=y.from;}if(!Number.isFinite(w)||w<=1e-12)break;for(M=e;M!==t;){let y=g.get(M);if(!y)break;let S=d.get(y.from)?.[y.edgeIndex],v=S?d.get(S.to)?.[S.reverseIndex]:void 0;if(!S||!v)break;S.capacity-=w,v.capacity+=w,S.originalIndex!==null?p[S.originalIndex]=(p[S.originalIndex]??0)+w:v.originalIndex!==null&&(p[v.originalIndex]=(p[v.originalIndex]??0)-w),M=y.from;}f+=w,I+=w*x,b+=1;}let N=s.map((m,g)=>({from:m.from,to:m.to,flow:Math.max(0,p[g]??0),capacity:m.capacity,cost:m.cost})).sort((m,g)=>{let x=A(m.from,g.from);return x!==0?x:A(m.to,g.to)});return {source:t,sink:e,flow:f,cost:I,complete:l===Number.POSITIVE_INFINITY?true:f>=l-1e-12,augmentations:b,flowByEdge:N}}function mt(c,n,t={}){if(!Array.isArray(c)||c.length===0)throw new R("INVALID_ARGUMENT","kMeansClustering requires at least one point.");let e=c[0]?.length??0;if(e===0)throw new R("INVALID_ARGUMENT","kMeansClustering requires points with at least one axis.");for(let N of c)if(N.length!==e)throw new R("INVALID_ARGUMENT","kMeansClustering requires all points to share dimensionality.");let o=Math.max(1,Math.min(c.length,Math.floor(n))),r=Math.max(1,t.maxIterations??120),i=Math.max(0,t.tolerance??1e-5),a=Math.max(1,t.nInit??8),s=t.normalization??"zscore",d=t.useKMeansPlusPlus??true,u=t.seed===void 0||!Number.isFinite(t.seed)?Math.floor(Math.random()*4294967295):Math.floor(t.seed),h=tt(c,s),l=c.map(N=>h.normalize(N)),f=null,I=u;for(let N=0;N<a;N+=1){let m=u+N*2654435761>>>0,g=Se(m),x=ot(l,o,g,r,i,d);(!f||x.inertia<f.inertia-1e-9)&&(f=x,I=m);}if(!f)throw new R("INVALID_ARGUMENT","kMeansClustering failed to produce a valid run.");let b=[];for(let N=0;N<o;N+=1){let m=[];for(let g=0;g<f.assignments.length;g+=1)f.assignments[g]===N&&m.push(g);b.push({centroid:h.denormalize(f.centroidsNormalized[N]??[]),indices:m});}let p=ve(l,f.assignments,o);return {assignments:f.assignments,clusters:b,iterations:f.iterations,inertia:f.inertia,converged:f.converged,silhouette:p,selectedSeed:I}}function At(c,n={}){if(!Array.isArray(c)||c.length===0)throw new R("INVALID_ARGUMENT","kMeansAuto requires at least one point.");let t=Math.max(2,n.kMin??2),e=Math.max(t,Math.min(c.length,n.kMax??Math.min(10,Math.ceil(Math.sqrt(c.length))+2))),o=null,r=t,i=[];for(let a=t;a<=e;a+=1){let s=mt(c,a,{...n,seed:n.seed===void 0||!Number.isFinite(n.seed)?void 0:n.seed+a*997});if(i.push({k:a,silhouette:s.silhouette,inertia:s.inertia}),!o){o=s,r=a;continue}let d=s.silhouette??Number.NEGATIVE_INFINITY,u=o.silhouette??Number.NEGATIVE_INFINITY;if(d>u+1e-9){o=s,r=a;continue}Math.abs(d-u)<=1e-9&&s.inertia<o.inertia-1e-9&&(o=s,r=a);}if(!o)throw new R("INVALID_ARGUMENT","kMeansAuto failed to choose a cluster configuration.");return {...o,selectedK:r,candidates:i}}function Pt(c,n,t={}){let e=k(c,n,t);if(e.nodes.length===0)return {order:[],distance:0,segments:[],visitedCount:0,complete:true,unreachableNodes:[],lowerBound:0,optimalityGap:0};if(D(e))throw new R("NEGATIVE_WEIGHT","TSP approximation requires non-negative graph weights.");let o=t.returnToStart??true,r=Math.max(0,t.twoOptPasses??3),i=st(e.nodes,t),a=new Map,s=new Map;for(let m of e.nodes){let g=oe(e,m,t.shortestPathAlgorithm??"auto");if(g.result.negativeCycleNodes.size>0)throw new R("NEGATIVE_CYCLE","TSP approximation cannot run when negative cycles are reachable.");a.set(m,g.result.distanceByNode),s.set(m,g.result.previousByNode);}let d=[],u=Number.POSITIVE_INFINITY;for(let m of i){let x=it(m,e.nodes,a,o);x.length>2&&(x=rt(x,a,r));let w=ce(x,a);w<u&&(u=w,d=x);}let h=new Set(d),l=e.nodes.filter(m=>!h.has(m)),f=l.length===0&&Number.isFinite(u),I=[],b=0;for(let m=1;m<d.length;m+=1){let g=d[m-1],x=d[m];if(!g||!x)continue;let w=a.get(g)?.get(x)??Number.POSITIVE_INFINITY,M=s.get(g)??new Map,y=ue(g,x,M);!Number.isFinite(w)||y.length===0||(b+=w,I.push({from:g,to:x,distance:w,path:y}));}f||(b=Number.POSITIVE_INFINITY);let p=dt(e.nodes,a),N=f&&Number.isFinite(b)&&Number.isFinite(p)&&p>0?(b-p)/p:null;return {order:d,distance:b,segments:I,visitedCount:h.size,complete:f,unreachableNodes:l,lowerBound:p,optimalityGap:N}}function kt(c,n,t={}){if(!Array.isArray(c)||c.length===0)throw new R("INVALID_ARGUMENT","hierarchicalClustering requires at least one point.");let e=c.length,o=Math.max(1,Math.min(e,Math.floor(n))),r=t.linkage??"average",i=t.distanceMetric??"euclidean",a=(m,g)=>{switch(i){case "cosine":return 1-se(m,g);case "manhattan":return ie(m,g);default:return U(m,g)}},s=c.map((m,g)=>[g]),d=new Array(e).fill(true),u=[],h=Array.from({length:e},()=>new Array(e).fill(0));for(let m=0;m<e;m+=1)for(let g=m+1;g<e;g+=1){let x=a(c[m],c[g]);h[m][g]=x,h[g][m]=x;}let l=Array.from({length:e},(m,g)=>[...h[g]]),f=e;for(;f>o;){let m=-1,g=-1,x=Number.POSITIVE_INFINITY;for(let M=0;M<s.length;M+=1)if(d[M])for(let y=M+1;y<s.length;y+=1)d[y]&&l[M][y]<x&&(x=l[M][y],m=M,g=y);if(m===-1||g===-1)break;u.push({left:m<e?-(m+1):m,right:g<e?-(g+1):g,distance:x,size:s[m].length+s[g].length});let w=s[m].concat(s[g]);s[m]=w,d[g]=false,f-=1;for(let M=0;M<s.length;M+=1){if(!d[M]||M===m)continue;let y;switch(r){case "single":y=Math.min(l[m][M],l[g][M]);break;case "complete":y=Math.max(l[m][M],l[g][M]);break;case "ward":{let S=s[m].length-s[g].length,v=s[g].length,E=s[M].length,G=S+v+E;y=Math.sqrt(((S+E)*l[m][M]*l[m][M]+(v+E)*l[g][M]*l[g][M]-E*x*x)/G);break}default:{let S=s[m].length-s[g].length,v=s[g].length;y=(l[m][M]*S+l[g][M]*v)/(S+v);break}}l[m][M]=y,l[M][m]=y;}}let I=new Array(e).fill(-1),b=[],p=0;for(let m=0;m<s.length;m+=1)if(d[m]){b.push(s[m]);for(let g of s[m])I[g]=p;p+=1;}let N=ve(c,I,b.length);return {clusters:b,assignments:I,dendrogram:u,silhouette:N}}function Gt(c,n,t,e={}){if(!Array.isArray(c)||c.length===0)throw new R("INVALID_ARGUMENT","dbscan requires at least one point.");let o=c.length,r=e.distanceMetric??"euclidean",i=(I,b)=>{switch(r){case "cosine":return 1-se(I,b);case "manhattan":return ie(I,b);default:return U(I,b)}},a=-2,s=-1,d=new Array(o).fill(a),u=0,h=I=>{let b=[];for(let p=0;p<o;p+=1)i(c[I],c[p])<=n&&b.push(p);return b};for(let I=0;I<o;I+=1){if(d[I]!==a)continue;let b=h(I);if(b.length<t){d[I]=s;continue}d[I]=u;let p=[...b],N=0;for(;N<p.length;){let m=p[N];if(N+=1,d[m]===s&&(d[m]=u),d[m]!==a)continue;d[m]=u;let g=h(m);if(g.length>=t)for(let x of g)p.includes(x)||p.push(x);}u+=1;}let l=Array.from({length:u},()=>[]),f=[];for(let I=0;I<o;I+=1)d[I]===s?f.push(I):l[d[I]].push(I);return {clusters:l,assignments:d,noise:f,clusterCount:u}}function gt(c){return typeof c=="string"?c:c.id}function ge(c,n,t){let e=new Set;for(let s of c)e.add(gt(s));for(let s of n)e.add(s.from),e.add(s.to);let o=Array.from(e).sort(),r=new Map,i=new Map,a=new Set;for(let s of o)r.set(s,new Set),i.set(s,new Set);for(let s of n){let d=s.from,u=s.to;r.get(d)?.add(u),i.get(u)?.add(d),a.add(`${d}\0${u}`),t||(r.get(u)?.add(d),i.get(d)?.add(u),a.add(`${u}\0${d}`));}return {nodeIds:o,outNeighbors:r,inNeighbors:i,edgeSet:a}}function Fe(c,n,t={}){let e=t.directed??false,{nodeIds:o,outNeighbors:r,edgeSet:i}=ge(c,n,e),a=new Map;for(let s of o){let d=r.get(s),u=d.size;if(u<2){a.set(s,0);continue}let h=Array.from(d),l=0;for(let I=0;I<h.length;I+=1)for(let b=I+1;b<h.length;b+=1){let p=h[I],N=h[b];e?(i.has(`${p}\0${N}`)&&(l+=1),i.has(`${N}\0${p}`)&&(l+=1)):(i.has(`${p}\0${N}`)||i.has(`${N}\0${p}`))&&(l+=1);}let f=e?u*(u-1):u*(u-1)/2;a.set(s,f>0?l/f:0);}return a}function Ot(c,n,t={}){let e=t.directed??false,{nodeIds:o,outNeighbors:r,inNeighbors:i,edgeSet:a}=ge(c,n,e),s=o.length,d=n.length,u=e?s*(s-1):s*(s-1)/2,h=u>0?d/u:0,l=[],f=[],I=[];for(let P of o){let C=r.get(P)?.size??0,O=i.get(P)?.size??0;I.push(C),f.push(O),l.push(e?C+O:C);}let b=new F({data:l}),p=new F({data:f}),N=new F({data:I}),m=Fe(c,n,{directed:e}),g=Array.from(m.values()),x=g.length>0?g.reduce((P,C)=>P+C,0)/g.length:0,w=new F({data:g.length>0?g:[0]}),M=0;if(e)for(let P of n)a.has(`${P.to}\0${P.from}`)&&(M+=1);let y=e&&d>0?M/d:e?0:1,S=Math.min(s,t.samplePathLength??Math.min(s,50)),v=0,E=0,G=0,z=Math.max(1,Math.floor(s/S));for(let P=0;P<s&&P/z<S;P+=z){let C=o[P],O=new Map;O.set(C,0);let _=[C],L=0;for(;L<_.length;){let K=_[L];L+=1;let X=O.get(K),J=r.get(K)??new Set;for(let Y of J)if(!O.has(Y)){let j=X+1;O.set(Y,j),_.push(Y),v+=j,E+=1,j>G&&(G=j);}}}let B=E>0?v/E:0,q=0;if(n.length>1){let P=[],C=[];for(let T of n)P.push(r.get(T.from)?.size??0),C.push(r.get(T.to)?.size??0);let O=P.length,_=0,L=0,K=0,X=0,J=0;for(let T=0;T<O;T+=1)_+=P[T],L+=C[T],K+=P[T]*C[T],X+=P[T]*P[T],J+=C[T]*C[T];let Y=O*K-_*L,j=Math.sqrt((O*X-_*_)*(O*J-L*L));q=j>0?Y/j:0;}return {nodeCount:s,edgeCount:d,density:h,degreeDistribution:b.describe(),inDegreeDistribution:p.describe(),outDegreeDistribution:N.describe(),clusteringCoefficients:m,globalClusteringCoefficient:x,clusteringDistribution:w.describe(),averagePathLength:B,diameter:G,reciprocity:y,degreeAssortativity:q}}function Dt(c,n,t={}){let e=t.method??"combined",o=t.threshold??2,r=false,{nodeIds:i,outNeighbors:a}=ge(c,n,r),s=[];if(e==="degree"||e==="combined"){let d=i.map(f=>a.get(f)?.size??0),u=new F({data:d}),h=u.mean(),l=u.stddev();if(l>0)for(let f=0;f<i.length;f+=1){let I=Math.abs(d[f]-h)/l;I>o&&s.push({nodeId:i[f],score:I,reason:`degree outlier (z=${I.toFixed(2)}, degree=${d[f]})`});}}if(e==="clustering"||e==="combined"){let d=Fe(c,n,{directed:r}),u=Array.from(d.values()),h=new F({data:u.length>0?u:[0]}),l=h.mean(),f=h.stddev();if(f>0){for(let b of i){let p=d.get(b)??0,N=Math.abs(p-l)/f;if(N>o){let m=s.find(g=>g.nodeId===b);m?(m.score=Math.max(m.score,N),m.reason+=`; clustering outlier (z=${N.toFixed(2)}, cc=${p.toFixed(3)})`):s.push({nodeId:b,score:N,reason:`clustering outlier (z=${N.toFixed(2)}, cc=${p.toFixed(3)})`});}}}}return s.sort((d,u)=>u.score-d.score)}var fe=class{projections=new Map;project(n,t,e,o={}){let r=n.trim();if(!r)throw new R("INVALID_ARGUMENT","Projection name must be non-empty.");if(this.projections.has(r)&&!(o.replace??false))throw new R("INVALID_ARGUMENT",`Projection "${r}" already exists.`);let i=o.directed??true,a=k(t,e,{directed:i}),s={name:r,nodes:a.nodes,edges:a.edges,directed:i,createdAt:Date.now(),metadata:o.metadata??{}};return this.projections.set(r,s),s}drop(n){return this.projections.delete(n)}clear(){this.projections.clear();}exists(n){return this.projections.has(n)}get(n){return this.projections.get(n)??null}list(){return Array.from(this.projections.values()).map(n=>({name:n.name,nodeCount:n.nodes.length,edgeCount:n.edges.length,directed:n.directed,createdAt:n.createdAt,metadata:n.metadata})).sort((n,t)=>n.name.localeCompare(t.name))}pageRank(n,t={}){let e=this.requireProjection(n);return he(e.nodes,e.edges,{directed:e.directed,...t})}louvain(n,t={}){let e=this.requireProjection(n);return Ae(e.nodes,e.edges,{directed:e.directed,...t})}labelPropagation(n,t={}){let e=this.requireProjection(n);return Ee(e.nodes,e.edges,{directed:e.directed,...t})}similarity(n,t={}){let e=this.requireProjection(n);return Pe(e.nodes,e.edges,{directed:e.directed,...t})}knn(n,t={}){let e=this.requireProjection(n);return ke(e.nodes,e.edges,{directed:e.directed,...t})}linkPrediction(n,t={}){let e=this.requireProjection(n);return me(e.nodes,e.edges,{directed:e.directed,...t})}shortestPath(n,t,e,o={}){let r=this.requireProjection(n);return ee(r.nodes,r.edges,t,e,{directed:r.directed,...o})}aStar(n,t,e,o={}){let r=this.requireProjection(n);return Ge(r.nodes,r.edges,t,e,{directed:r.directed,...o})}yen(n,t,e,o={}){let r=this.requireProjection(n);return Te(r.nodes,r.edges,t,e,{directed:r.directed,...o})}allPairs(n,t={}){let e=this.requireProjection(n);return Ce(e.nodes,e.edges,{directed:e.directed,...t})}maxFlow(n,t,e,o={}){let r=this.requireProjection(n);return Oe(r.nodes,r.edges,t,e,{directed:r.directed,...o})}minCostMaxFlow(n,t,e,o=null,r={}){let i=this.requireProjection(n),a=o??i.edges.map(s=>({from:s.from,to:s.to,capacity:s.weight,cost:s.weight}));return De(i.nodes,a,t,e,{directed:i.directed,...r})}runPipeline(n,t){let e=new Map;for(let o of t){let r=o.id.trim();if(!r)throw new R("INVALID_ARGUMENT","Pipeline step id must be non-empty.");if(e.has(r))throw new R("INVALID_ARGUMENT",`Duplicate pipeline step id "${r}".`);if(o.kind==="page-rank"){e.set(r,this.pageRank(n,o.options));continue}if(o.kind==="louvain"){e.set(r,this.louvain(n,o.options));continue}if(o.kind==="label-propagation"){e.set(r,this.labelPropagation(n,o.options));continue}if(o.kind==="similarity"){e.set(r,this.similarity(n,o.options));continue}if(o.kind==="knn"){e.set(r,this.knn(n,o.options));continue}if(o.kind==="link-prediction"){e.set(r,this.linkPrediction(n,o.options));continue}if(o.kind==="shortest-path"){e.set(r,this.shortestPath(n,o.source,o.target,o.options));continue}if(o.kind==="a-star"){e.set(r,this.aStar(n,o.source,o.target,o.options));continue}if(o.kind==="yen-k-shortest-paths"){e.set(r,this.yen(n,o.source,o.target,o.options));continue}if(o.kind==="max-flow"){e.set(r,this.maxFlow(n,o.source,o.sink,o.options));continue}if(o.kind==="min-cost-max-flow"){e.set(r,this.minCostMaxFlow(n,o.source,o.sink,o.edges??null,o.options));continue}if(o.kind==="all-pairs-shortest-paths"){e.set(r,this.allPairs(n,o.options));continue}let i=o;throw new R("INVALID_ARGUMENT",`Unsupported pipeline step kind "${String(i)}".`)}return e}requireProjection(n){let t=this.projections.get(n);if(!t)throw new R("INVALID_ARGUMENT",`Projection "${n}" does not exist.`);return t}};function ft(){return new fe}var _t=ft();var H=100,Nt=0,xe=1e3,Ne=1.5,Be=1,_e=2,re=2;function Le(c=H){return Math.floor(Math.random()*c)}function ze(c=xe,n=H){let t=[];for(let e=0;e<c;e++)t.push(Le(n));return t}function je(c=re,n=H){let t=[];for(let e=0;e<c;e++)t.push(Math.floor(Math.random()*(n/10)%n));return t}function Ie(c=xe,n=re,t=H){let e=[];for(let o=0;o<c;o++)e.push(je(n,t));return e}var F=class{data;constructor(n={}){this.data={original:n.data??ze()};}sorted(){return this.data.sorted||(this.data.sorted=this.getSorted(this.data.original)),this.data.sorted}getSorted(n){return [...n].sort((t,e)=>t>e?1:t===e?0:-1)}median(){return this.sorted(),this.data.median===void 0&&(this.data.median=this.getMedian(this.data.sorted)),this.data.medianDepth===void 0&&(this.data.medianDepth=this.getMedianDepth(this.data.sorted)),{datum:this.data.median,depth:this.data.medianDepth}}getMedianDepth(n,t=0){return n.length?t+(n.length+1)/2:NaN}getMedian(n){let t=n.length;if(!t)return NaN;if(t===1)return n[0];let e=Math.floor(t/2);return t%2===0?(n[e-1]+n[e])/2:n[e]}mean(){return this.data.mean===void 0&&(this.data.mean=this.getMean(this.data.original)),this.data.mean}getMean(n){if(!n.length)return NaN;let t=0;for(let e of n)t+=e;return t/n.length}variance(){if(this.data.variance===void 0){let n=this.mean(),t=this.data.original;if(t.length<2)this.data.variance=NaN;else {let e=0;for(let o of t){let r=o-n;e+=r*r;}this.data.variance=e/(t.length-1);}}return this.data.variance}stddev(){return this.data.stddev===void 0&&(this.data.stddev=Math.sqrt(this.variance())),this.data.stddev}ema(n){let t=this.data.original;if(t.length===0)return [];let e=[t[0]];for(let o=1;o<t.length;o+=1){let r=e[o-1];e.push(r*(1-n)+t[o]*n);}return e}zscore(){let n=this.mean(),t=this.stddev();return t===0||isNaN(t)?this.data.original.map(()=>0):this.data.original.map(e=>(e-n)/t)}skewness(){if(this.data.skewness===void 0){let n=this.data.original,t=n.length;if(t<3)this.data.skewness=NaN;else {let e=this.mean(),o=this.stddev();if(o===0)this.data.skewness=0;else {let r=0;for(let i of n){let a=(i-e)/o;r+=a*a*a;}this.data.skewness=t/((t-1)*(t-2))*r;}}}return this.data.skewness}kurtosis(){if(this.data.kurtosis===void 0){let n=this.data.original,t=n.length;if(t<4)this.data.kurtosis=NaN;else {let e=this.mean(),o=this.stddev();if(o===0)this.data.kurtosis=0;else {let r=0;for(let s of n){let d=(s-e)/o;r+=d*d*d*d;}let i=t*(t+1)/((t-1)*(t-2)*(t-3))*r,a=3*(t-1)*(t-1)/((t-2)*(t-3));this.data.kurtosis=i-a;}}}return this.data.kurtosis}mode(){return this.data.mode||(this.sorted(),this.data.mode=this.getMode(this.data.sorted)),this.data.mode}getMode(n){if(!n.length)return {count:0,data:[]};let t={},e=0;for(let r of n)t[r]=(t[r]||0)+1,t[r]>e&&(e=t[r]);let o=[];for(let[r,i]of Object.entries(t))i===e&&o.push(Number(r));return {count:e,data:o.sort((r,i)=>r-i)}}extremes(){return this.data.extremes||(this.sorted(),this.data.extremes=this.getExtremes(this.data.sorted)),this.data.extremes}getExtremes(n){return n.length?[n[0],n[n.length-1]]:[]}counts(){return this.data.counts||(this.sorted(),this.data.counts=this.getCounts(this.data.sorted)),this.data.counts}getCounts(n){let t=new Map;for(let o of n)t.set(o,(t.get(o)||0)+1);let e=[];for(let[o,r]of t)e.push([o,r]);return e.sort((o,r)=>o[0]-r[0])}hinges(){return this.data.hinges||(this.sorted(),this.data.hinges=this.getHinges(this.data.sorted)),this.data.hinges}getHinges(n,t=2,e=[]){let o=[...n],r=o.length,i=t;if(i%2!==0&&i++,r<=i||i<=0)return e;let a=Math.floor(r/i),s=Math.floor(r/a)-1;for(let d=0;d<=s;d++){let u=o.slice(d*a,d*a+a);e.push({datum:this.getMedian(u),depth:this.getMedianDepth(u,d*a)});}return e}iqr(){return this.data.iqr===void 0&&(this.hinges(),this.data.iqr=this.getIQR(this.data.hinges)),this.data.iqr}getIQR(n){let t=n[0]?.datum,e=n[1]?.datum;return t===void 0||e===void 0?NaN:Math.abs(t-e)}fences(){return this.data.fences||(this.median(),this.iqr(),this.data.fences=this.getFences()),this.data.fences}getFences(n=Ne){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=e*n;return [t-o,t+o]}outer(){return this.data.outer||(this.median(),this.iqr(),this.data.outer=this.getOuter()),this.data.outer}getOuter(n=Ne){let t=this.data.median,e=this.data.iqr;if(t===void 0||e===void 0||isNaN(e))return [];let o=2*e*n;return [t-o,t+o]}outside(){return this.data.outside||(this.outer(),this.data.outside=this.getOutside()),this.data.outside}getOutside(){let n=[],t=this.data.sorted,e=this.data.outer;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let i of t)(i>r||i<o)&&n.push(i);return n}inside(){return this.data.inside||(this.fences(),this.data.inside=this.getInside()),this.data.inside}getInside(){let n=[],t=this.data.sorted,e=this.data.fences;if(!e||e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let i of t)i<r&&i>o&&n.push(i);return n}outliers(){return this.data.outliers||(this.fences(),this.data.outliers=this.getOutliers()),this.data.outliers}getOutliers(){let n=[],t=this.data.sorted,e=this.data.fences;if(e.length===0)return [];let o=Math.min(...e),r=Math.max(...e);for(let i of t)(i>r||i<o)&&n.push(i);return n}ranked(){return this.data.ranked||(this.sorted(),this.data.ranked=this.getRanked(this.data.sorted)),this.data.ranked}getRanked(n,t=true){let e={},o={},r=n.length,i=[],a=NaN,s=[],d=()=>{a=NaN,s=[];};for(let h=0;h<n.length;h++){let l=n[h];if(!t)e[l]={rank:h+1,peers:0},o[l]={rank:r-h,peers:0};else {let f=h+1,I=h-1;l===n[I]?(!isNaN(a)&&s.length===0?(s.push(l),i.push(s),d()):(s.push(l),a=I),l!==n[f]&&(i.push(s),d())):l!==n[f]?s.length>0?(i.push(s),d()):i.push(l):s.push(l);}}let u=0;for(let h=0;h<i.length;h++){let l=i[h];if(typeof l=="number")o[l]={rank:h+1+u,peers:0},e[l]={rank:r-h-u,peers:0};else if(Array.isArray(l)){u+=l.length;let f=l[0];o[f]={rank:h+1+u,peers:l.length},e[f]={rank:r-h-u,peers:l.length};}else u+=1;}return {up:e,down:o,groups:{down:[...i],up:[...i].reverse()}}}adjacent(){return this.data.adjacent||(this.fences(),this.data.adjacent=this.getAdjacent(this.data.sorted,this.data.fences)),this.data.adjacent}getAdjacent(n,t){if(t.length===0)return [];let e=t[0],o=[],r=t[1],i=[];for(let a of n)a>e&&o.push(a),a<r&&i.push(a);return o.sort((a,s)=>a-s),i.sort((a,s)=>a-s),[o[0],i[i.length-1]]}binned(n=NaN){return this.data.binned||(this.sorted(),this.extremes(),this.data.binned=this.getBinned(this.data.sorted,n)),this.data.binned}getBinned(n,t=10,e=NaN,o=true){let r={},i=n.length,a=o?0:1;if(i===0)return {bins:0,width:NaN,binned:{}};let s=this.data.extremes,d=e;if(s&&isNaN(d)&&s.length===2){d=(s[1]-s[0])/(Math.log(n.length)/Math.LN2),d=Math.floor(d);let h=true;for(let l of n)if(l%1!==0){h=false;break}h&&(d=Math.floor(d));}let u=Math.floor(s[1]/d)+1;(!u||u<1)&&(u=1);for(let h of n){let l=Math.floor((h-a)/d);r[l]||(r[l]={from:l*d+a,to:(l+1)*d+a-1,data:[]}),r[l].data.push(h);}return {bins:u,width:d,binned:r}}logs(){return this.data.logs||(this.data.logs=this.getLogs(this.data.original)),this.data.logs}getLogs(n){return n.map(t=>Math.log(t))}roots(){return this.data.roots||(this.data.roots=this.getRoots(this.data.original)),this.data.roots}getRoots(n){return n.map(t=>Math.sqrt(t))}inverse(){return this.data.inverse||(this.data.inverse=this.getInverse(this.data.original)),this.data.inverse}getInverse(n){return n.map(t=>1/t)}hanning(){return this.data.hanning||(this.data.hanning=this.getSkipMeans(this.data.original)),this.data.hanning}getSkipMeans(n){let t=[];for(let e=0;e<n.length;e++)e!==0&&e!==n.length-1&&t.push((n[e]+n[e+1])/2);return t.unshift(n[0]),t.push(n[n.length-1]),t}smooth(){return this.data.smooth||(this.sorted(),this.data.smooth=this.getSmooth(this.data.original)),this.data.rough=this.getRough(this.data.original,this.data.smooth),this.data.smooth}getRough(n,t){let e=[];for(let o=0;o<n.length;o++)e.push(n[o]-t[o]);return e}getSmooth(n,t=3){let e=[...n];return e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothSplit(e,2),e=this.smoothMedian(e,t),e=this.smoothExtremes(e,-1),e=this.smoothMedian(e,t),e}smoothExtremes(n,t=1,e=0,o="both"){let r=n.length;if(r<=2)return [...n];let i=[...n];for(let a=e;a<t||t===-1;a++){let s=false;if(o==="both"||o==="head"){let d=i[0],u=i[1],h=i[2],l=u-2*(h-u),f=d<=u?u<=l?u:d<=l?l:d:d<=l?d:u<=l?l:u;i[0]!==f&&(i[0]=f,s=true);}if(o==="both"||o==="tail"){let d=i[r-3],u=i[r-2],h=i[r-1],l=u-2*(d-u),f=h<=u?u<=l?u:h<=l?l:h:h<=l?h:u<=l?l:u;i[r-1]!==f&&(i[r-1]=f,s=true);}if(t===-1&&!s)break}return i}smoothSplit(n,t=_e,e=0){let o=[...n],r=n.length;for(let i=e;i<t||t===-1;i++){let a=false;for(let s=2;s<r-1;s++){let d=o[s],u=o[s-1],h=o[s-2],l=o[s+1];if(d===u&&(u>h&&d>l||u<h&&d<l)){let f=this.smoothExtremes(o.slice(0,s)),I=this.smoothExtremes(o.slice(s));o=f.concat(I),a=true;}}if(t===-1&&!a)return o}return o}smoothMedian(n,t=1,e=0){let o=n,r=n.length;if(r<=2)return [...n];for(let i=e;i<t||t===-1;i++){let a=new Array(r);a[0]=o[0],a[r-1]=o[r-1];let s=false;for(let d=1;d<r-1;d++){let u=o[d],h=Math.min(Math.max(o[d-1],u),o[d+1]);a[d]=h,h!==u&&(s=true);}if(t===-1&&!s)return o;o=a;}return o}jitter(n,t=1,e=NaN,o=Be,r=NaN,i=0){let a=i+1,s=[...n];if(a<=t){let d=[];for(let u of s){let h=r;!h&&!isNaN(h)&&(h=(1+Math.floor(u/10))*(Math.random()>.5?1:-1));let l=u+Math.floor(Math.random()*o*h);!isNaN(e)&&l<e&&(l=e),d.push(l);}return this.jitter(d,t,e,o,r,a)}return s}trimean(){let n=this.median(),t=this.hinges();return t.length<2?n.datum:(t[0].datum+2*n.datum+t[1].datum)/4}letterValues(){this.sorted();let n=this.data.sorted.length;if(n<2)return [];let t=["M","F","E","D","C","B","A","Z","Y","X","W","V","U","T","S"],e=[],o=(n+1)/2,r=this.median().datum;e.push({letter:"M",depth:o,lower:r,upper:r,mid:r,spread:0});let i=o,a=1;for(;i>1&&a<t.length&&(i=Math.floor((Math.floor(i)+1)/2),!(i<1));){let s=Math.ceil(i)-1,d=n-Math.ceil(i);if(s<0||d>=n||s>=d)break;let u=this.data.sorted[s],h=this.data.sorted[d],l=(u+h)/2,f=h-u;e.push({letter:t[a],depth:i,lower:u,upper:h,mid:l,spread:f}),a++;}return e}rough(){return this.data.rough||this.smooth(),this.data.rough||[]}stemLeaf(n=1){this.sorted();let t=this.data.sorted;if(!t.length)return {stems:[],leaves:{},display:[]};let e=Math.pow(10,n),o=new Map;for(let d of t){let u=Math.floor(d/e),h=Math.abs(Math.round(d%e));o.has(u)||o.set(u,[]),o.get(u).push(h);}let r=Array.from(o.keys()).sort((d,u)=>d-u),i=[],a={},s=[];for(let d of r){let u=String(d);i.push(u);let h=o.get(d).sort((l,f)=>l-f).map(String);a[u]=h,s.push(`${u.padStart(4)} | ${h.join(" ")}`);}return {stems:i,leaves:a,display:s}}midSummaries(){return this.letterValues().map(({depth:t,mid:e,spread:o})=>({depth:t,mid:e,spread:o}))}describe(){return this.data.description={original:this.data.original,summary:{median:this.median(),mean:this.mean(),mode:this.mode(),hinges:this.hinges(),adjacent:this.adjacent(),outliers:this.outliers(),outer:this.outer(),outside:this.outside(),inside:this.inside(),extremes:this.extremes(),iqr:this.iqr(),fences:this.fences()},smooths:{smooth:this.smooth(),hanning:this.hanning()},transforms:{logs:this.logs(),roots:this.roots(),inverse:this.inverse()},counts:this.counts(),sorted:this.sorted(),ranked:this.ranked(),binned:this.binned()},this.data.description}},pe=class c{data;dimension;count;constructor(n={}){typeof n=="number"?(this.count=n,this.dimension=re,this.data={original:Ie(this.count,this.dimension)}):(this.dimension=n.dimensionality??2,this.count=n.count??100,this.data={original:n.data??Ie(this.count,this.dimension)},n.data&&n.data.length>0&&(this.dimension=n.data[0].length,this.count=n.data.length));}centroid(){if(!this.data.centroid){let n=this.data.original,t=this.dimension,e=n.length;if(e===0)return this.data.centroid=[],this.data.centroid;let o=new Array(t).fill(0);for(let r of n)for(let i=0;i<t;i+=1)o[i]+=r[i]??0;this.data.centroid=o.map(r=>r/e);}return this.data.centroid}variances(){if(!this.data.variances){let n=this.data.original,t=this.dimension,e=n.length;if(e<2)return this.data.variances=new Array(t).fill(NaN),this.data.variances;let o=this.centroid(),r=new Array(t).fill(0);for(let i of n)for(let a=0;a<t;a+=1){let s=(i[a]??0)-o[a];r[a]+=s*s;}this.data.variances=r.map(i=>i/(e-1));}return this.data.variances}standardDeviations(){return this.data.stddevs||(this.data.stddevs=this.variances().map(n=>Math.sqrt(n))),this.data.stddevs}covarianceMatrix(){if(!this.data.covarianceMatrix){let n=this.data.original,t=this.dimension,e=n.length,o=this.centroid(),r=Array.from({length:t},()=>new Array(t).fill(0));if(e<2)return this.data.covarianceMatrix=r,this.data.covarianceMatrix;for(let i of n)for(let a=0;a<t;a+=1){let s=(i[a]??0)-o[a];for(let d=a;d<t;d+=1){let u=(i[d]??0)-o[d];r[a][d]+=s*u;}}for(let i=0;i<t;i+=1)for(let a=i;a<t;a+=1)r[i][a]/=e-1,r[a][i]=r[i][a];this.data.covarianceMatrix=r;}return this.data.covarianceMatrix}correlationMatrix(){if(!this.data.correlationMatrix){let n=this.covarianceMatrix(),t=this.dimension,e=this.standardDeviations(),o=Array.from({length:t},()=>new Array(t).fill(0));for(let r=0;r<t;r+=1)for(let i=0;i<t;i+=1){let a=e[r]*e[i];o[r][i]=a===0?r===i?1:0:n[r][i]/a;}this.data.correlationMatrix=o;}return this.data.correlationMatrix}mahalanobis(n){let t=this.centroid(),e=this.variances(),o=Math.min(n.length,t.length,e.length);if(o===0)return 0;let r=0;for(let i=0;i<o;i+=1){let a=(n[i]??0)-t[i],s=Math.max(e[i],1e-8);r+=a*a/s;}return Math.sqrt(r)}mahalanobisAll(){if(!this.data.mahalanobisDistances){let n=this.centroid(),t=this.variances();this.data.mahalanobisDistances=this.data.original.map(e=>{let o=Math.min(e.length,n.length,t.length),r=0;for(let i=0;i<o;i+=1){let a=(e[i]??0)-n[i],s=Math.max(t[i],1e-8);r+=a*a/s;}return Math.sqrt(r)});}return this.data.mahalanobisDistances}outliersByMahalanobis(n=3){let t=this.mahalanobisAll(),e=[];for(let o=0;o<t.length;o+=1)t[o]>n&&e.push(this.data.original[o]);return e}normalizeL2(){let n=this.data.original.map(t=>{let e=0;for(let r of t)e+=r*r;let o=Math.sqrt(e);return o===0?t.map(()=>0):t.map(r=>r/o)});return new c({data:n})}normalizeZscore(){let n=this.centroid(),t=this.standardDeviations(),e=this.dimension,o=this.data.original.map(r=>{let i=new Array(e);for(let a=0;a<e;a+=1){let s=t[a];i[a]=s===0||isNaN(s)?0:((r[a]??0)-n[a])/s;}return i});return new c({data:o})}describe(){let n=this.mahalanobisAll(),t=n.filter(r=>r>3).length,e=this.dimension,o=[];for(let r=0;r<e;r+=1){let i=this.data.original.map(s=>s[r]??0),a=new F({data:i});o.push(a.describe());}return this.data.description={original:this.data.original,centroid:this.centroid(),variances:this.variances(),correlationMatrix:this.correlationMatrix(),mahalanobisDistances:n,outlierCount:t,dimensionSummaries:o},this.data.description}},be=class{smoothed=false;static DEFAULT_MAX_RANDOM_INTEGER=H;static DEFAULT_MIN_RANDOM_INTEGER=Nt;static DEFAULT_RANDOM_SERIES_COUNT=xe;static DEFAULT_OUTLIER_MULTIPLE=Ne;static DEFAULT_JITTER_MULTIPLIER=Be;static DEFAULT_SPLIT_PASSES=_e;static DEFAULT_MAX_RANDOM_DIMENSIONALITY=re;static Series=F;static Points=pe;static randomInteger=Le;static randomSeries=ze;static randomPoint=je;static randomPoints=Ie},zt=be;export{R as GraphAlgorithmError,fe as GraphCatalog,pe as Points,F as Series,be as Twokeys,Ge as aStarShortestPath,Ce as allPairsShortestPaths,Rt as analyzeGraph,ht as articulationPointsAndBridges,lt as betweennessCentrality,k as buildGraphAdjacency,ut as closenessCentrality,Fe as clusteringCoefficient,se as cosineSimilarity,bt as cosineSimilaritySparse,ft as createGraphCatalog,Gt as dbscan,zt as default,ct as degreeCentrality,U as euclideanDistance,_t as gds,Ot as graphEda,Dt as graphOutliers,kt as hierarchicalClustering,xt as jaccardSimilarity,At as kMeansAuto,mt as kMeansClustering,ke as kNearestNeighbors,Ee as labelPropagationCommunities,Et as linkPrediction,Ae as louvainCommunities,It as mahalanobisDistance,ie as manhattanDistance,Oe as maximumFlow,De as minCostMaxFlow,vt as minimumSpanningTree,Pe as nodeSimilarity,pt as normalizeL2,Mt as overlapCoefficient,he as pageRank,me as predictLinks,ee as shortestPath,V as squaredEuclideanDistance,Re as stronglyConnectedComponents,St as topologicalSort,Pt as travelingSalesmanApprox,at as weaklyConnectedComponents,Te as yenKShortestPaths};//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map