@graphty/cytoscape-extensions 0.0.29 → 0.0.30

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.
@@ -1 +1 @@
1
- import{I as M,r as V,m as L,a as fe,b as ct,c as bt,d as tt,f as dr,e as fs,g as hs,h as fr,i as gs,j as hr,k as ws,_ as Gt,l as ps,G as ms,n as gr,o as yt,p as wt,q as wr}from"./preload-helper-DEpZAvig.js";function $(t,e){return Object.assign(t,{code:e})}function Re(t,e,n,r){const o=n===void 0?`${t}: ${String(e)} nodes need ${String(r)} bytes of n x n memory, more than this runtime could allocate. Pass a maxNodes to refuse such graphs up front.`:`${t}: ${String(e)} nodes exceeds maxNodes ${String(n)}; the result would allocate ${String(r)} bytes. Pass a larger maxNodes to allow it.`;return Object.assign($(new RangeError(o),"E_TOO_LARGE"),{params:{nodeCount:e,maxNodes:n??null,bytes:r}})}function pr(t,e,n,r,o){if(n!==void 0&&(e>n||Number.isNaN(n)))throw Re(t,e,n,r);try{return o()}catch(s){throw s instanceof RangeError?Re(t,e,void 0,r):s}}class mr extends Error{constructor(e,n,r){super(`${e} did not converge in ${String(n)} iterations (tolerance ${String(r)}); raise maxIterations or tolerance`),this.algorithm=e,this.iterations=n,this.tolerance=r,this.name="ConvergenceError",this.code="E_NOT_CONVERGED"}}class De extends Error{constructor(e,n,r){super(`cannot walk the shortest path from node ${String(e)} to node ${String(n)}: the predecessor array has a ${r}`),this.source=e,this.target=n,this.reason=r,this.name="PathWalkError",this.code="E_BAD_PATH"}}class ys extends Error{constructor(e){super(`${e}: the accelerator's shortest-path counts overflowed, so its scores are wrong; run the CPU ${e} instead`),this.algorithm=e,this.name="PathCountOverflowError",this.code="E_PATH_COUNT_OVERFLOW"}}function yr(t,e){let n=0,r=t.length-1;for(;n<r;){const o=n+r>>1;t[o+1]<=e?n=o+1:r=o}return n}class _t{constructor(e,n=!1){this.size=0,this.tieBreakByIndex=n,this.heap=new Uint32Array(e),this.slot=new Uint32Array(e).fill(M),this.key=new Float64Array(e)}isEmpty(){return this.size===0}has(e){return this.slot[e]!==M}keyOf(e){return this.key[e]}peekKey(){return this.size===0?1/0:this.key[this.heap[0]]}push(e,n){if(!(e>=0&&e<this.slot.length))throw new RangeError(`Node index ${String(e)} is outside the heap's ${String(this.slot.length)} nodes`);this.key[e]=n,this.heap[this.size]=e,this.slot[e]=this.size,this.size++,this.siftUp(this.size-1)}pushOrDecrease(e,n){const r=this.slot[e];if(r===M){this.push(e,n);return}n<this.key[e]&&(this.key[e]=n,this.siftUp(r))}pop(){const e=this.heap[0];if(this.slot[e]=M,this.size--,this.size>0){const n=this.heap[this.size];this.heap[0]=n,this.slot[n]=0,this.siftDown(0)}return e}before(e,n){const r=this.key[e],o=this.key[n];return r<o||this.tieBreakByIndex&&r===o&&e<n}siftUp(e){let n=e;const r=this.heap[n];for(;n>0;){const o=n-1>>1,s=this.heap[o];if(!this.before(r,s))break;this.heap[n]=s,this.slot[s]=n,n=o}this.heap[n]=r,this.slot[r]=n}siftDown(e){let n=e;const r=this.heap[n];for(;;){const o=2*n+1;if(o>=this.size)break;const s=o+1,i=s<this.size&&this.before(this.heap[s],this.heap[o])?s:o,a=this.heap[i];if(!this.before(a,r))break;this.heap[n]=a,this.slot[a]=n,n=i}this.heap[n]=r,this.slot[r]=n}}function Ar(t,e,n,r){const o=[];let s=r;for(;s!==n;){if(o.length>=t.nodeCount-1)throw new De(n,r,"cycle");const i=e[s];if(!(i<t.arcCount))throw new De(n,r,"gap");o.push(i),s=yr(t.rowPtr,i)}return o}function zt(t,e,n,r){const o=V(t,r);if(o===n)return Uint32Array.of(n);if(e[o]===M)return new Uint32Array(0);const s=[o,...Ar(t,e,n,o).map(a=>yr(t.rowPtr,a))],i=new Uint32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[s.length-1-a];return i}function Bt(t,e,n,r){const o=V(t,r);if(o===n||e[o]===M)return new Uint32Array(0);const s=Ar(t,e,n,o).map(a=>t.arcToEdge[a]),i=new Uint32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[s.length-1-a];return i}function vr(t,e,n={}){const r=V(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.weights??t.weights,l=new Float64Array(o).fill(1/0),c=new Uint32Array(o).fill(M),u=new _t(o),d=n.cutoff??1/0;for(l[r]=0,u.push(r,0);!u.isEmpty();){const f=u.pop(),h=l[f],g=s[f+1];for(let w=s[f];w<g;w++){const m=a===null?1:a[w],A=i[w],p=h+m;p<l[A]&&p<=d&&(l[A]=p,c[A]=w,u.pushOrDecrease(A,p))}}return{dist:l,predArc:c,pathTo:f=>zt(t,c,r,f),pathEdges:f=>Bt(t,c,r,f)}}const Er=5792;function As(t,e){if(e.weighted===!1)return{w:null,negative:!1};if(e.weights!==void 0&&e.weights.length!==t.arcCount)throw $(new RangeError(`allPairsShortestPath: the weights override has ${String(e.weights.length)} entries; the snapshot has ${String(t.arcCount)} arcs`),"E_BAD_OPTION");const n=e.weights??t.weights;if(n===null)return{w:null,negative:!1};let r=!0,o=!1;for(let s=0;s<t.arcCount;s++){const i=n[s];if(!Number.isFinite(i)){const a=e.weights===void 0&&!Number.isNaN(i)?"; a finite weight above the f32 range (3.4e38) becomes Infinity in the snapshot's f32 arc weights -- pass the exact f64 weights through the weights override":"";throw $(new RangeError(`allPairsShortestPath: arc ${String(s)} has weight ${String(i)}; weights must be finite${a}`),"E_BAD_WEIGHT")}r&&(r=i===1),o||(o=i<0)}return{w:r?null:n,negative:o}}function vs(t,e,n,r,o){const{nodeCount:s,rowPtr:i,colIdx:a}=t;r.fill(1/0);for(let l=0;l<s;l++){const c=l*s;for(let u=i[l];u<i[l+1];u++){const d=a[u],f=e===null?1:e[u];d!==l&&f<r[c+d]&&(r[c+d]=f,o!==null&&(o[c+d]=u))}r[c+l]=0}for(let l=0;l<s;l++){const c=l*s,u=r.subarray(c,c+s);for(let d=0;d<s;d++){const f=d*s,h=r[f+l];if(h===1/0)continue;const g=r.subarray(f,f+s);if(o===null)for(let w=0;w<g.length;w++){const m=h+u[w];m<g[w]&&(g[w]=m)}else{const w=o.subarray(c,c+s),m=o.subarray(f,f+s);for(let A=0;A<g.length;A++){const p=h+u[A];p<g[A]&&(g[A]=p,m[A]=w[A])}}}if(n){for(let d=0;d<s;d++)if(r[d*s+d]<0)return!0}}return!1}function Es(t,e){for(let n=0;n<t.nodeCount;n++)for(let r=t.rowPtr[n];r<t.rowPtr[n+1];r++)if(e[r]<0&&(!t.directed||t.colIdx[r]===n))return!0;return!1}function bs(t,e,n){const{nodeCount:r,rowPtr:o,colIdx:s}=t,i=new Uint32Array(r);e.fill(1/0);for(let a=0;a<r;a++){const l=e.subarray(a*r,a*r+r),c=n?.subarray(a*r,a*r+r);l[a]=0,i[0]=a;let u=1;for(let d=0;d<u;d++){const f=i[d],h=l[f]+1;for(let g=o[f];g<o[f+1];g++){const w=s[g];l[w]===1/0&&(l[w]=h,c!==void 0&&(c[w]=g),i[u++]=w)}}}}function Cs(t,e,n,r){const{nodeCount:o,rowPtr:s,colIdx:i}=t,a=new _t(o);n.fill(1/0);for(let l=0;l<o;l++){const c=n.subarray(l*o,l*o+o),u=r?.subarray(l*o,l*o+o);for(c[l]=0,a.push(l,0);!a.isEmpty();){const d=a.pop(),f=c[d];for(let h=s[d];h<s[d+1];h++){const g=i[h],w=f+e[h];w<c[g]&&(c[g]=w,u!==void 0&&(u[g]=h),a.pushOrDecrease(g,w))}}}}function Is(t,e,n,r){if(r==="floyd-warshall")return"floyd-warshall";if(e===null)return"bfs";if(n){if(r==="per-source")throw $(new Error('allPairsShortestPath: method "per-source" runs Dijkstra, which is incorrect with a negative weight; use "auto" or "floyd-warshall"'),"E_BAD_OPTION");return"floyd-warshall"}const o=t.nodeCount;return r==="per-source"||3*t.arcCount<o*o?"dijkstra":"floyd-warshall"}function br(t,e={}){const n=t.nodeCount,r=e.maxNodes??Er;if(!(n<=r))throw Re("allPairsShortestPath",n,r,(e.paths===!0?12:8)*n*n);const{w:o,negative:s}=As(t,e),i=Is(t,o,s,e.method),a=new Float64Array(n*n),l=e.paths===!0?new Uint32Array(n*n).fill(M):null;let c=!1;i==="bfs"?bs(t,a,l):i==="dijkstra"&&o!==null?Cs(t,o,a,l):c=s&&o!==null&&Es(t,o)||vs(t,o,s,a,l),c&&a.fill(Number.NaN);const u=(d,f)=>{const h=V(t,d),g=V(t,f);if(l===null)throw $(new Error("allPairsShortestPath: pass paths: true to walk shortest paths"),"E_BAD_OPTION");if(c)throw new De(h,g,"cycle");return l.subarray(h*n,h*n+n)};return{dist:a,n,hasNegativeCycle:c,method:i,predArc:l,pathTo:(d,f)=>zt(t,u(d,f),V(t,d),f),pathEdges:(d,f)=>Bt(t,u(d,f),V(t,d),f)}}function Cr(t,e,n={}){const r=V(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.weights??t.weights,l=n.cutoff??1/0,c=new Float64Array(o).fill(1/0),u=new Uint32Array(o).fill(M);c[r]=0;const d=()=>{let g=!1;for(let w=0;w<o;w++){const m=c[w];if(m===1/0)continue;const A=s[w+1];for(let p=s[w];p<A;p++){const y=i[p],v=m+(a===null?1:a[p]);v<c[y]&&v<=l&&(c[y]=v,u[y]=p,g=!0)}}return g};let f=!1;for(let g=0;g<o-1&&!f;g++)f=!d();const h=!f&&d();return{dist:c,predArc:u,hasNegativeCycle:h,pathTo:g=>zt(t,u,r,g),pathEdges:g=>Bt(t,u,r,g)}}const xs=2654435769;function Ps(t,e){const n=Array.from({length:t},(o,s)=>s);let r=xs;for(let o=0;o<e;o++){r=r+1831565813>>>0;let s=Math.imul(r^r>>>15,r|1);s=s+Math.imul(s^s>>>7,s|61)^s;const i=((s^s>>>14)>>>0)/2**32,a=o+Math.floor(i*(t-o));[n[o],n[a]]=[n[a],n[o]]}return n.slice(0,e)}function Jt(t,e,n,r="betweenness"){const o=t.nodeCount,s=e===void 0?void 0:Array.from(fe(t,e));if(n!==void 0&&(!Number.isInteger(n)||n<0||n>o))throw $(new RangeError(`${r}: k must be an integer in [0, ${o}], got ${n}`),"E_BAD_OPTION");if(s!==void 0){for(const i of s)if(!Number.isInteger(i)||i<0||i>=o)throw $(new RangeError(`${r}: sources must be node indices in [0, ${o}), got ${i}`),"E_BAD_NODE");if(n!==void 0&&n!==s.length)throw $(new RangeError(`${r}: k (${n}) must be absent or equal sources.length (${s.length})`),"E_BAD_OPTION");return s}return n===void 0?Array.from({length:o},(i,a)=>a):Ps(o,n)}function Ir(t,e,n,r,o,s){const{nodeCount:i,rowPtr:a,colIdx:l,arcToEdge:c}=t,u=new Int32Array(i).fill(-1),d=new Float64Array(i),f=new Float64Array(i),h=new Uint32Array(i);for(const g of e){let w=0,m=0;for(u[g]=0,d[g]=1,h[m++]=g;w<m;){const A=h[w++],p=u[A]+1;let y=-1;for(let v=a[A],E=a[A+1];v<E;v++){const b=l[v];b===y||o!==null&&!L(o,c[v])||(y=b,u[b]<0&&(u[b]=p,h[m++]=b),u[b]===p&&(d[b]+=d[A]))}}for(let A=m-1;A>=0;A--){const p=h[A],y=u[p]+1;let v=-1;for(let E=a[p],b=a[p+1];E<b;E++){const C=l[E];if(!(C===v||o!==null&&!L(o,c[E]))&&(v=C,u[C]===y)){const x=d[p]/d[C]*(1+f[C]);f[p]+=x,r!==null&&(r[c[E]]+=x)}}n!==null&&p!==g&&(n[p]+=s?f[p]+1:f[p])}n!==null&&s&&(n[g]+=m-1);for(let A=0;A<m;A++){const p=h[A];u[p]=-1,d[p]=0,f[p]=0}}}function xr(t,e,n,r){const o=t.directed?1:2,s=n===!0&&r>0?r:o;for(let i=0;i<e.length;i++)e[i]/=s;return s}function Pr(t,e={}){const n=t.nodeCount,r=Jt(t,e.sources,e.k),o=new Float64Array(n),s=e.endpoints===!0;Ir(t,r,o,null,null,s);const i=xr(t,o,e.normalized,s?n*(n-1):(n-1)*(n-2));return{scores:o,iterations:r.length,converged:!0,divisor:i}}function Ze(t,e={}){const n=t.nodeCount,r=e.alive??null;if(r!==null&&r.length<Math.ceil(t.edgeCount/32))throw $(new RangeError(`edgeBetweennessCentrality: alive covers fewer than ${t.edgeCount} edges`),"E_BAD_OPTION");const o=new Float64Array(t.edgeCount);Ir(t,Jt(t,e.sources,e.k),null,o,r,!1);const s=xr(t,o,e.normalized,(n-1)*(n-2));return{scores:o,divisor:s}}function Je(t,e,n){return{pathTo:r=>zt(t,n,e,r),pathEdges:r=>Bt(t,n,e,r)}}function he(t,e,n="start"){const r=V(t,e);if(!Number.isInteger(r)||r<0||r>=t.nodeCount)throw $(new RangeError(`${n} node index ${String(r)} is out of range for ${String(t.nodeCount)} nodes`),"E_BAD_NODE");return r}function Nr(t,e){return e===void 0?M:he(t,e,"target")}function ge(t,e){if(e===void 0)return null;const{rowPtr:n,nodeCount:r}=t;if(e.length!==t.arcCount)throw $(new RangeError(`arcOrder has ${String(e.length)} entries, expected ${String(t.arcCount)}`),"E_BAD_OPTION");const o=new Uint8Array(e.length);for(let s=0;s<r;s++){const i=n[s],a=n[s+1];for(let l=i;l<a;l++){if(e[l]<i||e[l]>=a)throw $(new RangeError(`arcOrder[${String(l)}] = ${String(e[l])} is not an arc of node ${String(s)}`),"E_BAD_OPTION");if(o[e[l]]===1)throw $(new RangeError(`arcOrder[${String(l)}] = ${String(e[l])} repeats an arc of node ${String(s)}`),"E_BAD_OPTION");o[e[l]]=1}}return e}function Fr(t,e,n={}){const r=he(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=ge(t,n.arcOrder),l=new Uint32Array(o).fill(M),c=new Uint32Array(o).fill(M),u=new Uint32Array(o).fill(M),d=new Uint32Array(o),f=n.maxDepth??M,h=Nr(t,n.target);let g=0,w=0;for(d[w++]=r,u[r]=0;g<w;){const m=d[g++];if(m===h)break;const A=u[m];if(A>=f)continue;const p=s[m+1];for(let y=s[m];y<p;y++){const v=a===null?y:a[y],E=i[v];u[E]===M&&(u[E]=A+1,l[E]=m,c[E]=v,d[w++]=E)}}return{order:d.subarray(0,w),parent:l,depth:u,visitedCount:w,predArc:c,...Je(t,r,c)}}function Ns(t,e,n,r){const{rowPtr:o,colIdx:s,arcToEdge:i}=t;let a=o[n],l=o[n+1];for(;a<l;){const u=a+l>>>1;s[u]<r?a=u+1:l=u}const c=o[n+1];for(;a<c&&s[a]===r&&i[a]!==i[e];)a++;return a}function Fs(t,e,n={}){const r=he(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.alpha??1,l=n.beta??18;let c=null;const u=new Uint32Array(o).fill(M),d=new Uint32Array(o).fill(M),f=new Uint32Array(o).fill(M),h=new Uint32Array(o);h[0]=r,f[r]=0;let g=0,w=1;const m=t.inDegree();let A=t.arcCount-m[r],p=!1;for(let y=0;g<w;y++){const v=w-g;if(p)p=v>=o/l;else{let b=0;for(let C=g;C<w;C++)b+=s[h[C]+1]-s[h[C]];p=b>A/a}const E=w;if(p){c??(c=t.reverse());for(let b=0;b<o;b++){if(f[b]!==M)continue;const C=c.rowPtr[b+1];for(let x=c.rowPtr[b];x<C;x++){const _=c.colIdx[x];if(f[_]===y){u[b]=_,d[b]=t.directed?c.fwdArc[x]:Ns(t,x,_,b),f[b]=y+1,h[w++]=b;break}}}}else{for(let b=g;b<E;b++){const C=h[b],x=s[C+1];for(let _=s[C];_<x;_++){const S=i[_];f[S]===M&&(u[S]=C,d[S]=_,f[S]=y+1,h[w++]=S)}}h.subarray(E,w).sort()}for(let b=E;b<w;b++)A-=m[h[b]];g=E}return{order:h.subarray(0,w),parent:u,depth:f,visitedCount:w,predArc:d,...Je(t,r,d)}}const Ie=2;function kr(t,e={}){const{nodeCount:n}=t,r=t.directed&&e.arcs!=="out"?[t,t.reverse()]:[t],o=new Uint8Array(n).fill(Ie),s=new Uint32Array(n);for(let a=0;a<n;a++){if(o[a]!==Ie)continue;o[a]=0;let l=0,c=0;for(s[c++]=a;l<c;){const u=s[l++];for(const{rowPtr:d,colIdx:f}of r){const h=d[u+1];for(let g=d[u];g<h;g++){const w=f[g];if(o[w]===Ie)o[w]=o[u]^1,s[c++]=w;else if(o[w]===o[u])return{bipartite:!1,sides:null}}}}}const i=ct(n);for(let a=0;a<n;a++)o[a]===1&&bt(i,a,!0);return{bipartite:!0,sides:i}}function $e(t,e,n){const{rowPtr:r,colIdx:o,weights:s}=e,i=new Float64Array(t).fill(1/0),a=new Uint32Array(t),l=s===null?null:new _t(t),c=new Uint8Array(t);return{dist:i,reached:a,run(u){let d=0;if(i[u]=0,a[d++]=u,l===null||s===null){for(let f=0;f<d;f++){const h=a[f];if(!(i[h]>=n))for(let g=r[h],w=r[h+1];g<w;g++){const m=o[g];i[m]===1/0&&(i[m]=i[h]+1,a[d++]=m)}}return d}for(l.push(u,0);!l.isEmpty();){const f=l.pop();if(c[f]=1,!(i[f]>=n))for(let h=r[f],g=r[f+1];h<g;h++){const w=o[h];if(c[w]===1)continue;const m=i[f]+s[h];m<i[w]&&(i[w]===1/0&&(a[d++]=w),i[w]=m,l.pushOrDecrease(w,m))}}return d},clear(u){for(let d=0;d<u;d++)i[a[d]]=1/0,c[a[d]]=0}}}function Sr(t,e,n,r,o){return t.harmonic===!0?t.normalized===!0&&e>1?o/(e-1):o:r<=0?0:t.normalized===!0&&e>1?n/r/(e-1):1/r}function _r(t,e,n,r){const o=t.run(r);let s=0,i=0;for(let a=1;a<o;a++){const l=t.dist[t.reached[a]];s+=l,i+=l>0?1/l:0}return t.clear(o),Sr(e,n,o-1,s,i)}function ze(t,e,n){const r=e.weighted===!0;if(!n||!t.directed)return{rowPtr:t.rowPtr,colIdx:t.colIdx,weights:r?e.weights??t.weights:null};const o=t.reverse(),s=e.weights;let i=null;return r&&(i=s===void 0?o.weights:Float64Array.from(o.fwdArc,a=>s[a])),{rowPtr:o.rowPtr,colIdx:o.colIdx,weights:i}}function Mr(t,e={}){const n=t.nodeCount,r=e.cutoff??1/0,o=new Float64Array(n);if(e.sources===void 0&&e.k===void 0){const u=$e(n,ze(t,e,!1),r);for(let d=0;d<n;d++)o[d]=_r(u,e,n,d);return{scores:o,iterations:n,converged:!0,sourcesUsed:n}}const s=Jt(t,e.sources,e.k,"closenessCentrality"),i=$e(n,ze(t,e,!0),r),a=new Float64Array(n),l=new Float64Array(n),c=new Uint32Array(n);for(const u of s){const d=i.run(u);for(let f=1;f<d;f++){const h=i.reached[f],g=i.dist[h];a[h]+=g,l[h]+=g>0?1/g:0,c[h]+=1}i.clear(d)}for(let u=0;u<n;u++)o[u]=Sr(e,n,c[u],a[u],l[u]);return{scores:o,iterations:s.length,converged:!0,sourcesUsed:s.length}}function ks(t,e,n={}){const r=V(t,e);if(!Number.isInteger(r)||r<0||r>=t.nodeCount)throw $(new RangeError(`nodeClosenessCentrality: node must be an index in [0, ${t.nodeCount}), got ${r}`),"E_BAD_NODE");const o=$e(t.nodeCount,ze(t,n,!1),n.cutoff??1/0);return _r(o,n,t.nodeCount,r)}function Ur(t,e,n,r,o){let s=t.rowPtr[n];const i=t.rowPtr[n+1];let a=e.rowPtr[r];const l=e.rowPtr[r+1];let c=0;for(;s<i&&a<l;){const u=t.colIdx[s],d=e.colIdx[a];if(u===d)for(c+=o===void 0?1:o(u),s++,a++;a<l&&e.colIdx[a]===d;)a++;else u<d?s++:a++}return c}function Ss(t,e,n,r={}){return Ur(t,r.directed===!0?t.reverse():t,e,n)}class qt{constructor(e){this.parent=new Uint32Array(e),this.rank=new Uint32Array(e);for(let n=0;n<e;n++)this.parent[n]=n}find(e){let n=e;for(;this.parent[n]!==n;)this.parent[n]=this.parent[this.parent[n]],n=this.parent[n];return n}union(e,n){const r=this.find(e),o=this.find(n);return r===o?!1:(this.rank[r]<this.rank[o]?this.parent[r]=o:this.rank[r]>this.rank[o]?this.parent[o]=r:(this.parent[o]=r,this.rank[r]++),!0)}toLabels(){const e=new Uint32Array(this.parent.length);for(let n=0;n<e.length;n++)e[n]=this.find(n);return tt(e)}}function K(t,e){let n=null;return{labels:t,count:e,groups(){if(n===null){const r=new Uint32Array(e);for(let i=0;i<t.length;i++)r[t[i]]++;const o=[];for(let i=0;i<e;i++)o.push(new Uint32Array(r[i]));const s=new Uint32Array(e);for(let i=0;i<t.length;i++){const a=t[i];o[a][s[a]++]=i}n=o}return n}}}function Or(t){const e=new qt(t.nodeCount),n=t.edgeList();for(let s=0;s<t.edgeCount;s++)e.union(n.src[s],n.dst[s]);const{labels:r,count:o}=e.toLabels();return K(r,o)}function Tr(t){if(t.directed)throw $(new Error("Connected components requires an undirected graph. Use weaklyConnectedComponents, or pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");return Or(t)}function Rr(t){return Or(t)}function vn(t,e){const{rowPtr:n,colIdx:r}=t;for(let o=0;o<t.nodeCount;o++){let s=-1;for(let i=n[o],a=n[o+1];i<a;i++)r[i]!==s&&(s=r[i],e[o]++)}}function _s(t,e={}){const n=new Float64Array(t.nodeCount),r=t.directed?e.mode??"total":"out";r!=="in"&&vn(t,n),r!=="out"&&vn(t.reverse(),n);const o=t.nodeCount-1;if(e.normalized===!0&&o>0)for(let s=0;s<n.length;s++)n[s]/=o;return n}function Dr(t){const{src:e,dst:n}=t.edgeList(),r=new Uint32Array(t.nodeCount),o=new Uint32Array(t.nodeCount);for(let s=0;s<e.length;s++)o[e[s]]++,r[n[s]]++;return{inDegree:r,outDegree:o}}class $r{constructor(e){this.min=new _t(e,!0)}isEmpty(){return this.min.isEmpty()}has(e){return this.min.has(e)}keyOf(e){return-this.min.keyOf(e)}push(e,n){this.min.push(e,-n)}pushOrIncrease(e,n){this.min.pushOrDecrease(e,-n)}pop(){return this.min.pop()}}function zr(t){const e=new Uint32Array(t.arcCount),n=t.rowPtr.slice(0,t.nodeCount),{src:r}=t.edgeList();for(let o=0;o<t.edgeCount;o++)e[n[r[o]]++]=t.edgeToArc[o];return e}function Br(t,e,n){const r=new Float64Array(t.nodeCount);for(let o=0;o<t.nodeCount;o++){let s=0;for(let i=t.rowPtr[o];i<t.rowPtr[o+1];i++)s+=n===null?1:n[e[i]];r[o]=s}return r}function Qe(t){let e=0;for(let n=0;n<t.length;n++)e+=t[n];if(e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Ms(t,e,n){if(t===void 0)return null;if(t.length!==e)throw $(new RangeError(`${n} has ${String(t.length)} entries; the graph has ${String(e)} nodes`),"E_BAD_OPTION");const r=Float64Array.from(t);return Qe(r),r}function qr(t,e){if(e===void 0)return t.weights;if(e.length!==t.arcCount)throw $(new RangeError(`weights has ${String(e.length)} entries; the graph has ${String(t.arcCount)} arcs`),"E_BAD_OPTION");return e}function Us(t){if(t<0||t>1)throw $(new Error("Damping factor must be between 0 and 1"),"E_BAD_OPTION")}class Os{constructor(e,n={}){if(!e.directed)throw $(new Error("DeltaPageRank requires a directed graph"),"E_NEEDS_DIRECTED");this.s=e,this.order=zr(e),this.weights=qr(e,n.weights),this.outW=Br(e,this.order,this.weights),this.scores=new Float64Array(e.nodeCount),this.deltas=new Float64Array(e.nodeCount).fill(1/e.nodeCount),this.active=new Uint8Array(e.nodeCount).fill(1)}compute(e={}){return this.run(e,!1)}update(e,n={}){const{s:r}=this,o=r.reverse(),s=new Uint8Array(r.nodeCount);for(let i=0;i<e.length;i++){const a=e[i];if(!(!Number.isInteger(a)||a<0||a>=r.nodeCount)){s[a]=1;for(let l=o.rowPtr[a];l<o.rowPtr[a+1];l++)s[o.colIdx[l]]=1;for(let l=r.rowPtr[a];l<r.rowPtr[a+1];l++)s[r.colIdx[l]]=1}}return this.active=s,this.run(n,e.length>0)}run(e,n){const r=e.dampingFactor??.85,o=e.tolerance??1e-6,s=e.maxIterations??100,i=e.deltaThreshold??o/10;Us(r);const{s:a,order:l,scores:c}=this,u=a.nodeCount;if(u===0)return new Float64Array(0);const d=Ms(e.personalization,u,"personalization"),f=e.weighted===!0?this.weights:null,h=(1-r)/u;let g=this.active.reduce((w,m)=>w+m,0);for(let w=0;(g>0||w===0&&n)&&w<s;w++){const m=new Float64Array(u);let A=0;for(let y=0;y<u;y++)a.rowPtr[y]===a.rowPtr[y+1]&&(A+=c[y]+this.deltas[y]);if(A>0){const y=r*A/u;for(let v=0;v<u;v++)m[v]+=d===null?y:y*d[v]}for(let y=0;y<u;y++){const v=this.deltas[y];if(this.active[y]===0||Math.abs(v)<i)continue;c[y]+=v;const E=this.outW[y];if(E>0)for(let b=a.rowPtr[y];b<a.rowPtr[y+1];b++){const C=l[b];m[a.colIdx[C]]+=r*v*((f===null?1:f[C])/E)}}let p=0;g=0;for(let y=0;y<u;y++){m[y]+=d===null?h:(1-r)*d[y];const v=Math.abs(m[y]);p=Math.max(p,v),this.active[y]=v>=i?1:0,g+=this.active[y]}if(this.deltas=m,p<o)break}return Qe(c),c.slice()}}class Ts{constructor(e,n={}){if(!e.directed)throw $(new Error("PriorityDeltaPageRank requires a directed graph"),"E_NEEDS_DIRECTED");this.s=e,this.order=zr(e),this.weights=qr(e,n.weights),this.outW=Br(e,this.order,this.weights),this.scores=new Float64Array(e.nodeCount),this.deltas=new Float64Array(e.nodeCount).fill(1/e.nodeCount),this.heap=new $r(e.nodeCount);for(let r=0;r<e.nodeCount;r++)this.heap.push(r,1/e.nodeCount)}computeWithPriority(e={}){const n=e.dampingFactor??.85,r=e.tolerance??1e-6,o=e.maxIterations??100,s=e.deltaThreshold??r/10,{s:i,order:a,scores:l,deltas:c,heap:u}=this,d=i.nodeCount,f=e.weighted===!0?this.weights:null;let h=0,g=0;for(;!u.isEmpty()&&h<o;){const w=u.pop(),m=c[w];if(Math.abs(m)<s)continue;l[w]+=m,c[w]=0;const A=f===null?i.rowPtr[w+1]-i.rowPtr[w]:this.outW[w];if(A>0)for(let p=i.rowPtr[w];p<i.rowPtr[w+1];p++){const y=a[p],v=i.colIdx[y],E=c[v]+n*m*((f===null?1:f[y])/A);c[v]=E,Math.abs(E)>=s&&u.pushOrIncrease(v,Math.abs(E))}if(g++,g%1e3===0){let p=0;for(let y=0;y<d;y++)p=Math.max(p,Math.abs(c[y]));if(p<r)break}h++}for(let w=0;w<d;w++)Math.abs(c[w])>=s&&(l[w]+=c[w]);return Qe(l),l.slice()}}const tn=0,Be=1,Rs=2;function en(t){return{state:new Uint8Array(t),parent:new Uint32Array(t).fill(M),predArc:new Uint32Array(t).fill(M),depth:new Uint32Array(t).fill(M),cursor:new Uint32Array(t),stack:new Uint32Array(t),pre:new Uint32Array(t),post:new Uint32Array(t),preCount:0,postCount:0,backArc:!1}}function Ds(t,e,n,r){const{rowPtr:o,colIdx:s}=t,{state:i,parent:a,predArc:l,depth:c,cursor:u,pre:d}=n,f=u[e]++,h=r===null?f:r[f],g=s[h];return i[g]===tn?(i[g]=Be,a[g]=e,l[g]=h,c[g]=c[e]+1,u[g]=o[g],d[n.preCount++]=g,g):(i[g]===Be&&(n.backArc=!0),M)}function nn(t,e,n,r,o){const{rowPtr:s}=t,{state:i,depth:a,cursor:l,stack:c,pre:u,post:d}=n;let f=0;if(i[e]=Be,a[e]=0,l[e]=s[e],u[n.preCount++]=e,c[f++]=e,e!==r)for(;f>0;){const h=c[f-1];if(l[h]<s[h+1]){const g=Ds(t,h,n,o);if(g!==M&&(c[f++]=g,g===r))return}else i[h]=Rs,d[n.postCount++]=h,f--}}function Vr(t,e,n={}){const r=he(t,e),o=ge(t,n.arcOrder),s=Nr(t,n.target),i=en(t.nodeCount),a=n.order==="post";return nn(t,r,i,a?M:s,o),{order:a?i.post.subarray(0,i.postCount):i.pre.subarray(0,i.preCount),parent:i.parent,depth:i.depth,visitedCount:i.preCount,predArc:i.predArc,...Je(t,r,i.predArc)}}function $s(t){return t.directed?zs(t):Bs(t)}function zs(t){const{nodeCount:e}=t,n=en(e);for(let r=0;r<e&&!n.backArc;r++)n.state[r]===tn&&nn(t,r,n,M,null);return n.backArc}function Bs(t){const{nodeCount:e,rowPtr:n,colIdx:r}=t,o=new qt(e);for(let s=0;s<e;s++){const i=n[s+1];for(let a=n[s];a<i;a++){const l=r[a];if(l===s||l>s&&(a===n[s]||r[a-1]!==l)&&!o.union(s,l))return!0}}return!1}function qs(t,e={}){if(!t.directed)throw $(new Error("Topological sort requires a directed graph"),"E_NEEDS_DIRECTED");const n=ge(t,e.arcOrder),r=en(t.nodeCount);for(let o=0;o<t.nodeCount&&!r.backArc;o++)r.state[o]===tn&&nn(t,o,r,M,n);return r.backArc?null:r.post.reverse()}function Wr(t,e={}){const n=t.nodeCount,r=e.maxIterations??100,o=e.tolerance??1e-6,s=e.startVector;if(s!==void 0&&s.length!==n)throw $(new Error(`eigenvectorCentrality: startVector has ${String(s.length)} entries for ${String(n)} nodes`),"E_BAD_OPTION");let i=new Float64Array(n);if(n===0)return{scores:i,iterations:0,converged:!0};const a=Vs(t,e.mode??"in");if(Ws(a,n))return{scores:i,iterations:0,converged:!0};for(let d=0;d<n;d++)a.rowPtr[d+1]>a.rowPtr[d]&&(i[d]=s===void 0?1:s[d]);En(i);let l=new Float64Array(n),c=0,u=!1;for(;c<r&&!u;c++){for(let f=0;f<n;f++){let h=i[f];const g=a.rowPtr[f+1];for(let w=a.rowPtr[f];w<g;w++)h+=i[a.colIdx[w]];l[f]=h}En(l);let d=0;for(let f=0;f<n;f++)d+=Math.abs(l[f]-i[f]);[i,l]=[l,i],u=d<n*o}if(!u)throw new mr("eigenvectorCentrality",r,o);return e.normalized!==!1&&Lr(i),{scores:i,iterations:c,converged:u}}function Lr(t){let e=1/0,n=0;for(const s of t)e=Math.min(e,s),n=Math.max(n,s);const r=n-e,o=n>0?1:0;for(let s=0;s<t.length;s++)t[s]=r>0?(t[s]-e)/r:o}function Vs(t,e){const n=t.nodeCount,r=!t.directed||e==="out"?t:t.reverse(),o=t.directed&&e==="total",s=o?t.rowPtr:new Uint32Array(n+1),i=o?t.colIdx:new Uint32Array(0),a=new Uint32Array(n+1),l=new Uint32Array(r.colIdx.length+i.length);let c=0;for(let u=0;u<n;u++){let d=r.rowPtr[u];const f=r.rowPtr[u+1];let h=s[u];const g=s[u+1],w=c;for(;d<f||h<g;){const m=h>=g||d<f&&r.colIdx[d]<=i[h]?r.colIdx[d++]:i[h++];(c===w||l[c-1]!==m)&&(l[c++]=m)}a[u+1]=c}return{rowPtr:a,colIdx:l.subarray(0,c)}}function Ws(t,e){const n=new Uint32Array(e);for(const s of t.colIdx)n[s]++;const r=new Uint32Array(e);let o=0;for(let s=0;s<e;s++)n[s]===0&&(r[o++]=s);for(let s=0;s<o;s++){const i=r[s];for(let a=t.rowPtr[i];a<t.rowPtr[i+1];a++)--n[t.colIdx[a]]===0&&(r[o++]=t.colIdx[a])}return o===e}function En(t){let e=0;for(const n of t)e+=n*n;if(e=Math.sqrt(e),e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Dt(t,e){const n=new Uint32Array(e),r=e>0&&L(t,0);let o=!1;for(let s=0;s<e;s++)L(t,s)!==r&&(n[s]=1,o=!0);return e===0?K(n,0):K(n,o?2:1)}function rn(t,e){const n=t.edgeList(),r=new Float64Array(t.edgeCount);for(let o=0;o<t.edgeCount;o++)r[o]=e!==void 0?e[n.arc[o]]:n.weights?.[o]??1;return r}function on(t,e,n,r,o){const{src:s,dst:i}=t.edgeList(),a=[];for(let l=0;l<t.edgeCount;l++){const c=L(e,s[l]);(n?c&&!L(e,i[l]):c!==L(e,i[l]))&&(!o||r[l]>0)&&a.push(l)}return Uint32Array.from(a)}function Ls(t,e){const n=t.nodeCount,r=t.edgeCount,{src:o,dst:s}=t.edgeList(),i=new Uint32Array(2*r),a=new Uint32Array(2*r);i.set(o),i.set(s,r),a.set(s),a.set(o,r);const l=dr({directed:!0,nodeCount:n,src:i,dst:a}),{rowPtr:c,colIdx:u,arcToEdge:d,edgeToArc:f}=l,h=l.arcCount,g=new Float64Array(h),w=new Uint32Array(h),m=new Uint8Array(h);for(let b=0;b<n;b++)for(let C=c[b];C<c[b+1];C++){const x=C>c[b]&&u[C]===u[C-1]?w[C-1]:C;w[C]=x;const _=d[C];(_<r||!t.directed)&&(m[x]=1,g[x]+=Math.max(e[_<r?_:_-r],0))}const A=new Uint32Array(h);for(let b=0;b<h;b++)if(w[b]===b){const C=d[b];A[b]=w[f[C<r?C+r:C-r]]}const p=new Uint32Array(h),y=new Uint32Array(n),v=new Uint8Array(h),E=(b,C)=>{v[C]===0&&(v[C]=1,p[c[b]+y[b]++]=C)};for(let b=0;b<r;b++)E(o[b],w[f[b]]),t.directed||E(s[b],w[f[b+r]]);return{rowPtr:c,colIdx:u,arcToEdge:d,capacity:g,groupOf:w,twin:A,order:p,placed:y,inOrder:v,real:m}}function sn(t,e,n,r={}){const o=V(t,e),s=V(t,n),i=t.nodeCount;if(!(Number.isInteger(o)&&o>=0&&o<i&&Number.isInteger(s)&&s>=0&&s<i))throw $(new RangeError(`source ${String(o)} and sink ${String(s)} must be node indices below ${i}`),"E_BAD_NODE");if(o===s)throw $(new RangeError(`source and sink are the same node (${o})`),"E_BAD_OPTION");const a=rn(t,r.weights),l=Ls(t,a),c=new Float64Array(l.colIdx.length),u=new Uint32Array(i),d=r.algorithm==="ford-fulkerson"?Ks:Hs;let f=0;for(;d(l,o,s,u);)f+=js(l,o,s,u,c);const h=Ys(l,o);return{...Dt(h,t.nodeCount),maxFlow:f,flow:Gs(t,l,a,c),sourceSide:h,cutEdges:on(t,h,t.directed,a,!0)}}function js(t,e,n,r,o){const{colIdx:s,twin:i}=t;let a=1/0;for(let l=n;l!==e;l=s[i[r[l]]])a=Math.min(a,t.capacity[r[l]]);for(let l=n;l!==e;){const c=r[l],u=i[c];t.capacity[c]-=a,t.capacity[u]+=a,t.inOrder[u]===0&&(t.inOrder[u]=1,t.order[t.rowPtr[l]+t.placed[l]++]=u),t.real[c]===1?o[c]+=a:o[u]-=a,l=s[u]}return a}function Gs(t,e,n,r){const o=t.edgeCount,s=new Float64Array(o),{rowPtr:i,colIdx:a,arcToEdge:l,groupOf:c}=e;for(let u=0;u<t.nodeCount;u++)for(let d=i[u];d<i[u+1];d++){const f=a[d];if(c[d]!==d||f<=u)continue;const h=r[d]-r[e.twin[d]];if(h===0)continue;const g=h>0?d:e.twin[d],w=i[a[e.twin[g]]+1];let m=Math.abs(h),A=-1,p=1;for(let y=g;y<w&&c[y]===g;y++){const v=l[y];if(!(v>=o&&t.directed)){if(A>=0){const E=Math.min(m,Math.max(n[A],0));s[A]+=p*E,m-=E}A=v<o?v:v-o,p=v<o?1:-1}}A>=0&&(s[A]+=p*m)}return s}function jr(t,e,n,r={}){const o=V(t,e),s=V(t,n),i=sn(t,o,s,{...r,algorithm:r.algorithm??"ford-fulkerson"});return{...Dt(i.sourceSide,t.nodeCount),cutValue:i.maxFlow,side:i.sourceSide,cutEdges:i.cutEdges}}function Hs(t,e,n,r){const{rowPtr:o,colIdx:s,order:i,placed:a,capacity:l}=t;r.fill(M);const c=new Uint32Array(r.length);let u=0,d=0;for(c[d++]=e;u<d;){const f=c[u++],h=o[f]+a[f];for(let g=o[f];g<h;g++){const w=i[g],m=s[w];if(l[w]>0&&m!==e&&r[m]===M){if(r[m]=w,m===n)return!0;c[d++]=m}}}return!1}function Ks(t,e,n,r){const{rowPtr:o,colIdx:s,order:i,placed:a,capacity:l}=t,c=r.length;r.fill(M);const u=new Uint8Array(c),d=new Uint32Array(c),f=new Uint32Array(c);let h=0;for(d[h++]=e,u[e]=1,f[e]=o[e];h>0;){const g=d[h-1],w=f[g]++;if(w>=o[g]+a[g]){h--;continue}const m=i[w],A=s[m];if(l[m]>0&&u[A]===0){if(r[A]=m,A===n)return!0;u[A]=1,f[A]=o[A],d[h++]=A}}return!1}function Ys(t,e){const{rowPtr:n,colIdx:r,order:o,placed:s,capacity:i}=t,a=n.length-1,l=ct(a),c=new Uint32Array(a);let u=0,d=0;for(c[d++]=e,bt(l,e,!0);u<d;){const f=c[u++],h=n[f]+s[f];for(let g=n[f];g<h;g++){const w=r[o[g]];i[o[g]]>0&&!L(l,w)&&(bt(l,w,!0),c[d++]=w)}}return l}function Vt(t){let e=t>>>0;return()=>{e=e+1831565813|0;let n=Math.imul(e^e>>>15,1|e);return n=n+Math.imul(n^n>>>7,61|n)^n,((n^n>>>14)>>>0)/4294967296}}const Xs=2147483647;function Zs(t,e){for(let n=0;n<t.nodeCount;n++){const r=t.rowPtr[n+1];for(let o=t.rowPtr[n];o<r;o++)if(t.colIdx[o]!==n&&(e===null||e[o]>0))return!0}return!1}function Gr(t,e,n){for(let r=0;r<e.length;r++){if(n!==null&&n[r]===1)continue;const o=t.collect(r,e);if(o!==0&&t.voteOf(e[r])!==o)return!1}return!0}function qe(t,e){if(e===null)return t.weights;const n=new Float64Array(t.arcCount);for(let r=0;r<n.length;r++)n[r]=e[t.arcToEdge[r]];return n}function ce(t){const e=t.edges.byRole("weight");return e!==null&&e.dtype==="f64"?e.data:null}function an(t,e){const n=e?qe(t,ce(t)):null;if(n!==null)for(let o=0;o<n.length;o++){const s=n[o];if(!(s>=0)||s===1/0)throw $(new RangeError(`arc ${o} has weight ${s}; label propagation needs finite, non-negative weights`),"E_BAD_WEIGHT")}const r=t.directed?t.reverse():null;return{sides:r===null?1:2,rowPtrs:[t.rowPtr,r===null?t.rowPtr:r.rowPtr],colIdxs:[t.colIdx,r===null?t.colIdx:r.colIdx],arcWeights:[n,n===null||r===null?n:qe(r,ce(t))],weights:n}}function ln(t,e,n){if(!Number.isInteger(e)||e<0)throw $(new RangeError(`maxIterations must be a non-negative integer, got ${e}`),"E_BAD_OPTION");if(n!==null&&!Number.isInteger(n))throw $(new RangeError(`randomSeed must be a finite integer, got ${n}`),"E_BAD_OPTION");if(e*t>Number.MAX_SAFE_INTEGER)throw $(new RangeError(`maxIterations * nodeCount (${e} * ${t}) exceeds Number.MAX_SAFE_INTEGER`),"E_BAD_OPTION")}class cn{constructor(e,n,r){this.rows=n,this.epoch=-1,this.count=0,this.acc=new Float64Array(e),this.stamp=new Int32Array(e).fill(-1),this.touched=new Uint32Array(e),this.seen=r?null:new Int32Array(e).fill(-1)}collect(e,n){++this.epoch===Xs&&(this.stamp.fill(-1),this.seen?.fill(-1),this.epoch=0);const{acc:r,stamp:o,touched:s,seen:i,rows:a}=this,l=this.epoch;let c=0;for(let d=0;d<a.sides;d++){const f=a.rowPtrs[d],h=a.colIdxs[d],g=a.arcWeights[d],w=f[e+1];for(let m=f[e];m<w;m++){const A=h[m];if(A===e)continue;if(i!==null){if(i[A]===l)continue;i[A]=l}const p=n[A];if(p===M)continue;const y=g===null?1:g[m];o[p]!==l?(o[p]=l,r[p]=y,s[c++]=p):r[p]+=y}}this.count=c;let u=0;for(let d=0;d<c;d++)r[s[d]]>u&&(u=r[s[d]]);return u}voteOf(e){return this.stamp[e]===this.epoch?this.acc[e]:0}}function Hr(t,e,n,r,o,s,i){const a=t.nodeCount,l=Vt(i),c=new cn(a,e,n),u=a+1,d=new Uint32Array(u),f=new Uint8Array(a);let h=0;for(let v=0;v<a;v++)(o===null||o[v]===0)&&(d[h++]=v,f[v]=1);for(let v=h-1;v>=1;v--){const E=Math.floor(l()*(v+1)),b=d[v];d[v]=d[E],d[E]=b}let g=0,w=h;const m=s*a;let A=0;const{touched:p,acc:y}=c;for(;h>0&&A<m;){const v=d[g];g=g+1===u?0:g+1,h--,f[v]=0,A++;const E=c.collect(v,r);if(E===0)continue;let b=0,C=0;for(let x=0;x<c.count;x++){const _=p[x];y[_]===E&&(C++,(C===1||l()*C<1)&&(b=_))}if(b!==r[v]){r[v]=b;for(let x=0;x<e.sides;x++){const _=e.rowPtrs[x],S=e.colIdxs[x],P=_[v+1];for(let I=_[v];I<P;I++){const F=S[I];f[F]===0&&r[F]!==b&&(o===null||o[F]===0)&&(f[F]=1,d[w]=F,w=w+1===u?0:w+1,h++)}}}}return{visits:A,size:h}}function Kr(t,e={}){const n=e.maxIterations??100,r=e.randomSeed??42,o=e.weighted??!0,s=t.nodeCount;ln(s,n,r);const i=an(t,o),a=new Uint32Array(s);for(let f=0;f<s;f++)a[f]=f;if(s===0||n===0){const{labels:f,count:h}=tt(a);return{...K(f,h),iterations:0,converged:!Zs(t,i.weights)}}const{visits:l,size:c}=Hr(t,i,o,a,null,n,r),{labels:u,count:d}=tt(a);return{...K(u,d),iterations:Math.ceil(l/s),converged:c===0}}function Js(t,e,n={}){const r=n.maxIterations??100,o=n.randomSeed??42,s=n.weighted??!0,i=t.nodeCount;if(e.length!==i)throw $(new RangeError(`seeds has ${e.length} entries; the snapshot has ${i} nodes`),"E_BAD_OPTION");ln(i,r,o);const a=an(t,s),l=new Uint32Array(i),c=new Uint8Array(i),u=new Map;for(let m=0;m<i;m++){const A=e[m];if(A===M){l[m]=M;continue}let p=u.get(A);p===void 0&&(p=m,u.set(A,m)),l[m]=p,c[m]=1}const{visits:d,size:f}=Hr(t,a,s,l,c,r,o),h=f===0||r===0&&Gr(new cn(i,a,s),l,c);Qs(a,l);const{labels:g,count:w}=tt(l);return{...K(g,w),iterations:i===0?0:Math.ceil(d/i),converged:h}}function Qs(t,e){const n=[];for(let r=0;r<e.length;r++)if(e[r]===M)for(e[r]=r,n.push(r);n.length>0;)ti(t,e,n.pop(),r,n)}function ti(t,e,n,r,o){for(let s=0;s<t.sides;s++){const i=t.rowPtrs[s],a=t.colIdxs[s],l=t.arcWeights[s],c=i[n+1];for(let u=i[n];u<c;u++){const d=a[u];e[d]===M&&(l===null||l[u]>0)&&(e[d]=r,o.push(d))}}}function Yr(t,e={}){const n=e.maxIterations??100,r=e.weighted??!0,o=t.nodeCount;ln(o,n,null);const s=an(t,r),i=new cn(o,s,r),{touched:a,acc:l}=i;let c=new Uint32Array(o),u=new Uint32Array(o),d=new Uint32Array(o).fill(M);const f=new Uint32Array(o);for(let y=0;y<o;y++)c[y]=y,f[y]=ei(y);let h=0,g=0,w=!1;for(;g<2&&h<n&&!w;){const y=h%2===0;h++;let v=!1;for(let E=0;E<o;E++){const b=c[E];u[E]=b;const C=i.collect(E,c);if(C===0||i.voteOf(b)===C)continue;let x=M;for(let _=0;_<i.count;_++){const S=a[_];l[S]===C&&(x===M||f[S]<f[x])&&(x=S)}(y?f[x]>f[b]:f[x]<f[b])&&(u[E]=x,v=!0)}w=v&&ni(u,d),[d,c,u]=[c,u,d],g=v?0:g+1}const m=g>=2||Gr(i,c,null),{labels:A,count:p}=tt(c);return{...K(A,p),iterations:h,converged:m}}function ei(t){return t^=t>>>16,t=Math.imul(t,2246822507),t^=t>>>13,t=Math.imul(t,3266489909),t^=t>>>16,t>>>0}function ni(t,e){for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function un(t,e,n={}){const r=n.resolution??1,o=n.weights??t.weights;if(e.length!==t.nodeCount)throw $(new RangeError(`labels has ${e.length} entries; the snapshot has ${t.nodeCount} nodes`),"E_BAD_OPTION");if(o!==null&&o.length!==t.arcCount)throw $(new RangeError(`weights has ${o.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const s=new Map,i=new Map,a=(u,d,f)=>{d!==M&&u.set(d,(u.get(d)??0)+f)};let l=0;for(let u=0;u<t.nodeCount;u++){const d=t.rowPtr[u+1];for(let f=t.rowPtr[u];f<d;f++){const h=t.colIdx[f],g=o===null?1:o[f],w=t.directed||h===u?2*g:g;l+=w,t.directed?(a(s,e[u],g),a(s,e[h],g)):a(s,e[u],w),e[u]===e[h]&&a(i,e[u],w)}}if(l===0)return 0;let c=0;for(const[u,d]of s){const f=d/l;c+=(i.get(u)??0)/l-r*f*f}return c}const ri=1e-10;function oi(t,e){const n=new qt(t.nodeCount),r=t.edgeList();for(let o=0;o<t.edgeCount;o++)L(e,o)&&n.union(r.src[o],r.dst[o]);return n.toLabels()}function Xr(t,e={}){if(t.directed)throw $(new Error("Girvan-Newman requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.maxCommunities??0,r=e.minCommunitySize??1,o=e.maxIterations??100,{nodeCount:s,edgeCount:i}=t,a=qe(t,ce(t)),l=ct(i,!0),c=[],u=[],d=[],f=()=>{const{labels:g,count:w}=oi(t,l);c.push(g),d.push(w),u.push(un(t,g,a===null?{}:{weights:a}));const m=new Uint32Array(w);for(let A=0;A<s;A++)m[g[A]]++;return m.filter(A=>A>=r).length};f();for(let g=0;g<o&&fs(l,i)>0;g++){const w=Ze(t,{alive:l}).scores;let m=-1/0;for(let p=0;p<i;p++)L(l,p)&&w[p]>m&&(m=w[p]);for(let p=0;p<i;p++)L(l,p)&&Math.abs(w[p]-m)<ri&&bt(l,p,!1);const A=f();if(n>0&&A>=n||A===s)break}let h=0;for(let g=1;g<u.length;g++)(u[g]>u[h]||Number.isNaN(u[h])&&!Number.isNaN(u[g]))&&(h=g);return{...K(c[h],d[h]),bestLevel:h,levels:c,modularity:Float64Array.from(u)}}function si(t,e={}){const{maxDepth:n=10,minClusterSize:r=2,tolerance:o=1e-6,maxIterations:s=100,seed:i=42,weighted:a=!0}=e,l=t.nodeCount;if(l===0)throw $(new Error("Cannot cluster empty graph"),"E_EMPTY_GRAPH");const c=Vt(i),u=a?t.weights:null;if(u!==null)for(let D=0;D<u.length;D++){const k=u[D];if(!(k>=0)||k===1/0)throw $(new RangeError(`arc ${String(D)} has weight ${String(k)}; grsbm needs finite, non-negative weights`),"E_BAD_WEIGHT")}const d=a?t.weightedDegree():t.degree(),f=a?t.totalWeight():t.edgeCount,h=t.directed?1:2,g=!t.directed||!a,{rowPtr:w,colIdx:m}=t,A=new Int32Array(l).fill(-1),p=D=>{let k=0,N=0;for(let O=0;O<D.length;O++){const T=D[O];N+=d[T];for(let B=w[T];B<w[T+1];B++){const z=m[B];if(A[z]===A[T]){const q=u===null?1:u[B];k+=z===T?h*q:q}}}return g&&(k/=2),(k-N*N/(4*f))/f},y=D=>{if(f===0)return 0;let k=0;D.forEach((N,O)=>{for(let T=0;T<N.length;T++)A[N[T]]=O});for(const N of D)N.length>0&&(k+=p(N));for(const N of D)for(let O=0;O<N.length;O++)A[N[O]]=-1;return k},v=new Uint32Array(l);for(let D=0;D<l;D++)v[D]=D;const E=y([v]),b=[{serial:0,members:v,depth:0,modularity:E,spectralScore:0,left:M,right:M,split:null}],C=[E];let x=E;const _=new Int32Array(l).fill(-1);let S=1;for(let D=0;D<b.length;D++){const k=b[D],{members:N}=k,O=N.length;if(k.depth>=n||O<r*2||O<4)continue;const T=ii(N,_,t,u,c,o,s),B=Array.from({length:O},(j,it)=>it).sort((j,it)=>T[j]-T[it]),z=Uint32Array.from(B,j=>N[j]);let q=Math.floor(O/2),W=-1/0;for(let j=Math.floor(O*.2);j<=Math.floor(O*.8);j++){const it=y([z.subarray(0,j),z.subarray(j)]);it>W&&(W=it,q=j)}const Y=z.slice(0,q),et=z.slice(q),nt=Array.from(T).sort((j,it)=>Math.abs(it)-Math.abs(j)),ut=Math.abs(nt[Math.floor(O*.1)]),rt=[];for(let j=0;j<O&&rt.length<5;j++)Math.abs(T[j])>=ut&&rt.push(N[j]);const ot=Math.abs(nt[0]-nt[O-1]),st=S++,mt=S++,dt=W-y([N]);if(dt<=0)continue;x+=dt;const xt=(j,it)=>({serial:j,members:it,depth:k.depth+1,modularity:x,spectralScore:ot,left:M,right:M,split:null});k.left=b.length,k.right=b.length+1,k.split={improvement:dt,bisectionModularity:x,keyNodes:Uint32Array.from(rt),spectralValues:T},b.push(xt(st,Y),xt(mt,et)),C.push(x)}const P=new Uint32Array(l);let I=0;const F=[0];for(;F.length>0;){const D=b[F.pop()??0];if(D.left===M){for(const k of D.members)P[k]=I;I++}else F.push(D.right,D.left)}const{labels:U,count:R}=tt(P);return{...K(U,R),clusters:b,modularityScores:Float64Array.from(C)}}function ii(t,e,n,r,o,s,i){const a=t.length;for(let p=0;p<a;p++)e[t[p]]=p;const l=new Uint32Array(a+1),c=[],u=[],d=new Map;let f=0;for(let p=0;p<a;p++){const y=t[p];d.clear();let v=0;for(let E=n.rowPtr[y];E<n.rowPtr[y+1];E++){const b=e[n.colIdx[E]];if(b>=0&&b!==p){const C=r===null?1:r[E];d.set(b,(d.get(b)??0)-C),v+=C}}d.set(p,v),f=Math.max(f,v);for(const E of[...d.keys()].sort((b,C)=>b-C))c.push(E),u.push(d.get(E)??0);l[p+1]=c.length}for(let p=0;p<a;p++)e[t[p]]=-1;let h=new Float64Array(a);for(let p=0;p<a;p++)h[p]=o()-.5;const g=p=>{let y=0;for(const E of p)y+=E;const v=y/a;for(let E=0;E<a;E++)p[E]-=v},w=p=>{let y=0;for(const v of p)y+=v*v;return Math.sqrt(y)};g(h);const m=w(h);if(m>0)for(let p=0;p<a;p++)h[p]/=m;const A=2*f;for(let p=0;p<i;p++){const y=new Float64Array(a);for(let b=0;b<a;b++){let C=0;for(let x=l[b];x<l[b+1];x++)C+=u[x]*h[c[x]];y[b]=A*h[b]-C}g(y);const v=w(y);if(v<s)break;let E=0;for(let b=0;b<a;b++)y[b]/=v,E+=Math.abs(y[b]-h[b]);if(h=y,E<s)break}return h}function ai(t,e){const n=t.nodeCount;e.fill(M);const r=new Uint32Array(n);for(let o=0;o<n;o++){const s=o*n;e[s+o]=0,r[0]=o;let i=0,a=1;for(;i<a;){const l=r[i++],c=e[s+l]+1,u=t.rowPtr[l+1];for(let d=t.rowPtr[l];d<u;d++){const f=t.colIdx[d];e[s+f]===M&&(e[s+f]=c,r[a++]=f)}}}}function li(t){const e=Math.max(0,2*t-1),n=Array.from({length:e},(s,i)=>i<t?`leaf-${i}`:`cluster-${i}`),r=Array.from({length:e},(s,i)=>i).sort((s,i)=>+(n[s]>n[i])-+(n[s]<n[i])),o=new Uint32Array(e);return r.forEach((s,i)=>o[s]=i),o}function ci(t,e={}){const n=e.linkage??"single";if(!["single","complete","average","ward"].includes(n))throw $(new RangeError(`unknown linkage "${n}"`),"E_BAD_OPTION");const r=t.nodeCount,{hops:o,min:s,max:i,sum:a,count:l}=pr("hierarchicalClustering",r,e.maxNodes,28*r*r,()=>({hops:new Uint32Array(r*r),min:new Uint32Array(r*r),max:new Uint32Array(r*r),sum:new Float64Array(r*r),count:new Float64Array(r*r)}));ai(t,o);for(let I=0;I<r*r;I++){const F=o[I];F!==M&&(s[I]=F,i[I]=F,a[I]=F,l[I]=1)}const c=Math.max(0,2*r-1),u=li(r),d=new Uint32Array(c),f=new Float64Array(c),h=new Uint32Array(c);for(let I=0;I<r;I++)d[I]=I,f[I]=1;const g=[],w=[],m=[],A=(I,F)=>{const U=d[I]*r+d[F],R=l[U];if(R===0)return 1/0;switch(n){case"single":return s[U];case"complete":return i[U];case"average":return a[U]/R;default:return a[U]/R*(f[I]*f[F]/(f[I]+f[F]))}},p=(I,F)=>{const[U,R]=I<F?[I,F]:[F,I];return R<r?A(U,R):A(R,U)};let y=Array.from({length:r},(I,F)=>F);for(;y.length>1;){let I=1/0,F=-1,U=-1;for(const N of y)for(const O of y){if(u[N]>=u[O])continue;const T=p(N,O);T<I&&(I=T,F=N,U=O)}if(F<0)break;const R=r+g.length;g.push(F),w.push(U),m.push(I),h[R]=Math.max(h[F],h[U])+1,f[R]=f[F]+f[U];const D=d[F],k=d[U];for(const N of y){if(N===F||N===U)continue;const O=d[N];for(const[T,B,z]of[[D*r+O,D*r+O,k*r+O],[O*r+D,O*r+D,O*r+k]]){const q=l[B],W=l[z];if(q===0||W===0){const Y=q===0?z:B;s[T]=s[Y],i[T]=i[Y]}else s[T]=Math.min(s[B],s[z]),i[T]=Math.max(i[B],i[z]);a[T]=a[B]+a[z],l[T]=q+W}}d[R]=D,y=y.filter(N=>N!==F&&N!==U),y.push(R)}const v=g.length,E=Uint32Array.from(g),b=Uint32Array.from(w),C=I=>{if(!(I>=0&&I<r+v))throw $(new RangeError(`cluster ${I} is not in [0, ${r+v})`),"E_BAD_OPTION");const F=[],U=[I];for(;U.length>0;){const R=U.pop();R<r?F.push(R):U.push(b[R-r],E[R-r])}return Uint32Array.from(F)},x=Uint32Array.from(y),_=Float64Array.from(m),S=new Uint32Array(v);for(let I=0;I<v;I++)S[I]=E[I]<r?E[I]:S[E[I]-r];const P=I=>I<r?I:S[I-r];return{nodeCount:r,left:E,right:b,distance:_,height:h.slice(0,r+v),roots:x,members:C,cut(I){const F=[],U=R=>{R<r||h[R]<=I?F.push(C(R)):(U(E[R-r]),U(b[R-r]))};return x.forEach(U),F},cutAt(I){let F=0;if("clusters"in I){const k=I.clusters;if(!Number.isInteger(k)||k<x.length||k>r)throw $(new RangeError(`clusters must be an integer in [${x.length}, ${r}], got ${k}`),"E_BAD_OPTION");F=r-k}else{if(Number.isNaN(I.distance))throw $(new RangeError("distance must not be NaN"),"E_BAD_OPTION");for(;F<v&&_[F]<=I.distance;)F++}const U=new qt(r);for(let k=0;k<F;k++)U.union(P(E[k]),P(b[k]));const{labels:R,count:D}=U.toLabels();return K(R,D)}}}function bn(t){let e=0;for(let r=0;r<t.length;r++)e+=t[r]*t[r];const n=Math.sqrt(e);if(n>0)for(let r=0;r<t.length;r++)t[r]/=n}function Cn(t){let e=0;for(let n=0;n<t.length;n++)t[n]>e&&(e=t[n]);if(e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Zr(t,e={}){const n=t.nodeCount,r=e.maxIterations??100,o=e.tolerance??1e-6,s=t.reverse(),i=e.weighted===!0?t.weights:null,a=e.weighted===!0?s.weights:null;let l=new Float64Array(n).fill(n===0?0:1/Math.sqrt(n)),c=new Float64Array(n).fill(n===0?0:1/Math.sqrt(n)),u=new Float64Array(n),d=new Float64Array(n),f=0,h=!1;for(;f<r&&!h;f++){for(let w=0;w<n;w++){let m=0;const A=s.rowPtr[w+1];for(let v=s.rowPtr[w];v<A;v++)m+=l[s.colIdx[v]]*(a===null?1:a[v]);d[w]=m;let p=0;const y=t.rowPtr[w+1];for(let v=t.rowPtr[w];v<y;v++)p+=c[t.colIdx[v]]*(i===null?1:i[v]);u[w]=p}bn(d),bn(u);let g=0;for(let w=0;w<n;w++){const m=Math.abs(u[w]-l[w]);m>g&&(g=m);const A=Math.abs(d[w]-c[w]);A>g&&(g=A)}[l,u]=[u,l],[c,d]=[d,c],h=g<o}return e.normalized===!1&&(Cn(l),Cn(c)),{hubs:l,authorities:c,iterations:f,converged:h}}function In(t,e){let n=0,r=!1,o=M;for(let s=t.rowPtr[e];s<t.rowPtr[e+1];s++){const i=t.colIdx[s];i!==o&&(o=i,i===e?r=!0:n++)}return{degree:n,loop:r}}function xn(t){const e=new Float64Array(t.nodeCount),n=t.directed?t.reverse():null;for(let r=0;r<t.nodeCount;r++){const o=In(t,r),s=n===null?0:In(n,r).degree;e[r]=(o.degree*2**26+s)*2+(o.loop?1:0)}return e}function ui(t,e){const n=new Uint32Array(e),r=new Uint32Array(e).fill(M),o=new Uint8Array(e),s=new Uint8Array(e);let i=0;for(let a=0;a<e;a++){if(s[a]===1)continue;s[a]=1;let l=i;for(n[i++]=a;l<i;)i=di(t,n[l++],{order:n,parent:r,parentView:o},s,i)}return{order:n,parent:r,parentView:o}}function di(t,e,n,r,o){const{order:s,parent:i,parentView:a}=n;for(let l=0;l<t.length;l++){const{rowPtr:c,colIdx:u}=t[l];for(let d=c[e];d<c[e+1];d++){const f=u[d];r[f]===0&&(r[f]=1,i[f]=e,a[f]=l,s[o++]=f)}}return o}function Jr(t,e,n,r){const o=t.nodeCount;if(t.directed!==e.directed||o!==e.nodeCount)return;const s=xn(t),i=xn(e),a=s.slice().sort(),l=i.slice().sort();if(a.some((P,I)=>P!==l[I]))return;const c=t.directed?[t,t.reverse()]:[t],u=e.directed?[e,e.reverse()]:[e],{order:d,parent:f,parentView:h}=ui(c,o),g=new Uint32Array(o).fill(M),w=new Uint32Array(o).fill(M),m=u.map(()=>new Uint32Array(o)),A=u.map(()=>new Uint32Array(o));let p=0;const{nodeMatch:y,edgeMatch:v}=n,E=(P,I)=>{if(s[P]!==i[I]||y!==void 0&&!y(P,I,t,e))return!1;if(v!==void 0&&s[P]%2===1){const F=t.arcToEdge[t.findArc(P,P)],U=e.arcToEdge[e.findArc(I,I)];if(!v(F,U,t,e))return!1}p++;for(let F=0;F<c.length;F++){const U=c[F],R=u[F];let D=0,k=M;for(let O=R.rowPtr[I];O<R.rowPtr[I+1];O++){const T=R.colIdx[O];T===k||T===I||(k=T,m[F][T]=p,A[F][T]=O,w[T]!==M&&D++)}let N=0;k=M;for(let O=U.rowPtr[P];O<U.rowPtr[P+1];O++){const T=U.colIdx[O];if(T===k||T===P)continue;k=T;const B=g[T];if(B!==M&&(m[F][B]!==p||(N++,v!==void 0&&!v(U.arcToEdge[O],R.arcToEdge[A[F][B]],t,e))))return!1}if(N!==D)return!1}return!0},b=new Uint32Array(o+1);let C=0,x=!1;const _=P=>{const I=f[d[P]];if(I===M)return{view:null,from:0,to:o};const F=u[h[d[P]]],U=g[I];return{view:F,from:F.rowPtr[U],to:F.rowPtr[U+1]}};if(o===0){r(g);return}let S=_(0);for(b[0]=S.from;!x;){const P=d[C];let I=!1;for(;b[C]<S.to;){const U=b[C]++,R=S.view===null?U:S.view.colIdx[U];if(!(S.view!==null&&U>S.from&&S.view.colIdx[U-1]===R)&&!(w[R]!==M||!E(P,R))){if(g[P]=R,w[R]=P,C===o-1){if(x=r(g),g[P]=M,w[R]=M,x)break;continue}C++,S=_(C),b[C]=S.from,I=!0;break}}if(x||I)continue;if(C===0)return;C--;const F=d[C];w[g[F]]=M,g[F]=M,S=_(C)}}function fi(t,e,n={}){let r=null;return Jr(t,e,n,o=>(r=o.slice(),!0)),{isomorphic:r!==null,mapping:r}}function hi(t,e,n={}){const r=[];return Jr(t,e,n,o=>(r.push(o.slice()),!1)),r}function Qr(t){if(t.directed)throw $(new Error("k-core requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const e=t.nodeCount,{rowPtr:n,colIdx:r}=t,o=new Uint32Array(e);let s=0;for(let f=0;f<e;f++){let h=0,g=-1;const w=n[f+1];for(let m=n[f];m<w;m++){const A=r[m];A!==f&&A!==g&&h++,g=A}o[f]=h,h>s&&(s=h)}const i=new Uint32Array(s+1);for(let f=0;f<e;f++)i[o[f]]++;let a=0;for(let f=0;f<=s;f++){const h=i[f];i[f]=a,a+=h}const l=new Uint32Array(e),c=new Uint32Array(e);for(let f=0;f<e;f++)c[f]=i[o[f]],l[c[f]]=f,i[o[f]]++;for(let f=s;f>0;f--)i[f]=i[f-1];i[0]=0;let u=0;for(let f=0;f<e;f++){const h=l[f],g=o[h];g>u&&(u=g);const w=n[h+1];let m=-1;for(let A=n[h];A<w;A++){const p=r[A],y=p===h||p===m;if(m=p,y)continue;const v=o[p];if(v>g){const E=c[p],b=i[v],C=l[b];p!==C&&(l[E]=C,l[b]=p,c[p]=b,c[C]=E),i[v]++,o[p]=v-1}}}let d=null;return{coreness:o,maxCore:u,cores(){if(d===null){const f=new Uint32Array(u+1);for(let w=0;w<e;w++)f[o[w]]++;const h=[];for(let w=0;w<=u;w++)h.push(new Uint32Array(f[w]));const g=new Uint32Array(u+1);for(let w=0;w<e;w++){const m=o[w];h[m][g[m]++]=w}d=h}return d}}}function to(t,e={}){const n=t.nodeCount,r=e.alpha??.1,o=e.beta??1,s=e.maxIterations??100,i=e.tolerance??1e-6,a=t.reverse(),l=e.weighted===!0?a.weights:null;let c=new Float64Array(n).fill(o),u=new Float64Array(n),d=0,f=!1;for(;d<s&&!f;d++){let h=0;for(let g=0;g<n;g++){let w=0;const m=a.rowPtr[g+1];for(let p=a.rowPtr[g];p<m;p++)w+=c[a.colIdx[p]]*(l===null?1:l[p]);u[g]=r*w+o;const A=Math.abs(u[g]-c[g]);A>h&&(h=A)}[c,u]=[u,c],f=h<i}if(e.normalized!==!1){let h=1/0,g=-1/0;for(let m=0;m<n;m++)c[m]<h&&(h=c[m]),c[m]>g&&(g=c[m]);const w=g-h;if(w>0)for(let m=0;m<n;m++)c[m]=(c[m]-h)/w}return{scores:c,iterations:d,converged:f}}const gi=.01,wi=3;function eo(t,e){const{nodeCount:n,rowPtr:r,colIdx:o,arcToEdge:s}=t,i=new Float64Array(t.arcCount),a=new Float64Array(n);for(let l=0;l<n;l++)for(let c=r[l],u=r[l+1];c<u;c++){const d=e[s[c]];i[c]=d,a[l]+=o[c]===l?2*d:d}return{s:t,edgeW:e,arcW:i,deg:a}}function no(t,e){const n=new Uint32Array(t);for(let r=0;r<t;r++)n[r]=r;for(let r=t-1;r>=1;r--){const o=Math.floor(e()*(r+1)),s=n[r];n[r]=n[o],n[o]=s}return n}function pi(t,e,n,r,o){const{s,arcW:i,deg:a}=t,{nodeCount:l,rowPtr:c,colIdx:u}=s,d=new Float64Array(l),f=new Uint32Array(l);for(let C=0;C<l;C++)d[e[C]]+=a[C],f[e[C]]++;const h=[];for(let C=l-1;C>=0;C--)f[C]===0&&h.push(C);const g=new Float64Array(l),w=new Int32Array(l).fill(-1),m=new Uint32Array(l),A=l+1,p=new Uint32Array(A);p.set(no(l,n));const y=new Uint8Array(l).fill(1);let v=0,E=l,b=0;for(;v!==E;){const C=p[v];v=v+1===A?0:v+1,y[C]=0,b++;const x=e[C],_=a[C];d[x]-=_,f[x]--;let S=0;for(let F=c[C],U=c[C+1];F<U;F++){const R=u[F];if(R===C)continue;const D=e[R];w[D]!==b&&(w[D]=b,g[D]=0,m[S++]=D),g[D]+=i[F]}let P=x,I=(w[x]===b?g[x]:0)-r*_*d[x]/o;for(let F=0;F<S;F++){const U=m[F],R=g[U]-r*_*d[U]/o;U!==x&&R>I&&(I=R,P=U)}if(f[x]>0&&I<0&&(P=h[h.length-1]),P!==x&&f[P]===0&&h.pop(),d[P]+=_,f[P]++,P!==x){e[C]=P,f[x]===0&&h.push(x);for(let F=c[C],U=c[C+1];F<U;F++){const R=u[F];y[R]===0&&e[R]!==P&&(y[R]=1,p[E]=R,E=E+1===A?0:E+1)}}}}function mi(t,e,n,r,o){const{s,arcW:i,deg:a}=t,{nodeCount:l,rowPtr:c,colIdx:u}=s,d=new Float64Array(l);for(let v=0;v<l;v++)d[e[v]]+=a[v];const f=new Uint32Array(l),h=new Float64Array(l),g=new Uint32Array(l),w=new Float64Array(l);for(let v=0;v<l;v++){f[v]=v,h[v]=a[v],g[v]=1;for(let E=c[v],b=c[v+1];E<b;E++){const C=u[E];C!==v&&e[C]===e[v]&&(w[v]+=i[E])}}const m=new Float64Array(l),A=new Int32Array(l).fill(-1),p=new Uint32Array(l),y=no(l,n);for(let v=0;v<l;v++){const E=y[v],b=e[E],C=a[E];if(g[E]!==1||w[E]<r*C*(d[b]-C)/o)continue;let x=0;for(let D=c[E],k=c[E+1];D<k;D++){const N=u[D];if(N===E||e[N]!==b)continue;const O=f[N];A[O]!==E&&(A[O]=E,m[O]=0,p[x++]=O),m[O]+=i[D]}const _=D=>m[D]-r*C*h[D]/o;let S=-1/0,P=0;for(let D=0;D<x;D++){const k=p[D],N=_(k);N>=0&&w[k]>=r*h[k]*(d[b]-h[k])/o&&(p[P++]=k,S=Math.max(S,N))}if(P===0)continue;const I=D=>hs((_(D)-S)*2/o/gi);let F=0;for(let D=0;D<P;D++)F+=I(p[D]);let U=n()*F,R=p[P-1];for(let D=0;D<P-1;D++)if(U-=I(p[D]),U<0){R=p[D];break}f[E]=R,g[E]=0,g[R]++,h[R]+=C,w[R]+=w[E]-2*m[R]}return f}function yi(t,e,n,r,o){const s=t.s.nodeCount,i=new Uint32Array(s);for(let d=0;d<s;d++)i[d]=d;let a=t,l=e.slice(),c=0;for(;;){pi(a,l,n,r,o),c++;const d=tt(l);if(d.count===a.s.nodeCount)break;let f=mi(a,d.labels,n,r,o);tt(f).count===a.s.nodeCount&&(f=d.labels);const h=a.s.contract(f),{nodeRemap:g,edgeRemap:w}=h,m=h.snapshot,A=new Float64Array(m.edgeCount);for(let y=0;y<a.edgeW.length;y++)A[w===null?y:w[y]]+=a.edgeW[y];const p=new Uint32Array(m.nodeCount);for(let y=0;y<a.s.nodeCount;y++)p[g===null?y:g[y]]=d.labels[y];if(g!==null)for(let y=0;y<s;y++)i[y]=g[i[y]];l=tt(p).labels,a=eo(m,A)}const u=new Uint32Array(s);for(let d=0;d<s;d++)u[d]=l[i[d]];return{comm:u,levels:c}}function Ai(t){const e=ce(t)??t.edgeList().weights,n=e===null?new Float64Array(t.edgeCount).fill(1):Float64Array.from(e);for(let r=0;r<n.length;r++){const o=n[r];if(!(o>=0)||o===1/0)throw $(new RangeError(`edge ${r} has weight ${o}; Leiden needs finite, non-negative weights`),"E_BAD_WEIGHT")}return n}function ro(t,e={}){if(t.directed)throw $(new Error("Leiden requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.resolution??1,r=e.randomSeed??42,o=e.maxIterations??100,s=e.threshold??1e-7;if(!Number.isInteger(r))throw $(new RangeError(`randomSeed must be a finite integer, got ${r}`),"E_BAD_OPTION");const i=t.nodeCount,a=eo(t,Ai(t));let l=0;for(let w=0;w<i;w++)l+=a.deg[w];let c=new Uint32Array(i);for(let w=0;w<i;w++)c[w]=w;if(l===0)return{...K(c,i),modularity:0,iterations:0};const u=w=>un(t,w,{resolution:n,weights:a.arcW}),d=Vt(r);let f=u(c),h=0,g=0;for(let w=0;w<o&&g<wi;w++){const{comm:m,levels:A}=yi(a,c,d,n,l);h+=A;const p=tt(m).labels,y=u(p);y>f?(g=y-f<=s?g+1:0,c=p,f=y):g++}return{...K(c,tt(c).count),modularity:f,iterations:h}}const Pn=2**36;function vi(t){return t>1?Math.round(Pn/fr(t))/Pn:t===1?1:0}function Nn(t,e){const n=t.rowPtr[e],r=t.rowPtr[e+1];let o=0;for(let s=n;s<r;s++)(s===n||t.colIdx[s]!==t.colIdx[s-1])&&o++;return o}function oo(t,e){const n=t.directed&&!e?t.reverse():void 0;return r=>vi(Nn(t,r)+(n===void 0?0:Nn(n,r)))}function Wt(t,e,n){const r=e.directed===!0?t.reverse():t,o=t.nodeCount;return(s,i)=>s<o&&i<o?Ur(t,r,s,i,n):0}function we(t,e){const n=oo(t,e.directed===!0),r=Float64Array.from({length:t.nodeCount},(o,s)=>n(s));return Wt(t,e,o=>r[o])}function so(t,e,n,r){const o=Array.from(n,(s,i)=>i).sort((s,i)=>n[i]-n[s]).slice(0,r);return{sources:Uint32Array.from(o,s=>t[s]),targets:Uint32Array.from(o,s=>e[s]),scores:Float64Array.from(o,s=>n[s])}}function Ei(t,e,n,r,o,s,i){t.push(r),e.push(o),n.push(s),i&&(t.push(o),e.push(r),n.push(s))}function io(t,e,n){const r=[],o=[],s=[],i=e.directed!==!0&&e.uniquePairs!==!0;for(let l=0;l<t.nodeCount;l++)for(let c=l+1;c<t.nodeCount;c++){if(e.includeExisting!==!0&&t.hasArc(l,c))continue;const u=n(l,c);u>0&&Ei(r,o,s,l,c,u,i)}const{topK:a}=e;return so(r,o,s,a!==void 0&&a>0?a:s.length)}function $t(t,e){return Float64Array.from(t.sources,(n,r)=>e(n,t.targets[r]))}function ao(t,e,n,r){const o=t.nodeCount,s=[],i=[],a=[];if(e<o){const l=n.candidates===void 0?Array.from({length:o},(c,u)=>u):fe(t,n.candidates);for(let c=0;c<l.length;c++){const u=l[c];if(u===e||n.includeExisting!==!0&&u<o&&t.hasArc(e,u))continue;const d=r(e,u);d>0&&(s.push(e),i.push(u),a.push(d))}}return so(s,i,a,n.topK??10)}function lo(t,e){const n=[...t,...e],r=Array.from(n,(u,d)=>d).sort((u,d)=>n[d]-n[u]);let o=0,s=0,i=0,a=0,l=0,c=0;for(const u of r){u<t.length?o++:(c+=o,s++);const d=o/(o+s),f=o/t.length,h=d+f>0?2*(d*f)/(d+f):0;h>i&&(i=h,a=d,l=f)}return c=o>0&&s>0?c/(o*s):.5,{precision:a,recall:l,f1Score:i,auc:c}}function co(t,e={}){return io(t,e,Wt(t,e))}function bi(t,e,n={}){return $t(e,Wt(t,n))}function Ci(t,e,n={}){const r=V(t,e);return ao(t,r,n,Wt(t,n))}function Fn(t,e,n,r={}){const o=Wt(t,r);return lo($t(e,o),$t(n,o))}function Ii(t,e,n,r={}){const o=V(t,e),s=V(t,n);return Wt(t,r,oo(t,r.directed===!0))(o,s)}function uo(t,e={}){return io(t,e,we(t,e))}function xi(t,e,n={}){return $t(e,we(t,n))}function Pi(t,e,n={}){const r=V(t,e);return ao(t,r,n,we(t,n))}function kn(t,e,n,r={}){const o=we(t,r);return lo($t(e,o),$t(n,o))}function Ni(t){const e=t.nodeCount,n=new Uint32Array(e+1);for(let r=0;r<e;r++){let o=0;const s=t.rowPtr[r+1];for(let i=t.rowPtr[r];i<s;i++)t.colIdx[i]!==r&&o++;n[r+1]=n[r]+o}return n}function Fi(t){const e=t.nodeCount,n=Ni(t),r=n[e],o=new Uint32Array(r),s=new Float64Array(r),i=new Float64Array(e),a=new Float64Array(e);let l=0;for(let c=0;c<e;c++){let u=0;const d=t.rowPtr[c+1];for(let f=t.rowPtr[c];f<d;f++){const h=t.colIdx[f],g=t.weights===null?1:t.weights[f];h===c?i[c]+=g:(o[l]=h,s[l]=g,l++,u+=g)}a[c]=u+2*i[c]}return{n:e,rowPtr:n,colIdx:o,w:s,loop:i,deg:a}}function Sn(t,e,n,r,o){const s=new Float64Array(n),i=new Float64Array(n);for(let l=0;l<t.n;l++){const c=e[l];i[c]+=t.deg[l],s[c]+=2*t.loop[l];const u=t.rowPtr[l+1];for(let d=t.rowPtr[l];d<u;d++)e[t.colIdx[d]]===c&&(s[c]+=t.w[d])}let a=0;for(let l=0;l<n;l++){const c=i[l]/o;a+=s[l]/o-r*c*c}return a}function ki(t,e,n,r,o){const{n:s}=t,i=new Float64Array(s);for(let y=0;y<s;y++)e[y]=y,i[y]=t.deg[y];const a=new Float64Array(s),l=new Int32Array(s).fill(-1),c=new Uint32Array(s),u=s+1,d=new Uint32Array(u),f=new Uint8Array(s);let h=0,g=s;for(let y=0;y<s;y++)d[y]=y,f[y]=1;let w=0,m=0;const A=o*s;let p=!1;for(;h!==g&&m<A;){const y=d[h];h=h+1===u?0:h+1,f[y]=0,m++;const v=e[y],E=t.deg[y];i[v]-=E,w++;let b=0;const C=t.rowPtr[y+1];for(let S=t.rowPtr[y];S<C;S++){const P=e[t.colIdx[S]];l[P]!==w&&(l[P]=w,a[P]=0,c[b++]=P),a[P]+=t.w[S]}let x=v,_=(l[v]===w?a[v]:0)-n*E*i[v]/r;for(let S=0;S<b;S++){const P=c[S];if(P===v)continue;const I=a[P]-n*E*i[P]/r;I>_&&(_=I,x=P)}if(i[x]+=E,x!==v){e[y]=x,p=!0;for(let S=t.rowPtr[y];S<C;S++){const P=t.colIdx[S];f[P]===0&&e[P]!==x&&(f[P]=1,d[g]=P,g=g+1===u?0:g+1)}}}return p}function Si(t,e,n){const r=new Uint32Array(n+1);for(let i=0;i<t;i++)r[e[i]+1]++;for(let i=0;i<n;i++)r[i+1]+=r[i];const o=new Uint32Array(t),s=r.slice(0,n);for(let i=0;i<t;i++)o[s[e[i]]++]=i;return{start:r,members:o}}function _i(t,e,n,r,o,s){let i=0;for(let a=0;a<e;a++){const l=t[a];r[s+a]=l,o[s+a]=n[l],i+=n[l]}return i}function Mi(t,e,n){const{start:r,members:o}=Si(t.n,e,n),s=new Uint32Array(n+1),i=new Uint32Array(t.colIdx.length),a=new Float64Array(t.colIdx.length),l=new Float64Array(n),c=new Float64Array(n),u=new Float64Array(n),d=new Int32Array(n).fill(-1),f=new Uint32Array(n);let h=0;for(let g=0;g<n;g++){s[g]=h;let w=0,m=0;for(let p=r[g];p<r[g+1];p++){const y=o[p];l[g]+=t.loop[y];const v=t.rowPtr[y+1];for(let E=t.rowPtr[y];E<v;E++){const b=e[t.colIdx[E]];if(b===g){w+=t.w[E];continue}d[b]!==g&&(d[b]=g,u[b]=0,f[m++]=b),u[b]+=t.w[E]}}l[g]+=w/2;const A=_i(f,m,u,i,a,h);h+=m,c[g]=A+2*l[g]}return s[n]=h,{n,rowPtr:s,colIdx:i.subarray(0,h),w:a.subarray(0,h),loop:l,deg:c}}function fo(t,e={}){if(t.directed)throw $(new Error("Louvain requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.resolution??1,r=e.maxIterations??100,o=e.tolerance??1e-6,s=t.nodeCount,i=new Uint32Array(s);for(let f=0;f<s;f++)i[f]=f;let a=Fi(t),l=0;for(let f=0;f<s;f++)l+=a.deg[f];if(l===0)return{...K(i,s),modularity:0,iterations:0};let c=Sn(a,i,s,n,l),u=0;for(let f=0;f<r;f++){const h=new Uint32Array(a.n);if(!ki(a,h,n,l,r))break;const g=tt(h),{count:w}=g,m=g.labels,A=Sn(a,m,w,n,l);for(let y=0;y<s;y++)i[y]=m[i[y]];u=f+1;const p=A-c;if(c=A,p<o||w===a.n)break;a=Mi(a,m,w)}const d=tt(i);return{...K(d.labels,d.count),modularity:c,iterations:u}}function Ve(t){for(let e=0;e+1<t.ptr.length;e++){let n=0;for(let r=t.ptr[e];r<t.ptr[e+1];r++)n+=t.val[r];if(n>0)for(let r=t.ptr[e];r<t.ptr[e+1];r++)t.val[r]/=n}}function Ui(t,e,n,r){const o=n.length,s=new Uint32Array(o+1),i=[],a=[],l=[];for(let c=0;c<o;c++){l.length=0;for(let u=e.ptr[c];u<e.ptr[c+1];u++){const d=e.row[u],f=e.val[u];for(let h=t.ptr[d];h<t.ptr[d+1];h++){const g=t.row[h];r[g]===0&&(r[g]=1,l.push(g)),n[g]+=t.val[h]*f}}l.sort((u,d)=>u-d);for(const u of l)i.push(u),a.push(n[u]),n[u]=0,r[u]=0;s[c+1]=i.length}return{ptr:s,row:Uint32Array.from(i),val:Float64Array.from(a)}}function Oi(t,e,n){const r={ptr:t.ptr,row:t.row,val:t.val.map(c=>gs(c,e))};Ve(r);const o=t.ptr.length-1,s=new Uint32Array(o+1),i=[],a=[];for(let c=0;c<o;c++){for(let u=r.ptr[c];u<r.ptr[c+1];u++)r.val[u]>=n&&(i.push(r.row[u]),a.push(r.val[u]));s[c+1]=i.length}const l={ptr:s,row:Uint32Array.from(i),val:Float64Array.from(a)};return Ve(l),l}function Ti(t,e,n){for(let r=0;r+1<t.ptr.length;r++){let o=t.ptr[r],s=e.ptr[r];const i=t.ptr[r+1],a=e.ptr[r+1];for(;o<i||s<a;){let l;if(s>=a||o<i&&t.row[o]<e.row[s]?l=t.val[o++]:o>=i||e.row[s]<t.row[o]?l=e.val[s++]:l=t.val[o++]-e.val[s++],Math.abs(l)>n)return!1}}return!0}function Ri(t,e){const n=t===null?1:t[e];if(!(n>=0)||n===1/0)throw $(new RangeError(`arc ${e} has weight ${n}; Markov clustering needs finite, non-negative weights`),"E_BAD_WEIGHT");return n}function Di(t,e,n){const r=t.nodeCount,o=new Uint32Array(r+1);for(let h=0;h<t.arcCount;h++)o[t.colIdx[h]+1]++;if(n)for(let h=0;h<r;h++)o[h+1]++;for(let h=0;h<r;h++)o[h+1]+=o[h];const s=o.slice(0,r),i=new Uint32Array(o[r]),a=new Float64Array(o[r]),l=(h,g,w,m)=>{const A=s[g]-1;A>=o[g]&&i[A]===h?a[A]=m?w:a[A]+w:(i[s[g]]=h,a[s[g]++]=w)};for(let h=0;h<r;h++){for(let g=t.rowPtr[h];g<t.rowPtr[h+1];g++){const w=t.colIdx[g];l(h,w,Ri(e,g),!1)}n&&l(h,h,1,!0)}const c=new Uint32Array(r+1),u=[],d=[];for(let h=0;h<r;h++){for(let g=o[h];g<s[h];g++)u.push(i[g]),d.push(a[g]);c[h+1]=u.length}const f={ptr:c,row:Uint32Array.from(u),val:Float64Array.from(d)};return Ve(f),f}function $i(t,e={}){const n=e.expansion??2,r=e.inflation??2,o=e.maxIterations??100,s=e.tolerance??1e-6,i=e.pruningThreshold??1e-5;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`expansion must be an integer of at least 1, got ${n}`),"E_BAD_OPTION");if(!(r>0)||r===1/0)throw $(new RangeError(`inflation must be finite and above 0, got ${r}`),"E_BAD_OPTION");if(!Number.isInteger(o)||o<0)throw $(new RangeError(`maxIterations must be a non-negative integer, got ${o}`),"E_BAD_OPTION");if(!(s>=0)||!(i>=0))throw $(new RangeError("tolerance and pruningThreshold must be non-negative"),"E_BAD_OPTION");const a=e.weights??t.weights;if(a!==null&&a.length!==t.arcCount)throw $(new RangeError(`weights has ${a.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const l=t.nodeCount;let c=Di(t,a,e.selfLoops??!0);const u=new Float64Array(l),d=new Uint8Array(l);let f=l===0,h=0;for(;!f&&h<o;){let y=c;for(let E=1;E<n;E++)y=Ui(y,c,u,d);const v=Oi(y,r,i);h++,f=Ti(c,v,s),c=v}const g=[],w=new Int32Array(l).fill(-1);for(let y=0;y<l;y++){let v=0;for(let E=c.ptr[y];E<c.ptr[y+1];E++)c.row[E]===y&&c.val[E]>0&&g.push(y),c.val[E]>v&&(v=c.val[E],w[y]=c.row[E])}const m=new Uint32Array(l),A=new Map;for(let y=0;y<l;y++){const v=w[y];if(v>=0){let E=A.get(v);E===void 0&&(E=A.size,A.set(v,E)),m[y]=E}}let p=A.size;for(let y=0;y<l;y++)w[y]<0&&(m[y]=p++);return{...K(m,p),attractors:Uint32Array.from(g),iterations:h,converged:f}}function _n(t,e){return e instanceof Uint32Array?e:hr(t,e)}function ho(t,e){const n=t.directed&&e.arcs!=="out"?[t,t.reverse()]:[t];if(e.left!==void 0&&e.right!==void 0)return{left:_n(t,e.left),right:_n(t,e.right),views:n};const{sides:r}=kr(t,{arcs:e.arcs});if(r===null)throw $(new Error("Graph is not bipartite"),"E_NOT_BIPARTITE");const o=ct(t.nodeCount);for(let i=0;i<o.length;i++)o[i]=~r[i];const s=t.nodeCount%32;return s!==0&&(o[o.length-1]&=(1<<s)-1),{left:o,right:r,views:n}}function zi(t,e,n){const r=new Uint32Array(e+1),o=new Uint32Array(n+1);for(const{rowPtr:u,arcToEdge:d}of t){for(let f=0;f<e;f++)r[f+1]+=u[f+1]-u[f];for(const f of d)o[f+1]++}for(let u=0;u<e;u++)r[u+1]+=r[u];for(let u=0;u<n;u++)o[u+1]+=o[u];const s=r[e],i=new Uint32Array(s),a=new Uint32Array(s);for(const{rowPtr:u,colIdx:d,arcToEdge:f}of t)for(let h=0;h<e;h++)for(let g=u[h];g<u[h+1];g++){const w=o[f[g]]++;i[w]=h,a[w]=d[g]}const l=r.slice(0,e),c=new Uint32Array(s);for(let u=0;u<s;u++)c[l[i[u]]++]=a[u];return{start:r,nbr:c}}function go(t,e={}){const{left:n,right:r,views:o}=ho(t,e),{nodeCount:s}=t,{start:i,nbr:a}=zi(o,s,t.edgeCount),l=new Uint32Array(s),c=new Uint8Array(s);let u=0;for(let p=0;p<s;p++){if(c[p]===1)continue;c[p]=1;let y=u;for(l[u++]=p;y<u;){const v=l[y++];for(let E=i[v];E<i[v+1];E++)c[a[E]]===0&&(c[a[E]]=1,l[u++]=a[E])}}const d=new Uint32Array(s).fill(M),f=new Uint32Array(s).fill(M),h=new Uint32Array(s),g=new Uint32Array(s),w=new Uint32Array(s),m=new Uint32Array(s);let A=0;for(let p=0;p<s;p++){const y=l[p];if(!L(n,y))continue;const v=p+1;let E=0;g[E++]=y,w[y]=i[y];let b=M;t:for(;E>0;){const C=g[E-1];for(;w[C]<i[C+1];){const x=a[w[C]++];if(h[x]===v||!L(r,x))continue;h[x]=v;const _=f[x];if(_===M){b=x;break t}m[C]=x,g[E++]=_,w[_]=i[_];continue t}E--}if(b!==M){for(let C=E-1;C>=0;C--){const x=g[C],_=C===E-1?b:m[x];d[x]=_,f[_]=x}A++}}return{matching:d,size:A}}function Bi(t,e={}){const{left:n,right:r,views:o}=ho(t,e),{nodeCount:s}=t,i=new Uint32Array(s).fill(M),a=new Uint8Array(s);let l=0;for(let c=0;c<s;c++)if(L(n,c))t:for(const{rowPtr:u,colIdx:d}of o){const f=u[c+1];for(let h=u[c];h<f;h++){const g=d[h];if(a[g]===0&&L(r,g)){a[g]=1,i[c]=g,l++;break t}}}return{matching:i,size:l}}function wo(t,e={}){const n=t.nodeCount,r=rn(t,e.weights);if(n<2){const p=ct(n,!0);return{...Dt(p,n),cutValue:0,side:p,cutEdges:new Uint32Array(0)}}const{src:o,dst:s}=t.edgeList(),i=new Uint32Array(n+1);for(let p=0;p<t.edgeCount;p++)i[o[p]+1]++,i[s[p]+1]++;for(let p=0;p<n;p++)i[p+1]+=i[p];const a=i.slice(0,n),l=new Uint32Array(i[n]),c=new Float64Array(i[n]);for(let p=0;p<t.edgeCount;p++)l[a[o[p]]]=s[p],c[a[o[p]]++]=r[p],l[a[s[p]]]=o[p],c[a[s[p]]++]=r[p];const u=new Uint32Array(n),d=new Uint32Array(n).fill(M),f=new Uint32Array(n);for(let p=0;p<n;p++)u[p]=p,f[p]=p;const h=new Float64Array(n),g=new Uint8Array(n),w=new $r(n);let m=1/0,A=ct(n);for(let p=n;p>1;p--){let y=M;for(let b=0;b<n;b++)u[b]===b&&(g[b]=0,h[b]=0,y===M?y=b:w.push(b,0));let v=M,E=y;for(;;){g[E]=1;for(let b=E;b!==M;b=d[b])for(let C=i[b];C<i[b+1];C++){const x=u[l[C]];g[x]===0&&(h[x]+=c[C],w.pushOrIncrease(x,h[x]))}if(w.isEmpty())break;v=E,E=w.pop()}if(h[E]<m){m=h[E],A=ct(n);for(let b=E;b!==M;b=d[b])bt(A,b,!0)}for(let b=E;b!==M;b=d[b])u[b]=v;d[f[v]]=E,f[v]=f[E]}return{...Dt(A,n),cutValue:m,side:A,cutEdges:on(t,A,!1,r,!1)}}function po(t,e={}){const n=e.iterations??100,r=e.randomSeed??42;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`iterations must be a positive integer, got ${n}`),"E_BAD_OPTION");if(!Number.isInteger(r))throw $(new RangeError(`randomSeed must be a finite integer, got ${r}`),"E_BAD_OPTION");const o=t.nodeCount,s=rn(t,e.weights);if(o<2){const f=ct(o);return{...Dt(f,o),cutValue:0,side:f,cutEdges:new Uint32Array(0)}}const{src:i,dst:a}=t.edgeList(),l=Vt(r),c=new Uint32Array(t.edgeCount);let u=1/0,d=ct(o);for(let f=0;f<n;f++){for(let A=0;A<c.length;A++)c[A]=A;for(let A=c.length-1;A>0;A--){const p=Math.floor(l()*(A+1)),y=c[A];c[A]=c[p],c[p]=y}const h=new qt(o);let g=o;for(let A=0;A<c.length&&g>2;A++)h.union(i[c[A]],a[c[A]])&&g--;const w=h.find(0);let m=0;for(let A=0;A<t.edgeCount;A++)h.find(i[A])===w!=(h.find(a[A])===w)&&(m+=s[A]);if(m<u){u=m,d=ct(o);for(let A=0;A<o;A++)bt(d,A,h.find(A)===w)}}return{...Dt(d,o),cutValue:u,side:d,cutEdges:on(t,d,!1,s,!1)}}function mo(t,e={}){const n=t.edgeList(),r=t.edgeCount,o=new Float64Array(r);if(e.weights!==void 0)for(let u=0;u<r;u++)o[u]=e.weights[n.arc[u]];else if(n.weights!==null)for(let u=0;u<r;u++)o[u]=n.weights[u];else o.fill(1);const s=new Array(r);for(let u=0;u<r;u++)s[u]=u;s.sort((u,d)=>o[u]-o[d]||u-d);const i=new qt(t.nodeCount),a=new Uint32Array(Math.max(t.nodeCount-1,0));let l=0,c=0;for(const u of s)i.union(n.src[u],n.dst[u])&&(a[l++]=u,c+=o[u]);return{edges:a.subarray(0,l),totalWeight:c}}function yo(t,e={}){if(t.directed)throw $(new Error("Prim's algorithm requires an undirected graph"),"E_NEEDS_UNDIRECTED");const{nodeCount:n,rowPtr:r,colIdx:o}=t,s=e.weights??t.weights,i=new Float64Array(n).fill(1/0),a=new Uint32Array(n).fill(M),l=new Uint8Array(n),c=new _t(n),u=new Uint32Array(Math.max(n-1,0));let d=0,f=0;const h=g=>{for(c.push(g,0);!c.isEmpty();){const w=c.pop();l[w]=1,a[w]!==M&&(u[d++]=t.arcToEdge[a[w]],f+=i[w]);for(let m=r[w];m<r[w+1];m++){const A=o[m],p=s===null?1:s[m];l[A]===0&&p<i[A]&&(i[A]=p,a[A]=m,c.pushOrDecrease(A,p))}}};if(n>0&&h(e.start===void 0?0:V(t,e.start)),e.forest===!0)for(let g=0;g<n;g++)l[g]===0&&h(g);else if(d<n-1)throw $(new Error("Graph is not connected"),"E_NOT_CONNECTED");return{edges:u.subarray(0,d),totalWeight:f,predArc:a}}function Ao(t,e={}){return Eo(t,null,e)}function vo(t,e,n={}){return Eo(t,bo(e,t.nodeCount,"personalization"),n)}function Eo(t,e,n){const r=t.nodeCount,o=n.dampingFactor??.85,s=n.maxIterations??100,i=n.tolerance??1e-6,a=n.convergenceNorm==="max";if(r===0)return{scores:new Float64Array(0),iterations:0,converged:!0};const l=t.reverse(),c=n.weighted===!0&&l.weights!==null,u=c?t.edges.byRole("weight"):null,d=u?.dtype==="f64"?u.data:null;let f=c?l.weights:null,h=c?t.weightedOutDegree():t.outDegree();d!==null&&(f=Mn(d,l.arcToEdge),h=qi(t,Mn(d,t.arcToEdge)));let g=(n.initialRanks===void 0?null:bo(n.initialRanks,r,"initialRanks"))??new Float64Array(r).fill(n.initialRanks===void 0?1/r:0),w=new Float64Array(r),m=0,A=!1;for(;m<s&&!A;m++){let p=0;for(let E=0;E<r;E++)h[E]===0&&(p+=g[E]);const y=1-o+o*p*(e===null?1:1/r);let v=0;for(let E=0;E<r;E++){let b=0;const C=l.rowPtr[E+1];for(let _=l.rowPtr[E];_<C;_++){const S=l.colIdx[_],P=h[S];P>0&&(b+=g[S]*(f===null?1:f[_])/P)}w[E]=y*(e===null?1/r:e[E])+o*b;const x=Math.abs(w[E]-g[E]);v=a?Math.max(v,x):v+x}[g,w]=[w,g],A=v<i}return{scores:g,iterations:m,converged:A}}function Mn(t,e){const n=new Float64Array(e.length);for(let r=0;r<n.length;r++)n[r]=t[e[r]];return n}function qi(t,e){const n=new Float64Array(t.nodeCount);for(let r=0;r<t.nodeCount;r++)for(let o=t.rowPtr[r];o<t.rowPtr[r+1];o++)n[r]+=e[o];return n}function bo(t,e,n){if(t.length!==e)throw $(new Error(`PageRank: ${n} has ${String(t.length)} entries for ${String(e)} nodes`),"E_BAD_OPTION");let r=0;for(const s of t){if(!Number.isFinite(s)||s<0)throw $(new Error(`PageRank: ${n} must be finite and non-negative, got ${String(s)}`),"E_BAD_OPTION");r+=s}if(r===0)return null;const o=new Float64Array(e);for(let s=0;s<e;s++)o[s]=t[s]/r;return o}const Co={distance:1/0,path:new Uint32Array(0),edges:new Uint32Array(0)};function Vi(t,e,n,r={}){const o=V(t,e),s=V(t,n);if(o===s)return{distance:0,path:Uint32Array.of(o),edges:new Uint32Array(0)};const i=t.nodeCount,a=r.weights??t.weights;if(a?.some(S=>S<0)===!0)throw $(new Error("Bidirectional Dijkstra does not support negative edge weights"),"E_BAD_WEIGHT");const l=t.reverse(),c=S=>a===null?1:a[S],u=new Float64Array(i).fill(1/0),d=new Float64Array(i).fill(1/0),f=new Uint32Array(i).fill(M),h=new Uint32Array(i).fill(M),g=new Uint32Array(i).fill(M),w=new _t(i),m=new _t(i);u[o]=0,d[s]=0,w.push(o,0),m.push(s,0);let A=1/0,p=M,y=M,v=M,E=0;for(;w.peekKey()+m.peekKey()<A;)if(w.peekKey()<=m.peekKey()){const S=w.pop();for(let P=t.rowPtr[S];P<t.rowPtr[S+1];P++){const I=t.colIdx[P],F=c(P),U=u[S]+F;U<u[I]&&(u[I]=U,f[I]=P,w.pushOrDecrease(I,U)),U+d[I]<A&&(A=U+d[I],[p,y,v,E]=[S,I,t.arcToEdge[P],F])}}else{const S=m.pop();for(let P=l.rowPtr[S];P<l.rowPtr[S+1];P++){const I=l.colIdx[P],F=c(l.fwdArc[P]),U=d[S]+F;U<d[I]&&(d[I]=U,h[I]=P,g[I]=S,m.pushOrDecrease(I,U)),u[I]+U<A&&(A=u[I]+U,[p,y,v,E]=[I,S,l.arcToEdge[P],F])}}if(p===M)return Co;const b=[],C=[],x=[];for(let S=p;S!==o;S=t.arcSource(f[S]))b.push(S),C.push(t.arcToEdge[f[S]]),x.push(c(f[S]));b.push(o),b.reverse(),C.reverse(),x.reverse(),C.push(v),x.push(E),b.push(y);for(let S=y;S!==s;S=g[S])C.push(l.arcToEdge[h[S]]),x.push(c(l.fwdArc[h[S]])),b.push(g[S]);let _=0;for(const S of x)_+=S;return{distance:_,path:Uint32Array.from(b),edges:Uint32Array.from(C)}}function Wi(t,e,n,r,o){const{gScore:s,fScore:i,predArc:a,closed:l,heap:c}=o;for(let u=t.rowPtr[e];u<t.rowPtr[e+1];u++){const d=t.colIdx[u];if(l[d]===1)continue;const f=s[e]+(n===null?1:n[u]);f<s[d]&&(s[d]=f,i[d]=f+r(d),a[d]=u,c.pushOrDecrease(d,i[d]))}}function Li(t,e,n,r,o={}){const s=V(t,e),i=V(t,n),a=t.nodeCount;if(!(i>=0&&i<a))throw $(new RangeError(`Target index ${String(i)} is outside the graph's ${String(a)} nodes`),"E_BAD_NODE");const l=o.weights??t.weights,c=new Float64Array(a).fill(1/0),u=new Float64Array(a).fill(1/0),d=new Uint32Array(a).fill(M),f=new Uint8Array(a),h=[],g=new _t(a);for(c[s]=0,u[s]=r(s,i),g.push(s,u[s]);!g.isEmpty();){const A=g.pop();if(A===i)break;f[A]=1,h.push(A),Wi(t,A,l,p=>r(p,i),{gScore:c,fScore:u,predArc:d,closed:f,heap:g})}const w={gScore:c,fScore:u,visited:Uint32Array.from(h)};if(c[i]===1/0)return{...Co,...w};const m=zt(t,d,s,i);return{distance:c[i],path:m,edges:Bt(t,d,s,i),...w}}function dn(t,e={}){const{labels:n,count:r}=ji(t,e);return K(n,r)}function ji(t,e={}){if(!t.directed)throw $(new Error("Strongly connected components require a directed graph. Use connectedComponents for an undirected one."),"E_NEEDS_DIRECTED");const{nodeCount:n,rowPtr:r,colIdx:o}=t,s=ge(t,e.arcOrder),i=new Uint32Array(n).fill(M),a=new Uint32Array(n),l=new Uint32Array(n),c=new Uint8Array(n),u=new Uint32Array(n),d=new Uint32Array(n),f=new Uint32Array(n),h=new Uint32Array(n);let g=0,w=0,m=0,A=0,p=0;const y=v=>{i[v]=w,a[v]=w,w++,l[v]=r[v],d[A++]=v,c[v]=1,u[p++]=v};for(let v=0;v<n;v++)if(i[v]===M)for(y(v);p>0;){const E=u[p-1];if(l[E]<r[E+1]){const b=l[E]++,C=o[s===null?b:s[b]];i[C]===M?y(C):c[C]===1&&i[C]<a[E]&&(a[E]=i[C]);continue}if(p--,a[E]===i[E]){let b;do b=d[--A],c[b]=0,f[b]=m,h[g++]=b;while(b!==E);m++}p>0&&a[E]<a[u[p-1]]&&(a[u[p-1]]=a[E])}return{labels:f,count:m,popped:h}}function Gi(t,e={}){const n=dn(t,e),r=t.contract(n.labels,{selfLoops:"drop",parallel:"merge",weights:"first"});return{components:n,condensed:r}}const Hi=1e-11,Un=3e3;function Ki(t,e){const n=t.nodeCount,r=[{view:t,arcOf:c=>c}];if(t.directed){const c=t.reverse();r.push({view:c,arcOf:u=>c.fwdArc[u]})}const o=new Uint32Array(n+1);for(let c=0;c<n;c++){let u=0;for(const{view:d}of r)for(let f=d.rowPtr[c];f<d.rowPtr[c+1];f++)u+=d.colIdx[f]===c?0:1;o[c+1]=o[c]+u}const s=new Uint32Array(o[n]),i=new Float64Array(o[n]),a=new Float64Array(n);let l=0;for(let c=0;c<n;c++)for(const{view:u,arcOf:d}of r)for(let f=u.rowPtr[c];f<u.rowPtr[c+1];f++){const h=e===null?1:e[d(f)];if(!(h>=0)||h===1/0)throw $(new RangeError(`an arc has weight ${h}; spectral clustering needs finite, non-negative weights`),"E_BAD_WEIGHT");u.colIdx[f]!==c&&(s[l]=u.colIdx[f],i[l++]=h,a[c]+=h)}return{rowPtr:o,colIdx:s,w:i,degree:a}}function On(t,e){for(let n=0;n<t.length;n++){const r=t[n];for(let o=0;;o++){for(let i=0;i<2;i++)for(let a=0;a<n;a++){const l=t[a];let c=0;for(let u=0;u<r.length;u++)c+=l[u]*r[u];for(let u=0;u<r.length;u++)r[u]-=c*l[u]}let s=0;for(let i=0;i<r.length;i++)s+=r[i]*r[i];if(s=Math.sqrt(s),s>1e-10||o===3){for(let i=0;i<r.length;i++)r[i]=s>0?r[i]/s:0;break}for(let i=0;i<r.length;i++)r[i]=e()-.5}}}function Yi(t,e){const n=new Float64Array(e*e);for(let r=0;r<e;r++)n[r*e+r]=1;for(let r=0;r<100;r++){let o=0;for(let s=0;s<e;s++)for(let i=s+1;i<e;i++)o+=t[s*e+i]*t[s*e+i];if(o<1e-30)break;for(let s=0;s<e;s++)for(let i=s+1;i<e;i++){const a=t[s*e+i];if(a===0)continue;const l=(t[i*e+i]-t[s*e+s])/(2*a),c=Math.sign(l||1)/(Math.abs(l)+Math.sqrt(l*l+1)),u=1/Math.sqrt(c*c+1),d=c*u;for(let f=0;f<e;f++){const h=t[f*e+s],g=t[f*e+i];t[f*e+s]=u*h-d*g,t[f*e+i]=d*h+u*g}for(let f=0;f<e;f++){const h=t[s*e+f],g=t[i*e+f];t[s*e+f]=u*h-d*g,t[i*e+f]=d*h+u*g}for(let f=0;f<e;f++){const h=n[f*e+s],g=n[f*e+i];n[f*e+s]=u*h-d*g,n[f*e+i]=d*h+u*g}}}return{values:Float64Array.from({length:e},(r,o)=>t[o*e+o]),vectors:n}}function Xi(t,e,n){const r=t.length,o=new Float64Array(r*r);for(let s=0;s<r;s++)for(let i=s;i<r;i++){let a=0;for(let l=0;l<n;l++)a+=t[s][l]*e[i][l];o[s*r+i]=a,o[i*r+s]=a}return o}function Zi(t,e,n,r,o){const s=Math.min(e,n+Math.max(n,8));let i=Array.from({length:s},()=>Float64Array.from({length:e},()=>o()-.5));On(i,o);const a=new Float64Array(e),l=w=>(t(w,a),w.map((m,A)=>r*m-a[A]));let c=new Float64Array(s),u=[],d=new Float64Array(s*s),f=!1;for(let w=0;w<Un;w++){const m=i.map(l);if({values:c,vectors:d}=Yi(Xi(i,m,e),s),u=Array.from({length:s},(p,y)=>y).sort((p,y)=>c[y]-c[p]),f=u.slice(0,n).every(p=>{let y=0;for(let v=0;v<e;v++){let E=0;for(let b=0;b<s;b++)E+=d[b*s+p]*(m[b][v]-c[p]*i[b][v]);y+=E*E}return Math.sqrt(y)<=Hi*r}),f||w===Un-1)break;i=m,On(i,o)}const h=new Float64Array(n),g=[];for(let w=0;w<n;w++){const m=u[w];h[w]=Math.max(0,r-c[m]);const A=new Float64Array(e);for(let p=0;p<s;p++){const y=d[p*s+m];for(let v=0;v<e;v++)A[v]+=y*i[p][v]}g.push(A)}return{values:h,vectors:g,converged:f}}const Ji=10;function Qi(t,e,n,r,o){const s=new Float64Array(o*r),i=new Float64Array(o);for(let l=0;l<n;l++){i[e[l]]++;for(let c=0;c<r;c++)s[e[l]*r+c]+=t[l*r+c]}let a=0;for(let l=0;l<n;l++){const c=e[l];for(let u=0;u<r;u++){const d=t[l*r+u]-s[c*r+u]/i[c];a+=d*d}}return a}function ta(t,e,n,r,o,s,i){const a=(f,h,g)=>{let w=0;for(let m=0;m<n;m++){const A=t[f*n+m]-h[g*n+m];w+=A*A}return w},l=new Float64Array(r*n),c=new Float64Array(e).fill(1/0);let u=Math.floor(i()*e);for(let f=0;f<r;f++){l.set(t.subarray(u*n,u*n+n),f*n);let h=0;for(let w=0;w<e;w++)c[w]=Math.min(c[w],a(w,l,f)),h+=c[w];let g=i()*h;u=0;for(let w=0;w<e&&h>0;w++)if(g-=c[w],g<0){u=w;break}}const d=new Uint32Array(e);for(let f=0;f<o;f++){let h=f===0;for(let A=0;A<e;A++){let p=0,y=1/0;for(let v=0;v<r;v++){const E=a(A,l,v);E<y&&(y=E,p=v)}h||(h=d[A]!==p),d[A]=p}if(!h)break;const g=new Float64Array(r*n),w=new Float64Array(r);for(let A=0;A<e;A++){w[d[A]]++;for(let p=0;p<n;p++)g[d[A]*n+p]+=t[A*n+p]}let m=0;for(let A=0;A<r;A++){if(w[A]===0)continue;let p=0;for(let y=0;y<n;y++){const v=g[A*n+y]/w[A],E=v-l[A*n+y];p+=E*E,l[A*n+y]=v}m=Math.max(m,Math.sqrt(p))}if(m<s)break}return d}function ea(t,e){const{k:n}=e,r=e.laplacianType??"normalized",o=e.maxIterations??100,s=e.tolerance??1e-4,i=e.seed??42;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`k must be a positive integer, got ${n}`),"E_BAD_OPTION");if(!["unnormalized","normalized","randomWalk"].includes(r))throw $(new RangeError(`unknown laplacianType "${r}"`),"E_BAD_OPTION");if(!Number.isInteger(o)||o<0||!(s>=0)||!Number.isInteger(i))throw $(new RangeError("maxIterations and seed must be integers, maxIterations and tolerance non-negative"),"E_BAD_OPTION");const a=e.weights??t.weights;if(a!==null&&a.length!==t.arcCount)throw $(new RangeError(`weights has ${a.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const l=t.nodeCount;if(n>=l){const E=Uint32Array.from({length:l},(b,C)=>C);return{...K(E,l),eigenvalues:new Float64Array(0),eigenvectors:[],converged:!0}}const c=Ki(t,a),u=c.degree.map(E=>E>0?1/Math.sqrt(E):0),d=r!=="unnormalized",f=(E,b)=>{for(let C=0;C<l;C++){let x=0;for(let _=c.rowPtr[C];_<c.rowPtr[C+1];_++){const S=c.colIdx[_];x+=d?c.w[_]*u[S]*E[S]:c.w[_]*E[S]}d?b[C]=c.degree[C]>0?E[C]-u[C]*x:0:b[C]=c.degree[C]*E[C]-x}};let h=2;if(!d){h=1;for(let E=0;E<l;E++)for(let b=c.rowPtr[E];b<c.rowPtr[E+1];b++)h=Math.max(h,c.degree[E]+c.degree[c.colIdx[b]])}const g=Vt(i),w=Zi(f,l,n,h,g);if(r==="randomWalk")for(const E of w.vectors)for(let b=0;b<l;b++)E[b]*=c.degree[b]>0?u[b]:1;const m=new Float64Array(l*n);for(let E=0;E<n;E++)for(let b=0;b<l;b++)m[b*n+E]=w.vectors[E][b];if(r==="normalized")for(let E=0;E<l;E++){const b=m.subarray(E*n,E*n+n),C=ws(...b);C>0&&b.forEach((x,_)=>b[_]=x/C)}let A=new Uint32Array(l),p=1/0;for(let E=0;E<Ji;E++){const b=ta(m,l,n,n,o,s,g),C=Qi(m,b,l,n,n);C<p&&(p=C,A=b)}const{labels:y,count:v}=tt(A);return{...K(y,v),eigenvalues:w.values,eigenvectors:w.vectors,converged:w.converged}}const na=10;function ue(t,e,n,r,o){let s=0;for(let i=0;i<o;i++){const a=t[e*o+i]-n[r*o+i];s+=a*a}return Math.sqrt(s)}function ra(t,e,n){const r=t.nodeCount,o=new Float64Array(r*n),s=t.directed?t.degree():t.outDegree();for(let i=0;i<r;i++){const a=s[i]/Math.max(1,r-1);for(let l=0;l<n;l++)o[i*n+l]=(e()-.5)*.1+a*.1}return o}function oa(t,e){let n=1/0;for(let r=0;r<t.centreCount;r++)n=Math.min(n,ue(t.emb,e,t.centres,r,t.dim));return n}function sa(t,e,n){const{n:r,dim:o,emb:s,centres:i}=t;for(let a=0;a<r;a++)if(n-=e[a],n<=0){i.set(s.subarray(a*o,(a+1)*o),t.centreCount*o),t.centreCount++;return}}function ia(t,e,n){const{n:r,dim:o,emb:s,centres:i}=t,a=Math.floor(n()*r);i.set(s.subarray(a*o,(a+1)*o),0),t.centreCount=1;const l=new Float64Array(r);for(let c=1;c<e;c++){let u=0;for(let d=0;d<r;d++){const f=oa(t,d);l[d]=f*f,u+=f*f}sa(t,l,n()*u)}}function Tn(t){const{n:e,dim:n,emb:r,centres:o,centreCount:s,labels:i}=t;for(let a=0;a<e;a++){let l=0,c=1/0;for(let u=0;u<s;u++){const d=ue(r,a,o,u,n);d<c&&(c=d,l=u)}i[a]=l}}function aa(t,e,n,r){const{n:o,dim:s,rowPtr:i,colIdx:a,emb:l}=t;e.fill(0);for(let c=0;c<o;c++){const u=c*s;for(let d=i[c];d<i[c+1];d++){const f=a[d]*s;for(let h=0;h<s;h++)e[u+h]+=n*(l[u+h]-l[f+h])}}for(let c=0;c<o;c++){const u=i[c+1]-i[c],d=n===0||u===0?0:r/(n*u);for(let f=c*s;f<(c+1)*s;f++)l[f]-=d*e[f]}}function la(t,e,n){const{n:r,dim:o,emb:s,centres:i,centreCount:a,labels:l}=t;e.fill(0),n.fill(0);for(let c=0;c<r;c++){const u=l[c];for(let d=0;d<o;d++)e[u*o+d]+=s[c*o+d];n[u]++}for(let c=0;c<a;c++)if(n[c]>0)for(let u=0;u<o;u++)i[c*o+u]=e[c*o+u]/n[c]}function ca(t,e){const{n,dim:r,rowPtr:o,colIdx:s,emb:i,centres:a,labels:l}=t;let c=0,u=0,d=0;for(let f=0;f<n;f++){const h=ue(i,f,a,l[f],r);c+=h*h}for(let f=0;f<n;f++)for(let h=o[f];h<o[f+1];h++){const g=ue(i,f,i,s[h],r);u+=g*g}for(const f of i)d+=f*f;return c+e*u+e*d}function ua(t,e){return t.every((n,r)=>n===e[r])}function da(t,e){const{numClusters:n,maxIterations:r=1e3,tolerance:o=1e-6,seed:s=42,learningRate:i=.5,lambda:a=.1}=e,l=t.nodeCount;if(l===0)return{...K(new Uint32Array(0),n),embeddings:new Float64Array(0),dimensions:0,loss:0,previousLoss:1/0,iterations:0,converged:!0};if(!Number.isInteger(n)||n<=0||n>l)throw $(new Error(`Invalid number of clusters: ${String(n)}. Must be between 1 and ${String(l)}`),"E_BAD_OPTION");const c=Vt(s),u=Math.min(64,l),d={n:l,dim:u,rowPtr:t.rowPtr,colIdx:t.colIdx,emb:ra(t,c,u),centres:new Float64Array(n*u),centreCount:0,labels:new Uint32Array(l)};ia(d,n,c);const f=new Float64Array(l*u),h=new Float64Array(n*u),g=new Uint32Array(n);let w=1/0,m=1/0,A=0,p=!1;const y=new Uint32Array(l);let v=0;for(;A<r;)if(A++,Tn(d),v=A>1&&ua(d.labels,y)?v+1:0,y.set(d.labels),aa(d,f,a,i),la(d,h,g),A>1&&(w=m),m=ca(d,a),Math.abs(w-m)<o||v>=na){p=!0;break}return Tn(d),{...K(d.labels,n),embeddings:d.emb,dimensions:u,loss:m,previousLoss:w,iterations:A,converged:p}}const fa=100;function ha(t,e,n){const r=t.nodeCount;if(!e){for(let s=0;s<r;s++)for(let i=s+1;i<r;i++)n[s*r+i]=n[i*r+s]=t.hasArc(s,i)?1:2;return}n.fill(1/0);const o=new Uint32Array(r);for(let s=0;s<r;s++)ga(t,s,n,o)}function ga(t,e,n,r){const o=e*t.nodeCount;n[o+e]=0,r[0]=e;let s=0,i=1;for(;s<i;){const a=r[s++],l=n[o+a]+1;for(let c=t.rowPtr[a];c<t.rowPtr[a+1];c++){const u=t.colIdx[c];n[o+u]===1/0&&(n[o+u]=l,r[i++]=u)}}}function wa(t,e={}){const{linkage:n="average",numClusters:r,distanceThreshold:o,useGraphDistance:s=!0}=e;if(r!==void 0&&(r<1||!Number.isInteger(r)))throw $(new Error("numClusters must be a positive integer"),"E_BAD_OPTION");const i=t.nodeCount;if(i===0)throw $(new Error("Cannot cluster empty graph"),"E_EMPTY_GRAPH");const a=pr("teraHAC",i,e.maxNodes,8*i*i,()=>new Float64Array(i*i));if(ha(t,s,a),n==="ward")for(let N=0;N<a.length;N++)a[N]*=a[N];const c={single:Math.min,complete:Math.max,average:null,ward:null}[n],u=Math.max(0,i-1),d=new Uint32Array(u),f=new Uint32Array(u),h=new Float64Array(u),g=new Uint32Array(u),w=new Uint32Array(i),m=new Uint32Array(i).fill(1),A=new Uint8Array(i).fill(1);for(let N=0;N<i;N++)w[N]=N;const p=(N,O)=>{const T=w[N],B=w[O];return T<i&&B<i?T<B:T>B},y=(N,O)=>{const[T,B]=p(N,O)?[N,O]:[O,N];let z=a[T*i+B];return w[T]<i&&w[B]<i?z=n==="ward"?Math.sqrt(z):z:c||(z/=m[T]*m[B]),z===1/0?fa:z},v=(N,O)=>{const T=w[N],B=w[O],z=Math.min(T,B),q=Math.max(T,B);return q<i?z*i+q:i*i+q*2*i+z},E=new Int32Array(i).fill(-1),b=new Float64Array(i),C=new Float64Array(i),x=(N,O)=>{const T=y(N,O),B=v(N,O);(E[N]===-1||T<b[N]||T===b[N]&&B<C[N])&&(E[N]=O,b[N]=T,C[N]=B)},_=N=>{E[N]=-1;for(let O=0;O<i;O++)A[O]&&O!==N&&x(N,O)};for(let N=0;N<i;N++)_(N);let S=i,P=0;for(;S>1&&(r===void 0||S>r);){let N=-1;for(let z=0;z<i;z++)A[z]&&(N===-1||b[z]<b[N]||b[z]===b[N]&&C[z]<C[N])&&(N=z);const O=b[N];if(o!==void 0&&O>o)break;const[T,B]=p(N,E[N])?[N,E[N]]:[E[N],N];d[P]=w[T],f[P]=w[B],h[P]=O,g[P]=m[T]+m[B];for(let z=0;z<i;z++)if(A[z]&&z!==T&&z!==B){const q=T*i+z,W=B*i+z,Y=z*i+T,et=z*i+B;a[q]=c?c(a[q],a[W]):a[q]+a[W],a[Y]=c?c(a[Y],a[et]):a[Y]+a[et]}w[T]=i+P,m[T]=g[P],A[B]=0,P++,S--,_(T);for(let z=0;z<i;z++)A[z]&&z!==T&&(E[z]===T||E[z]===B?_(z):x(z,T))}const I=Array.from(w.filter((N,O)=>A[O]===1)).sort((N,O)=>N-O);let F=I[0];const U=N=>N<i?1:g[N-i];for(let N=1;N<I.length;N++){const O=P+N-1;d[O]=F,f[O]=I[N],h[O]=1/0,g[O]=U(F)+U(I[N]),F=i+O}const R=pa(i,d,f,F,r??S),{labels:D,count:k}=tt(R);return{...K(D,k),left:d,right:f,distance:h,size:g,merges:P,root:F}}function pa(t,e,n,r,o){const s=[],i=[r];let a=0;for(;a<i.length&&s.length<o;){const c=i[a++];c<t||o===1||s.length+(i.length-a)+1>=o?s.push(c):i.push(e[c-t],n[c-t])}const l=new Uint32Array(t);return s.forEach((c,u)=>{const d=[c];for(;d.length>0;){const f=d.pop()??0;f<t?l[f]=u:d.push(e[f-t],n[f-t])}}),l}function ma(t,e,n){let r=e;for(let o=e;o<n;o++)(o===e||t[o]!==t[o-1])&&(t[r++]=t[o]);return r}function Io(t){const e=t.nodeCount,n=t.directed?[t,t.reverse()]:[t],r=new Uint32Array(e+1),o=new Uint32Array(n.reduce((i,a)=>i+a.colIdx.length,0));let s=0;for(let i=0;i<e;i++){r[i]=s;const a=s;for(const c of n)for(let u=c.rowPtr[i];u<c.rowPtr[i+1];u++)c.colIdx[u]!==i&&(o[s++]=c.colIdx[u]);const l=o.subarray(a,s);n.length>1&&l.sort(),s=ma(o,a,s)}return r[e]=s,{rowPtr:r,colIdx:o.slice(0,s)}}function xo(t,e,n){const r=t.length,o=new Float64Array(r);let s=0;for(let i=0;i<r;i++){const a=n[i+1]-n[i],l=a*(a-1)/2;s+=l,o[i]=l>0?t[i]/l:0}return{coefficient:o,transitivity:s>0?3*e/s:0}}function ya(t,e,n,r,o){let s=0,i=e[n],a=e[r];const l=e[n+1],c=e[r+1];for(;i<l&&a<c;)t[i]<t[a]?i++:(t[i]>t[a]||(o[n]++,o[r]++,o[t[i]]++,s++,i++),a++);return s}function Po(t){const e=t.nodeCount,{rowPtr:n,colIdx:r}=Io(t),o=(u,d)=>{const f=n[u+1]-n[u],h=n[d+1]-n[d];return h>f||h===f&&d>u},s=new Uint32Array(e+1),i=new Uint32Array(r.length/2);let a=0;for(let u=0;u<e;u++){s[u]=a;for(let d=n[u];d<n[u+1];d++)o(u,r[d])&&(i[a++]=r[d])}s[e]=a;const l=new Uint32Array(e);let c=0;for(let u=0;u<e;u++){const d=s[u+1];for(let f=s[u];f<d;f++)c+=ya(i,s,u,i[f],l)}return{perNode:l,total:c,...xo(l,c,n)}}function lt(t){return new Promise(e=>{e(t())})}function Rn(t,e){if(t.sigmaOverflow===!0)throw new ys(e);return t}function Dn(t,e){return{normalized:e?.normalized,sources:Jt(t,e?.sources,e?.k)}}function Aa(t,e,n){const r=n?.sources!==void 0||n?.k!==void 0,o=n?.harmonic===!0;return(!r||!e.directed)&&(!o||t.harmonicCloseness===!0&&!r)&&n?.normalized!==!0&&n?.cutoff===void 0&&n?.weights===void 0}function va(t,e){const n=e?.weighted===!0;return e?.sources===void 0&&e?.k===void 0?e?.harmonic===!0?{weighted:n,harmonic:!0}:{weighted:n}:{weighted:n,sources:Jt(t,e.sources,e.k,"closenessCentrality")}}function Ea(t,e){return e?.weights===void 0&&(e?.method??"auto")==="auto"&&e?.maxNodes===void 0&&e?.paths!==!0&&e?.weighted!==!1&&t.nodeCount<=Er&&t.flags.nonNegativeWeights&&t.flags.finiteWeights}function No(t){return t?.initialRanks!==void 0||t?.convergenceNorm==="max"}function $n(t){return{dampingFactor:t?.dampingFactor,maxIterations:t?.maxIterations,tolerance:t?.tolerance,weighted:t?.weighted===!0}}function ba(t,e,n){const r=n?.weighted===!0&&t.weights!==null?t.weightedOutDegree():t.outDegree();return No(n)||!e.some(o=>o>0)||r.includes(0)}function Ca(t,e){return e?.startVector===void 0&&!t.directed&&!t.flags.multigraph&&!Ia(t)}function Ia(t){const e=t.nodeCount,n=new Int8Array(e).fill(-1),r=new Uint32Array(e);for(let o=0;o<e;o++){if(n[o]!==-1||t.rowPtr[o]===t.rowPtr[o+1])continue;n[o]=0;let s=0;r[s++]=o;let i=!1;for(let a=0;a<s;a++){const l=r[a];for(let c=t.rowPtr[l];c<t.rowPtr[l+1];c++){const u=t.colIdx[c];n[u]===-1?(n[u]=1-n[l],r[s++]=u):n[u]===n[l]&&(i=!0)}}if(!i)return!0}return!1}function xa(t,e){if(!t.converged)throw new mr("eigenvectorCentrality",e?.maxIterations??100,e?.tolerance??1e-6);if(e?.normalized===!1)return t;const n=Float64Array.from(t.scores);return Lr(n),{scores:n,iterations:t.iterations,converged:!0}}function Pa(t,e){if(e?.normalized===!1||e?.alpha===0)return!1;const n=t.reverse(),r=e?.weighted===!0?n.weights:null,o=new Float64Array(t.nodeCount);let s,i=!1;for(let c=0;c<t.nodeCount;c++){let u=0;for(let d=n.rowPtr[c];d<n.rowPtr[c+1];d++)u+=r===null?1:r[d],o[c]+=r===null?1:Math.abs(r[d]);s??(s=u),i||(i=u!==s)}if(!i)return!1;let a=0;for(let c=0;c<t.nodeCount;c++)for(let u=n.rowPtr[c];u<n.rowPtr[c+1];u++)a=Math.max(a,o[c]*o[n.colIdx[u]]);const l=e?.alpha??.1;return l*l*a<1}function Na(t){let e=1/0,n=-1/0;for(const o of t)e=Math.min(e,o),n=Math.max(n,o);const r=n-e;if(r>0)for(let o=0;o<t.length;o++)t[o]=(t[o]-e)/r}function Fa(t){const e=Float64Array.from(t.scores);return Na(e),{scores:e,iterations:t.iterations,converged:t.converged}}function zn(t,e){let n=0;for(const r of t)n=e==="l2"?n+r*r:Math.max(n,r);if(n=e==="l2"?Math.sqrt(n):n,n>0)for(let r=0;r<t.length;r++)t[r]/=n}function ka(t,e){const n=e?.normalized===!1?"max":"l2",r=Float64Array.from(t.hubs),o=Float64Array.from(t.authorities);return zn(r,n),zn(o,n),{hubs:r,authorities:o,iterations:t.iterations,converged:t.converged}}function Bn(t,e,n){const{predArc:r}=n;return{dist:n.dist,predArc:r,pathTo:o=>zt(t,r,e,o),pathEdges:o=>Bt(t,r,e,o)}}function Sa(t,e){const{perNode:n,total:r,coefficient:o,transitivity:s}=e;return o!==void 0&&s!==void 0?{perNode:n,total:r,coefficient:o,transitivity:s}:{perNode:n,total:r,...xo(n,r,Io(t).rowPtr)}}function _a(t){return{accelerator:t??null,pageRank:(e,n)=>t?.pageRank!==void 0&&!No(n)?t.pageRank(e,$n(n)):Promise.resolve(Ao(e,n)),personalizedPageRank:(e,n,r)=>t?.personalizedPageRank!==void 0&&!ba(e,n,r)?t.personalizedPageRank(e,n,$n(r)):Promise.resolve(vo(e,n,r)),eigenvectorCentrality:(e,n)=>t?.eigenvectorCentrality!==void 0&&Ca(e,n)?t.eigenvectorCentrality(e,{maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:!1}).then(r=>xa(r,n)):new Promise(r=>{r(Wr(e,n))}),sssp:(e,n,r)=>{const o=V(e,n);return t?.sssp!==void 0?t.sssp(e,o,r).then(s=>Bn(e,o,s)):Promise.resolve(vr(e,o,r))},bellmanFord:(e,n,r)=>{const o=V(e,n);return t?.bellmanFord!==void 0&&(r?.weights===void 0||r.weights instanceof Float32Array)?t.bellmanFord(e,o,r).then(s=>({...Bn(e,o,s),hasNegativeCycle:s.hasNegativeCycle})):Promise.resolve(Cr(e,o,r))},breadthFirstSearch:(e,n,r)=>{const o=V(e,n);return t?.breadthFirstSearch!==void 0&&r?.target===void 0&&r?.arcOrder===void 0?t.breadthFirstSearch(e,o,r):Promise.resolve(Fr(e,o,r))},connectedComponents:e=>t?.connectedComponents!==void 0?t.connectedComponents(e):Promise.resolve(Tr(e)),weaklyConnectedComponents:e=>t?.weaklyConnectedComponents!==void 0?t.weaklyConnectedComponents(e):Promise.resolve(Rr(e)),minimumSpanningTree:(e,n)=>t?.minimumSpanningTree!==void 0&&n?.weights===void 0?t.minimumSpanningTree(e):Promise.resolve(mo(e,n)),kCoreDecomposition:e=>t?.kCoreDecomposition!==void 0?t.kCoreDecomposition(e):Promise.resolve(Qr(e)),katzCentrality:(e,n)=>t?.katzCentrality!==void 0&&Pa(e,n)?t.katzCentrality(e,{alpha:n?.alpha,beta:n?.beta,maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:n?.weighted===!0}).then(Fa):Promise.resolve(to(e,n)),hits:(e,n)=>t?.hits!==void 0?t.hits(e,{maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:n?.weighted===!0}).then(r=>ka(r,n)):Promise.resolve(Zr(e,n)),louvain:(e,n)=>t?.louvain!==void 0?t.louvain(e,n):Promise.resolve(fo(e,n)),allPairsShortestPath:(e,n)=>t?.allPairsShortestPath!==void 0&&Ea(e,n)?t.allPairsShortestPath(e).then(({dist:r,n:o})=>({dist:r,n:o,hasNegativeCycle:!1})):Promise.resolve(br(e,n)),betweennessCentrality:(e,n)=>t?.betweennessCentrality!==void 0&&!e.flags.multigraph&&n?.endpoints!==!0?t.betweennessCentrality(e,Dn(e,n)).then(r=>Rn(r,"betweennessCentrality")):Promise.resolve(Pr(e,n)),edgeBetweennessCentrality:(e,n)=>t?.edgeBetweennessCentrality!==void 0&&!e.flags.multigraph&&n?.alive===void 0?t.edgeBetweennessCentrality(e,Dn(e,n)).then(r=>Rn(r,"edgeBetweennessCentrality")):Promise.resolve(Ze(e,n)),closenessCentrality:(e,n)=>t?.closenessCentrality!==void 0&&Aa(t,e,n)?t.closenessCentrality(e,va(e,n)):Promise.resolve(Mr(e,n)),labelPropagation:(e,n)=>t?.labelPropagation!==void 0&&n?.randomSeed===void 0?t.labelPropagation(e,n):Promise.resolve(Kr(e,n)),labelPropagationSynchronous:(e,n)=>t?.labelPropagation!==void 0?t.labelPropagation(e,{maxIterations:n?.maxIterations,weighted:n?.weighted}):Promise.resolve(Yr(e,n)),triangleCount:e=>t?.triangleCount!==void 0?t.triangleCount(e).then(n=>Sa(e,n)):Promise.resolve(Po(e)),depthFirstSearch:(e,n,r)=>lt(()=>Vr(e,n,r)),degrees:e=>lt(()=>Dr(e)),stronglyConnectedComponents:(e,n)=>lt(()=>dn(e,n)),leiden:(e,n)=>lt(()=>ro(e,n)),girvanNewman:(e,n)=>lt(()=>Xr(e,n)),maxFlow:(e,n,r,o)=>lt(()=>sn(e,n,r,o)),minSTCut:(e,n,r,o)=>lt(()=>jr(e,n,r,o)),stoerWagner:(e,n)=>lt(()=>wo(e,n)),kargerMinCut:(e,n)=>lt(()=>po(e,n)),commonNeighborsPrediction:(e,n)=>lt(()=>co(e,n)),adamicAdarPrediction:(e,n)=>lt(()=>uo(e,n)),primMST:(e,n)=>lt(()=>yo(e,n)),maximumBipartiteMatching:(e,n)=>lt(()=>go(e,n))}}const Ma={graphtyAStar:["directed","goal","gpu","heuristic","root","weight"],graphtyAdamicAdarForPairs:["directed","gpu","pairs"],graphtyAdamicAdarPrediction:["directed","gpu","includeExisting","topK"],graphtyAdamicAdarScore:["directed","gpu","source","target"],graphtyAllPairsShortestPath:["directed","gpu","maxNodes","method","paths","weight"],graphtyBellmanFord:["cutoff","directed","field","gpu","root","weight"],graphtyBetweennessCentrality:["directed","endpoints","field","gpu","k","normalized","sources"],graphtyBidirectionalDijkstra:["directed","goal","gpu","root","weight"],graphtyBreadthFirstSearch:["directed","field","goal","gpu","maxDepth","root"],graphtyClosenessCentrality:["cutoff","directed","field","gpu","harmonic","k","normalized","sources","weight"],graphtyCommonNeighborsForPairs:["directed","gpu","pairs"],graphtyCommonNeighborsPrediction:["directed","gpu","includeExisting","topK"],graphtyCommonNeighborsScore:["directed","gpu","source","target"],graphtyCompareAdamicAdarWithCommonNeighbors:["directed","edges","gpu","nonEdges"],graphtyCondensation:["directed","field","gpu"],graphtyConnectedComponents:["directed","field","gpu"],graphtyDegreeCentrality:["directed","field","gpu","mode","normalized"],graphtyDegrees:["directed","gpu"],graphtyDeltaPageRank:["dampingFactor","deltaThreshold","directed","field","gpu","maxIterations","personalization","priority","tolerance","weight"],graphtyDepthFirstSearch:["directed","field","goal","gpu","order","root"],graphtyDijkstra:["cutoff","directed","field","gpu","root","weight"],graphtyDirectionOptimizedBfs:["alpha","beta","directed","field","gpu","root"],graphtyEdgeBetweennessCentrality:["directed","field","gpu","k","normalized","sources"],graphtyEigenvectorCentrality:["directed","field","gpu","maxIterations","mode","normalized","startVector","tolerance"],graphtyEvaluateAdamicAdar:["directed","edges","gpu","nonEdges"],graphtyEvaluateCommonNeighbors:["directed","edges","gpu","nonEdges"],graphtyFindAllIsomorphisms:["directed","edgeMatch","gpu","nodeMatch","other"],graphtyGirvanNewman:["directed","field","gpu","maxCommunities","maxIterations","minCommunitySize","weight"],graphtyGreedyBipartiteMatching:["arcs","directed","gpu","left","right"],graphtyGrsbm:["directed","field","gpu","maxDepth","maxIterations","minClusterSize","seed","tolerance","weight"],graphtyHasCycle:["directed","gpu"],graphtyHierarchicalClustering:["directed","gpu","linkage","maxNodes"],graphtyHits:["directed","field","gpu","maxIterations","normalized","tolerance","weight"],graphtyIsBipartite:["arcs","directed","field","gpu"],graphtyIsGraphIsomorphic:["directed","edgeMatch","gpu","nodeMatch","other"],graphtyKCoreDecomposition:["directed","field","gpu"],graphtyKargerMinCut:["directed","field","gpu","iterations","randomSeed","weight"],graphtyKatzCentrality:["alpha","beta","directed","field","gpu","maxIterations","normalized","tolerance","weight"],graphtyKruskalMST:["directed","gpu","weight"],graphtyLabelPropagation:["directed","field","gpu","maxIterations","randomSeed","weight"],graphtyLabelPropagationSemiSupervised:["directed","field","gpu","maxIterations","randomSeed","seeds","weight"],graphtyLabelPropagationSynchronous:["directed","field","gpu","maxIterations","weight"],graphtyLeiden:["directed","field","gpu","maxIterations","randomSeed","resolution","threshold","weight"],graphtyLouvain:["directed","field","gpu","maxIterations","resolution","tolerance","weight"],graphtyMarkovClustering:["directed","expansion","field","gpu","inflation","maxIterations","pruningThreshold","selfLoops","tolerance","weight"],graphtyMaxFlow:["algorithm","directed","field","gpu","sink","source","weight"],graphtyMaximumBipartiteMatching:["arcs","directed","gpu","left","right"],graphtyMinSTCut:["algorithm","directed","field","gpu","sink","source","weight"],graphtyModularity:["clusters","directed","gpu","resolution","weight"],graphtyNodeClosenessCentrality:["cutoff","directed","gpu","harmonic","normalized","root","weight"],graphtyPageRank:["convergenceNorm","dampingFactor","directed","field","gpu","initialRanks","maxIterations","tolerance","weight"],graphtyPersonalizedPageRank:["convergenceNorm","dampingFactor","directed","field","gpu","initialRanks","maxIterations","personalization","tolerance","weight"],graphtyPrimMST:["directed","forest","gpu","root","weight"],graphtySpectralClustering:["directed","field","gpu","k","laplacianType","maxIterations","seed","tolerance","weight"],graphtyStoerWagner:["directed","field","gpu","weight"],graphtyStronglyConnectedComponents:["directed","field","gpu"],graphtySyncClustering:["directed","field","gpu","lambda","learningRate","maxIterations","numClusters","seed","tolerance"],graphtyTeraHAC:["directed","distanceThreshold","field","gpu","linkage","maxNodes","numClusters","useGraphDistance"],graphtyTopAdamicAdarCandidatesForNode:["candidates","directed","gpu","includeExisting","root","topK"],graphtyTopCandidatesForNode:["candidates","directed","gpu","includeExisting","root","topK"],graphtyTopologicalSort:["directed","gpu"],graphtyTriangleCount:["directed","field","gpu"],graphtyWeaklyConnectedComponents:["directed","field","gpu"]},Oc={ak:["k"],"balanced-tree":["branching","height","seed","weights"],"barabasi-albert":["m","n","seed","triadProbability","weights"],barbell:["cliqueSize","pathLength","seed","weights"],"bianconi-barabasi":["fitness","m","n","seed","weights"],"bipartite-configuration-model":["leftDegrees","multiEdges","rightDegrees","seed","weights"],caveman:["cliques","seed","size","weights"],"chung-lu":["expectedDegrees","seed","weights"],"circular-ladder":["n","seed","weights"],complete:["n","seed","weights"],"complete-bipartite":["a","b","seed","weights"],"complete-multipartite":["seed","sizes","weights"],"configuration-model":["degrees","multiEdges","seed","selfLoops","weights"],"connected-caveman":["cliques","seed","size","weights"],cycle:["n","seed","weights"],"degree-corrected-sbm":["expectedDegrees","mixing","seed","sizes","weights"],"directed-configuration-model":["inDegrees","multiEdges","outDegrees","seed","selfLoops","weights"],"duplication-divergence":["n","retention","seed","weights"],empty:["n","seed","weights"],"erdos-renyi":["directed","n","p","seed","weights"],"erdos-renyi-gnm":["m","n","seed","weights"],"forest-fire":["backward","forward","maxBurn","n","seed","weights"],genrmf:["a","b","c1","c2","seed"],grid:["cols","diagonals","directed","obstacles","periodic","positions","rows","seed","weights"],"grid-3d":["cols","diagonals","directed","layers","obstacles","periodic","positions","rows","seed","weights"],"grid-flow-network":["cols","maxCapacity","minCapacity","rows","seed"],"hexagonal-lattice":["cols","positions","rows","seed","weights"],hyperbolic:["averageDegree","exponent","n","seed","temperature","weights"],hypercube:["dimension","seed","weights"],knn:["clusters","dimension","directed","k","n","seed","spread","weights"],kronecker:["directed","edges","initiator","multiEdges","permute","power","seed","selfLoops","weights"],ladder:["n","seed","weights"],"layered-flow-network":["layers","maxCapacity","minCapacity","p","seed"],lfr:["communityExponent","degreeExponent","maxCommunity","maxDegree","minCommunity","minDegree","mixing","n","seed","weights"],lollipop:["cliqueSize","pathLength","seed","weights"],"mobius-ladder":["n","seed","weights"],named:["name","seed","weights"],"newman-watts":["k","n","p","seed","weights"],path:["n","seed","weights"],petersen:["seed","weights"],"planted-partition":["groupSize","groups","pIn","pOut","seed","weights"],price:["attractiveness","citations","n","seed","weights"],"random-apollonian":["n","seed","weights"],"random-bipartite":["n1","n2","p","perfectMatching","seed","weights"],"random-dag":["layers","p","seed","weights"],"random-geometric":["dimension","n","periodic","radius","seed","weights"],"random-order-dag":["n","p","seed","weights"],"random-recursive-tree":["n","seed","weights"],"random-regular":["d","n","seed","weights"],"random-tree":["n","seed","weights"],"ring-of-cliques":["cliques","seed","size","weights"],rmat:["a","b","c","d","directed","edgeFactor","multiEdges","permute","scale","seed","selfLoops","weights"],star:["n","seed","weights"],"stochastic-block-model":["probabilities","seed","sizes","weights"],"triangular-lattice":["cols","positions","rows","seed","weights"],"watts-strogatz":["beta","k","n","seed","weights"],waxman:["alpha","beta","n","seed","weights"],wheel:["n","seed","weights"],"wilson-maze":["cols","rows","seed","weights"]},qn=5e3;function Ht(t,e){const n=setTimeout(()=>{console.warn(`graphty: ${e} has waited ${String(qn/1e3)} s for part of @graphty/cytoscape-extensions to load. If a module awaits ${e} at its top level, a Vite 6 or 7 production build never loads it: call it inside an async function, or with .then(), instead. On a slow network the call carries on.`)},qn),r=()=>{clearTimeout(n)};return t.then(r,r),t}const Vn=new WeakMap,Tt=new Map,We=new Map;function pe(t){return t.cy()}function Fo(t){let e=Vn.get(t);if(e===void 0){const n={version:0,entries:[],dropListeners:new Set};t.on("add remove data move",()=>{n.version++,ko(n,n.entries),n.entries=[]}),Vn.set(t,n),e=n}return e}function ko(t,e){const n=[];for(const{value:r}of e)Tt.has(r.snapshot)?We.set(r.snapshot,t):n.push(r.snapshot);So(t,n)}function So(t,e){if(e.length>0)for(const n of t.dropListeners)n(e)}function _o(t){Tt.set(t,(Tt.get(t)??0)+1);let e=!0;return()=>{if(!e)return;e=!1;const n=(Tt.get(t)??1)-1;if(n>0){Tt.set(t,n);return}Tt.delete(t);const r=We.get(t);r!==void 0&&(We.delete(t),So(r,[t]))}}function Ua(t,e){const n=Fo(t);return n.dropListeners.add(e),()=>{n.dropListeners.delete(e)}}function fn(t,e={}){const n=e.directed??!1,r=t;if(typeof e.weight=="function")return Wn(r,n,e.weight);const o=`${String(n)}|${e.weight??""}`,s=Fo(pe(t)),i=s.entries.find(l=>l.key===o&&l.version===s.version&&Oa(l.eles,r));if(i!==void 0)return i.value;const a=Wn(r,n,e.weight);return ko(s,s.entries.filter(l=>l.key===o)),s.entries=[...s.entries.filter(l=>l.key!==o),{key:o,eles:r,version:s.version,value:a}],a}function Oa(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function Wn(t,e,n){const r=t.nodes(),o=[],s=new Map;r.forEach((d,f)=>{o.push(d.id()),s.set(d.id(),f)});const i=t.edges().filter(d=>s.has(d.source().id())&&s.has(d.target().id())),a=new Uint32Array(i.length),l=new Uint32Array(i.length),c=n===void 0?void 0:new Float64Array(i.length);return i.forEach((d,f)=>{if(a[f]=s.get(d.source().id())??0,l[f]=s.get(d.target().id())??0,c!==void 0&&n!==void 0){const h=typeof n=="function"?n(d):d.data(n);c[f]=typeof h=="number"&&Number.isFinite(h)?h:1}}),{snapshot:dr({directed:e,ids:o,src:a,dst:l,weights:c}),nodes:r,edges:i}}function Qt(t,e,n){pe(t).batch(()=>{t.forEach((r,o)=>{r.data(n,e[o])})})}function ft(t,e){return(typeof e=="string"?t.nodes.filter(e):t.nodes.intersection(e)).map(r=>t.snapshot.ids.requireIndex(r.id()))}function Rt(t,e,n){if(e===void 0)return;const[r]=ft(t,e);if(r===void 0)throw new Error(`${n} matches no node of the collection`);return r}class Ta extends Error{constructor(e,n,r=null){super(n),this.name="GpuUnavailableError",this.code=e,this.fix=r}}function Kt(){return Object.assign(new Error("graphty: the Cytoscape core was destroyed"),{code:"E_CORE_DESTROYED"})}const Ra=["auto","off","require"];function hn(t,e){if(e!==void 0&&!Ra.includes(e)){const r=e===null||typeof e=="number"||typeof e=="boolean"?String(e):`a value of type ${typeof e}`,o=typeof e=="string"?JSON.stringify(e):r;throw new TypeError(`${t}: gpu must be "auto", "off" or "require"; got ${o}`)}}const Da=5e3;let Mo={},$a,Ut=null,St=null,Le=!1;const Ln=new WeakMap;function Tc(t={}){Mo={...t},Ut=null,St=null,Le=!1}function Uo(){const t=globalThis.navigator;return t?.gpu!==void 0&&t.gpu!==null?null:globalThis.process?.versions?.node===void 0?"this runtime has no WebGPU (navigator.gpu is undefined)":null}function za(){if(Ut===null){const t=Uo();if(t!==null)St={reason:t??"WebGPU was disabled",fix:null},Ut=Promise.resolve(St);else{const e=Ht(Gt(()=>import("./gpu-platform-browser-VBah74fl.js"),[],import.meta.url),"an ...Async method or a simulation layout").then(n=>n.gpuProvider(Mo),n=>({reason:`@graphty/webgpu-graph-algorithms did not load: ${To(n)}`,fix:"check that @graphty/webgpu-graph-algorithms is installed and that your bundler serves its chunk"}));Ut=e,e.then(n=>{Ut===e&&(St=n)})}}return Ut}function Oo(t){return"acquire"in t}function To(t){return t instanceof Error?t.message:String(t)}function Ro(t){t?.then(e=>e.gpu?.accelerator.dispose(),()=>{})}function Ba(t){let e=Ln.get(t);if(e===void 0){const n={decision:null,by:null,destroyed:!1};t.one("destroy",()=>{n.destroyed=!0,Ro(n.decision),n.decision=null}),Ln.set(t,n),e=n}return e}async function qa(t,e,n){let r;try{r=await n.acquire()}catch(i){const a=i instanceof Ta?i.fix:null;return{gpu:null,reason:`no usable WebGPU device: ${To(i)}`,fix:a}}if(e.destroyed)return r.accelerator.dispose(),{gpu:null,reason:"the core was destroyed"};const o=Ua(t,i=>{for(const a of i)r.accelerator.release(a)}),s=e.decision;return r.lost.then(()=>{o(),e.decision===s&&(e.decision=null)}),{gpu:r,reason:null}}function Do(t="auto"){if(t==="off")return{gpu:null,reason:'gpu: "off" was requested',fix:null};if(Ut===null&&$a===void 0){const e=Uo();if(e!==null)return{gpu:null,reason:e,fix:null}}return St===null||Oo(St)?null:{gpu:null,...St}}async function $o(t,e="auto"){let n=Do(e);if(n===null){const r=Ba(t),o=za(),s=St??await o;if(!Oo(s))n={gpu:null,...s};else{if(r.destroyed)throw Kt();(r.decision===null||r.by!==s)&&(Ro(r.decision),r.decision=Promise.resolve().then(()=>qa(t,r,s)),r.by=s),n=await r.decision}}if(e==="require"&&n.gpu===null)throw new Error(`graphty: gpu: "require" but ${n.reason??"no device"}`);return n}function je(t,e){Le||t.gpu!==null||t.fix===void 0||t.fix===null||e<Da||(Le=!0,console.warn(`graphty: a ${e.toLocaleString("en-US")}-node graph ran on the CPU because ${t.reason??"no GPU"}. To use the GPU: ${t.fix}. Pass gpu: "off" to run on the CPU without this warning.`))}function Va(t){let e=!1;return{accelerator:new Proxy(t,{get(r,o,s){const i=Reflect.get(r,o,s);return typeof i!="function"||o==="release"||o==="dispose"?i:(...a)=>(e=!0,i.apply(r,a))}}),used:()=>e}}function Ge(t,e,n){return t.gpu===null?{ran:"cpu",reason:t.reason,device:null}:e?{ran:"gpu",reason:null,device:t.gpu.device}:{ran:"cpu",reason:n,device:t.gpu.device}}const Wa={breadthFirstSearch:Fr,sssp:vr,bellmanFord:Cr,allPairsShortestPath:br,pageRank:Ao,personalizedPageRank:vo,eigenvectorCentrality:Wr,katzCentrality:to,hits:Zr,closenessCentrality:Mr,betweennessCentrality:Pr,edgeBetweennessCentrality:Ze,connectedComponents:Tr,weaklyConnectedComponents:Rr,triangleCount:Po,labelPropagation:Kr,labelPropagationSynchronous:Yr};function X(t,e){return t instanceof Promise?t.then(e):e(t)}const La=new Set(["gpu","directed","weight","field","root","goal","target","source","sink","heuristic","personalization","seeds","left","right","clusters","pairs","edges","nonEdges","nodeMatch","edgeMatch","priority","candidates","sources","alive","startVector","initialRanks"]),ja=new Set(["breadthFirstSearch","directionOptimizedBfs","depthFirstSearch","hasCycle","topologicalSort","degreeCentrality","degrees","eigenvectorCentrality","betweennessCentrality","edgeBetweennessCentrality","connectedComponents","weaklyConnectedComponents","stronglyConnectedComponents","condensation","kCoreDecomposition","triangleCount","isBipartite","isGraphIsomorphic","findAllIsomorphisms","hierarchicalClustering","teraHAC","syncClustering","maximumBipartiteMatching","greedyBipartiteMatching","commonNeighborsScore","adamicAdarScore","commonNeighborsPrediction","adamicAdarPrediction","commonNeighborsForPairs","adamicAdarForPairs","topCandidatesForNode","topAdamicAdarCandidatesForNode","evaluateCommonNeighbors","evaluateAdamicAdar","compareAdamicAdarWithCommonNeighbors"]),Ga=new Set(["hasCycle","topologicalSort","bidirectionalDijkstra","aStar","allPairsShortestPath","degrees","nodeClosenessCentrality","isGraphIsomorphic","findAllIsomorphisms","modularity","hierarchicalClustering","kruskalMST","primMST","maximumBipartiteMatching","greedyBipartiteMatching","commonNeighborsScore","adamicAdarScore","commonNeighborsPrediction","adamicAdarPrediction","commonNeighborsForPairs","adamicAdarForPairs","topCandidatesForNode","topAdamicAdarCandidatesForNode","evaluateCommonNeighbors","evaluateAdamicAdar","compareAdamicAdarWithCommonNeighbors"]),se={maxIterations:100,tolerance:1e-6},xe={...se,dampingFactor:.85},Ha={pageRank:xe,personalizedPageRank:xe,deltaPageRank:xe,eigenvectorCentrality:se,hits:se,katzCentrality:{...se,alpha:.1,beta:1},labelPropagation:{maxIterations:100},labelPropagationSynchronous:{maxIterations:100},labelPropagationSemiSupervised:{maxIterations:100},allPairsShortestPath:{paths:!0},maxFlow:{algorithm:"edmonds-karp"},minSTCut:{algorithm:"ford-fulkerson"}},zo=new Set(["topologicalSort","stronglyConnectedComponents","condensation","deltaPageRank"]);function Yt(t,e){return t.cy.collection(e)}function Pe(t){if(t.s.weights?.some(e=>e<0)===!0)throw Object.assign(new Error(`${t.name}: a weight is negative; use graphtyBellmanFord`),{code:"E_BAD_WEIGHT"})}function G(t,e,n){const r=Rt(t.cs,e,`${t.name}: ${n}`);if(r===void 0)throw new Error(`${t.name}: the ${n} option is required`);return r}function At(t,e,n=!1){const r=n?t.cs.edges:t.cs.nodes,s=(typeof e=="string"?r.filter(e):e)[0];if(s===void 0)return;if(!n){const a=t.s.ids.indexOf(s.id());return a===M?void 0:a}const i=t.cs.edges.toArray().indexOf(s);return i<0?void 0:i}function Q(t,e,n=!1){return r=>{const o=At(t,r,n);return o===void 0?void 0:e[o]}}function Xt(t,e){return Yt(t,Array.from(e,n=>t.cs.nodes[n]))}function He(t,e){return Yt(t,Array.from(e,n=>t.cs.edges[n]))}function de(t,e,n){const r=[];for(let o=0;o<e.length;o++)o>0&&r.push(t.cs.edges[n[o-1]]),r.push(t.cs.nodes[e[o]]);return Yt(t,r)}function Pt(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);return{sources:e.map(r=>G(t,r[0],n)),targets:e.map(r=>G(t,r[1],n))}}function ee(t,e){if(e===void 0)return;const n=ct(t.s.nodeCount);for(const r of ft(t.cs,e))bt(n,r,!0);return n}function gt(t,e,n,r=!1){return t.field!==void 0&&Qt(r?t.cs.edges:t.cs.nodes,e,t.field),{score:Q(t,e,r),...n}}function Z(t,e,n){const r=[];for(let s=0;s<e.length;s++){const i=e[s];if(i!==M){const a=r[i]??[];a.push(s),r[i]=a}}t.field!==void 0&&Qt(t.cs.nodes,e,t.field);const o=Array.from(r,s=>Xt(t,s??[]));return Object.assign(o,n,{cluster:s=>{const i=At(t,s);return i===void 0||e[i]===M?void 0:e[i]}})}function Ne(t,e,n){const r=[];for(let s=0;s<e.visitedCount;s++){const i=e.order[s],a=e.parent[i];if(a!==M){const l=t.cs.nodes[a],c=t.cs.nodes[i],d=(t.s.directed?l.edgesTo(c):l.edgesWith(c)).intersection(t.cs.edges)[0];d!==void 0&&r.push(d)}r.push(t.cs.nodes[i])}const o=n!==void 0&&e.depth[n]!==M;return t.field!==void 0&&Qt(t.cs.nodes,Array.from(e.depth,s=>s===M?Number.NaN:s),t.field),{path:Yt(t,r),found:Yt(t,o?[t.cs.nodes[n]]:[]),depth:s=>{const i=At(t,s);return i===void 0||e.depth[i]===M?void 0:e.depth[i]},parent:s=>{const i=At(t,s);return i===void 0||e.parent[i]===M?void 0:t.cs.nodes[e.parent[i]]}}}function jn(t,e){return t.field!==void 0&&Qt(t.cs.nodes,e.dist,t.field),{distanceTo:n=>{const r=At(t,n);return r===void 0?1/0:e.dist[r]},pathTo:n=>{const r=At(t,n);return r===void 0?t.cy.collection():de(t,e.pathTo(r),e.pathEdges(r))}}}function Ke(t,e,n,r){const o=[],s=[];for(let i=0;i<t.s.nodeCount;i++)(L(e,i)?o:s).push(i);return t.field!==void 0&&Qt(t.cs.nodes,Array.from({length:t.s.nodeCount},(i,a)=>L(e,a)?0:1),t.field),{value:r,cut:He(t,n),partitionFirst:Xt(t,o),partitionSecond:Xt(t,s)}}function Fe(t,e){return Ke(t,e.side,e.cutEdges,e.cutValue)}function ne(t,e){return Array.from(e.scores,(n,r)=>({source:t.cs.nodes[e.sources[r]],target:t.cs.nodes[e.targets[r]],score:n}))}function Gn(t,e,n){return r=>{const o=At(t,r);return o===void 0?void 0:e.nodes[n[o]]}}function Hn(t,e,n){const{nodeMatch:r,edgeMatch:o}=n;return{...t.rest,nodeMatch:r&&((s,i)=>r(t.cs.nodes[s],e.nodes[i])),edgeMatch:o&&((s,i)=>o(t.cs.edges[s],e.edges[i]))}}function Kn(t,e){const n=e.other;if(n===void 0)throw new Error(`${t.name}: the other option (the collection to compare with) is required`);let r=n;if(typeof n=="string"?r=t.cy.$(n):Array.isArray(n)&&(r=t.cy.collection(n)),typeof r?.nodes!="function")throw new TypeError(`${t.name}: the other option must be a Cytoscape collection, a selector or an array of elements`);return fn(r,{directed:t.s.directed})}function ie(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);if(typeof e=="function")return Float64Array.from(t.cs.nodes.toArray(),o=>e(o));const r=new Float64Array(t.s.nodeCount);for(const o of ft(t.cs,e))r[o]=1;return r}function Yn(t,e){const n=ie(t,e,"personalization");if(!n.some(r=>r>0))throw new Error(`${t.name}: personalization ${typeof e=="function"?"gives no node a positive weight":"matches no node"}`);return n}function Xn(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);const r=new Uint32Array(t.s.nodeCount).fill(M);if(typeof e=="string"){const o=new Map;t.cs.nodes.forEach((s,i)=>{const a=s.data(e);a!==void 0&&(r[i]=o.get(a)??o.set(a,o.size).size-1)})}else e.forEach((o,s)=>{for(const i of ft(t.cs,o))r[i]=s});if(r.every(o=>o===M))throw new Error(typeof e=="string"?`${t.name}: ${n} names the data field "${e}", which no node has`:`${t.name}: ${n} matches no node`);return r}function Ka(t,e){const n=(r,o)=>{const s=At(t,r),i=At(t,o);return s===void 0||i===void 0?void 0:[s,i]};return{hasNegativeWeightCycle:e.hasNegativeCycle,distance:(r,o)=>{const s=n(r,o);return s===void 0?1/0:e.dist[s[0]*e.n+s[1]]},path:(r,o)=>{const{pathTo:s,pathEdges:i}=e;if(s===void 0||i===void 0)throw new Error("allPairsShortestPath: pass paths: true to walk shortest paths");const a=n(r,o);return a===void 0?t.cy.collection():de(t,s(a[0],a[1]),i(a[0],a[1]))}}}function Zn(t,e){return gt(t,e.scores,{rank:Q(t,e.scores),iterations:e.iterations,converged:e.converged})}function Jn(t,e){return Z(t,e.labels,{iterations:e.iterations,converged:e.converged})}function Qn(t){return t.topK!==void 0&&t.topK<=0?1/0:t.topK}function tr(t,e){const n=new Uint32Array(t.s.nodeCount).fill(M);return e.matching.forEach((r,o)=>{r!==M&&(n[o]=r,n[r]=o)}),{size:e.size,mate:r=>{const o=At(t,r);return o===void 0||n[o]===M?void 0:t.cs.nodes[n[o]]}}}function Ya(t){let e=0;for(let n=1;n<t.length;n++)t[n]>t[e]&&(e=n);return e}const gn={breadthFirstSearch:(t,e)=>{const n=Rt(t.cs,e.goal,`${t.name}: goal`);return X(t.call("breadthFirstSearch",t.s,G(t,e.root,"root"),{...t.rest,target:n}),r=>Ne(t,r,n))},directionOptimizedBfs:(t,e)=>Ne(t,Fs(t.s,G(t,e.root,"root"),t.rest),void 0),depthFirstSearch:(t,e)=>{const n=Rt(t.cs,e.goal,`${t.name}: goal`);if(n!==void 0&&e.order==="post")throw new Error(`${t.name}: goal stops only a pre-order walk; leave it out with order "post"`);return Ne(t,Vr(t.s,G(t,e.root,"root"),{...t.rest,target:n}),n)},hasCycle:(t,e={})=>$s(t.s),topologicalSort:(t,e={})=>{const n=qs(t.s);return n===null?null:Xt(t,n)},dijkstra:(t,e)=>(Pe(t),X(t.call("sssp",t.s,G(t,e.root,"root"),t.rest),n=>jn(t,n))),bellmanFord:(t,e)=>X(t.call("bellmanFord",t.s,G(t,e.root,"root"),t.rest),n=>({...jn(t,n),hasNegativeWeightCycle:n.hasNegativeCycle})),bidirectionalDijkstra:(t,e)=>{Pe(t);const n=Vi(t.s,G(t,e.root,"root"),G(t,e.goal,"goal"),t.rest);return{found:n.path.length>0,distance:n.distance,path:de(t,n.path,n.edges)}},aStar:(t,e)=>{const n=e.heuristic;Pe(t);const r=Li(t.s,G(t,e.root,"root"),G(t,e.goal,"goal"),n===void 0?()=>0:o=>n(t.cs.nodes[o]),t.rest);return{found:r.path.length>0,distance:r.distance,path:de(t,r.path,r.edges)}},allPairsShortestPath:(t,e={})=>X(t.call("allPairsShortestPath",t.s,t.rest),n=>Ka(t,n)),degreeCentrality:(t,e={})=>{const n=_s(t.s,t.rest);return gt(t,n,{degree:Q(t,n)})},degrees:(t,e={})=>{const n=Dr(t.s);if(!t.s.directed){const r=Float64Array.from(n.inDegree,(o,s)=>o+n.outDegree[s]);return{indegree:Q(t,r),outdegree:Q(t,r)}}return{indegree:Q(t,n.inDegree),outdegree:Q(t,n.outDegree)}},pageRank:(t,e={})=>{const n=e.initialRanks&&ie(t,e.initialRanks,"initialRanks");return X(t.call("pageRank",t.s,{...t.rest,initialRanks:n,weighted:t.weighted}),r=>Zn(t,r))},personalizedPageRank:(t,e)=>{const n=Yn(t,e.personalization),r=e.initialRanks&&ie(t,e.initialRanks,"initialRanks");return X(t.call("personalizedPageRank",t.s,n,{...t.rest,initialRanks:r,weighted:t.weighted}),o=>Zn(t,o))},deltaPageRank:(t,e={})=>{const n=e.personalization===void 0?void 0:Yn(t,e.personalization),r=e.priority===!0&&e.maxIterations===void 0?{maxIterations:1/0}:{},o={...t.rest,...r,personalization:n,weighted:t.weighted},s=e.priority===!0?new Ts(t.s).computeWithPriority(o):new Os(t.s).compute(o);if(s.some(i=>!Number.isFinite(i)))throw new RangeError(`${t.name}: the ranks overflowed (maxIterations ${String(t.rest.maxIterations)} is too high for this graph); lower maxIterations, or pass priority: true`);return gt(t,s,{rank:Q(t,s)})},eigenvectorCentrality:(t,e={})=>{const n=e.startVector&&ie(t,e.startVector,"startVector");return X(t.call("eigenvectorCentrality",t.s,{...t.rest,startVector:n}),r=>gt(t,r.scores,{iterations:r.iterations,converged:r.converged}))},katzCentrality:(t,e={})=>X(t.call("katzCentrality",t.s,{...t.rest,weighted:t.weighted}),n=>gt(t,n.scores,{iterations:n.iterations,converged:n.converged})),hits:(t,e={})=>X(t.call("hits",t.s,{...t.rest,weighted:t.weighted}),n=>gt(t,n.authorities,{hub:Q(t,n.hubs),authority:Q(t,n.authorities),iterations:n.iterations,converged:n.converged})),closenessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("closenessCentrality",t.s,{...t.rest,sources:n,weighted:t.weighted}),r=>gt(t,r.scores,{closeness:Q(t,r.scores)}))},nodeClosenessCentrality:(t,e)=>ks(t.s,G(t,e.root,"root"),{...t.rest,weighted:t.weighted}),betweennessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("betweennessCentrality",t.s,{...t.rest,sources:n}),r=>{const o=t.s.nodeCount,s=e.endpoints===!0?o*(o-1):(o-1)*(o-2),i=t.s.directed?1:2,a=e.normalized===!0&&s>0?s:i,l=Float64Array.from(r.scores,u=>u*a),c=l.reduce((u,d)=>Math.max(u,d),0);return gt(t,r.scores,{betweenness:Q(t,l),betweennessNormalized:Q(t,l.map(u=>c===0?0:u/c))})})},edgeBetweennessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("edgeBetweennessCentrality",t.s,{...t.rest,sources:n}),r=>gt(t,r.scores,{},!0))},connectedComponents:(t,e={})=>X(t.call("connectedComponents",t.s),n=>Z(t,n.labels,{})),weaklyConnectedComponents:(t,e={})=>X(t.call("weaklyConnectedComponents",t.s),n=>Z(t,n.labels,{})),stronglyConnectedComponents:(t,e={})=>Z(t,dn(t.s).labels,{}),condensation:(t,e={})=>{const n=Gi(t.s);return Z(t,n.components.labels,{condensed:n.condensed})},kCoreDecomposition:(t,e={})=>{const n=Qr(t.s),r=o=>Xt(t,Array.from(n.coreness.keys()).filter(s=>n.coreness[s]>=o));return gt(t,n.coreness,{maxCore:n.maxCore,core:r})},triangleCount:(t,e={})=>X(t.call("triangleCount",t.s),n=>gt(t,n.perNode,{coefficient:Q(t,n.coefficient),total:n.total,transitivity:n.transitivity})),isBipartite:(t,e={})=>{const n=kr(t.s,t.rest);if(n.sides===null){const s=t.cy.collection();return{bipartite:!1,partitionFirst:s,partitionSecond:s}}const{partitionFirst:r,partitionSecond:o}=Ke(t,n.sides,[],0);return{bipartite:!0,partitionFirst:r,partitionSecond:o}},isGraphIsomorphic:(t,e)=>{const n=Kn(t,e),r=fi(t.s,n.snapshot,Hn(t,n,e)),o=r.mapping;return{isomorphic:r.isomorphic,mapping:o===null?()=>{}:Gn(t,n,o)}},findAllIsomorphisms:(t,e)=>{const n=Kn(t,e);return hi(t.s,n.snapshot,Hn(t,n,e)).map(r=>Gn(t,n,r))},modularity:(t,e)=>un(t.s,Xn(t,e.clusters,"clusters"),t.rest),louvain:(t,e={})=>{const n=fo(t.s,t.rest);return Z(t,n.labels,{modularity:n.modularity,iterations:n.iterations})},leiden:(t,e={})=>{const n=ro(t.s,t.rest);return Z(t,n.labels,{modularity:n.modularity,iterations:n.iterations})},labelPropagation:(t,e={})=>X(t.call("labelPropagation",t.s,{...t.rest,weighted:t.weighted}),n=>Jn(t,n)),labelPropagationSynchronous:(t,e={})=>X(t.call("labelPropagationSynchronous",t.s,{...t.rest,weighted:t.weighted}),n=>Jn(t,n)),labelPropagationSemiSupervised:(t,e)=>{const n=Js(t.s,Xn(t,e.seeds,"seeds"),{...t.rest,weighted:t.weighted});return Z(t,n.labels,{iterations:n.iterations,converged:n.converged})},girvanNewman:(t,e={})=>{const n=Xr(t.s,t.rest),r={...t,field:void 0},o=i=>Z(r,n.levels[i]??[],{}),s=Ya(n.modularity);return Z(t,n.levels[s]??[],{modularity:n.modularity[s]??Number.NaN,levels:n.levels.length,level:o,modularities:Array.from(n.modularity)})},markovClustering:(t,e={})=>{const n=$i(t.s,t.rest);return Z(t,n.labels,{iterations:n.iterations,converged:n.converged})},spectralClustering:(t,e)=>{if(e.k===void 0)throw new Error(`${t.name}: the k option is required`);const n=ea(t.s,t.rest);return Z(t,n.labels,{converged:n.converged,eigenvalues:n.eigenvalues})},hierarchicalClustering:(t,e={})=>{const n=ci(t.s,t.rest),r={...t,field:void 0},o=i=>{const a=new Uint32Array(t.s.nodeCount).fill(M);return n.cut(i).forEach((l,c)=>{for(const u of l)a[u]=c}),Z(r,a,{})},s=i=>Z(r,n.cutAt(i).labels,{});return{merges:n.left.length,cut:o,cutAt:s}},teraHAC:(t,e={})=>{const n=wa(t.s,t.rest);return Z(t,n.labels,{merges:n.merges})},grsbm:(t,e={})=>Z(t,si(t.s,{...t.rest,weighted:t.weighted}).labels,{}),syncClustering:(t,e)=>{if(e.numClusters===void 0)throw new Error(`${t.name}: the numClusters option is required`);const n=da(t.s,t.rest);return Z(t,n.labels,{loss:n.loss,iterations:n.iterations,converged:n.converged})},kruskalMST:(t,e={})=>{const n=mo(t.s);return Object.assign(t.cs.nodes.union(He(t,n.edges)),{totalWeight:n.totalWeight})},primMST:(t,e={})=>{const n=Rt(t.cs,e.root,`${t.name}: root`),r=yo(t.s,{...t.rest,start:n});return Object.assign(t.cs.nodes.union(He(t,r.edges)),{totalWeight:r.totalWeight})},maxFlow:(t,e)=>{const n=sn(t.s,G(t,e.source,"source"),G(t,e.sink,"sink"),t.rest);return{...Ke(t,n.sourceSide,n.cutEdges,n.maxFlow),flow:Q(t,n.flow,!0)}},minSTCut:(t,e)=>Fe(t,jr(t.s,G(t,e.source,"source"),G(t,e.sink,"sink"),t.rest)),stoerWagner:(t,e={})=>Fe(t,wo(t.s)),kargerMinCut:(t,e={})=>Fe(t,po(t.s,t.rest)),maximumBipartiteMatching:(t,e={})=>tr(t,go(t.s,{...t.rest,left:ee(t,e.left),right:ee(t,e.right)})),greedyBipartiteMatching:(t,e={})=>tr(t,Bi(t.s,{...t.rest,left:ee(t,e.left),right:ee(t,e.right)})),commonNeighborsScore:(t,e)=>Ss(t.s,G(t,e.source,"source"),G(t,e.target,"target"),{directed:t.s.directed}),adamicAdarScore:(t,e)=>Ii(t.s,G(t,e.source,"source"),G(t,e.target,"target"),{directed:t.s.directed}),commonNeighborsPrediction:(t,e={})=>ne(t,co(t.s,{...t.rest,directed:t.s.directed,uniquePairs:!0})),adamicAdarPrediction:(t,e={})=>ne(t,uo(t.s,{...t.rest,directed:t.s.directed,uniquePairs:!0})),commonNeighborsForPairs:(t,e)=>Array.from(bi(t.s,Pt(t,e.pairs,"pairs"),{directed:t.s.directed})),adamicAdarForPairs:(t,e)=>Array.from(xi(t.s,Pt(t,e.pairs,"pairs"),{directed:t.s.directed})),topCandidatesForNode:(t,e)=>{const n=e.candidates===void 0?void 0:ft(t.cs,e.candidates);return ne(t,Ci(t.s,G(t,e.root,"root"),{...t.rest,topK:Qn(e),candidates:n,directed:t.s.directed}))},topAdamicAdarCandidatesForNode:(t,e)=>{const n=e.candidates===void 0?void 0:ft(t.cs,e.candidates);return ne(t,Pi(t.s,G(t,e.root,"root"),{...t.rest,topK:Qn(e),candidates:n,directed:t.s.directed}))},evaluateCommonNeighbors:(t,e)=>Fn(t.s,Pt(t,e.edges,"edges"),Pt(t,e.nonEdges,"nonEdges"),{directed:t.s.directed}),evaluateAdamicAdar:(t,e)=>kn(t.s,Pt(t,e.edges,"edges"),Pt(t,e.nonEdges,"nonEdges"),{directed:t.s.directed}),compareAdamicAdarWithCommonNeighbors:(t,e)=>{const n=Pt(t,e.edges,"edges"),r=Pt(t,e.nonEdges,"nonEdges"),o={directed:t.s.directed};return{adamicAdar:kn(t.s,n,r,o),commonNeighbors:Fn(t.s,n,r,o)}}};function Ct(t){return`graphty${t.charAt(0).toUpperCase()}${t.slice(1)}`}const Rc=Object.keys(gn).map(Ct),wn=["breadthFirstSearch","dijkstra","bellmanFord","allPairsShortestPath","pageRank","personalizedPageRank","eigenvectorCentrality","katzCentrality","hits","closenessCentrality","betweennessCentrality","edgeBetweennessCentrality","connectedComponents","weaklyConnectedComponents","triangleCount","labelPropagation","labelPropagationSynchronous"],Dc=wn.map(t=>`${Ct(t)}Async`);function Bo(t,e,n,r){const o=Ct(e);if(typeof n!="object"||n===null||Array.isArray(n))throw new TypeError(`${o}: takes one options object; pass a weight function as { weight: fn }`);if(n.weight!==void 0&&ja.has(e))throw new Error(`${o}: this algorithm reads no edge weights; remove the weight option`);if(n.field!==void 0&&Ga.has(e))throw new Error(`${o}: this algorithm has no per-element value to write; remove the field option`);const s=Ma[o]??[];for(const[h,g]of Object.entries(n))if(g!==void 0&&!s.includes(h))throw new Error(`${o}: unknown option ${h}; the options are ${s.join(", ")}`);const i=n.directed??zo.has(e),{mode:a}=n;if((a==="in"||a==="out")&&!i)throw new Error(`${o}: mode: "${a}" needs directed: true`);const l=fn(t,{directed:i,weight:n.weight}),c={...Ha[e],...Object.fromEntries(Object.entries(n).filter(([h,g])=>!La.has(h)&&g!==void 0))},u={name:o,cy:pe(t),cs:l,s:l.snapshot,field:n.field,weighted:n.weight!==void 0,rest:c,call:(h,...g)=>r[h](...g)},d=h=>{throw Xa(h,e)};let f;try{f=gn[e](u,n)}catch(h){d(h)}return{result:f instanceof Promise?f.catch(d):f,snapshot:l.snapshot}}function Xa(t,e){if(!(t instanceof Error))return t;const n=Ct(e),{code:r}=t;if(r==="E_NEEDS_UNDIRECTED"){const o=e==="connectedComponents"?", or call graphtyWeaklyConnectedComponents":"";t.message=`${n}: needs an undirected graph; leave out directed: true${o}`}else r==="E_NEEDS_DIRECTED"?t.message=`${n}: needs a directed graph; ${zo.has(e)?"leave out directed: false":"pass directed: true"}`:t.message.startsWith(`${n}:`)||(t.message=`${n}: ${t.message}`);return t}function er(t,e,n={}){if(hn(Ct(e),n?.gpu),n.gpu==="require"){const r=Ct(e);throw new Error(wn.includes(e)?`${r}: runs on the CPU; call ${r}Async for the GPU`:`${r}: has no GPU implementation and runs only on the CPU; leave out gpu: "require"`)}return Bo(t,e,n,Wa).result}async function nr(t,e,n={}){hn(`${Ct(e)}Async`,n?.gpu);const r=pe(t),o=await $o(r,n.gpu);if(r.destroyed())throw Kt();const s=o.gpu===null?null:Va(o.gpu.accelerator);je(o,t.nodes().length);const{result:i,snapshot:a}=Bo(t,e,n,_a(s?.accelerator??null)),l=_o(a);let c;try{c=await i}catch(d){throw r.destroyed()?Kt():d}finally{l(),o.gpu!==null&&typeof n.weight=="function"&&o.gpu.accelerator.release(a)}const u=Ge(o,s?.used()??!1,"the options or the graph need the CPU implementation");return Object.assign(c,{backend:u})}function Za(t){for(const e of Object.keys(gn)){const n=Ct(e);t("collection",n,function(r){return er(this,e,r)}),t("core",n,function(r){return er(this.elements(),e,r)})}for(const e of wn){const n=`${Ct(e)}Async`;t("collection",n,function(r){return nr(this,e,r)}),t("core",n,function(r){return nr(this.elements(),e,r)})}}class Ja extends Error{constructor(e){super(`graphty: the core already has an element with id "${e}", so the new graph would merge into it; remove the existing elements (cy.elements().remove()) or add the graph to an empty core`),this.code="E_ID_TAKEN",this.name="IdTakenError",this.id=e}}function ke(t,e){if(t.destroyed())throw Kt();for(const n of e)if(n.group==="nodes"&&n.data.id!==void 0&&t.getElementById(n.data.id).nonempty())throw new Ja(n.data.id);for(const n of e)n.group==="edges"&&n.data.id!==void 0&&t.getElementById(n.data.id).nonempty()&&delete n.data.id;return t.add(e)}function Qa(t){const e=new Set(t.elements("[graphty_import]").map(r=>String(r.data("graphty_import"))));let n=e.size+1;for(;e.has(String(n));)n++;return String(n)}function tl(t,e,n){let r=0;e.forEach((o,s)=>{const i=t[r];if(i?.id()!==o.data.id)return;r++;const a=n[s];a!=null&&i.style(a)})}function el(t){t("core","graphtyGenerate",async function(n,r){const{generateElements:o}=await Ht(Gt(()=>import("./samples-BkLzaDJ3.js"),[],import.meta.url),"graphtyGenerate"),s=o(n,r);return{elements:ke(this,s.elements),directed:s.directed}}),t("core","graphtyDataset",async function(n,r){const{datasetElements:o}=await Ht(Gt(()=>import("./samples-BkLzaDJ3.js"),[],import.meta.url),"graphtyDataset"),s=await o(n,r);return r?.signal?.throwIfAborted(),{elements:ke(this,s.elements),directed:s.directed}}),t("core","graphtyImport",async function(n,r,o){const{importElements:s}=await Ht(Gt(()=>import("../io.js"),[],import.meta.url),"graphtyImport"),i=o?.styles!==void 0&&o.styles!==!1,a=await s(n,r,{...o,...i&&o.importId===void 0?{importId:Qa(this)}:{}}),l=o?.applyStyles===!0&&a.stylesheet!==void 0,c=ke(this,a.elements);l&&a.bypass!==void 0&&tl(c,a.elements,a.bypass);const u=this.style();if(l&&a.stylesheet!==void 0&&u!==void 0){let d=u;for(const f of a.stylesheet)d=d.selector(f.selector).style(f.style);d.update()}return{elements:c,directed:a.directed,format:a.format,report:a.report,...a.stylesheet===void 0?{}:{stylesheet:a.stylesheet}}});const e=async(n,r,o)=>{const{exportElements:s}=await Ht(Gt(()=>import("../io.js"),[],import.meta.url),"graphtyExport");return s(n,r,o)};t("core","graphtyExport",function(n,r){return e(this.elements(),n,r)}),t("collection","graphtyExport",function(n,r){return e(this,n,r)})}function nl(t,e,n,r){const{positions:o,dim:s,n:i}=t,a=new Float32Array(3*i),l=[0,0,0];for(let c=0;c<i;c++)for(let u=0;u<3;u++)a[3*c+u]=u<s?o[s*c+u]*e+l[u]:l[u];return a}function rl(t,e,n){const r=new Float64Array(n),o=new Float64Array(n);for(let s=0;s<e*n;s++){const i=t[s];Number.isNaN(i)||(r[s%n]+=i,o[s%n]++)}for(let s=0;s<n;s++)r[s]=o[s]>0?r[s]/o[s]:0;return r}function ol(t,e,n,r){let o=0;for(let s=0;s<e;s++){let i=0;for(let a=0;a<n;a++){const l=t[n*s+a]-r[a];Number.isNaN(l)||(i+=l*l)}o=Math.max(o,Math.sqrt(i))}return o}function Mt(t,e,n=1,r){const o=Math.floor(t.length/e),s=rl(t,o,e),i=ol(t,o,e,s),a=i>0?n/i:0;for(let l=0;l<o*e;l++){const c=t[l],u=r?.[l%e]??0;Number.isNaN(c)||(t[l]=a===0?u:(c-s[l%e])*a+u)}return t}const Se=34359738337,sl=185852,il=1;class qo{constructor(e){this.seed=e===null||e===0||Number.isNaN(e)?Math.floor(Math.random()*1e6):e,this.state=this.seed%Se}next(){return this.state=(sl*this.state+il)%Se,this.state/Se}}function al(t){const e=[0,0,0];if(t===null)return e;for(let n=0;n<3&&n<t.length;n++){const r=t[n];if(!Number.isFinite(r))throw new RangeError(`center[${n}] is not finite`);e[n]=r}return e}function me(t,e,n,r,o,s,i){const a=t.nodeCount;if(r!==2&&r!==3)throw new RangeError(`dim must be 2 or 3, got ${String(r)}`);if(e.length!==3*a)throw new RangeError(`positions has ${e.length} entries, expected ${3*a}`);if(!Number.isFinite(o)||o<=0)throw new RangeError(`scale must be a finite number > 0, got ${o}`);const l=al(s),c=[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY],u=[Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY];let d=0,f=0;for(let p=0;p<a;p++){let y=!0;for(let v=0;v<r;v++){const E=e[3*p+v];Number.isFinite(E)?(E<c[v]&&(c[v]=E),E>u[v]&&(u[v]=E)):y=!1}y?d++:f++}if(f===0)return;const h=i==="fr"?0:-1,g=1,w=[h,h,h],m=[g,g,g];if(d>0){for(let p=0;p<r;p++)if(Number.isFinite(c[p])&&Number.isFinite(u[p])&&(w[p]=(c[p]-l[p])/o,m[p]=(u[p]-l[p])/o,w[p]===m[p])){const y=(g-h)/2;w[p]-=y,m[p]+=y}}const A=new qo(n);for(let p=0;p<a;p++){let y=!1;for(let v=0;v<r;v++)Number.isFinite(e[3*p+v])||(y=!0);if(y){for(let v=0;v<r;v++){const E=3*p+v;if(!Number.isFinite(e[E])){const b=A.next(),C=w[v]+b*(m[v]-w[v]);e[E]=C*o+l[v]}}r===2&&(e[3*p+2]=l[2])}}}const rr=new WeakMap;function ll(t){if(t!=null)return typeof t=="number"?t:typeof t=="string"?Number(t):Number.NaN}function Lt(t,e=null){if(ps(t)){if(!t.directed)return t;const r=rr.get(t);if(r!==void 0)return r;const o=t.toUndirected().snapshot;return rr.set(t,o),o}const n=new ms({directed:!1,weighted:"auto"});if(Array.isArray(t))n.addNodes(t);else{n.addNodes(t.nodes());for(const[r,o]of t.edges()){const s=e!==null&&t.getEdgeData!==void 0?t.getEdgeData(r,o,e):void 0;n.addEdge(r,o,ll(s))}}return n.freeze()}function H(t,e,n){return Object.assign(t,{code:e,params:n})}function pt(t,e){const n=e.dim??2;if(n!==2&&n!==3)throw H(new Error(`dim must be 2 or 3, got ${String(n)}`),"E_BAD_OPTION",{option:"dim",value:n});const r=Array.from({length:n},(o,s)=>e.center?.[s]??0);return{n:t.nodeCount,dim:n,scale:e.scale??1,center:r}}function ht(t,e,n){return{positions:Float32Array.from(t),dim:e,n}}function It(t,e,n){if(e===2)return t;const r=t.length/2,o=new Float64Array(3*r);for(let s=0;s<r;s++)o[3*s]=t[2*s],o[3*s+1]=t[2*s+1],o[3*s+2]=Number.isNaN(t[2*s])?Number.NaN:n[2];return o}function Vo(t){const e=t??2;if(e!==2&&e!==3)throw H(new RangeError(`dim must be 2 or 3, got ${String(e)}`),"E_BAD_OPTION",{option:"dim",value:e});return e}function cl(t,e,n){const r=t.nodeCount;if(e==null)return new Float32Array(3*r).fill(Number.NaN);if(e.length!==n*r)throw H(new RangeError(`pos has ${e.length} values, expected ${n*r} (dim * nodeCount)`),"E_BAD_OPTION",{option:"pos",length:e.length,expected:n*r});return nl({positions:e,dim:n,n:r},1)}function ul(t,e={}){const{scaling:n=1,a:r=1.1,maxIter:o=1e3}=e;if(r<=1)throw H(new Error("The parameter a should be larger than 1"),"E_BAD_OPTION",{option:"a",value:r});const s=Lt(t),i=Vo(e.dim),a=s.nodeCount,l=Float64Array.from(cl(s,e.pos,i));me(s,l,e.seed??null,i,1,null,"fr");const c=new Float64Array(i*a);for(let y=0;y<a;y++)for(let v=0;v<i;v++)c[i*y+v]=l[3*y+v];const{rowPtr:u,colIdx:d}=s,f=n*Math.sqrt(a),h=.001,g=1e-6,w=new Float64Array(i*a),m=new Float64Array(i),A=new Int32Array(a);let p=g+1;for(let y=0;p>g&&y<o;y++){w.fill(0);for(let v=0;v<a;v++){for(let E=u[v];E<u[v+1];E++)A[d[E]]=v+1;for(let E=0;E<a;E++){if(v===E)continue;let b=0;for(let _=0;_<i;_++)m[_]=c[i*v+_]-c[i*E+_],b+=m[_]*m[_];const C=Math.sqrt(b)||.01,x=A[E]===v+1?r:1;for(let _=0;_<i;_++)w[i*v+_]+=f/C*m[_]-x*m[_]}}p=0;for(let v=0;v<a;v++){let E=0;for(let b=0;b<i;b++)c[i*v+b]+=w[i*v+b]*h,E+=w[i*v+b]*w[i*v+b];p+=Math.sqrt(E)}}return(e.scale!==void 0||e.center!==void 0)&&Mt(c,i,e.scale??1,e.center),{positions:Float32Array.from(c),dim:i,n:a}}function pn(t,e,n,r,o,s,i="subsets"){if(r!=="vertical"&&r!=="horizontal")throw H(new Error("align must be either vertical or horizontal"),"E_BAD_OPTION",{option:"align",value:r});const a=new Float64Array(2*t).fill(Number.NaN);if(e.forEach((l,c)=>{for(let u=0;u<l.length;u++){const d=l[u];if(!Number.isInteger(d)||d<0||d>=t)throw H(new Error(`layer node index ${String(d)} is not a node of the graph`),"E_BAD_NODE",{option:i,index:d});[a[2*d],a[2*d+1]]=n(c,e.length,u,l.length)}}),r==="vertical")return Mt(a,2,o,s);Mt(a,2,o,[s[1],s[0]]);for(let l=0;l<t;l++)[a[2*l],a[2*l+1]]=[a[2*l+1],a[2*l]];return a}const Wo=(t,e,n,r)=>[t-(e-1)/2,n-(r-1)/2];function Lo(t,e,n){const r=t.nodes.get(e);if(r===null)throw H(new Error(`${n} names node column "${e}", which the snapshot does not hold`),"E_UNKNOWN_COLUMN",{option:n,column:e});if(r.dtype!=="u32"&&r.dtype!=="dict")throw H(new Error(`${n} column "${e}" must be u32 or dict, not ${r.dtype}`),"E_COLUMN_TYPE",{option:n,column:e,dtype:r.dtype,expected:["u32","dict"]});const o=r.dtype==="u32"?r.data:r.codes,s=new Map;for(let i=0;i<t.nodeCount;i++)if(r.isSet(i)){const a=s.get(o[i]);a===void 0?s.set(o[i],[i]):a.push(i)}return[...s.keys()].sort((i,a)=>i-a).map(i=>s.get(i)??[])}function dl(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{subsets:i="subset",align:a="vertical"}=e,l=typeof i=="string"?Lo(t,i,"subsets"):i.map(c=>fe(t,c));return ht(It(pn(n,l,Wo,a,o,s),r,s),r,n)}function fl(t,e){const n=t.nodeCount;if(!Number.isInteger(e)||e<0||e>=n)throw H(new Error(`start node ${String(e)} is not in the graph`),"E_BAD_NODE",{option:"start",index:e});const r=new Uint8Array(n);r[e]=1;let o=1;const s=[[e]];for(let i=s[0];;){const a=[];for(const l of i)for(let c=t.rowPtr[l];c<t.rowPtr[l+1];c++){const u=t.colIdx[c];r[u]===0&&(r[u]=1,a.push(u))}if(a.length===0)break;o+=a.length,s.push(a),i=a}if(o<n)throw Object.assign(new Error("bfs_layout didn't include all nodes. Graph may be disconnected."),{code:"E_NOT_CONNECTED",params:{nodeCount:n,reached:o}});return s}function hl(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{start:i=0,align:a="vertical"}=e,l=n===0?[]:fl(Lt(t),V(t,i));return ht(It(pn(n,l,Wo,a,o,s),r,s),r,n)}function gl(t,e,n,r,o,s,i){const a=r;return pn(t,[e,n],(l,c,u,d)=>[l*a-a/2,u/d-1/2],o,s,i)}function wl(t,e){const n=typeof e=="string"||e instanceof Uint32Array?e:hr(t,e);if(typeof n!="string"){if(n.length<Math.ceil(t.nodeCount/32))throw H(new Error(`top mask has ${n.length} words; ${t.nodeCount} nodes need ${Math.ceil(t.nodeCount/32)}`),"E_BAD_OPTION",{option:"top",length:n.length,expected:Math.ceil(t.nodeCount/32)});return o=>(n[o>>>5]>>>(o&31)&1)===1}const r=t.nodes.get(n);if(r===null)throw H(new Error(`top names node column "${n}", which the snapshot does not hold`),"E_UNKNOWN_COLUMN",{option:"top",column:n});if(r.dtype!=="bool")throw H(new Error(`top column "${n}" must be bool, not ${r.dtype}`),"E_COLUMN_TYPE",{option:"top",column:n,dtype:r.dtype,expected:["bool"]});return o=>r.isSet(o)&&(r.data[o>>>5]>>>(o&31)&1)===1}function or(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{top:i,align:a="vertical",aspectRatio:l=4/3}=e,c=i===void 0?f=>f%2===0:wl(t,i),u=[],d=[];for(let f=0;f<n;f++)(c(f)?u:d).push(f);return ht(It(gl(n,u,d,l,a,o,s),r,s),r,n)}const Zt={zeros:function(t){return typeof t=="number"?new Array(t).fill(0):t.length===1?new Array(t[0]).fill(0):new Array(t[0]).fill(0).map(()=>this.zeros(t.slice(1)))},ones:function(t){return typeof t=="number"?new Array(t).fill(1):t.length===1?new Array(t[0]).fill(1):new Array(t[0]).fill(1).map(()=>this.ones(t.slice(1)))},linspace:function(t,e,n){if(n===1)return[t];const r=(e-t)/(n-1);return Array.from({length:n},(o,s)=>t+s*r)},array:function(t){return Array.isArray(t)?[...t]:[t]},repeat:function(t,e){const n=[];for(let r=0;r<e;r++)n.push(...Zt.array(t));return n},mean:function(t,e=null){if(e===null){const n=Array.isArray(t[0])?t.flat(1/0):t;return n.reduce((o,s)=>o+s,0)/n.length}if(e===0){const n=[],r=t;for(let o=0;o<r[0].length;o++){let s=0;for(let i=0;i<r.length;i++)s+=r[i][o];n.push(s/r.length)}return n}return t.map(n=>Zt.mean(n))},add:function(t,e){return!Array.isArray(t)&&!Array.isArray(e)?t+e:Array.isArray(t)?Array.isArray(e)?t.map((n,r)=>n+e[r]):t.map(n=>n+e):e.map(n=>t+n)},subtract:function(t,e){return!Array.isArray(t)&&!Array.isArray(e)?t-e:Array.isArray(t)?Array.isArray(e)?t.map((n,r)=>n-e[r]):t.map(n=>n-e):e.map(n=>t-n)},max:function(t){return Array.isArray(t)?Math.max(...t.flat(1/0)):t},min:function(t){return Array.isArray(t)?Math.min(...t.flat(1/0)):t},norm:function(t){return Math.sqrt(t.reduce((e,n)=>e+n*n,0))}};function pl(t,e,n,r){const o=new Float64Array(t*e);if(t===1)o.set(r);else if(e===2){const s=Zt.linspace(0,2*Math.PI,t+1);for(let i=0;i<t;i++)o[2*i]=yt(s[i])*n+r[0],o[2*i+1]=wt(s[i])*n+r[1]}else{const s=(1+Math.sqrt(5))/2;for(let i=0;i<t;i++){const a=2*Math.PI*i/s,l=gr(1-2*(i+.5)/t);o[3*i]=wt(l)*yt(a)*n+r[0],o[3*i+1]=wt(l)*wt(a)*n+r[1],o[3*i+2]=yt(l)*n+r[2]}}return o}function ml(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(pl(n,r,o,s),r,n)}const J=Object.freeze({maxIter:100,jitterTolerance:1,scalingRatio:2,gravity:1,strongGravity:!1,distributedAction:!1,linlog:!1,dissuadeHubs:!1,dim:2,scale:1,settleThreshold:.001,settleWindow:10,iterationsPerStep:1}),ae=.01,yl=1e-4,jo=1e-8,Ot=256,sr=.9,Al=5,vl=1e4;function ir(t,e,n){if(n.meta.components!==1)throw new RangeError(`node column "${e}" has ${n.meta.components} components; a node vector has one`);switch(n.dtype){case"f32":case"f64":{const r=t.nodes.gpuView(e);return r instanceof Float32Array?r:new Float32Array(r)}case"u32":case"i32":return new Float32Array(t.nodes.gpuView(e));case"u8":return new Float32Array(n.data);default:throw new TypeError(`node column "${e}" is ${n.dtype}, not numeric`)}}function El(t,e,n){const r=e.nodeCount;if(t==null){const s=e.nodes.byRole("mass");if(s!==null)return ir(e,s.meta.name,s);const i=new Float32Array(r);for(let a=0;a<r;a++)i[a]=n(a);return i}if(t instanceof Float32Array){if(t.length!==r)throw new RangeError(`the node vector has ${t.length} values, expected ${r}`);return t}if(typeof t=="string"){const s=e.nodes.get(t);if(s===null)throw new RangeError(`the option names node column "${t}", which the snapshot does not hold`);return ir(e,t,s)}const o=new Float32Array(r);for(let s=0;s<r;s++){const i=t[e.ids.idOf(s)];o[s]=i===void 0?n(s):i}return o}function Go(t,e){if(t===!0)return e.weights;if(t===!1||t===null||t===void 0)return null;const n=e.edges.get(t);if(n===null)throw new RangeError(`weight names edge column "${t}", which the snapshot does not hold`);if(n.meta.components!==1)throw new RangeError(`edge column "${t}" has ${n.meta.components} components; a weight column has one`);switch(n.dtype){case"f32":case"f64":case"u32":case"i32":{const r=wr(e,e.edges.gpuView(t));return r instanceof Float32Array?r:new Float32Array(r)}default:throw new TypeError(`edge column "${t}" is ${n.dtype}; a weight column is f32, f64, u32 or i32`)}}function Nt(t,e,n,r,o){if(e===void 0)return n;if(typeof e!="number"||!r(e))throw new RangeError(`ForceAtlas2Simulation: ${t} must be ${o}, got ${String(e)}`);return e}function _e(t,e,n){if(e===void 0)return n;if(typeof e!="boolean")throw new TypeError(`ForceAtlas2Simulation: ${t} must be a boolean, got ${String(e)}`);return e}function bl(t){const e=[0,0,0];if(t===void 0)return e;if(t.length>3)throw new RangeError(`ForceAtlas2Simulation: center has ${t.length} components, at most 3 expected`);for(let n=0;n<t.length;n++){const r=t[n];if(typeof r!="number"||!Number.isFinite(r))throw new RangeError(`ForceAtlas2Simulation: center[${n}] is not finite`);e[n]=r}return e}function le(t){return Number.isInteger(t)&&t>=1}function Cl(t){const e=t??{},n=e.dim??J.dim;if(n!==2&&n!==3)throw new RangeError(`ForceAtlas2Simulation: dim must be 2 or 3, got ${String(n)}`);const r=e.compat??"paper";if(r!=="paper"&&r!=="networkx")throw new RangeError(`ForceAtlas2Simulation: compat must be "paper" or "networkx", got ${String(r)}`);return{maxIter:Nt("maxIter",e.maxIter,J.maxIter,le,"an integer >= 1"),jitterTolerance:Nt("jitterTolerance",e.jitterTolerance,J.jitterTolerance,o=>o>0,"> 0"),scalingRatio:Nt("scalingRatio",e.scalingRatio,J.scalingRatio,o=>o>0,"> 0"),gravity:Nt("gravity",e.gravity,J.gravity,o=>o>=0,">= 0"),strongGravity:_e("strongGravity",e.strongGravity,J.strongGravity),distributedAction:_e("distributedAction",e.distributedAction,J.distributedAction),linlog:_e("linlog",e.linlog,J.linlog),nodeMass:e.nodeMass??null,weight:e.weight??null,dim:n,scale:Nt("scale",e.scale,J.scale,o=>Number.isFinite(o)&&o>0,"a finite number > 0"),center:bl(e.center),settleThreshold:Nt("settleThreshold",e.settleThreshold,J.settleThreshold,o=>o>=0,">= 0"),settleWindow:Nt("settleWindow",e.settleWindow,J.settleWindow,le,"an integer >= 1"),iterationsPerStep:Nt("iterationsPerStep",e.iterationsPerStep,J.iterationsPerStep,le,"an integer >= 1"),compat:r,seed:e.seed??null}}function Me(t){return{attraction:new Float64Array(3*t),force:new Float64Array(3*t),swing:new Float64Array(t),traction:new Float64Array(t),sumX:new Float64Array(t),sumY:new Float64Array(t),sumZ:new Float64Array(t),sumW:new Float64Array(t),disp:new Float64Array(t),free:new Float64Array(t)}}function Ho(t){for(let e=Ot/2;e>0;e=Math.floor(e/2))for(let n=0;n<e;n++)t[n]=t[n]+t[n+e];return t[0]}function Ft(t,e){const n=Math.ceil(e/Ot),r=new Float64Array(n),o=new Float64Array(Ot);for(let s=0;s<n;s++){for(let i=0;i<Ot;i++){const a=s*Ot+i;o[i]=a<e?t[a]:0}r[s]=Ho(o)}return r}function kt(t){const e=new Float64Array(Ot);for(let n=0;n<t.length;n++){const r=n%Ot;e[r]=e[r]+t[n]}return Ho(e)}function vt(t,e,n){return Math.sqrt(t*t+e*e+n*n)}function Ko(t){let e=t>>>0;return e=(e^e>>>16)>>>0,e=Math.imul(e,2146121005)>>>0,e=(e^e>>>15)>>>0,e=Math.imul(e,2221713035)>>>0,e=(e^e>>>16)>>>0,e}function Il(t,e){const n=Math.min(t,e),r=Math.max(t,e);return Ko((Math.imul(n,2654435769)>>>0^r)>>>0)}function ar(t){return(t>>>8)*(1/16777216)}function Yo(t,e,n){const r=Il(t,e),o=6.283185307179586*ar(r);let s;if(n===2)s=[yt(o),wt(o),0];else{const i=2*ar(Ko((r^1540483477)>>>0))-1,a=Math.sqrt(Math.max(0,1-i*i));s=[a*yt(o),a*wt(o),i]}return t>e?[-s[0],-s[1],-s[2]]:s}function xl(t,e,n,r,o,s){const i=t,a=s,l=.05*Math.sqrt(i),c=Math.sqrt(l),u=10,d=Math.max(n,1e-30),f=Math.min(u,l*n/(i*i));let h=a*Math.max(c,f),g=o;e>2*d&&(g>.05&&(g=g*.5),h=Math.max(h,a));const w=e===0?1e30:h*g*n/e;return e>h*n?g>.05&&(g=g*.7):r<1e3&&(g=g*1.3),{speed:r+Math.min(w-r,.5*r),speedEfficiency:g}}function Pl(t,e,n,r){for(let o=0;o<n;o++){if(t[o]===e[o])continue;const s=Math.min(r,o*32+32);for(let i=o*32;i<s;i++)if(L(t,i)&&!L(e,i))return!0}return!1}class Nl{constructor(e){this.state="created",this.n=0,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.weights=null,this.mass=new Float64Array(0),this.owner=new Float32Array(0),this.positions=new Float64Array(0),this.oldForce=new Float64Array(0),this.fixed=null,this.scratch=Me(0),this.speedValue=1,this.speedEfficiencyValue=1,this.swingValue=1,this.tractionValue=1,this.settledCountValue=0,this.iterationsDoneValue=0,this.centroid=[0,0,0],this.pending=null,this.options=Cl(e),this.dim=this.options.dim,this.swingMode=this.options.compat==="networkx"?1:0,this.gravityCenter=this.options.compat==="networkx"?1:0}get settled(){return this.state!=="loaded"?!1:this.n===0||this.iterationsDoneValue>=this.options.maxIter||this.settledCountValue>=this.options.settleWindow}get iterationsDone(){return this.iterationsDoneValue}load(e,n){if(this.assertNotDisposed(),e.directed)throw new Error("ForceAtlas2Simulation: the snapshot must be undirected (pass toUndirected().snapshot)");const r=e.nodeCount;if(n.length!==3*r)throw new Error(`ForceAtlas2Simulation: positions.length ${n.length} !== 3 x ${r}`);const o=e.outDegree(),s=El(this.options.nodeMass,e,m=>o[m]+1),i=Go(this.options.weight,e),a=new Float64Array(r);for(let m=0;m<r;m++)if(a[m]=s[m],!(a[m]>0))throw new Error(`ForceAtlas2Simulation: mass[${m}] must be positive`);const l=i===null?null:Float64Array.from(i);r!==this.n&&(this.fixed=null),this.n=r,this.rowPtr=e.rowPtr,this.colIdx=e.colIdx,this.mass=a,this.weights=l,this.owner=n;const{scale:c,center:u,seed:d}=this.options;me(e,n,d,this.dim,c,u,"fa2");const[f,h,g]=u,w=new Float64Array(3*r);for(let m=0;m<r;m++)w[3*m]=(n[3*m]-f)/c,w[3*m+1]=(n[3*m+1]-h)/c,w[3*m+2]=this.dim===2?0:(n[3*m+2]-g)/c;this.positions=w,this.oldForce=new Float64Array(3*r),this.scratch=Me(r),this.speedValue=1,this.speedEfficiencyValue=1,this.swingValue=1,this.tractionValue=1,this.pending=null,this.writeInitialStatistics(),this.state="loaded",this.reheat()}step(e=this.options.iterationsPerStep){if(this.state==="disposed")throw new Error("ForceAtlas2Simulation: disposed");if(this.state==="created")throw new Error("ForceAtlas2Simulation: not loaded (call load() first)");if(!le(e))throw new RangeError(`ForceAtlas2Simulation: iterations must be an integer >= 1, got ${String(e)}`);if(this.settled)return;let n=0;for(;n<e&&!this.settled;)this.iterate(),this.iterationsDoneValue+=1,n+=1;this.writeBack()}setFixed(e){this.assertLoaded();const{n}=this,r=Math.ceil(n/32);if(e.length<r)throw new RangeError(`ForceAtlas2Simulation: setFixed: the mask has ${e.length} words, ${r} needed for ${n} nodes`);const o=this.fixed,s=o!==null&&Pl(o,e,r,n),i=new Uint32Array(r);i.set(e.subarray(0,r)),this.fixed=i,s&&this.reheat()}setPosition(e,n,r,o){this.assertLoaded();const{n:s,owner:i}=this;if(!Number.isInteger(e)||e<0||e>=s)throw new RangeError(`ForceAtlas2Simulation: setPosition(${e}): index out of range [0, ${s})`);if(!Number.isFinite(n)||!Number.isFinite(r)||!Number.isFinite(o))throw new RangeError("ForceAtlas2Simulation: setPosition: coordinates must be finite");i[3*e]=n,i[3*e+1]=r,i[3*e+2]=o;const{scale:a}=this.options,[l,c,u]=this.options.center;this.positions[3*e]=(i[3*e]-l)/a,this.positions[3*e+1]=(i[3*e+1]-c)/a,this.positions[3*e+2]=this.dim===2?0:(i[3*e+2]-u)/a,this.reheat()}reheat(){this.assertNotDisposed(),this.iterationsDoneValue=0,this.settledCountValue=0,this.swingMode===1&&(this.swingValue=1,this.tractionValue=1)}dispose(){this.state="disposed",this.owner=new Float32Array(0),this.scratch=Me(0),this.pending=null,this.positions=new Float64Array(0),this.oldForce=new Float64Array(0),this.mass=new Float64Array(0),this.weights=null,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.fixed=null,this.n=0}assertNotDisposed(){if(this.state==="disposed")throw new Error("ForceAtlas2Simulation: disposed")}assertLoaded(){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("ForceAtlas2Simulation: not loaded (call load() first)")}isFixed(e){return this.fixed!==null&&L(this.fixed,e)}writeBack(){const{n:e,owner:n,positions:r}=this,{scale:o}=this.options,[s,i,a]=this.options.center;for(let l=0;l<e;l++)this.isFixed(l)||(n[3*l]=r[3*l]*o+s,n[3*l+1]=r[3*l+1]*o+i,n[3*l+2]=this.dim===2?a:r[3*l+2]*o+a)}writeInitialStatistics(){const{n:e,positions:n}=this,r=[0,0,0];for(let o=0;o<e;o++)for(let s=0;s<3;s++)r[s]+=n[3*o+s];e===0?this.centroid=[0,0,0]:this.centroid=[r[0]/e,r[1]/e,r[2]/e],this.settledCountValue=0}foldTotals(e){return{sum:[kt(e.sumX),kt(e.sumY),kt(e.sumZ)],sumSq:kt(e.sumW),disp:kt(e.disp),free:kt(e.free)}}fold(e){const n=this.foldTotals(e),r=this.n,o=Math.sqrt(Math.max(n.sumSq,0)/r),s=n.free===0?0:n.disp/n.free;this.centroid=[n.sum[0]/r,n.sum[1]/r,n.sum[2]/r],this.settledCountValue=s<=this.options.settleThreshold*o?this.settledCountValue+1:0}iterate(){const{n:e,scratch:n}=this;this.pending!==null&&this.fold(this.pending);const{positions:r,mass:o,rowPtr:s,colIdx:i,weights:a,oldForce:l}=this,{scalingRatio:c,gravity:u,linlog:d,distributedAction:f,strongGravity:h}=this.options,{attraction:g,force:w,swing:m,traction:A,sumX:p,sumY:y,sumZ:v,sumW:E,disp:b,free:C}=n;for(let k=0;k<e;k++){let N=0,O=0,T=0;const B=r[3*k],z=r[3*k+1],q=r[3*k+2];for(let W=s[k];W<s[k+1];W++){const Y=i[W];if(Y===k)continue;const et=a===null?1:a[W],nt=r[3*Y]-B,ut=r[3*Y+1]-z,rt=r[3*Y+2]-q;let ot=et;if(d){const st=Math.max(vt(nt,ut,rt),ae);ot=et*fr(1+st)/st}N=N+nt*ot,O=O+ut*ot,T=T+rt*ot}f&&(N=N/o[k],O=O/o[k],T=T/o[k]),g[3*k]=N,g[3*k+1]=O,g[3*k+2]=T}m.fill(0),A.fill(0);const x=this.centroid[0],_=this.centroid[1],S=this.centroid[2];for(let k=0;k<e;k++){const N=r[3*k],O=r[3*k+1],T=r[3*k+2],B=o[k];let z=0,q=0,W=0;for(let at=0;at<e;at++){if(at===k)continue;const An=o[at],ye=N-r[3*at],Ae=O-r[3*at+1],ve=T-r[3*at+2];let te=ye*ye+Ae*Ae+ve*ve;if(te<jo){const be=Yo(k,at,this.dim),Ce=c*B*An/ae;z=z+be[0]*Ce,q=q+be[1]*Ce,W=W+be[2]*Ce;continue}te=Math.max(te,yl);const Ee=c*B*An/te;z=z+ye*Ee,q=q+Ae*Ee,W=W+ve*Ee}let Y=0,et=0,nt=0;const ut=this.gravityCenter===0?N-x:N,rt=this.gravityCenter===0?O-_:O,ot=this.gravityCenter===0?T-S:T,st=-u*B;if(h)Y=st*ut,et=st*rt,nt=st*ot;else{const at=vt(ut,rt,ot);at>ae&&(Y=st*ut/at,et=st*rt/at,nt=st*ot/at)}z=z+Y,q=q+et,W=W+nt;const mt=g[3*k]+z,dt=g[3*k+1]+q,xt=g[3*k+2]+W;w[3*k]=mt,w[3*k+1]=dt,w[3*k+2]=xt;const j=l[3*k],it=l[3*k+1],yn=l[3*k+2];this.swingMode===1?(m[k]=B*vt(N-mt,O-dt,T-xt),A[k]=.5*B*vt(N+mt,O+dt,T+xt)):this.isFixed(k)||(m[k]=B*vt(mt-j,dt-it,xt-yn),A[k]=.5*B*vt(mt+j,dt+it,xt+yn))}const P=kt(Ft(m,e)),I=kt(Ft(A,e));let F=P,U=I;this.swingMode===1&&(F=this.swingValue+P,U=this.tractionValue+I);const R=xl(e,F,U,this.speedValue,this.speedEfficiencyValue,this.options.jitterTolerance);this.speedValue=R.speed,this.speedEfficiencyValue=R.speedEfficiency,this.swingValue=F,this.tractionValue=U;const D=this.speedValue;b.fill(0),C.fill(0);for(let k=0;k<e;k++){const N=w[3*k],O=w[3*k+1],T=w[3*k+2],B=o[k];let z=B*vt(N,O,T);this.swingMode===0&&(z=B*vt(N-l[3*k],O-l[3*k+1],T-l[3*k+2]));const q=D/(1+Math.sqrt(D*z)),W=this.isFixed(k),Y=W?0:N*q,et=W?0:O*q;let nt=W?0:T*q;this.dim===2&&(nt=0);const ut=r[3*k]+Y,rt=r[3*k+1]+et,ot=r[3*k+2]+nt;r[3*k]=ut,r[3*k+1]=rt,r[3*k+2]=ot,this.swingMode===0&&(l[3*k]=N,l[3*k+1]=O,l[3*k+2]=T);const st=ut-x,mt=rt-_,dt=ot-S;p[k]=ut,y[k]=rt,v[k]=ot,E[k]=st*st+mt*mt+dt*dt,W||(b[k]=vt(Y,et,nt),C[k]=1)}this.pending={sumX:Ft(p,e),sumY:Ft(y,e),sumZ:Ft(v,e),sumW:Ft(E,e),disp:Ft(b,e),free:Ft(C,e)}}}const jt=.1,Fl=50,kl=.1,Sl=.7;function _l(t){const e=[0,0,0];if(t===void 0)return e;for(let n=0;n<3&&n<t.length;n++){const r=t[n];if(!Number.isFinite(r))throw new RangeError(`center[${n}] is not finite`);e[n]=r}return e}function re(t,e,n,r,o){if(!Number.isFinite(e)||e<n||e>r||o&&!Number.isInteger(e))throw new RangeError(`FruchtermanReingoldSimulation: ${t} must be ${o?"an integer":"a finite number"} in [${n}, ${r}], got ${e}`);return e}class Ml{constructor(e={}){this.state="created",this.n=0,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.owner=new Float32Array(0),this.positions=new Float64Array(0),this.displacement=new Float64Array(0),this.fixed=null,this.k=0,this.dt=0,this.t=jt,this.previousEnergy=Number.POSITIVE_INFINITY,this.progress=0,this.pendingEnergy=null,this.iterationsDoneValue=0,this.settledCountValue=0,this.settledValue=!1,this.centroid=[0,0,0],this.rmsRadiusValue=0,this.meanDisplacementValue=0;const n=e.dim??J.dim;if(n!==2&&n!==3)throw new RangeError(`FruchtermanReingoldSimulation: dim must be 2 or 3, got ${String(n)}`);this.dim=n;const r=e.scale??J.scale;if(!Number.isFinite(r)||r<=0)throw new RangeError(`FruchtermanReingoldSimulation: scale must be a finite number > 0, got ${r}`);this.scale=r,this.center=_l(e.center),this.seed=e.seed??null;const o=e.k??null;if(o===null||Number.isNaN(o)||o===0)this.kOption=null;else{if(!Number.isFinite(o)||o<0)throw new RangeError(`FruchtermanReingoldSimulation: k must be null, 0 or a finite number > 0, got ${o}`);this.kOption=o}const s=e.cooling??"linear";if(s!=="linear"&&s!=="adaptive")throw new RangeError(`FruchtermanReingoldSimulation: cooling must be "linear" or "adaptive", got ${String(s)}`);this.adaptive=s==="adaptive",this.iterations=re("iterations",e.iterations??(this.adaptive?vl:Fl),0,Number.MAX_SAFE_INTEGER,!0),this.settleThreshold=re("settleThreshold",e.settleThreshold??J.settleThreshold,0,Number.POSITIVE_INFINITY,!1),this.settleWindow=re("settleWindow",e.settleWindow??J.settleWindow,1,Number.MAX_SAFE_INTEGER,!0),this.iterationsPerStep=re("iterationsPerStep",e.iterationsPerStep??J.iterationsPerStep,1,Number.MAX_SAFE_INTEGER,!0),this.fixedOption=e.fixed??null}get settled(){return this.settledValue}get iterationsDone(){return this.iterationsDoneValue}get settledCount(){return this.settledCountValue}get meanDisplacement(){return this.meanDisplacementValue}get rmsRadius(){return this.rmsRadiusValue}get temperature(){return this.t}load(e,n){if(this.assertNotDisposed(),e.directed)throw new Error("FruchtermanReingoldSimulation: the snapshot must be undirected (pass toUndirected().snapshot)");const r=e.nodeCount;if(n.length!==3*r)throw new RangeError(`FruchtermanReingoldSimulation: positions has ${n.length} entries, expected ${3*r}`);const o=this.resolveFixed(e,r);r!==this.n&&(this.positions=new Float64Array(3*r),this.displacement=new Float64Array(3*r)),this.n=r,this.rowPtr=e.rowPtr,this.colIdx=e.colIdx,this.owner=n,this.fixed=o,me(e,n,this.seed,this.dim,this.scale,this.center,"fr");const s=this.positions,[i,a,l]=this.center,{scale:c}=this;for(let u=0;u<r;u++)s[3*u]=(n[3*u]-i)/c,s[3*u+1]=(n[3*u+1]-a)/c,s[3*u+2]=this.dim===2?0:(n[3*u+2]-l)/c;this.k=this.kOption??1/Math.sqrt(r),this.dt=jt/(this.iterations+1),this.t=jt,this.resetAdaptive(),this.pendingEnergy=null,this.writeInitialStatistics(),this.iterationsDoneValue=0,this.settledCountValue=0,this.state="loaded",this.settledValue=r===0||this.computeSettled()}step(e=this.iterationsPerStep){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("FruchtermanReingoldSimulation: not loaded");if(!Number.isInteger(e)||e<1)throw new RangeError(`FruchtermanReingoldSimulation: step(${e}): an integer >= 1 is required`);if(this.settledValue)return;let n=0;for(;n<e&&!this.settledValue;)this.iterate(),n++,this.iterationsDoneValue++,this.settledValue=this.computeSettled();this.writeBack()}setFixed(e){this.assertLoaded();const{n}=this,r=Math.ceil(n/32);if(e.length<r)throw new RangeError(`FruchtermanReingoldSimulation: setFixed: the mask has ${e.length} words, ${r} needed for ${n} nodes`);let o=!1;const s=this.fixed;if(s!==null)for(let i=0;i<n&&!o;i++)L(s,i)&&!L(e,i)&&(o=!0);this.fixed=new Uint32Array(e.subarray(0,r)),o&&this.reheat()}setPosition(e,n,r,o){this.assertLoaded();const{n:s}=this;if(!Number.isInteger(e)||e<0||e>=s)throw new RangeError(`FruchtermanReingoldSimulation: setPosition(${e}): index out of range [0, ${s})`);if(!Number.isFinite(n)||!Number.isFinite(r)||!Number.isFinite(o))throw new RangeError("FruchtermanReingoldSimulation: setPosition: coordinates must be finite");const{owner:i,positions:a,scale:l}=this,[c,u,d]=this.center;i[3*e]=n,i[3*e+1]=r,i[3*e+2]=o,a[3*e]=(n-c)/l,a[3*e+1]=(r-u)/l,a[3*e+2]=this.dim===2?0:(o-d)/l,this.reheat()}reheat(){this.assertNotDisposed(),this.settledCountValue=0,this.adaptive?(this.iterationsDoneValue=0,this.t=jt,this.resetAdaptive()):(this.iterationsDoneValue=Math.floor(Sl*this.iterations),this.t=jt-this.dt*this.iterationsDoneValue),this.state==="loaded"&&(this.settledValue=this.n===0||this.computeSettled())}dispose(){this.state="disposed",this.n=0,this.positions=new Float64Array(0),this.displacement=new Float64Array(0),this.owner=new Float32Array(0),this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.fixed=null}iterate(){this.adaptive&&this.pendingEnergy!==null&&this.updateAdaptive(this.pendingEnergy);const{n:e,k:n,t:r,positions:o,displacement:s,rowPtr:i,colIdx:a,fixed:l}=this;s.fill(0);for(let y=0;y<e;y++){const v=o[3*y],E=o[3*y+1],b=o[3*y+2];for(let C=y+1;C<e;C++){const x=v-o[3*C],_=E-o[3*C+1],S=b-o[3*C+2],P=x*x+_*_+S*S;let I,F,U;if(P<jo){const R=Yo(y,C,this.dim),D=n*n/ae;I=R[0]*D,F=R[1]*D,U=R[2]*D}else{const R=Math.sqrt(P),D=n*n/R;I=x/R*D,F=_/R*D,U=S/R*D}s[3*y]+=I,s[3*y+1]+=F,s[3*y+2]+=U,s[3*C]-=I,s[3*C+1]-=F,s[3*C+2]-=U}}for(let y=0;y<e;y++){const v=o[3*y],E=o[3*y+1],b=o[3*y+2];for(let C=i[y];C<i[y+1];C++){const x=a[C],_=v-o[3*x],S=E-o[3*x+1],P=b-o[3*x+2],I=Math.sqrt(_*_+S*S+P*P)||kl,F=I*I/n;s[3*y]-=_/I*F,s[3*y+1]-=S/I*F,s[3*y+2]-=P/I*F}}const[c,u,d]=this.centroid;let f=0,h=0,g=0,w=0,m=0,A=0,p=0;for(let y=0;y<e;y++){if(l===null||!L(l,y)){const S=s[3*y],P=s[3*y+1],I=s[3*y+2],F=S*S+P*P+I*I,U=Math.sqrt(F);p+=F;const R=Math.min(U,r);U!==0&&(o[3*y]+=S/U*R,o[3*y+1]+=P/U*R,o[3*y+2]+=I/U*R),m+=R,A++}const v=o[3*y],E=o[3*y+1],b=o[3*y+2];f+=v,h+=E,g+=b;const C=v-c,x=E-u,_=b-d;w+=C*C+x*x+_*_}this.adaptive?this.pendingEnergy=p:this.t-=this.dt,this.centroid=[f/e,h/e,g/e],this.rmsRadiusValue=Math.sqrt(Math.max(w,0)/e),this.meanDisplacementValue=A===0?0:m/A,this.settledCountValue=this.meanDisplacementValue<=this.settleThreshold*this.rmsRadiusValue?this.settledCountValue+1:0}resetAdaptive(){this.previousEnergy=Number.POSITIVE_INFINITY,this.progress=0}updateAdaptive(e){e<this.previousEnergy?(this.progress++,this.progress>=Al&&(this.progress=0,this.t/=sr)):(this.progress=0,this.t*=sr),this.previousEnergy=e}writeBack(){const{n:e,owner:n,positions:r,scale:o,fixed:s}=this,[i,a,l]=this.center;for(let c=0;c<e;c++)s!==null&&L(s,c)||(n[3*c]=r[3*c]*o+i,n[3*c+1]=r[3*c+1]*o+a,n[3*c+2]=r[3*c+2]*o+l)}writeInitialStatistics(){const{n:e,positions:n}=this;if(e===0){this.centroid=[0,0,0],this.rmsRadiusValue=0,this.meanDisplacementValue=0;return}let r=0,o=0,s=0;for(let a=0;a<e;a++)r+=n[3*a],o+=n[3*a+1],s+=n[3*a+2];r/=e,o/=e,s/=e;let i=0;for(let a=0;a<e;a++){const l=n[3*a]-r,c=n[3*a+1]-o,u=n[3*a+2]-s;i+=l*l+c*c+u*u}this.centroid=[r,o,s],this.rmsRadiusValue=Math.sqrt(i/e),this.meanDisplacementValue=0}computeSettled(){return this.iterationsDoneValue>=this.iterations||this.settledCountValue>=this.settleWindow}resolveFixed(e,n){const r=Math.ceil(n/32),o=this.fixedOption;if(typeof o=="string"){const i=e.nodes.get(o);if(i===null)throw new RangeError(`FruchtermanReingoldSimulation: fixed names node column "${o}", which the snapshot does not hold`);if(i.dtype!=="bool")throw new TypeError(`FruchtermanReingoldSimulation: fixed names node column "${o}", which is ${i.dtype}, not bool`);return new Uint32Array(i.data.subarray(0,r))}if(o!==null){if(o.length<r)throw new RangeError(`FruchtermanReingoldSimulation: the fixed mask has ${o.length} words, ${r} needed for ${n} nodes`);return new Uint32Array(o.subarray(0,r))}const s=e.nodes.byRole("fixed");return s!==null&&s.dtype==="bool"?new Uint32Array(s.data.subarray(0,r)):n===this.n?this.fixed:null}assertNotDisposed(){if(this.state==="disposed")throw new Error("FruchtermanReingoldSimulation: disposed")}assertLoaded(){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("FruchtermanReingoldSimulation: not loaded")}}function Ul(t,e,n,r){if(e!==null&&(!Number.isInteger(e)||e<1))throw H(new Error("columns must be a positive integer"),"E_BAD_OPTION",{option:"columns",value:e});const o=new Float64Array(2*t);if(t===0)return o;const s=Math.min(e??Math.ceil(Math.sqrt(t)),t),i=Math.ceil(t/s),a=Math.max(s-1,i-1),l=a===0?0:2*n/a;for(let c=0;c<t;c++)o[2*c]=r[0]+(c%s-(s-1)/2)*l,o[2*c+1]=r[1]+(Math.floor(c/s)-(i-1)/2)*l;return o}function Ol(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(It(Ul(n,e.columns??null,o,s),r,s),r,n)}function Tl(t,e,n,r){if(e.length===0)return t.map(c=>-c);const o=t.slice(),s=new Array(e.length).fill(0),i=[];for(let c=0;c<e.length;c++){const u=e[c],d=n[c];i.push(1/d.reduce((f,h,g)=>f+h*u[g],0))}for(let c=e.length-1;c>=0;c--){const u=e[c];s[c]=i[c]*u.reduce((d,f,h)=>d+f*o[h],0);for(let d=0;d<o.length;d++)o[d]-=s[c]*n[c][d]}let a=1;if(e.length>0&&n.length>0){const c=n[n.length-1];a=e[e.length-1].reduce((d,f,h)=>d+f*c[h],0)/c.reduce((d,f)=>d+f*f,0)}const l=o.map(c=>a*c);for(let c=0;c<e.length;c++){const u=e[c],d=n[c],f=i[c]*d.reduce((h,g,w)=>h+g*l[w],0);for(let h=0;h<l.length;h++)l[h]+=u[h]*(s[c]-f)}return l.map(c=>-c)}function Rl(t,e,n,r,o,s){const l=r.reduce((d,f,h)=>d+f*e[h],0);if(l>=0)return 0;let c=s;const u=20;for(let d=0;d<u;d++){const f=t.map((g,w)=>g+c*e[w]);if(o(f)<=n+1e-4*c*l)return c;c*=.5}return 0}function Dl(t,e,n){const r=e.length,o=.001,s=t.map(h=>h.map(g=>g===0?0:1/g)),i=e.flat(),a=500,l=1e-5,c=10,u=[],d=[];for(let h=0;h<a;h++){const[g,w]=Ue(i,s,o,n),m=Tl(w,u,d),A=Rl(i,m,g,w,C=>Ue(C,s,o,n)[0],1);if(A===0){if(u.length===0)break;u.length=0,d.length=0;continue}const p=[...i];for(let C=0;C<i.length;C++)i[C]+=A*m[C];const[,y]=Ue(i,s,o,n),v=i.map((C,x)=>C-p[x]),E=y.map((C,x)=>C-w[x]);if(v.reduce((C,x,_)=>C+x*E[_],0)>1e-10&&(u.push(v),d.push(E)),u.length>c&&(u.shift(),d.shift()),Math.sqrt(y.reduce((C,x)=>C+x*x,0))<l)break}const f=[];for(let h=0;h<r;h++)f.push(i.slice(h*n,(h+1)*n));return f}function $l(t,e,n){const r=n.length;for(let o=0;o<r;o++)for(let s=o+1;s<r;s++){const i=e[o].map((u,d)=>u-e[s][d]),a=Math.sqrt(i.reduce((u,d)=>u+d*d,0)),l=n[o][s],c=a*l-1;t+=c*c}return t}function zl(t,e,n,r){const o=n.length;for(let s=0;s<o;s++)for(let i=s+1;i<o;i++){const a=e[s].map((f,h)=>f-e[i][h]),l=Math.sqrt(a.reduce((f,h)=>f+h*h,0))||1e-10,c=a.map(f=>f/l),u=n[s][i],d=l*u-1;for(let f=0;f<r;f++){const h=2*u*d*c[f];t[s*r+f]+=h,t[i*r+f]-=h}}}function Ue(t,e,n,r){const o=e.length,s=[];for(let c=0;c<o;c++)s.push(t.slice(c*r,(c+1)*r));let i=0;const a=new Array(r).fill(0);for(let c=0;c<o;c++)for(let u=0;u<r;u++)a[u]+=s[c][u];i+=.5*n*a.reduce((c,u)=>c+u*u,0),i=$l(i,s,e);const l=new Array(t.length).fill(0);for(let c=0;c<o;c++)for(let u=0;u<r;u++)l[c*r+u]+=n*a[u];return zl(l,s,e,r),[i,l]}const Bl=1e6,ql=42,Vl=24;function Xo(t,e){const n=Vl*t*t,r=e===null?`kamadaKawai: ${t} nodes need about ${n} bytes of n x n memory, more than this runtime could allocate. Pass a maxNodes to refuse such graphs up front.`:`kamadaKawai: ${t} nodes exceeds maxNodes ${e}; it would allocate about ${n} bytes. Pass a larger maxNodes to allow it.`;return Object.assign(new RangeError(r),{code:"E_TOO_LARGE",params:{nodeCount:t,maxNodes:e,bytes:n}})}function Wl(t,e){let n=null;return e===!0?n=t.edges.byRole("weight"):typeof e=="string"&&(n=t.edges.get(e)),n?.dtype==="f64"&&n.meta.components===1?wr(t,n.data):Go(e,t)}function Ll(t,e){const n=t.nodeCount;let r;if(e.dist!==null&&e.dist!==void 0){if(e.dist.length!==n*n)throw H(new RangeError(`kamadaKawai: dist has ${e.dist.length} values, expected ${n*n} (n * n)`),"E_BAD_OPTION",{option:"dist",length:e.dist.length,expected:n*n});if(n>=2&&!e.dist.some((o,s)=>Number.isFinite(o)&&s%(n+1)!==0))throw H(new RangeError("kamadaKawai: dist has no finite distance between two different nodes; was it computed from a graph without edges?"),"E_BAD_OPTION",{option:"dist"});r=e.dist}else{const o=Wl(t,e.weight??!0);let s;try{s=new Float64Array(n*n).fill(Number.POSITIVE_INFINITY)}catch(l){throw l instanceof RangeError?Xo(n,null):l}const{rowPtr:i,colIdx:a}=t;for(let l=0;l<n;l++){s[l*n+l]=0;for(let c=i[l];c<i[l+1];c++){const u=o===null?1:o[c];if(!(u>=0))throw H(new RangeError(`kamadaKawai: weights are distances and must be >= 0, got ${u}`),"E_BAD_WEIGHT",{weight:u});const d=l*n+a[c];u<s[d]&&(s[d]=u)}}for(let l=0;l<n;l++)for(let c=0;c<n;c++){const u=s[c*n+l];if(u!==Number.POSITIVE_INFINITY)for(let d=0;d<n;d++){const f=u+s[l*n+d];f<s[c*n+d]&&(s[c*n+d]=f)}}r=s}return Array.from({length:n},(o,s)=>Array.from({length:n},(i,a)=>{const l=r[s*n+a];return s===a?0:Number.isFinite(l)?l:Bl}))}function jl(t,e,n){const r=t.nodeCount,{pos:o}=e;if(o!=null){if(o.length!==n*r)throw H(new RangeError(`kamadaKawai: pos has ${o.length} values, expected ${n*r} (dim * nodeCount)`),"E_BAD_OPTION",{option:"pos",length:o.length,expected:n*r});return Array.from({length:r},(i,a)=>Array.from(o.subarray(n*a,n*a+n),l=>Number.isNaN(l)?0:l))}if(n===2){const i=Zt.linspace(0,2*Math.PI,r+1);return Array.from({length:r},(a,l)=>[yt(i[l]),wt(i[l])])}const s=new Float64Array(3*r).fill(Number.NaN);return me(t,s,e.seed===void 0?ql:e.seed,3,1,null,"fr"),Array.from({length:r},(i,a)=>Array.from(s.subarray(3*a,3*a+3)))}function Gl(t,e={}){const n=Lt(t),r=Vo(e.dim),o=n.nodeCount;if(e.maxNodes!==void 0&&(o>e.maxNodes||Number.isNaN(e.maxNodes)))throw Xo(o,e.maxNodes);if(o<=1){const a=new Float32Array(r*o);return o===1&&a.set(Array.from({length:r},(l,c)=>e.center?.[c]??0)),{positions:a,dim:r,n:o}}const s=Dl(Ll(n,e),jl(n,e,r),r),i=Float64Array.from(s.flat());return Mt(i,r,e.scale??1,e.center),{positions:Float32Array.from(i),dim:r,n:o}}class Hl{constructor(e){this.n=e,this.cw=Array.from({length:e},()=>new Map),this.ccw=Array.from({length:e},()=>new Map),this.first=new Int32Array(e).fill(-1)}has(e,n){return this.cw[e].has(n)}addCw(e,n,r){if(r<0){this.cw[e].set(n,n),this.ccw[e].set(n,n),this.first[e]=n;return}const o=this.cw[e].get(r);if(o===void 0)throw new Error("Cannot add edge. Reference neighbor does not exist");this.cw[e].set(r,n),this.cw[e].set(n,o),this.ccw[e].set(o,n),this.ccw[e].set(n,r)}addCcw(e,n,r){if(r<0){this.addCw(e,n,-1);return}this.addCw(e,n,this.ccw[e].get(r)??-1),r===this.first[e]&&(this.first[e]=n)}addFirst(e,n){this.addCcw(e,n,this.first[e])}nextFace(e,n){return[n,this.ccw[n].get(e)??-1]}neighboursCw(e){const n=this.first[e];if(n<0)return[];const r=[n];for(let o=this.cw[e].get(n)??n;o!==n;o=this.cw[e].get(o)??n)r.push(o);return r}}const oe=(t=-1,e=-1)=>({low:t,high:e}),Et=t=>t.low<0&&t.high<0;class Kl{constructor(e){this.adj=e,this.lowpt=new Map,this.lowpt2=new Map,this.nesting=new Map,this.oriented=new Set,this.ordered=[],this.ref=new Map,this.side=new Map,this.stack=[],this.stackBottom=new Map,this.lowptEdge=new Map,this.leftRef=new Map,this.rightRef=new Map,this.roots=[],this.n=e.length,this.height=new Int32Array(this.n).fill(-1),this.parentEdge=new Float64Array(this.n).fill(-1),this.out=Array.from({length:this.n},()=>[])}key(e,n){return e*this.n+n}target(e){return e%this.n}low(e){return this.lowpt.get(e)??0}run(){const{n:e}=this,n=this.adj.reduce((s,i)=>s+i.length,0)/2;if(e>2&&n>3*e-6)return null;for(let s=0;s<e;s++)this.height[s]<0&&(this.height[s]=0,this.roots.push(s),this.orient(s));const r=s=>[...this.out[s]].sort((i,a)=>(this.nesting.get(this.key(s,i))??0)-(this.nesting.get(this.key(s,a))??0));this.ordered=Array.from({length:e},(s,i)=>r(i));for(const s of this.roots)if(!this.test(s))return null;for(const s of this.oriented)this.nesting.set(s,this.sign(s)*(this.nesting.get(s)??0));const o=new Hl(e);for(let s=0;s<e;s++){this.ordered[s]=r(s);let i=-1;for(const a of this.ordered[s])o.addCw(s,a,i),i=a}for(const s of this.roots)this.embed(s,o);return o}orient(e){const n=[e],r=new Int32Array(this.n),o=new Set;for(;n.length>0;){const s=n.pop()??0,i=this.parentEdge[s];for(;r[s]<this.adj[s].length;){const a=this.adj[s][r[s]],l=this.key(s,a),c=this.key(a,s);if(!o.has(l)){if(this.oriented.has(l)||this.oriented.has(c)){r[s]++;continue}if(this.oriented.add(l),this.out[s].push(a),this.lowpt.set(l,this.height[s]),this.lowpt2.set(l,this.height[s]),this.height[a]<0){this.parentEdge[a]=l,this.height[a]=this.height[s]+1,n.push(s,a),o.add(l);break}this.lowpt.set(l,this.height[a])}if(this.nesting.set(l,2*this.low(l)+((this.lowpt2.get(l)??0)<this.height[s]?1:0)),i>=0){const u=this.low(i),d=this.lowpt2.get(i)??0,f=this.low(l),h=this.lowpt2.get(l)??0;f<u?(this.lowpt2.set(i,Math.min(u,h)),this.lowpt.set(i,f)):f>u?this.lowpt2.set(i,Math.min(d,f)):this.lowpt2.set(i,Math.min(d,h))}r[s]++}}}conflicting(e,n){return!Et(e)&&this.low(e.high)>this.low(n)}lowest(e){return Et(e.left)?this.low(e.right.low):Et(e.right)?this.low(e.left.low):Math.min(this.low(e.left.low),this.low(e.right.low))}top(){return this.stack.length===0?null:this.stack[this.stack.length-1]}test(e){const n=[e],r=new Int32Array(this.n),o=new Set;for(;n.length>0;){const s=n.pop()??0,i=this.parentEdge[s];let a=!1;const l=this.ordered[s];for(;r[s]<l.length;){const c=l[r[s]],u=this.key(s,c);if(!o.has(u)){if(this.stackBottom.set(u,this.top()),u===this.parentEdge[c]){n.push(s,c),o.add(u),a=!0;break}this.lowptEdge.set(u,u),this.stack.push({left:oe(),right:oe(u,u)})}if(this.low(u)<this.height[s]){if(c===l[0])this.lowptEdge.set(i,this.lowptEdge.get(u)??-1);else if(!this.addConstraints(u,i))return!1}r[s]++}!a&&i>=0&&this.removeBackEdges(i)}return!0}addConstraints(e,n){const r={left:oe(),right:oe()};for(;;){const o=this.stack.pop();if(Et(o.left)||([o.left,o.right]=[o.right,o.left]),!Et(o.left))return!1;if(this.low(o.right.low)>this.low(n)?(Et(r.right)?r.right={...o.right}:this.ref.set(r.right.low,o.right.high),r.right.low=o.right.low):this.ref.set(o.right.low,this.lowptEdge.get(n)??-1),this.top()===(this.stackBottom.get(e)??null))break}for(let o=this.top();o!==null&&(this.conflicting(o.left,e)||this.conflicting(o.right,e));o=this.top()){const s=this.stack.pop();if(this.conflicting(s.right,e)&&([s.left,s.right]=[s.right,s.left]),this.conflicting(s.right,e))return!1;this.ref.set(r.right.low,s.right.high),s.right.low>=0&&(r.right.low=s.right.low),Et(r.left)?r.left={...s.left}:this.ref.set(r.left.low,s.left.high),r.left.low=s.left.low}return Et(r.left)&&Et(r.right)||this.stack.push(r),!0}removeBackEdges(e){const n=Math.floor(e/this.n),r=o=>this.ref.get(o)??-1;for(;this.stack.length>0&&this.lowest(this.top())===this.height[n];){const o=this.stack.pop();o.left.low>=0&&this.side.set(o.left.low,-1)}if(this.stack.length>0){const o=this.stack.pop();for(;o.left.high>=0&&this.target(o.left.high)===n;)o.left.high=r(o.left.high);for(o.left.high<0&&o.left.low>=0&&(this.ref.set(o.left.low,o.right.low),this.side.set(o.left.low,-1),o.left.low=-1);o.right.high>=0&&this.target(o.right.high)===n;)o.right.high=r(o.right.high);o.right.high<0&&o.right.low>=0&&(this.ref.set(o.right.low,o.left.low),this.side.set(o.right.low,-1),o.right.low=-1),this.stack.push(o)}if(this.low(e)<this.height[n]){const o=this.top(),s=o.left.high,i=o.right.high;this.ref.set(e,s>=0&&(i<0||this.low(s)>this.low(i))?s:i)}}sign(e){const n=[e],r=new Map;for(;n.length>0;){const o=n.pop()??0,s=this.ref.get(o)??-1;if(s>=0)n.push(o,s),r.set(o,s),this.ref.set(o,-1);else{const i=r.get(o)??-1;this.side.set(o,(this.side.get(o)??1)*(i>=0?this.side.get(i)??1:1))}}return this.side.get(e)??1}embed(e,n){const r=[e],o=new Int32Array(this.n);for(;r.length>0;){const s=r.pop()??0,i=this.ordered[s];for(;o[s]<i.length;){const a=i[o[s]++],l=this.key(s,a);if(l===this.parentEdge[a]){n.addFirst(a,s),this.leftRef.set(s,a),this.rightRef.set(s,a),r.push(s,a);break}(this.side.get(l)??1)===1?n.addCw(a,s,this.rightRef.get(a)??-1):(n.addCcw(a,s,this.leftRef.get(a)??-1),this.leftRef.set(a,s))}}}}function Yl(t,e,n,r){const{n:o}=t;if(r.has(e*o+n))return[];r.add(e*o+n);let s=e,i=n;const a=[e],l=new Set(a);let[,c]=t.nextFace(s,i);for(;i!==e||c!==n;)l.has(i)?(t.addCw(s,c,i),t.addCcw(c,s,i),r.add(i*o+c),r.add(c*o+s),i=s):(l.add(i),a.push(i)),s=i,[i,c]=t.nextFace(i,c),r.add(s*o+i);return a}function Xl(t,e,n){let[,r]=t.nextFace(e,n),[,o]=t.nextFace(n,r);if(!(e===n||e===r))for(;e!==o;)t.has(e,r)?[e,n,r]=[n,r,o]:(t.addCw(e,r,n),t.addCcw(r,e,n),[n,r]=[r,o]),[,o]=t.nextFace(n,r)}function Zl(t){const{n:e}=t,n=new Uint8Array(e),r=[];for(let a=0;a<e;a++)if(n[a]!==1){r.push(a),n[a]=1;for(const l=[a];l.length>0;)for(const c of t.neighboursCw(l.pop()??0))n[c]===0&&(n[c]=1,l.push(c))}for(let a=0;a+1<r.length;a++)t.addFirst(r[a],r[a+1]),t.addFirst(r[a+1],r[a]);let o=[];const s=[],i=new Set;for(let a=0;a<e;a++){const l=t.first[a];if(l<0)continue;let c=l;do{const u=Yl(t,a,c,i);u.length>0&&(s.push(u),u.length>o.length&&(o=u)),c=t.cw[a].get(c)??l}while(c!==l)}for(const a of s)a!==o&&Xl(t,a[0],a[1]);return o}class Jl{constructor(e,n){this.embedding=e,this.ccwNbr=new Map,this.cwNbr=new Map;const{n:r}=e;this.v1=n[0],this.v2=n[1],this.chords=new Int32Array(r),this.marked=new Uint8Array(r),this.ready=new Set(n);let o=this.v2;for(let s=2;s<n.length;s++)this.ccwNbr.set(o,n[s]),o=n[s];this.ccwNbr.set(o,this.v1),o=this.v1;for(let s=n.length-1;s>0;s--)this.cwNbr.set(o,n[s]),o=n[s];for(const s of n)this.countChords(s,null)}run(){const{n:e}=this.embedding,n=new Array(e);n[0]=[this.v1,[]],n[1]=[this.v2,[]],this.ready.delete(this.v1),this.ready.delete(this.v2);for(let r=e-1;r>1;r--){const o=this.ready.values().next().value;this.ready.delete(o),this.marked[o]=1;const[s,i]=this.contourEnds(o),a=this.replaceContour(o,s,i);a.length===2?(this.releaseChord(s),this.releaseChord(i)):this.addInner(new Set(a.slice(1,-1))),n[r]=[o,a]}return n}isOuterNbr(e,n){return this.ccwNbr.get(e)===n||this.cwNbr.get(e)===n}isOnOuter(e){return this.marked[e]===0&&(this.ccwNbr.has(e)||e===this.v1)}countChords(e,n){for(const r of this.embedding.neighboursCw(e))this.isOnOuter(r)&&!this.isOuterNbr(e,r)&&(this.chords[e]++,this.ready.delete(e),n!==null&&!n.has(r)&&(this.chords[r]++,this.ready.delete(r)))}contourEnds(e){let n=-1,r=-1;for(const o of this.embedding.neighboursCw(e))if(this.isOnOuter(o)&&(o===this.v1?n=this.v1:o===this.v2?r=this.v2:this.cwNbr.get(o)===e?n=o:r=o),n>=0&&r>=0)break;return[n,r]}replaceContour(e,n,r){const o=[n];for(let s=n;s!==r;){const i=this.embedding.ccw[e].get(s)??r;o.push(i),this.cwNbr.set(s,i),this.ccwNbr.set(i,s),s=i}return o}releaseChord(e){--this.chords[e]===0&&this.ready.add(e)}addInner(e){for(const n of e)this.ready.add(n),this.countChords(n,e)}}function Ql(t,e){return new Jl(t,e).run()}function tc(t){const{n:e}=t,n=new Float64Array(2*e);if(e<4){const d=[0,0,2,0,1,1];return n.set(d.slice(0,2*e)),n}const r=Ql(t,Zl(t)),o=new Int32Array(e).fill(-1),s=new Int32Array(e).fill(-1),i=new Float64Array(e),a=new Float64Array(e),[l,c,u]=[r[0][0],r[1][0],r[2][0]];s[l]=u,i[c]=1,i[u]=1,a[u]=1,s[u]=c;for(let d=3;d<e;d++){const[f,h]=r[d],g=h[0],w=h[1],m=h[h.length-1],A=h[h.length-2],p=h.length>2;i[w]+=1,i[m]+=1;let y=0;for(let v=1;v<h.length;v++)y+=i[h[v]];i[f]=Math.floor((-a[g]+y+a[m])/2),a[f]=Math.floor((a[g]+y+a[m])/2),i[m]=y-i[f],p&&(i[w]-=i[f]),s[g]=f,s[f]=m,p?(o[f]=w,s[A]=-1):o[f]=-1}n[2*l+1]=a[l];for(const d=[l];d.length>0;){const f=d.pop()??0;for(const h of[o[f],s[f]])h>=0&&(n[2*h]=n[2*f]+i[h],n[2*h+1]=a[h],d.push(h))}return n}function ec(t){const e=[];for(let n=0;n<t.nodeCount;n++){const r=[];for(let o=t.rowPtr[n];o<t.rowPtr[n+1];o++){const s=t.colIdx[o];s!==n&&s!==r[r.length-1]&&r.push(s)}e.push(r)}return e}function nc(t,e,n){const r=new Kl(ec(t)).run();if(r===null)throw Object.assign(new Error("G is not planar."),{code:"E_NOT_PLANAR",params:{nodeCount:t.nodeCount,edgeCount:t.edgeCount}});return Mt(tc(r),2,e,n)}function rc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(It(nc(Lt(t),o,s),r,s),r,n)}function Zo(t,e,n,r,o=0){const s=new Float64Array(2*t).fill(Number.NaN);if(e.length===0)return s;const i=o==="networkx"?Math.PI/e.length:o;if(!Number.isFinite(i))throw H(new TypeError(`rotate must be a finite number of radians or "networkx", not ${String(o)}`),"E_BAD_OPTION",{option:"rotate",value:o});const a=n/e.length;let l=a;return e[0].length===1&&(s[2*e[0][0]]=r[0],s[2*e[0][0]+1]=r[1]),e.forEach((c,u)=>{if(c.length===0||u===0&&c.length===1)return;const d=Zt.linspace(0,2*Math.PI,c.length+1),f=(u+1)*i;for(let h=0;h<c.length;h++)s[2*c[h]]=yt(d[h]+f)*l+r[0],s[2*c[h]+1]=wt(d[h]+f)*l+r[1];l+=a}),s}function oc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{nlist:i}=e,a=typeof i=="string"?Lo(t,i,"nlist"):i?.map(l=>fe(t,l))??[Array.from({length:n},(l,c)=>c)];for(const l of a)for(let c=0;c<l.length;c++)if(!Number.isInteger(l[c])||l[c]<0||l[c]>=n)throw H(new Error(`shell node index ${String(l[c])} is not a node of the graph`),"E_BAD_NODE",{option:"nlist",index:l[c]});return ht(It(Zo(n,a,o,s,e.rotate),r,s),r,n)}function sc(t,e){const n=new Map;for(let r=t.rowPtr[e];r<t.rowPtr[e+1];r++)n.has(t.colIdx[r])||n.set(t.colIdx[r],t.arcToEdge[r]);return[...n.keys()].sort((r,o)=>(n.get(r)??0)-(n.get(o)??0))}function ic(t,e){const n=t.nodeCount,r=new Int32Array(n).fill(-1);if(n===0)return{rings:[],parent:r};if(e===null){let a=0,l=-1;for(let c=0;c<n;c++){let u=0;for(let d=t.rowPtr[c];d<t.rowPtr[c+1];d++)(d===t.rowPtr[c]||t.colIdx[d]!==t.colIdx[d-1])&&u++;u>l&&(a=c,l=u)}e=a}else if(!Number.isInteger(e)||e<0||e>=n)throw H(new Error(`root node ${String(e)} is not in the graph`),"E_BAD_NODE",{option:"root",index:e});const o=new Uint8Array(n);o[e]=1;const s=[[e]];for(let a=s[0];a.length>0;){const l=[];for(const c of a)for(const u of sc(t,c))o[u]===0&&(o[u]=1,r[u]=c,l.push(u));l.length>0&&s.push(l),a=l}const i=[];for(let a=0;a<n;a++)o[a]===0&&i.push(a);return i.length>0&&s.push(i),{rings:s,parent:r}}function ac(t,e,n,r){const o=new Float64Array(t);for(let s=r-1;s>=0;s--)for(const i of e[s])o[i]||=1,s>0&&(o[n[i]]+=o[i]);return o}function lc(t,e,n,r,o){const s=new Float64Array(2*t);if(t===0)return s;const i=e.length>1?r/(e.length-1):0,a=e.at(-1)??[],l=e.length>1&&n[a[0]]===-1?e.length-1:e.length,c=ac(t,e,n,l),u=new Float64Array(t),d=new Float64Array(t),f=new Float64Array(t);d[e[0][0]]=2*Math.PI,l>1&&e[1].length===1&&(u[e[0][0]]=Math.PI/4-Math.PI);const h=(g,w,m)=>{s[2*g]=yt(m)*w+o[0],s[2*g+1]=wt(m)*w+o[1]};h(e[0][0],0,0);for(let g=1;g<l;g++){const w=2*gr(g/(g+1));for(const m of e[g]){const A=n[m],p=d[A]*c[m]/c[A],y=u[A]+f[A]+p/2;f[A]+=p,h(m,g*i,y),d[m]=Math.min(p,w),u[m]=y-d[m]/2}}return l<e.length&&a.forEach((g,w)=>{h(g,r,2*Math.PI*w/a.length)}),s}function cc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),i=e.root??null,{arrangement:a="rings",rotate:l}=e;if(a!=="rings"&&a!=="sectors")throw H(new Error(`arrangement must be "rings" or "sectors", not ${String(a)}`),"E_BAD_OPTION",{option:"arrangement",value:a});if(a==="sectors"&&l!==void 0&&l!==0)throw H(new Error('rotate turns each ring separately, which only arrangement: "rings" allows'),"E_BAD_OPTION",{option:"rotate",value:l});const{rings:c,parent:u}=ic(Lt(t),i===null?null:V(t,i));if(a==="sectors")return ht(It(lc(n,c,u,o,s),r,s),r,n);const d=c.length>1?c.length/(c.length-1):1;return ht(It(Zo(n,c,o*d,s,l),r,s),r,n)}class Jo{constructor(e){this.seed=e??Math.floor(Math.random()*1e6),this.m=2**35-31,this.a=185852,this.c=1,this._state=this.seed%this.m}_next(){return this._state=(this.a*this._state+this.c)%this.m,this._state/this.m}rand(e=null){if(e===null)return this._next();if(typeof e=="number"){const r=[];for(let o=0;o<e;o++)r.push(this._next());return r}if(e.length===1){const r=[];for(let o=0;o<e[0];o++)r.push(this._next());return r}const n=[];for(let r=0;r<e[0];r++)n.push(this.rand(e.slice(1)));return n}}function uc(t,e,n,r,o){const s=new Jo(o??void 0),i=new Float64Array(t*e);for(let a=0;a<t*e;a++)i[a]=s.rand()*n+r[a%e];return i}function dc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(uc(n,r,o,s,e.seed??null),r,n)}const fc=1e-9,hc=5e3;function gc(t,e,n,r,o){const s=t.nodeCount,i=new Float64Array(s*e);if(s<=2){for(let w=0;w<s;w++){const m=w===0?-n:n;for(let A=0;A<e;A++)i[e*w+A]=s===1?r[A]:r[A]+m}return i}const{rowPtr:a,colIdx:l}=t,c=(w,m)=>l[m]!==w&&(m===a[w]||l[m]!==l[m-1]),u=new Float64Array(s);for(let w=0;w<s;w++)for(let m=a[w];m<a[w+1];m++)u[w]+=c(w,m)?1:0;const d=w=>{const m=new Float64Array(s);for(let A=0;A<s;A++){let p=u[A]*w[A];for(let y=a[A];y<a[A+1];y++)c(A,y)&&(p-=w[l[y]]);m[A]=p}return m},f=fc*2*Math.max(1,...u),h=new Jo(o??void 0),g=[new Float64Array(s).fill(1/Math.sqrt(s))];for(let w=0;w<e;w++){const m=Float64Array.from({length:s},()=>h.rand()-.5),A=Qo(m,g)?wc(d,m,g,f):m.fill(0);g.push(A);for(let p=0;p<s;p++)i[e*p+w]=A[p]}Mt(i,e,n);for(let w=0;w<s*e;w++)i[w]+=r[w%e];return i}function wc(t,e,n,r){let o=t(e),s=null;for(let i=0;i<hc;i++){const a=Ye(e,o),l=o.map((m,A)=>m-a*e[A]);if(Xe(l)<r)break;const c=[e];for(const m of s===null?[l]:[l,s])Qo(m,[...n,...c])&&c.push(m);const u=[o,...c.slice(1).map(t)],d=c.map(m=>u.map(A=>Ye(m,A))),f=pc(d),h=new Float64Array(e.length),g=new Float64Array(e.length),w=new Float64Array(e.length);c.forEach((m,A)=>{for(let p=0;p<e.length;p++)h[p]+=f[A]*m[p],g[p]+=f[A]*u[A][p],A>0&&(w[p]+=f[A]*m[p])}),e.set(h),o=g,s=w}return e}function pc(t){const e=t.length,n=t.map((o,s)=>t.map((i,a)=>s===a?1:0));for(let o=0;o<50;o++)for(let s=0;s<e;s++)for(let i=s+1;i<e;i++)mc(t,n,s,i);let r=0;for(let o=1;o<e;o++)t[o][o]<t[r][r]&&(r=o);return n.map(o=>o[r])}function mc(t,e,n,r){if(Math.abs(t[n][r])<1e-300)return;const o=(t[r][r]-t[n][n])/(2*t[n][r]),s=Math.sign(o||1)/(Math.abs(o)+Math.sqrt(o*o+1)),i=1/Math.sqrt(s*s+1),a=s*i;for(const l of[t,e])for(const c of l)[c[n],c[r]]=[i*c[n]-a*c[r],a*c[n]+i*c[r]];[t[n],t[r]]=[t[n].map((l,c)=>i*l-a*t[r][c]),t[n].map((l,c)=>a*l+i*t[r][c])]}function Qo(t,e){const n=Xe(t);for(let o=0;o<2;o++)for(const s of e){const i=Ye(t,s);for(let a=0;a<t.length;a++)t[a]-=i*s[a]}const r=Xe(t);if(r<=1e-8*n)return!1;for(let o=0;o<t.length;o++)t[o]/=r;return!0}function Ye(t,e){let n=0;for(let r=0;r<t.length;r++)n+=t[r]*e[r];return n}function Xe(t){let e=0;for(const n of t)e+=n*n;return Math.sqrt(e)}function yc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(gc(Lt(t),r,o,s,e.seed??null),r,n)}function Ac(t,e,n,r,o){const s=new Float64Array(2*t);if(o){let l=r;l+=1/(.5*l);for(let c=0;c<t;c++){const u=.5*l;l+=1/u,s[2*c]=yt(l)*u,s[2*c+1]=wt(l)*u}}else for(let i=0;i<t;i++)s[2*i]=yt(r*i)*i,s[2*i+1]=wt(r*i)*i;return Mt(s,2,e,n)}function vc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),i=Ac(n,o,s,e.resolution??.35,e.equidistant??!1);return ht(It(i,r,s),r,n)}function Ec(t,e,n){switch(t){case"forceatlas2":return n?.forceAtlas2!==void 0?n.forceAtlas2(e):new Nl(e);case"fruchtermanReingold":case"spring":return n?.fruchtermanReingold!==void 0?n.fruchtermanReingold(e):new Ml(e);case"spring-electrical":if(n?.springElectrical===void 0)throw new Error('createSimulation: "spring-electrical" has no CPU simulation; inject an accelerator that implements springElectrical');return n.springElectrical(e);default:{const r=t;throw new Error(`createSimulation: unknown simulation type ${String(r)}`)}}}const bc={animate:!1,animationDuration:500,fit:!0,padding:30,randomize:!0,refresh:1};function Cc(t,e){const n=new Map;return t.nodes.forEach((o,s)=>{const i=o.data(e);if(typeof i=="number"||typeof i=="string"){const a=n.get(i);a===void 0?n.set(i,[s]):a.push(s)}}),[...n.keys()].sort((o,s)=>typeof o=="number"&&typeof s=="number"?o-s:String(o).localeCompare(String(s))).map(o=>n.get(o)??[])}const ts={random:(t,e)=>dc(t,e),circular:(t,e)=>ml(t,e),spiral:(t,e)=>vc(t,e),grid:(t,e)=>Ol(t,e),spectral:(t,e)=>yc(t,e),planar:(t,e)=>rc(t,e),arf:(t,e)=>ul(t,e),"kamada-kawai":(t,e)=>Gl(t,e),shell:(t,e,n)=>{const r=e.nlist;return oc(t,{...e,nlist:r?.map(o=>ft(n,o))})},multipartite:(t,e,n)=>{const r=e.subsets??"subset",o=typeof r=="string"?Cc(n,r):r.map(s=>ft(n,s));return dl(t,{...e,subsets:o})},bipartite:(t,e,n)=>{if(e.top===void 0)return or(t,e);const r=ct(t.nodeCount);for(const o of ft(n,e.top))bt(r,o,!0);return or(t,{...e,top:r})},bfs:(t,{root:e,...n},r)=>hl(t,{...n,start:Rt(r,e,"graphty layout: root")}),radial:(t,e,n)=>cc(t,{...e,root:Rt(n,e.root,"graphty layout: root")})},Ic={forceatlas2:{maxIter:100},fruchtermanReingold:{iterations:50}},es={forceatlas2:"forceatlas2","fruchterman-reingold":"fruchtermanReingold","spring-electrical":"spring-electrical"},$c=[...Object.keys(ts),...Object.keys(es)];function ns(t,e){if(t===void 0){const n=r=>Number.isFinite(r)?r:1;return{x1:0,y1:0,w:n(e.width()),h:n(e.height())}}return"w"in t?t:{x1:t.x1,y1:t.y1,w:t.x2-t.x1,h:t.y2-t.y1}}function mn(t){return{radius:Math.max(Math.min(t.w,t.h)/2,1),center:[t.x1+t.w/2,t.y1+t.h/2,0]}}function rs(t,e,n,r){const o=new Float64Array(2*n);for(let a=0;a<n;a++)o[2*a]=t[e*a],o[2*a+1]=t[e*a+1];const{radius:s,center:i}=mn(r);return Mt(o,2,s,i)}function xc(t,e,n,r){let o=0,s=0,i=0;for(let f=0;f<n;f++)L(e,f)&&(o+=t[3*f],s+=t[3*f+1],i++);o/=i,s/=i;const a=o>=r.x1&&o<=r.x1+r.w&&s>=r.y1&&s<=r.y1+r.h,l=(f,h,g,w)=>h===0?1/0:((h>0?w:g)-f)/h;let c=1/0,u=0;for(let f=0;f<n;f++)if(!L(e,f)){const h=t[3*f]-o,g=t[3*f+1]-s;c=Math.min(c,l(o,h,r.x1,r.x1+r.w),l(s,g,r.y1,r.y1+r.h)),u=Math.max(u,Math.hypot(h,g))}a?Number.isFinite(c)||(c=1):c=u>0?mn(r).radius/u:1;const d=new Float64Array(2*n);for(let f=0;f<n;f++){const h=L(e,f)?1:c;d[2*f]=o+h*(t[3*f]-o),d[2*f+1]=s+h*(t[3*f+1]-s)}return d}function Pc(t,e){const n={};for(const r of["nodeMass","nodeSize"]){const o=e[r];if(Array.isArray(o)||ArrayBuffer.isView(o)&&!(o instanceof Float32Array))n[r]=Float32Array.from(o);else if(typeof o=="string"){const s={};t.nodes.forEach(i=>{const a=i.data(o);typeof a=="number"&&Number.isFinite(a)&&(s[i.id()]=a)}),n[r]=s}}return n}function os(t){return(e,n)=>Number.isNaN(t[2*n])?e.position():{x:t[2*n],y:t[2*n+1]}}function ss(t,e,n){const r=os(n);e.layoutPositions(t,t.options,r)}function is(t){return{...t,dim:t.dim??2,scale:1,center:void 0,seed:t.seed??null,weight:t.weight!==void 0}}function as(t){return fn(t.eles.not(":parent"),{weight:t.weight})}function ls(t,e){if((e?t.animate!==!0&&!!t.animate:!!t.animate)&&!t.cy.styleEnabled())throw new Error(`${t.name}: animate needs a core that renders; create a headless core with styleEnabled: true, or pass animate: false`)}function Nc(t,e){const n=t.options;ls(n,!1);const r=as(n),o=e(r.snapshot,is(n),r);ss(t,r.nodes,rs(o.positions,o.dim,o.n,ns(n.boundingBox,n.cy)))}function cs(t,e){for(;!t.settled&&!e.stopped&&!e.options.cy.destroyed();){const n=t.step(50);if(n instanceof Promise)return n.then(()=>cs(t,e))}}function Fc(t,e){const n=t.options;if(ls(n,!0),hn(n.name,n.gpu),t.stopped=!1,n.accelerator!==void 0){t.backend=n.accelerator===null?{ran:"cpu",reason:"accelerator: null was passed",device:null}:{ran:"gpu",reason:null,device:n.accelerator.kind},Te(t,e,n.accelerator);return}const r=n.gpu==="require"?null:Do(n.gpu);if(r!==null){t.backend=Ge(r,!1,""),je(r,n.eles.nodes().length),Te(t,e,null);return}t.looping=!0,$o(n.cy,n.gpu).then(o=>{if(t.looping=!1,t.backend=Ge(o,o.gpu!==null,""),je(o,n.eles.nodes().length),t.stopped||n.cy.destroyed()){t.emit({type:"layoutstop",layout:t});return}Te(t,e,o.gpu?.accelerator??null)}).catch(o=>{t.looping=!1,n.cy.destroyed()||t.emit("layouterror",[o]),t.emit({type:"layoutstop",layout:t})})}const Oe=new WeakMap;function lr(t,e){let n=0,r=0;for(let o=0;o<e;o++)n+=t[3*o],r+=t[3*o+1];return e===0?[0,0,0]:[n/e,r/e,0]}function Te(t,e,n){const r=t.options;if(n===null&&e==="spring-electrical"){const P=t.backend?.reason??"none was available";t.backend=void 0;const I=new Error(`graphty-spring-electrical has no CPU simulation and runs only on the GPU; no GPU ran because ${P}`);us(t,I);return}const o=as(r),s=o.snapshot,i=s.nodeCount,a=ns(r.boundingBox,r.cy),{radius:l,center:c}=mn(a),u=r.dim??2,d=new Float32Array(3*i),f=ct(i),h=new qo(r.seed??null);let g=!1;o.nodes.forEach((P,I)=>{const F=P.locked();g||=F,bt(f,I,F);const U=P.position(),R=F||r.randomize===!1;d[3*I]=R?U.x:a.x1+h.next()*a.w,d[3*I+1]=R?U.y:a.y1+h.next()*a.h,d[3*I+2]=R||u===2?0:(h.next()*2-1)*l});const w=r.randomize===!1&&!g?Oe.get(r.cy)?.get(e):void 0,m=w??l,A=Ec(e,{...Ic[e],...is(r),...Pc(o,r),scale:m,center:w===void 0?c:lr(d,i),iterationsPerStep:1},n);A.load(s,d),g&&A.setFixed(f);const p=_o(s),y=()=>{if(A.dispose(),p(),!g){const[P,I]=lr(d,i);let F=0;for(let U=0;U<i;U++)F=Math.max(F,Math.hypot(d[3*U]-P,d[3*U+1]-I));if(F>0){const U=Oe.get(r.cy)??new Map;U.set(e,m*l/F),Oe.set(r.cy,U)}}},v=()=>g?xc(d,f,i,a):rs(d,3,i,a),E=P=>{t.looping=!1,y(),r.cy.destroyed()||t.emit("layouterror",[P]),t.emit({type:"layoutstop",layout:t})};if(r.animate!==!0){const P=()=>{t.looping=!1,y(),r.cy.destroyed()?t.emit({type:"layoutstop",layout:t}):ss(t,o.nodes,v())},I=cs(A,t);I===void 0?P():(t.looping=!0,I.then(P,E));return}t.looping=!0,t.emit({type:"layoutstart",layout:t});let b=!0;const C=typeof requestAnimationFrame=="function"?requestAnimationFrame:P=>setTimeout(P,16),x=()=>{t.looping=!1,y(),r.stop!==void 0&&t.one("layoutstop",r.stop),t.emit({type:"layoutstop",layout:t})},_=()=>{if(r.cy.destroyed()){x();return}o.nodes.positions(os(v())),r.fit!==!1&&r.cy.fit(r.eles,r.padding),b&&(b=!1,r.ready!==void 0&&t.one("layoutready",r.ready),t.emit({type:"layoutready",layout:t})),A.settled||t.stopped?x():C(S)},S=()=>{if(t.stopped||r.cy.destroyed()){_();return}let P;try{P=A.step(r.refresh??1)}catch(I){E(I);return}P instanceof Promise?P.then(_,E):_()};S()}function us(t,e){t.looping=!0,Promise.resolve().then(()=>{t.looping=!1,t.emit("layouterror",[e]),t.emit({type:"layoutstop",layout:t})})}const ds=new WeakMap;function kc(){return this.options.cy.destroyed()?us(this,Kt()):ds.get(this)?.(this),this}function Sc(){return this.looping?this.stopped=!0:this.emit({type:"layoutstop",layout:this}),this}function cr(t,e){function n(o){this.options={...bc,...o},this.stopped=!1,this.looping=!1,ds.set(this,t)}const r={run:kc};return e&&(r.stop=Sc),Object.assign(n.prototype,r),n}function _c(t){for(const[e,n]of Object.entries(ts))t("layout",`graphty-${e}`,cr(r=>{Nc(r,n)},!1));for(const[e,n]of Object.entries(es))t("layout",`graphty-${e}`,cr(r=>{Fc(r,n)},!0))}const ur=Symbol.for("@graphty/cytoscape-extensions");function zc(t){const e=t;e[ur]!==!0&&(e[ur]=!0,_c(t),Za(t),el(t))}export{Rc as A,Oc as G,Ja as I,$c as L,Ta as a,Dc as b,Da as c,Tc as d,zc as g,fn as t,Qt as w};
1
+ import{I as M,r as V,m as L,a as fe,b as ct,c as bt,d as tt,f as dr,e as fs,g as hs,h as fr,i as gs,j as hr,k as ws,_ as Gt,l as ps,G as ms,n as gr,o as yt,p as wt,q as wr}from"./preload-helper-Crt_PPe8.js";function $(t,e){return Object.assign(t,{code:e})}function Re(t,e,n,r){const o=n===void 0?`${t}: ${String(e)} nodes need ${String(r)} bytes of n x n memory, more than this runtime could allocate. Pass a maxNodes to refuse such graphs up front.`:`${t}: ${String(e)} nodes exceeds maxNodes ${String(n)}; the result would allocate ${String(r)} bytes. Pass a larger maxNodes to allow it.`;return Object.assign($(new RangeError(o),"E_TOO_LARGE"),{params:{nodeCount:e,maxNodes:n??null,bytes:r}})}function pr(t,e,n,r,o){if(n!==void 0&&(e>n||Number.isNaN(n)))throw Re(t,e,n,r);try{return o()}catch(s){throw s instanceof RangeError?Re(t,e,void 0,r):s}}class mr extends Error{constructor(e,n,r){super(`${e} did not converge in ${String(n)} iterations (tolerance ${String(r)}); raise maxIterations or tolerance`),this.algorithm=e,this.iterations=n,this.tolerance=r,this.name="ConvergenceError",this.code="E_NOT_CONVERGED"}}class De extends Error{constructor(e,n,r){super(`cannot walk the shortest path from node ${String(e)} to node ${String(n)}: the predecessor array has a ${r}`),this.source=e,this.target=n,this.reason=r,this.name="PathWalkError",this.code="E_BAD_PATH"}}class ys extends Error{constructor(e){super(`${e}: the accelerator's shortest-path counts overflowed, so its scores are wrong; run the CPU ${e} instead`),this.algorithm=e,this.name="PathCountOverflowError",this.code="E_PATH_COUNT_OVERFLOW"}}function yr(t,e){let n=0,r=t.length-1;for(;n<r;){const o=n+r>>1;t[o+1]<=e?n=o+1:r=o}return n}class _t{constructor(e,n=!1){this.size=0,this.tieBreakByIndex=n,this.heap=new Uint32Array(e),this.slot=new Uint32Array(e).fill(M),this.key=new Float64Array(e)}isEmpty(){return this.size===0}has(e){return this.slot[e]!==M}keyOf(e){return this.key[e]}peekKey(){return this.size===0?1/0:this.key[this.heap[0]]}push(e,n){if(!(e>=0&&e<this.slot.length))throw new RangeError(`Node index ${String(e)} is outside the heap's ${String(this.slot.length)} nodes`);this.key[e]=n,this.heap[this.size]=e,this.slot[e]=this.size,this.size++,this.siftUp(this.size-1)}pushOrDecrease(e,n){const r=this.slot[e];if(r===M){this.push(e,n);return}n<this.key[e]&&(this.key[e]=n,this.siftUp(r))}pop(){const e=this.heap[0];if(this.slot[e]=M,this.size--,this.size>0){const n=this.heap[this.size];this.heap[0]=n,this.slot[n]=0,this.siftDown(0)}return e}before(e,n){const r=this.key[e],o=this.key[n];return r<o||this.tieBreakByIndex&&r===o&&e<n}siftUp(e){let n=e;const r=this.heap[n];for(;n>0;){const o=n-1>>1,s=this.heap[o];if(!this.before(r,s))break;this.heap[n]=s,this.slot[s]=n,n=o}this.heap[n]=r,this.slot[r]=n}siftDown(e){let n=e;const r=this.heap[n];for(;;){const o=2*n+1;if(o>=this.size)break;const s=o+1,i=s<this.size&&this.before(this.heap[s],this.heap[o])?s:o,a=this.heap[i];if(!this.before(a,r))break;this.heap[n]=a,this.slot[a]=n,n=i}this.heap[n]=r,this.slot[r]=n}}function Ar(t,e,n,r){const o=[];let s=r;for(;s!==n;){if(o.length>=t.nodeCount-1)throw new De(n,r,"cycle");const i=e[s];if(!(i<t.arcCount))throw new De(n,r,"gap");o.push(i),s=yr(t.rowPtr,i)}return o}function zt(t,e,n,r){const o=V(t,r);if(o===n)return Uint32Array.of(n);if(e[o]===M)return new Uint32Array(0);const s=[o,...Ar(t,e,n,o).map(a=>yr(t.rowPtr,a))],i=new Uint32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[s.length-1-a];return i}function Bt(t,e,n,r){const o=V(t,r);if(o===n||e[o]===M)return new Uint32Array(0);const s=Ar(t,e,n,o).map(a=>t.arcToEdge[a]),i=new Uint32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[s.length-1-a];return i}function vr(t,e,n={}){const r=V(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.weights??t.weights,l=new Float64Array(o).fill(1/0),c=new Uint32Array(o).fill(M),u=new _t(o),d=n.cutoff??1/0;for(l[r]=0,u.push(r,0);!u.isEmpty();){const f=u.pop(),h=l[f],g=s[f+1];for(let w=s[f];w<g;w++){const m=a===null?1:a[w],A=i[w],p=h+m;p<l[A]&&p<=d&&(l[A]=p,c[A]=w,u.pushOrDecrease(A,p))}}return{dist:l,predArc:c,pathTo:f=>zt(t,c,r,f),pathEdges:f=>Bt(t,c,r,f)}}const Er=5792;function As(t,e){if(e.weighted===!1)return{w:null,negative:!1};if(e.weights!==void 0&&e.weights.length!==t.arcCount)throw $(new RangeError(`allPairsShortestPath: the weights override has ${String(e.weights.length)} entries; the snapshot has ${String(t.arcCount)} arcs`),"E_BAD_OPTION");const n=e.weights??t.weights;if(n===null)return{w:null,negative:!1};let r=!0,o=!1;for(let s=0;s<t.arcCount;s++){const i=n[s];if(!Number.isFinite(i)){const a=e.weights===void 0&&!Number.isNaN(i)?"; a finite weight above the f32 range (3.4e38) becomes Infinity in the snapshot's f32 arc weights -- pass the exact f64 weights through the weights override":"";throw $(new RangeError(`allPairsShortestPath: arc ${String(s)} has weight ${String(i)}; weights must be finite${a}`),"E_BAD_WEIGHT")}r&&(r=i===1),o||(o=i<0)}return{w:r?null:n,negative:o}}function vs(t,e,n,r,o){const{nodeCount:s,rowPtr:i,colIdx:a}=t;r.fill(1/0);for(let l=0;l<s;l++){const c=l*s;for(let u=i[l];u<i[l+1];u++){const d=a[u],f=e===null?1:e[u];d!==l&&f<r[c+d]&&(r[c+d]=f,o!==null&&(o[c+d]=u))}r[c+l]=0}for(let l=0;l<s;l++){const c=l*s,u=r.subarray(c,c+s);for(let d=0;d<s;d++){const f=d*s,h=r[f+l];if(h===1/0)continue;const g=r.subarray(f,f+s);if(o===null)for(let w=0;w<g.length;w++){const m=h+u[w];m<g[w]&&(g[w]=m)}else{const w=o.subarray(c,c+s),m=o.subarray(f,f+s);for(let A=0;A<g.length;A++){const p=h+u[A];p<g[A]&&(g[A]=p,m[A]=w[A])}}}if(n){for(let d=0;d<s;d++)if(r[d*s+d]<0)return!0}}return!1}function Es(t,e){for(let n=0;n<t.nodeCount;n++)for(let r=t.rowPtr[n];r<t.rowPtr[n+1];r++)if(e[r]<0&&(!t.directed||t.colIdx[r]===n))return!0;return!1}function bs(t,e,n){const{nodeCount:r,rowPtr:o,colIdx:s}=t,i=new Uint32Array(r);e.fill(1/0);for(let a=0;a<r;a++){const l=e.subarray(a*r,a*r+r),c=n?.subarray(a*r,a*r+r);l[a]=0,i[0]=a;let u=1;for(let d=0;d<u;d++){const f=i[d],h=l[f]+1;for(let g=o[f];g<o[f+1];g++){const w=s[g];l[w]===1/0&&(l[w]=h,c!==void 0&&(c[w]=g),i[u++]=w)}}}}function Cs(t,e,n,r){const{nodeCount:o,rowPtr:s,colIdx:i}=t,a=new _t(o);n.fill(1/0);for(let l=0;l<o;l++){const c=n.subarray(l*o,l*o+o),u=r?.subarray(l*o,l*o+o);for(c[l]=0,a.push(l,0);!a.isEmpty();){const d=a.pop(),f=c[d];for(let h=s[d];h<s[d+1];h++){const g=i[h],w=f+e[h];w<c[g]&&(c[g]=w,u!==void 0&&(u[g]=h),a.pushOrDecrease(g,w))}}}}function Is(t,e,n,r){if(r==="floyd-warshall")return"floyd-warshall";if(e===null)return"bfs";if(n){if(r==="per-source")throw $(new Error('allPairsShortestPath: method "per-source" runs Dijkstra, which is incorrect with a negative weight; use "auto" or "floyd-warshall"'),"E_BAD_OPTION");return"floyd-warshall"}const o=t.nodeCount;return r==="per-source"||3*t.arcCount<o*o?"dijkstra":"floyd-warshall"}function br(t,e={}){const n=t.nodeCount,r=e.maxNodes??Er;if(!(n<=r))throw Re("allPairsShortestPath",n,r,(e.paths===!0?12:8)*n*n);const{w:o,negative:s}=As(t,e),i=Is(t,o,s,e.method),a=new Float64Array(n*n),l=e.paths===!0?new Uint32Array(n*n).fill(M):null;let c=!1;i==="bfs"?bs(t,a,l):i==="dijkstra"&&o!==null?Cs(t,o,a,l):c=s&&o!==null&&Es(t,o)||vs(t,o,s,a,l),c&&a.fill(Number.NaN);const u=(d,f)=>{const h=V(t,d),g=V(t,f);if(l===null)throw $(new Error("allPairsShortestPath: pass paths: true to walk shortest paths"),"E_BAD_OPTION");if(c)throw new De(h,g,"cycle");return l.subarray(h*n,h*n+n)};return{dist:a,n,hasNegativeCycle:c,method:i,predArc:l,pathTo:(d,f)=>zt(t,u(d,f),V(t,d),f),pathEdges:(d,f)=>Bt(t,u(d,f),V(t,d),f)}}function Cr(t,e,n={}){const r=V(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.weights??t.weights,l=n.cutoff??1/0,c=new Float64Array(o).fill(1/0),u=new Uint32Array(o).fill(M);c[r]=0;const d=()=>{let g=!1;for(let w=0;w<o;w++){const m=c[w];if(m===1/0)continue;const A=s[w+1];for(let p=s[w];p<A;p++){const y=i[p],v=m+(a===null?1:a[p]);v<c[y]&&v<=l&&(c[y]=v,u[y]=p,g=!0)}}return g};let f=!1;for(let g=0;g<o-1&&!f;g++)f=!d();const h=!f&&d();return{dist:c,predArc:u,hasNegativeCycle:h,pathTo:g=>zt(t,u,r,g),pathEdges:g=>Bt(t,u,r,g)}}const xs=2654435769;function Ps(t,e){const n=Array.from({length:t},(o,s)=>s);let r=xs;for(let o=0;o<e;o++){r=r+1831565813>>>0;let s=Math.imul(r^r>>>15,r|1);s=s+Math.imul(s^s>>>7,s|61)^s;const i=((s^s>>>14)>>>0)/2**32,a=o+Math.floor(i*(t-o));[n[o],n[a]]=[n[a],n[o]]}return n.slice(0,e)}function Jt(t,e,n,r="betweenness"){const o=t.nodeCount,s=e===void 0?void 0:Array.from(fe(t,e));if(n!==void 0&&(!Number.isInteger(n)||n<0||n>o))throw $(new RangeError(`${r}: k must be an integer in [0, ${o}], got ${n}`),"E_BAD_OPTION");if(s!==void 0){for(const i of s)if(!Number.isInteger(i)||i<0||i>=o)throw $(new RangeError(`${r}: sources must be node indices in [0, ${o}), got ${i}`),"E_BAD_NODE");if(n!==void 0&&n!==s.length)throw $(new RangeError(`${r}: k (${n}) must be absent or equal sources.length (${s.length})`),"E_BAD_OPTION");return s}return n===void 0?Array.from({length:o},(i,a)=>a):Ps(o,n)}function Ir(t,e,n,r,o,s){const{nodeCount:i,rowPtr:a,colIdx:l,arcToEdge:c}=t,u=new Int32Array(i).fill(-1),d=new Float64Array(i),f=new Float64Array(i),h=new Uint32Array(i);for(const g of e){let w=0,m=0;for(u[g]=0,d[g]=1,h[m++]=g;w<m;){const A=h[w++],p=u[A]+1;let y=-1;for(let v=a[A],E=a[A+1];v<E;v++){const b=l[v];b===y||o!==null&&!L(o,c[v])||(y=b,u[b]<0&&(u[b]=p,h[m++]=b),u[b]===p&&(d[b]+=d[A]))}}for(let A=m-1;A>=0;A--){const p=h[A],y=u[p]+1;let v=-1;for(let E=a[p],b=a[p+1];E<b;E++){const C=l[E];if(!(C===v||o!==null&&!L(o,c[E]))&&(v=C,u[C]===y)){const x=d[p]/d[C]*(1+f[C]);f[p]+=x,r!==null&&(r[c[E]]+=x)}}n!==null&&p!==g&&(n[p]+=s?f[p]+1:f[p])}n!==null&&s&&(n[g]+=m-1);for(let A=0;A<m;A++){const p=h[A];u[p]=-1,d[p]=0,f[p]=0}}}function xr(t,e,n,r){const o=t.directed?1:2,s=n===!0&&r>0?r:o;for(let i=0;i<e.length;i++)e[i]/=s;return s}function Pr(t,e={}){const n=t.nodeCount,r=Jt(t,e.sources,e.k),o=new Float64Array(n),s=e.endpoints===!0;Ir(t,r,o,null,null,s);const i=xr(t,o,e.normalized,s?n*(n-1):(n-1)*(n-2));return{scores:o,iterations:r.length,converged:!0,divisor:i}}function Ze(t,e={}){const n=t.nodeCount,r=e.alive??null;if(r!==null&&r.length<Math.ceil(t.edgeCount/32))throw $(new RangeError(`edgeBetweennessCentrality: alive covers fewer than ${t.edgeCount} edges`),"E_BAD_OPTION");const o=new Float64Array(t.edgeCount);Ir(t,Jt(t,e.sources,e.k),null,o,r,!1);const s=xr(t,o,e.normalized,(n-1)*(n-2));return{scores:o,divisor:s}}function Je(t,e,n){return{pathTo:r=>zt(t,n,e,r),pathEdges:r=>Bt(t,n,e,r)}}function he(t,e,n="start"){const r=V(t,e);if(!Number.isInteger(r)||r<0||r>=t.nodeCount)throw $(new RangeError(`${n} node index ${String(r)} is out of range for ${String(t.nodeCount)} nodes`),"E_BAD_NODE");return r}function Nr(t,e){return e===void 0?M:he(t,e,"target")}function ge(t,e){if(e===void 0)return null;const{rowPtr:n,nodeCount:r}=t;if(e.length!==t.arcCount)throw $(new RangeError(`arcOrder has ${String(e.length)} entries, expected ${String(t.arcCount)}`),"E_BAD_OPTION");const o=new Uint8Array(e.length);for(let s=0;s<r;s++){const i=n[s],a=n[s+1];for(let l=i;l<a;l++){if(e[l]<i||e[l]>=a)throw $(new RangeError(`arcOrder[${String(l)}] = ${String(e[l])} is not an arc of node ${String(s)}`),"E_BAD_OPTION");if(o[e[l]]===1)throw $(new RangeError(`arcOrder[${String(l)}] = ${String(e[l])} repeats an arc of node ${String(s)}`),"E_BAD_OPTION");o[e[l]]=1}}return e}function Fr(t,e,n={}){const r=he(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=ge(t,n.arcOrder),l=new Uint32Array(o).fill(M),c=new Uint32Array(o).fill(M),u=new Uint32Array(o).fill(M),d=new Uint32Array(o),f=n.maxDepth??M,h=Nr(t,n.target);let g=0,w=0;for(d[w++]=r,u[r]=0;g<w;){const m=d[g++];if(m===h)break;const A=u[m];if(A>=f)continue;const p=s[m+1];for(let y=s[m];y<p;y++){const v=a===null?y:a[y],E=i[v];u[E]===M&&(u[E]=A+1,l[E]=m,c[E]=v,d[w++]=E)}}return{order:d.subarray(0,w),parent:l,depth:u,visitedCount:w,predArc:c,...Je(t,r,c)}}function Ns(t,e,n,r){const{rowPtr:o,colIdx:s,arcToEdge:i}=t;let a=o[n],l=o[n+1];for(;a<l;){const u=a+l>>>1;s[u]<r?a=u+1:l=u}const c=o[n+1];for(;a<c&&s[a]===r&&i[a]!==i[e];)a++;return a}function Fs(t,e,n={}){const r=he(t,e),{nodeCount:o,rowPtr:s,colIdx:i}=t,a=n.alpha??1,l=n.beta??18;let c=null;const u=new Uint32Array(o).fill(M),d=new Uint32Array(o).fill(M),f=new Uint32Array(o).fill(M),h=new Uint32Array(o);h[0]=r,f[r]=0;let g=0,w=1;const m=t.inDegree();let A=t.arcCount-m[r],p=!1;for(let y=0;g<w;y++){const v=w-g;if(p)p=v>=o/l;else{let b=0;for(let C=g;C<w;C++)b+=s[h[C]+1]-s[h[C]];p=b>A/a}const E=w;if(p){c??(c=t.reverse());for(let b=0;b<o;b++){if(f[b]!==M)continue;const C=c.rowPtr[b+1];for(let x=c.rowPtr[b];x<C;x++){const _=c.colIdx[x];if(f[_]===y){u[b]=_,d[b]=t.directed?c.fwdArc[x]:Ns(t,x,_,b),f[b]=y+1,h[w++]=b;break}}}}else{for(let b=g;b<E;b++){const C=h[b],x=s[C+1];for(let _=s[C];_<x;_++){const S=i[_];f[S]===M&&(u[S]=C,d[S]=_,f[S]=y+1,h[w++]=S)}}h.subarray(E,w).sort()}for(let b=E;b<w;b++)A-=m[h[b]];g=E}return{order:h.subarray(0,w),parent:u,depth:f,visitedCount:w,predArc:d,...Je(t,r,d)}}const Ie=2;function kr(t,e={}){const{nodeCount:n}=t,r=t.directed&&e.arcs!=="out"?[t,t.reverse()]:[t],o=new Uint8Array(n).fill(Ie),s=new Uint32Array(n);for(let a=0;a<n;a++){if(o[a]!==Ie)continue;o[a]=0;let l=0,c=0;for(s[c++]=a;l<c;){const u=s[l++];for(const{rowPtr:d,colIdx:f}of r){const h=d[u+1];for(let g=d[u];g<h;g++){const w=f[g];if(o[w]===Ie)o[w]=o[u]^1,s[c++]=w;else if(o[w]===o[u])return{bipartite:!1,sides:null}}}}}const i=ct(n);for(let a=0;a<n;a++)o[a]===1&&bt(i,a,!0);return{bipartite:!0,sides:i}}function $e(t,e,n){const{rowPtr:r,colIdx:o,weights:s}=e,i=new Float64Array(t).fill(1/0),a=new Uint32Array(t),l=s===null?null:new _t(t),c=new Uint8Array(t);return{dist:i,reached:a,run(u){let d=0;if(i[u]=0,a[d++]=u,l===null||s===null){for(let f=0;f<d;f++){const h=a[f];if(!(i[h]>=n))for(let g=r[h],w=r[h+1];g<w;g++){const m=o[g];i[m]===1/0&&(i[m]=i[h]+1,a[d++]=m)}}return d}for(l.push(u,0);!l.isEmpty();){const f=l.pop();if(c[f]=1,!(i[f]>=n))for(let h=r[f],g=r[f+1];h<g;h++){const w=o[h];if(c[w]===1)continue;const m=i[f]+s[h];m<i[w]&&(i[w]===1/0&&(a[d++]=w),i[w]=m,l.pushOrDecrease(w,m))}}return d},clear(u){for(let d=0;d<u;d++)i[a[d]]=1/0,c[a[d]]=0}}}function Sr(t,e,n,r,o){return t.harmonic===!0?t.normalized===!0&&e>1?o/(e-1):o:r<=0?0:t.normalized===!0&&e>1?n/r/(e-1):1/r}function _r(t,e,n,r){const o=t.run(r);let s=0,i=0;for(let a=1;a<o;a++){const l=t.dist[t.reached[a]];s+=l,i+=l>0?1/l:0}return t.clear(o),Sr(e,n,o-1,s,i)}function ze(t,e,n){const r=e.weighted===!0;if(!n||!t.directed)return{rowPtr:t.rowPtr,colIdx:t.colIdx,weights:r?e.weights??t.weights:null};const o=t.reverse(),s=e.weights;let i=null;return r&&(i=s===void 0?o.weights:Float64Array.from(o.fwdArc,a=>s[a])),{rowPtr:o.rowPtr,colIdx:o.colIdx,weights:i}}function Mr(t,e={}){const n=t.nodeCount,r=e.cutoff??1/0,o=new Float64Array(n);if(e.sources===void 0&&e.k===void 0){const u=$e(n,ze(t,e,!1),r);for(let d=0;d<n;d++)o[d]=_r(u,e,n,d);return{scores:o,iterations:n,converged:!0,sourcesUsed:n}}const s=Jt(t,e.sources,e.k,"closenessCentrality"),i=$e(n,ze(t,e,!0),r),a=new Float64Array(n),l=new Float64Array(n),c=new Uint32Array(n);for(const u of s){const d=i.run(u);for(let f=1;f<d;f++){const h=i.reached[f],g=i.dist[h];a[h]+=g,l[h]+=g>0?1/g:0,c[h]+=1}i.clear(d)}for(let u=0;u<n;u++)o[u]=Sr(e,n,c[u],a[u],l[u]);return{scores:o,iterations:s.length,converged:!0,sourcesUsed:s.length}}function ks(t,e,n={}){const r=V(t,e);if(!Number.isInteger(r)||r<0||r>=t.nodeCount)throw $(new RangeError(`nodeClosenessCentrality: node must be an index in [0, ${t.nodeCount}), got ${r}`),"E_BAD_NODE");const o=$e(t.nodeCount,ze(t,n,!1),n.cutoff??1/0);return _r(o,n,t.nodeCount,r)}function Ur(t,e,n,r,o){let s=t.rowPtr[n];const i=t.rowPtr[n+1];let a=e.rowPtr[r];const l=e.rowPtr[r+1];let c=0;for(;s<i&&a<l;){const u=t.colIdx[s],d=e.colIdx[a];if(u===d)for(c+=o===void 0?1:o(u),s++,a++;a<l&&e.colIdx[a]===d;)a++;else u<d?s++:a++}return c}function Ss(t,e,n,r={}){return Ur(t,r.directed===!0?t.reverse():t,e,n)}class qt{constructor(e){this.parent=new Uint32Array(e),this.rank=new Uint32Array(e);for(let n=0;n<e;n++)this.parent[n]=n}find(e){let n=e;for(;this.parent[n]!==n;)this.parent[n]=this.parent[this.parent[n]],n=this.parent[n];return n}union(e,n){const r=this.find(e),o=this.find(n);return r===o?!1:(this.rank[r]<this.rank[o]?this.parent[r]=o:this.rank[r]>this.rank[o]?this.parent[o]=r:(this.parent[o]=r,this.rank[r]++),!0)}toLabels(){const e=new Uint32Array(this.parent.length);for(let n=0;n<e.length;n++)e[n]=this.find(n);return tt(e)}}function K(t,e){let n=null;return{labels:t,count:e,groups(){if(n===null){const r=new Uint32Array(e);for(let i=0;i<t.length;i++)r[t[i]]++;const o=[];for(let i=0;i<e;i++)o.push(new Uint32Array(r[i]));const s=new Uint32Array(e);for(let i=0;i<t.length;i++){const a=t[i];o[a][s[a]++]=i}n=o}return n}}}function Or(t){const e=new qt(t.nodeCount),n=t.edgeList();for(let s=0;s<t.edgeCount;s++)e.union(n.src[s],n.dst[s]);const{labels:r,count:o}=e.toLabels();return K(r,o)}function Tr(t){if(t.directed)throw $(new Error("Connected components requires an undirected graph. Use weaklyConnectedComponents, or pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");return Or(t)}function Rr(t){return Or(t)}function vn(t,e){const{rowPtr:n,colIdx:r}=t;for(let o=0;o<t.nodeCount;o++){let s=-1;for(let i=n[o],a=n[o+1];i<a;i++)r[i]!==s&&(s=r[i],e[o]++)}}function _s(t,e={}){const n=new Float64Array(t.nodeCount),r=t.directed?e.mode??"total":"out";r!=="in"&&vn(t,n),r!=="out"&&vn(t.reverse(),n);const o=t.nodeCount-1;if(e.normalized===!0&&o>0)for(let s=0;s<n.length;s++)n[s]/=o;return n}function Dr(t){const{src:e,dst:n}=t.edgeList(),r=new Uint32Array(t.nodeCount),o=new Uint32Array(t.nodeCount);for(let s=0;s<e.length;s++)o[e[s]]++,r[n[s]]++;return{inDegree:r,outDegree:o}}class $r{constructor(e){this.min=new _t(e,!0)}isEmpty(){return this.min.isEmpty()}has(e){return this.min.has(e)}keyOf(e){return-this.min.keyOf(e)}push(e,n){this.min.push(e,-n)}pushOrIncrease(e,n){this.min.pushOrDecrease(e,-n)}pop(){return this.min.pop()}}function zr(t){const e=new Uint32Array(t.arcCount),n=t.rowPtr.slice(0,t.nodeCount),{src:r}=t.edgeList();for(let o=0;o<t.edgeCount;o++)e[n[r[o]]++]=t.edgeToArc[o];return e}function Br(t,e,n){const r=new Float64Array(t.nodeCount);for(let o=0;o<t.nodeCount;o++){let s=0;for(let i=t.rowPtr[o];i<t.rowPtr[o+1];i++)s+=n===null?1:n[e[i]];r[o]=s}return r}function Qe(t){let e=0;for(let n=0;n<t.length;n++)e+=t[n];if(e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Ms(t,e,n){if(t===void 0)return null;if(t.length!==e)throw $(new RangeError(`${n} has ${String(t.length)} entries; the graph has ${String(e)} nodes`),"E_BAD_OPTION");const r=Float64Array.from(t);return Qe(r),r}function qr(t,e){if(e===void 0)return t.weights;if(e.length!==t.arcCount)throw $(new RangeError(`weights has ${String(e.length)} entries; the graph has ${String(t.arcCount)} arcs`),"E_BAD_OPTION");return e}function Us(t){if(t<0||t>1)throw $(new Error("Damping factor must be between 0 and 1"),"E_BAD_OPTION")}class Os{constructor(e,n={}){if(!e.directed)throw $(new Error("DeltaPageRank requires a directed graph"),"E_NEEDS_DIRECTED");this.s=e,this.order=zr(e),this.weights=qr(e,n.weights),this.outW=Br(e,this.order,this.weights),this.scores=new Float64Array(e.nodeCount),this.deltas=new Float64Array(e.nodeCount).fill(1/e.nodeCount),this.active=new Uint8Array(e.nodeCount).fill(1)}compute(e={}){return this.run(e,!1)}update(e,n={}){const{s:r}=this,o=r.reverse(),s=new Uint8Array(r.nodeCount);for(let i=0;i<e.length;i++){const a=e[i];if(!(!Number.isInteger(a)||a<0||a>=r.nodeCount)){s[a]=1;for(let l=o.rowPtr[a];l<o.rowPtr[a+1];l++)s[o.colIdx[l]]=1;for(let l=r.rowPtr[a];l<r.rowPtr[a+1];l++)s[r.colIdx[l]]=1}}return this.active=s,this.run(n,e.length>0)}run(e,n){const r=e.dampingFactor??.85,o=e.tolerance??1e-6,s=e.maxIterations??100,i=e.deltaThreshold??o/10;Us(r);const{s:a,order:l,scores:c}=this,u=a.nodeCount;if(u===0)return new Float64Array(0);const d=Ms(e.personalization,u,"personalization"),f=e.weighted===!0?this.weights:null,h=(1-r)/u;let g=this.active.reduce((w,m)=>w+m,0);for(let w=0;(g>0||w===0&&n)&&w<s;w++){const m=new Float64Array(u);let A=0;for(let y=0;y<u;y++)a.rowPtr[y]===a.rowPtr[y+1]&&(A+=c[y]+this.deltas[y]);if(A>0){const y=r*A/u;for(let v=0;v<u;v++)m[v]+=d===null?y:y*d[v]}for(let y=0;y<u;y++){const v=this.deltas[y];if(this.active[y]===0||Math.abs(v)<i)continue;c[y]+=v;const E=this.outW[y];if(E>0)for(let b=a.rowPtr[y];b<a.rowPtr[y+1];b++){const C=l[b];m[a.colIdx[C]]+=r*v*((f===null?1:f[C])/E)}}let p=0;g=0;for(let y=0;y<u;y++){m[y]+=d===null?h:(1-r)*d[y];const v=Math.abs(m[y]);p=Math.max(p,v),this.active[y]=v>=i?1:0,g+=this.active[y]}if(this.deltas=m,p<o)break}return Qe(c),c.slice()}}class Ts{constructor(e,n={}){if(!e.directed)throw $(new Error("PriorityDeltaPageRank requires a directed graph"),"E_NEEDS_DIRECTED");this.s=e,this.order=zr(e),this.weights=qr(e,n.weights),this.outW=Br(e,this.order,this.weights),this.scores=new Float64Array(e.nodeCount),this.deltas=new Float64Array(e.nodeCount).fill(1/e.nodeCount),this.heap=new $r(e.nodeCount);for(let r=0;r<e.nodeCount;r++)this.heap.push(r,1/e.nodeCount)}computeWithPriority(e={}){const n=e.dampingFactor??.85,r=e.tolerance??1e-6,o=e.maxIterations??100,s=e.deltaThreshold??r/10,{s:i,order:a,scores:l,deltas:c,heap:u}=this,d=i.nodeCount,f=e.weighted===!0?this.weights:null;let h=0,g=0;for(;!u.isEmpty()&&h<o;){const w=u.pop(),m=c[w];if(Math.abs(m)<s)continue;l[w]+=m,c[w]=0;const A=f===null?i.rowPtr[w+1]-i.rowPtr[w]:this.outW[w];if(A>0)for(let p=i.rowPtr[w];p<i.rowPtr[w+1];p++){const y=a[p],v=i.colIdx[y],E=c[v]+n*m*((f===null?1:f[y])/A);c[v]=E,Math.abs(E)>=s&&u.pushOrIncrease(v,Math.abs(E))}if(g++,g%1e3===0){let p=0;for(let y=0;y<d;y++)p=Math.max(p,Math.abs(c[y]));if(p<r)break}h++}for(let w=0;w<d;w++)Math.abs(c[w])>=s&&(l[w]+=c[w]);return Qe(l),l.slice()}}const tn=0,Be=1,Rs=2;function en(t){return{state:new Uint8Array(t),parent:new Uint32Array(t).fill(M),predArc:new Uint32Array(t).fill(M),depth:new Uint32Array(t).fill(M),cursor:new Uint32Array(t),stack:new Uint32Array(t),pre:new Uint32Array(t),post:new Uint32Array(t),preCount:0,postCount:0,backArc:!1}}function Ds(t,e,n,r){const{rowPtr:o,colIdx:s}=t,{state:i,parent:a,predArc:l,depth:c,cursor:u,pre:d}=n,f=u[e]++,h=r===null?f:r[f],g=s[h];return i[g]===tn?(i[g]=Be,a[g]=e,l[g]=h,c[g]=c[e]+1,u[g]=o[g],d[n.preCount++]=g,g):(i[g]===Be&&(n.backArc=!0),M)}function nn(t,e,n,r,o){const{rowPtr:s}=t,{state:i,depth:a,cursor:l,stack:c,pre:u,post:d}=n;let f=0;if(i[e]=Be,a[e]=0,l[e]=s[e],u[n.preCount++]=e,c[f++]=e,e!==r)for(;f>0;){const h=c[f-1];if(l[h]<s[h+1]){const g=Ds(t,h,n,o);if(g!==M&&(c[f++]=g,g===r))return}else i[h]=Rs,d[n.postCount++]=h,f--}}function Vr(t,e,n={}){const r=he(t,e),o=ge(t,n.arcOrder),s=Nr(t,n.target),i=en(t.nodeCount),a=n.order==="post";return nn(t,r,i,a?M:s,o),{order:a?i.post.subarray(0,i.postCount):i.pre.subarray(0,i.preCount),parent:i.parent,depth:i.depth,visitedCount:i.preCount,predArc:i.predArc,...Je(t,r,i.predArc)}}function $s(t){return t.directed?zs(t):Bs(t)}function zs(t){const{nodeCount:e}=t,n=en(e);for(let r=0;r<e&&!n.backArc;r++)n.state[r]===tn&&nn(t,r,n,M,null);return n.backArc}function Bs(t){const{nodeCount:e,rowPtr:n,colIdx:r}=t,o=new qt(e);for(let s=0;s<e;s++){const i=n[s+1];for(let a=n[s];a<i;a++){const l=r[a];if(l===s||l>s&&(a===n[s]||r[a-1]!==l)&&!o.union(s,l))return!0}}return!1}function qs(t,e={}){if(!t.directed)throw $(new Error("Topological sort requires a directed graph"),"E_NEEDS_DIRECTED");const n=ge(t,e.arcOrder),r=en(t.nodeCount);for(let o=0;o<t.nodeCount&&!r.backArc;o++)r.state[o]===tn&&nn(t,o,r,M,n);return r.backArc?null:r.post.reverse()}function Wr(t,e={}){const n=t.nodeCount,r=e.maxIterations??100,o=e.tolerance??1e-6,s=e.startVector;if(s!==void 0&&s.length!==n)throw $(new Error(`eigenvectorCentrality: startVector has ${String(s.length)} entries for ${String(n)} nodes`),"E_BAD_OPTION");let i=new Float64Array(n);if(n===0)return{scores:i,iterations:0,converged:!0};const a=Vs(t,e.mode??"in");if(Ws(a,n))return{scores:i,iterations:0,converged:!0};for(let d=0;d<n;d++)a.rowPtr[d+1]>a.rowPtr[d]&&(i[d]=s===void 0?1:s[d]);En(i);let l=new Float64Array(n),c=0,u=!1;for(;c<r&&!u;c++){for(let f=0;f<n;f++){let h=i[f];const g=a.rowPtr[f+1];for(let w=a.rowPtr[f];w<g;w++)h+=i[a.colIdx[w]];l[f]=h}En(l);let d=0;for(let f=0;f<n;f++)d+=Math.abs(l[f]-i[f]);[i,l]=[l,i],u=d<n*o}if(!u)throw new mr("eigenvectorCentrality",r,o);return e.normalized!==!1&&Lr(i),{scores:i,iterations:c,converged:u}}function Lr(t){let e=1/0,n=0;for(const s of t)e=Math.min(e,s),n=Math.max(n,s);const r=n-e,o=n>0?1:0;for(let s=0;s<t.length;s++)t[s]=r>0?(t[s]-e)/r:o}function Vs(t,e){const n=t.nodeCount,r=!t.directed||e==="out"?t:t.reverse(),o=t.directed&&e==="total",s=o?t.rowPtr:new Uint32Array(n+1),i=o?t.colIdx:new Uint32Array(0),a=new Uint32Array(n+1),l=new Uint32Array(r.colIdx.length+i.length);let c=0;for(let u=0;u<n;u++){let d=r.rowPtr[u];const f=r.rowPtr[u+1];let h=s[u];const g=s[u+1],w=c;for(;d<f||h<g;){const m=h>=g||d<f&&r.colIdx[d]<=i[h]?r.colIdx[d++]:i[h++];(c===w||l[c-1]!==m)&&(l[c++]=m)}a[u+1]=c}return{rowPtr:a,colIdx:l.subarray(0,c)}}function Ws(t,e){const n=new Uint32Array(e);for(const s of t.colIdx)n[s]++;const r=new Uint32Array(e);let o=0;for(let s=0;s<e;s++)n[s]===0&&(r[o++]=s);for(let s=0;s<o;s++){const i=r[s];for(let a=t.rowPtr[i];a<t.rowPtr[i+1];a++)--n[t.colIdx[a]]===0&&(r[o++]=t.colIdx[a])}return o===e}function En(t){let e=0;for(const n of t)e+=n*n;if(e=Math.sqrt(e),e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Dt(t,e){const n=new Uint32Array(e),r=e>0&&L(t,0);let o=!1;for(let s=0;s<e;s++)L(t,s)!==r&&(n[s]=1,o=!0);return e===0?K(n,0):K(n,o?2:1)}function rn(t,e){const n=t.edgeList(),r=new Float64Array(t.edgeCount);for(let o=0;o<t.edgeCount;o++)r[o]=e!==void 0?e[n.arc[o]]:n.weights?.[o]??1;return r}function on(t,e,n,r,o){const{src:s,dst:i}=t.edgeList(),a=[];for(let l=0;l<t.edgeCount;l++){const c=L(e,s[l]);(n?c&&!L(e,i[l]):c!==L(e,i[l]))&&(!o||r[l]>0)&&a.push(l)}return Uint32Array.from(a)}function Ls(t,e){const n=t.nodeCount,r=t.edgeCount,{src:o,dst:s}=t.edgeList(),i=new Uint32Array(2*r),a=new Uint32Array(2*r);i.set(o),i.set(s,r),a.set(s),a.set(o,r);const l=dr({directed:!0,nodeCount:n,src:i,dst:a}),{rowPtr:c,colIdx:u,arcToEdge:d,edgeToArc:f}=l,h=l.arcCount,g=new Float64Array(h),w=new Uint32Array(h),m=new Uint8Array(h);for(let b=0;b<n;b++)for(let C=c[b];C<c[b+1];C++){const x=C>c[b]&&u[C]===u[C-1]?w[C-1]:C;w[C]=x;const _=d[C];(_<r||!t.directed)&&(m[x]=1,g[x]+=Math.max(e[_<r?_:_-r],0))}const A=new Uint32Array(h);for(let b=0;b<h;b++)if(w[b]===b){const C=d[b];A[b]=w[f[C<r?C+r:C-r]]}const p=new Uint32Array(h),y=new Uint32Array(n),v=new Uint8Array(h),E=(b,C)=>{v[C]===0&&(v[C]=1,p[c[b]+y[b]++]=C)};for(let b=0;b<r;b++)E(o[b],w[f[b]]),t.directed||E(s[b],w[f[b+r]]);return{rowPtr:c,colIdx:u,arcToEdge:d,capacity:g,groupOf:w,twin:A,order:p,placed:y,inOrder:v,real:m}}function sn(t,e,n,r={}){const o=V(t,e),s=V(t,n),i=t.nodeCount;if(!(Number.isInteger(o)&&o>=0&&o<i&&Number.isInteger(s)&&s>=0&&s<i))throw $(new RangeError(`source ${String(o)} and sink ${String(s)} must be node indices below ${i}`),"E_BAD_NODE");if(o===s)throw $(new RangeError(`source and sink are the same node (${o})`),"E_BAD_OPTION");const a=rn(t,r.weights),l=Ls(t,a),c=new Float64Array(l.colIdx.length),u=new Uint32Array(i),d=r.algorithm==="ford-fulkerson"?Ks:Hs;let f=0;for(;d(l,o,s,u);)f+=js(l,o,s,u,c);const h=Ys(l,o);return{...Dt(h,t.nodeCount),maxFlow:f,flow:Gs(t,l,a,c),sourceSide:h,cutEdges:on(t,h,t.directed,a,!0)}}function js(t,e,n,r,o){const{colIdx:s,twin:i}=t;let a=1/0;for(let l=n;l!==e;l=s[i[r[l]]])a=Math.min(a,t.capacity[r[l]]);for(let l=n;l!==e;){const c=r[l],u=i[c];t.capacity[c]-=a,t.capacity[u]+=a,t.inOrder[u]===0&&(t.inOrder[u]=1,t.order[t.rowPtr[l]+t.placed[l]++]=u),t.real[c]===1?o[c]+=a:o[u]-=a,l=s[u]}return a}function Gs(t,e,n,r){const o=t.edgeCount,s=new Float64Array(o),{rowPtr:i,colIdx:a,arcToEdge:l,groupOf:c}=e;for(let u=0;u<t.nodeCount;u++)for(let d=i[u];d<i[u+1];d++){const f=a[d];if(c[d]!==d||f<=u)continue;const h=r[d]-r[e.twin[d]];if(h===0)continue;const g=h>0?d:e.twin[d],w=i[a[e.twin[g]]+1];let m=Math.abs(h),A=-1,p=1;for(let y=g;y<w&&c[y]===g;y++){const v=l[y];if(!(v>=o&&t.directed)){if(A>=0){const E=Math.min(m,Math.max(n[A],0));s[A]+=p*E,m-=E}A=v<o?v:v-o,p=v<o?1:-1}}A>=0&&(s[A]+=p*m)}return s}function jr(t,e,n,r={}){const o=V(t,e),s=V(t,n),i=sn(t,o,s,{...r,algorithm:r.algorithm??"ford-fulkerson"});return{...Dt(i.sourceSide,t.nodeCount),cutValue:i.maxFlow,side:i.sourceSide,cutEdges:i.cutEdges}}function Hs(t,e,n,r){const{rowPtr:o,colIdx:s,order:i,placed:a,capacity:l}=t;r.fill(M);const c=new Uint32Array(r.length);let u=0,d=0;for(c[d++]=e;u<d;){const f=c[u++],h=o[f]+a[f];for(let g=o[f];g<h;g++){const w=i[g],m=s[w];if(l[w]>0&&m!==e&&r[m]===M){if(r[m]=w,m===n)return!0;c[d++]=m}}}return!1}function Ks(t,e,n,r){const{rowPtr:o,colIdx:s,order:i,placed:a,capacity:l}=t,c=r.length;r.fill(M);const u=new Uint8Array(c),d=new Uint32Array(c),f=new Uint32Array(c);let h=0;for(d[h++]=e,u[e]=1,f[e]=o[e];h>0;){const g=d[h-1],w=f[g]++;if(w>=o[g]+a[g]){h--;continue}const m=i[w],A=s[m];if(l[m]>0&&u[A]===0){if(r[A]=m,A===n)return!0;u[A]=1,f[A]=o[A],d[h++]=A}}return!1}function Ys(t,e){const{rowPtr:n,colIdx:r,order:o,placed:s,capacity:i}=t,a=n.length-1,l=ct(a),c=new Uint32Array(a);let u=0,d=0;for(c[d++]=e,bt(l,e,!0);u<d;){const f=c[u++],h=n[f]+s[f];for(let g=n[f];g<h;g++){const w=r[o[g]];i[o[g]]>0&&!L(l,w)&&(bt(l,w,!0),c[d++]=w)}}return l}function Vt(t){let e=t>>>0;return()=>{e=e+1831565813|0;let n=Math.imul(e^e>>>15,1|e);return n=n+Math.imul(n^n>>>7,61|n)^n,((n^n>>>14)>>>0)/4294967296}}const Xs=2147483647;function Zs(t,e){for(let n=0;n<t.nodeCount;n++){const r=t.rowPtr[n+1];for(let o=t.rowPtr[n];o<r;o++)if(t.colIdx[o]!==n&&(e===null||e[o]>0))return!0}return!1}function Gr(t,e,n){for(let r=0;r<e.length;r++){if(n!==null&&n[r]===1)continue;const o=t.collect(r,e);if(o!==0&&t.voteOf(e[r])!==o)return!1}return!0}function qe(t,e){if(e===null)return t.weights;const n=new Float64Array(t.arcCount);for(let r=0;r<n.length;r++)n[r]=e[t.arcToEdge[r]];return n}function ce(t){const e=t.edges.byRole("weight");return e!==null&&e.dtype==="f64"?e.data:null}function an(t,e){const n=e?qe(t,ce(t)):null;if(n!==null)for(let o=0;o<n.length;o++){const s=n[o];if(!(s>=0)||s===1/0)throw $(new RangeError(`arc ${o} has weight ${s}; label propagation needs finite, non-negative weights`),"E_BAD_WEIGHT")}const r=t.directed?t.reverse():null;return{sides:r===null?1:2,rowPtrs:[t.rowPtr,r===null?t.rowPtr:r.rowPtr],colIdxs:[t.colIdx,r===null?t.colIdx:r.colIdx],arcWeights:[n,n===null||r===null?n:qe(r,ce(t))],weights:n}}function ln(t,e,n){if(!Number.isInteger(e)||e<0)throw $(new RangeError(`maxIterations must be a non-negative integer, got ${e}`),"E_BAD_OPTION");if(n!==null&&!Number.isInteger(n))throw $(new RangeError(`randomSeed must be a finite integer, got ${n}`),"E_BAD_OPTION");if(e*t>Number.MAX_SAFE_INTEGER)throw $(new RangeError(`maxIterations * nodeCount (${e} * ${t}) exceeds Number.MAX_SAFE_INTEGER`),"E_BAD_OPTION")}class cn{constructor(e,n,r){this.rows=n,this.epoch=-1,this.count=0,this.acc=new Float64Array(e),this.stamp=new Int32Array(e).fill(-1),this.touched=new Uint32Array(e),this.seen=r?null:new Int32Array(e).fill(-1)}collect(e,n){++this.epoch===Xs&&(this.stamp.fill(-1),this.seen?.fill(-1),this.epoch=0);const{acc:r,stamp:o,touched:s,seen:i,rows:a}=this,l=this.epoch;let c=0;for(let d=0;d<a.sides;d++){const f=a.rowPtrs[d],h=a.colIdxs[d],g=a.arcWeights[d],w=f[e+1];for(let m=f[e];m<w;m++){const A=h[m];if(A===e)continue;if(i!==null){if(i[A]===l)continue;i[A]=l}const p=n[A];if(p===M)continue;const y=g===null?1:g[m];o[p]!==l?(o[p]=l,r[p]=y,s[c++]=p):r[p]+=y}}this.count=c;let u=0;for(let d=0;d<c;d++)r[s[d]]>u&&(u=r[s[d]]);return u}voteOf(e){return this.stamp[e]===this.epoch?this.acc[e]:0}}function Hr(t,e,n,r,o,s,i){const a=t.nodeCount,l=Vt(i),c=new cn(a,e,n),u=a+1,d=new Uint32Array(u),f=new Uint8Array(a);let h=0;for(let v=0;v<a;v++)(o===null||o[v]===0)&&(d[h++]=v,f[v]=1);for(let v=h-1;v>=1;v--){const E=Math.floor(l()*(v+1)),b=d[v];d[v]=d[E],d[E]=b}let g=0,w=h;const m=s*a;let A=0;const{touched:p,acc:y}=c;for(;h>0&&A<m;){const v=d[g];g=g+1===u?0:g+1,h--,f[v]=0,A++;const E=c.collect(v,r);if(E===0)continue;let b=0,C=0;for(let x=0;x<c.count;x++){const _=p[x];y[_]===E&&(C++,(C===1||l()*C<1)&&(b=_))}if(b!==r[v]){r[v]=b;for(let x=0;x<e.sides;x++){const _=e.rowPtrs[x],S=e.colIdxs[x],P=_[v+1];for(let I=_[v];I<P;I++){const F=S[I];f[F]===0&&r[F]!==b&&(o===null||o[F]===0)&&(f[F]=1,d[w]=F,w=w+1===u?0:w+1,h++)}}}}return{visits:A,size:h}}function Kr(t,e={}){const n=e.maxIterations??100,r=e.randomSeed??42,o=e.weighted??!0,s=t.nodeCount;ln(s,n,r);const i=an(t,o),a=new Uint32Array(s);for(let f=0;f<s;f++)a[f]=f;if(s===0||n===0){const{labels:f,count:h}=tt(a);return{...K(f,h),iterations:0,converged:!Zs(t,i.weights)}}const{visits:l,size:c}=Hr(t,i,o,a,null,n,r),{labels:u,count:d}=tt(a);return{...K(u,d),iterations:Math.ceil(l/s),converged:c===0}}function Js(t,e,n={}){const r=n.maxIterations??100,o=n.randomSeed??42,s=n.weighted??!0,i=t.nodeCount;if(e.length!==i)throw $(new RangeError(`seeds has ${e.length} entries; the snapshot has ${i} nodes`),"E_BAD_OPTION");ln(i,r,o);const a=an(t,s),l=new Uint32Array(i),c=new Uint8Array(i),u=new Map;for(let m=0;m<i;m++){const A=e[m];if(A===M){l[m]=M;continue}let p=u.get(A);p===void 0&&(p=m,u.set(A,m)),l[m]=p,c[m]=1}const{visits:d,size:f}=Hr(t,a,s,l,c,r,o),h=f===0||r===0&&Gr(new cn(i,a,s),l,c);Qs(a,l);const{labels:g,count:w}=tt(l);return{...K(g,w),iterations:i===0?0:Math.ceil(d/i),converged:h}}function Qs(t,e){const n=[];for(let r=0;r<e.length;r++)if(e[r]===M)for(e[r]=r,n.push(r);n.length>0;)ti(t,e,n.pop(),r,n)}function ti(t,e,n,r,o){for(let s=0;s<t.sides;s++){const i=t.rowPtrs[s],a=t.colIdxs[s],l=t.arcWeights[s],c=i[n+1];for(let u=i[n];u<c;u++){const d=a[u];e[d]===M&&(l===null||l[u]>0)&&(e[d]=r,o.push(d))}}}function Yr(t,e={}){const n=e.maxIterations??100,r=e.weighted??!0,o=t.nodeCount;ln(o,n,null);const s=an(t,r),i=new cn(o,s,r),{touched:a,acc:l}=i;let c=new Uint32Array(o),u=new Uint32Array(o),d=new Uint32Array(o).fill(M);const f=new Uint32Array(o);for(let y=0;y<o;y++)c[y]=y,f[y]=ei(y);let h=0,g=0,w=!1;for(;g<2&&h<n&&!w;){const y=h%2===0;h++;let v=!1;for(let E=0;E<o;E++){const b=c[E];u[E]=b;const C=i.collect(E,c);if(C===0||i.voteOf(b)===C)continue;let x=M;for(let _=0;_<i.count;_++){const S=a[_];l[S]===C&&(x===M||f[S]<f[x])&&(x=S)}(y?f[x]>f[b]:f[x]<f[b])&&(u[E]=x,v=!0)}w=v&&ni(u,d),[d,c,u]=[c,u,d],g=v?0:g+1}const m=g>=2||Gr(i,c,null),{labels:A,count:p}=tt(c);return{...K(A,p),iterations:h,converged:m}}function ei(t){return t^=t>>>16,t=Math.imul(t,2246822507),t^=t>>>13,t=Math.imul(t,3266489909),t^=t>>>16,t>>>0}function ni(t,e){for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function un(t,e,n={}){const r=n.resolution??1,o=n.weights??t.weights;if(e.length!==t.nodeCount)throw $(new RangeError(`labels has ${e.length} entries; the snapshot has ${t.nodeCount} nodes`),"E_BAD_OPTION");if(o!==null&&o.length!==t.arcCount)throw $(new RangeError(`weights has ${o.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const s=new Map,i=new Map,a=(u,d,f)=>{d!==M&&u.set(d,(u.get(d)??0)+f)};let l=0;for(let u=0;u<t.nodeCount;u++){const d=t.rowPtr[u+1];for(let f=t.rowPtr[u];f<d;f++){const h=t.colIdx[f],g=o===null?1:o[f],w=t.directed||h===u?2*g:g;l+=w,t.directed?(a(s,e[u],g),a(s,e[h],g)):a(s,e[u],w),e[u]===e[h]&&a(i,e[u],w)}}if(l===0)return 0;let c=0;for(const[u,d]of s){const f=d/l;c+=(i.get(u)??0)/l-r*f*f}return c}const ri=1e-10;function oi(t,e){const n=new qt(t.nodeCount),r=t.edgeList();for(let o=0;o<t.edgeCount;o++)L(e,o)&&n.union(r.src[o],r.dst[o]);return n.toLabels()}function Xr(t,e={}){if(t.directed)throw $(new Error("Girvan-Newman requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.maxCommunities??0,r=e.minCommunitySize??1,o=e.maxIterations??100,{nodeCount:s,edgeCount:i}=t,a=qe(t,ce(t)),l=ct(i,!0),c=[],u=[],d=[],f=()=>{const{labels:g,count:w}=oi(t,l);c.push(g),d.push(w),u.push(un(t,g,a===null?{}:{weights:a}));const m=new Uint32Array(w);for(let A=0;A<s;A++)m[g[A]]++;return m.filter(A=>A>=r).length};f();for(let g=0;g<o&&fs(l,i)>0;g++){const w=Ze(t,{alive:l}).scores;let m=-1/0;for(let p=0;p<i;p++)L(l,p)&&w[p]>m&&(m=w[p]);for(let p=0;p<i;p++)L(l,p)&&Math.abs(w[p]-m)<ri&&bt(l,p,!1);const A=f();if(n>0&&A>=n||A===s)break}let h=0;for(let g=1;g<u.length;g++)(u[g]>u[h]||Number.isNaN(u[h])&&!Number.isNaN(u[g]))&&(h=g);return{...K(c[h],d[h]),bestLevel:h,levels:c,modularity:Float64Array.from(u)}}function si(t,e={}){const{maxDepth:n=10,minClusterSize:r=2,tolerance:o=1e-6,maxIterations:s=100,seed:i=42,weighted:a=!0}=e,l=t.nodeCount;if(l===0)throw $(new Error("Cannot cluster empty graph"),"E_EMPTY_GRAPH");const c=Vt(i),u=a?t.weights:null;if(u!==null)for(let D=0;D<u.length;D++){const k=u[D];if(!(k>=0)||k===1/0)throw $(new RangeError(`arc ${String(D)} has weight ${String(k)}; grsbm needs finite, non-negative weights`),"E_BAD_WEIGHT")}const d=a?t.weightedDegree():t.degree(),f=a?t.totalWeight():t.edgeCount,h=t.directed?1:2,g=!t.directed||!a,{rowPtr:w,colIdx:m}=t,A=new Int32Array(l).fill(-1),p=D=>{let k=0,N=0;for(let O=0;O<D.length;O++){const T=D[O];N+=d[T];for(let B=w[T];B<w[T+1];B++){const z=m[B];if(A[z]===A[T]){const q=u===null?1:u[B];k+=z===T?h*q:q}}}return g&&(k/=2),(k-N*N/(4*f))/f},y=D=>{if(f===0)return 0;let k=0;D.forEach((N,O)=>{for(let T=0;T<N.length;T++)A[N[T]]=O});for(const N of D)N.length>0&&(k+=p(N));for(const N of D)for(let O=0;O<N.length;O++)A[N[O]]=-1;return k},v=new Uint32Array(l);for(let D=0;D<l;D++)v[D]=D;const E=y([v]),b=[{serial:0,members:v,depth:0,modularity:E,spectralScore:0,left:M,right:M,split:null}],C=[E];let x=E;const _=new Int32Array(l).fill(-1);let S=1;for(let D=0;D<b.length;D++){const k=b[D],{members:N}=k,O=N.length;if(k.depth>=n||O<r*2||O<4)continue;const T=ii(N,_,t,u,c,o,s),B=Array.from({length:O},(j,it)=>it).sort((j,it)=>T[j]-T[it]),z=Uint32Array.from(B,j=>N[j]);let q=Math.floor(O/2),W=-1/0;for(let j=Math.floor(O*.2);j<=Math.floor(O*.8);j++){const it=y([z.subarray(0,j),z.subarray(j)]);it>W&&(W=it,q=j)}const Y=z.slice(0,q),et=z.slice(q),nt=Array.from(T).sort((j,it)=>Math.abs(it)-Math.abs(j)),ut=Math.abs(nt[Math.floor(O*.1)]),rt=[];for(let j=0;j<O&&rt.length<5;j++)Math.abs(T[j])>=ut&&rt.push(N[j]);const ot=Math.abs(nt[0]-nt[O-1]),st=S++,mt=S++,dt=W-y([N]);if(dt<=0)continue;x+=dt;const xt=(j,it)=>({serial:j,members:it,depth:k.depth+1,modularity:x,spectralScore:ot,left:M,right:M,split:null});k.left=b.length,k.right=b.length+1,k.split={improvement:dt,bisectionModularity:x,keyNodes:Uint32Array.from(rt),spectralValues:T},b.push(xt(st,Y),xt(mt,et)),C.push(x)}const P=new Uint32Array(l);let I=0;const F=[0];for(;F.length>0;){const D=b[F.pop()??0];if(D.left===M){for(const k of D.members)P[k]=I;I++}else F.push(D.right,D.left)}const{labels:U,count:R}=tt(P);return{...K(U,R),clusters:b,modularityScores:Float64Array.from(C)}}function ii(t,e,n,r,o,s,i){const a=t.length;for(let p=0;p<a;p++)e[t[p]]=p;const l=new Uint32Array(a+1),c=[],u=[],d=new Map;let f=0;for(let p=0;p<a;p++){const y=t[p];d.clear();let v=0;for(let E=n.rowPtr[y];E<n.rowPtr[y+1];E++){const b=e[n.colIdx[E]];if(b>=0&&b!==p){const C=r===null?1:r[E];d.set(b,(d.get(b)??0)-C),v+=C}}d.set(p,v),f=Math.max(f,v);for(const E of[...d.keys()].sort((b,C)=>b-C))c.push(E),u.push(d.get(E)??0);l[p+1]=c.length}for(let p=0;p<a;p++)e[t[p]]=-1;let h=new Float64Array(a);for(let p=0;p<a;p++)h[p]=o()-.5;const g=p=>{let y=0;for(const E of p)y+=E;const v=y/a;for(let E=0;E<a;E++)p[E]-=v},w=p=>{let y=0;for(const v of p)y+=v*v;return Math.sqrt(y)};g(h);const m=w(h);if(m>0)for(let p=0;p<a;p++)h[p]/=m;const A=2*f;for(let p=0;p<i;p++){const y=new Float64Array(a);for(let b=0;b<a;b++){let C=0;for(let x=l[b];x<l[b+1];x++)C+=u[x]*h[c[x]];y[b]=A*h[b]-C}g(y);const v=w(y);if(v<s)break;let E=0;for(let b=0;b<a;b++)y[b]/=v,E+=Math.abs(y[b]-h[b]);if(h=y,E<s)break}return h}function ai(t,e){const n=t.nodeCount;e.fill(M);const r=new Uint32Array(n);for(let o=0;o<n;o++){const s=o*n;e[s+o]=0,r[0]=o;let i=0,a=1;for(;i<a;){const l=r[i++],c=e[s+l]+1,u=t.rowPtr[l+1];for(let d=t.rowPtr[l];d<u;d++){const f=t.colIdx[d];e[s+f]===M&&(e[s+f]=c,r[a++]=f)}}}}function li(t){const e=Math.max(0,2*t-1),n=Array.from({length:e},(s,i)=>i<t?`leaf-${i}`:`cluster-${i}`),r=Array.from({length:e},(s,i)=>i).sort((s,i)=>+(n[s]>n[i])-+(n[s]<n[i])),o=new Uint32Array(e);return r.forEach((s,i)=>o[s]=i),o}function ci(t,e={}){const n=e.linkage??"single";if(!["single","complete","average","ward"].includes(n))throw $(new RangeError(`unknown linkage "${n}"`),"E_BAD_OPTION");const r=t.nodeCount,{hops:o,min:s,max:i,sum:a,count:l}=pr("hierarchicalClustering",r,e.maxNodes,28*r*r,()=>({hops:new Uint32Array(r*r),min:new Uint32Array(r*r),max:new Uint32Array(r*r),sum:new Float64Array(r*r),count:new Float64Array(r*r)}));ai(t,o);for(let I=0;I<r*r;I++){const F=o[I];F!==M&&(s[I]=F,i[I]=F,a[I]=F,l[I]=1)}const c=Math.max(0,2*r-1),u=li(r),d=new Uint32Array(c),f=new Float64Array(c),h=new Uint32Array(c);for(let I=0;I<r;I++)d[I]=I,f[I]=1;const g=[],w=[],m=[],A=(I,F)=>{const U=d[I]*r+d[F],R=l[U];if(R===0)return 1/0;switch(n){case"single":return s[U];case"complete":return i[U];case"average":return a[U]/R;default:return a[U]/R*(f[I]*f[F]/(f[I]+f[F]))}},p=(I,F)=>{const[U,R]=I<F?[I,F]:[F,I];return R<r?A(U,R):A(R,U)};let y=Array.from({length:r},(I,F)=>F);for(;y.length>1;){let I=1/0,F=-1,U=-1;for(const N of y)for(const O of y){if(u[N]>=u[O])continue;const T=p(N,O);T<I&&(I=T,F=N,U=O)}if(F<0)break;const R=r+g.length;g.push(F),w.push(U),m.push(I),h[R]=Math.max(h[F],h[U])+1,f[R]=f[F]+f[U];const D=d[F],k=d[U];for(const N of y){if(N===F||N===U)continue;const O=d[N];for(const[T,B,z]of[[D*r+O,D*r+O,k*r+O],[O*r+D,O*r+D,O*r+k]]){const q=l[B],W=l[z];if(q===0||W===0){const Y=q===0?z:B;s[T]=s[Y],i[T]=i[Y]}else s[T]=Math.min(s[B],s[z]),i[T]=Math.max(i[B],i[z]);a[T]=a[B]+a[z],l[T]=q+W}}d[R]=D,y=y.filter(N=>N!==F&&N!==U),y.push(R)}const v=g.length,E=Uint32Array.from(g),b=Uint32Array.from(w),C=I=>{if(!(I>=0&&I<r+v))throw $(new RangeError(`cluster ${I} is not in [0, ${r+v})`),"E_BAD_OPTION");const F=[],U=[I];for(;U.length>0;){const R=U.pop();R<r?F.push(R):U.push(b[R-r],E[R-r])}return Uint32Array.from(F)},x=Uint32Array.from(y),_=Float64Array.from(m),S=new Uint32Array(v);for(let I=0;I<v;I++)S[I]=E[I]<r?E[I]:S[E[I]-r];const P=I=>I<r?I:S[I-r];return{nodeCount:r,left:E,right:b,distance:_,height:h.slice(0,r+v),roots:x,members:C,cut(I){const F=[],U=R=>{R<r||h[R]<=I?F.push(C(R)):(U(E[R-r]),U(b[R-r]))};return x.forEach(U),F},cutAt(I){let F=0;if("clusters"in I){const k=I.clusters;if(!Number.isInteger(k)||k<x.length||k>r)throw $(new RangeError(`clusters must be an integer in [${x.length}, ${r}], got ${k}`),"E_BAD_OPTION");F=r-k}else{if(Number.isNaN(I.distance))throw $(new RangeError("distance must not be NaN"),"E_BAD_OPTION");for(;F<v&&_[F]<=I.distance;)F++}const U=new qt(r);for(let k=0;k<F;k++)U.union(P(E[k]),P(b[k]));const{labels:R,count:D}=U.toLabels();return K(R,D)}}}function bn(t){let e=0;for(let r=0;r<t.length;r++)e+=t[r]*t[r];const n=Math.sqrt(e);if(n>0)for(let r=0;r<t.length;r++)t[r]/=n}function Cn(t){let e=0;for(let n=0;n<t.length;n++)t[n]>e&&(e=t[n]);if(e>0)for(let n=0;n<t.length;n++)t[n]/=e}function Zr(t,e={}){const n=t.nodeCount,r=e.maxIterations??100,o=e.tolerance??1e-6,s=t.reverse(),i=e.weighted===!0?t.weights:null,a=e.weighted===!0?s.weights:null;let l=new Float64Array(n).fill(n===0?0:1/Math.sqrt(n)),c=new Float64Array(n).fill(n===0?0:1/Math.sqrt(n)),u=new Float64Array(n),d=new Float64Array(n),f=0,h=!1;for(;f<r&&!h;f++){for(let w=0;w<n;w++){let m=0;const A=s.rowPtr[w+1];for(let v=s.rowPtr[w];v<A;v++)m+=l[s.colIdx[v]]*(a===null?1:a[v]);d[w]=m;let p=0;const y=t.rowPtr[w+1];for(let v=t.rowPtr[w];v<y;v++)p+=c[t.colIdx[v]]*(i===null?1:i[v]);u[w]=p}bn(d),bn(u);let g=0;for(let w=0;w<n;w++){const m=Math.abs(u[w]-l[w]);m>g&&(g=m);const A=Math.abs(d[w]-c[w]);A>g&&(g=A)}[l,u]=[u,l],[c,d]=[d,c],h=g<o}return e.normalized===!1&&(Cn(l),Cn(c)),{hubs:l,authorities:c,iterations:f,converged:h}}function In(t,e){let n=0,r=!1,o=M;for(let s=t.rowPtr[e];s<t.rowPtr[e+1];s++){const i=t.colIdx[s];i!==o&&(o=i,i===e?r=!0:n++)}return{degree:n,loop:r}}function xn(t){const e=new Float64Array(t.nodeCount),n=t.directed?t.reverse():null;for(let r=0;r<t.nodeCount;r++){const o=In(t,r),s=n===null?0:In(n,r).degree;e[r]=(o.degree*2**26+s)*2+(o.loop?1:0)}return e}function ui(t,e){const n=new Uint32Array(e),r=new Uint32Array(e).fill(M),o=new Uint8Array(e),s=new Uint8Array(e);let i=0;for(let a=0;a<e;a++){if(s[a]===1)continue;s[a]=1;let l=i;for(n[i++]=a;l<i;)i=di(t,n[l++],{order:n,parent:r,parentView:o},s,i)}return{order:n,parent:r,parentView:o}}function di(t,e,n,r,o){const{order:s,parent:i,parentView:a}=n;for(let l=0;l<t.length;l++){const{rowPtr:c,colIdx:u}=t[l];for(let d=c[e];d<c[e+1];d++){const f=u[d];r[f]===0&&(r[f]=1,i[f]=e,a[f]=l,s[o++]=f)}}return o}function Jr(t,e,n,r){const o=t.nodeCount;if(t.directed!==e.directed||o!==e.nodeCount)return;const s=xn(t),i=xn(e),a=s.slice().sort(),l=i.slice().sort();if(a.some((P,I)=>P!==l[I]))return;const c=t.directed?[t,t.reverse()]:[t],u=e.directed?[e,e.reverse()]:[e],{order:d,parent:f,parentView:h}=ui(c,o),g=new Uint32Array(o).fill(M),w=new Uint32Array(o).fill(M),m=u.map(()=>new Uint32Array(o)),A=u.map(()=>new Uint32Array(o));let p=0;const{nodeMatch:y,edgeMatch:v}=n,E=(P,I)=>{if(s[P]!==i[I]||y!==void 0&&!y(P,I,t,e))return!1;if(v!==void 0&&s[P]%2===1){const F=t.arcToEdge[t.findArc(P,P)],U=e.arcToEdge[e.findArc(I,I)];if(!v(F,U,t,e))return!1}p++;for(let F=0;F<c.length;F++){const U=c[F],R=u[F];let D=0,k=M;for(let O=R.rowPtr[I];O<R.rowPtr[I+1];O++){const T=R.colIdx[O];T===k||T===I||(k=T,m[F][T]=p,A[F][T]=O,w[T]!==M&&D++)}let N=0;k=M;for(let O=U.rowPtr[P];O<U.rowPtr[P+1];O++){const T=U.colIdx[O];if(T===k||T===P)continue;k=T;const B=g[T];if(B!==M&&(m[F][B]!==p||(N++,v!==void 0&&!v(U.arcToEdge[O],R.arcToEdge[A[F][B]],t,e))))return!1}if(N!==D)return!1}return!0},b=new Uint32Array(o+1);let C=0,x=!1;const _=P=>{const I=f[d[P]];if(I===M)return{view:null,from:0,to:o};const F=u[h[d[P]]],U=g[I];return{view:F,from:F.rowPtr[U],to:F.rowPtr[U+1]}};if(o===0){r(g);return}let S=_(0);for(b[0]=S.from;!x;){const P=d[C];let I=!1;for(;b[C]<S.to;){const U=b[C]++,R=S.view===null?U:S.view.colIdx[U];if(!(S.view!==null&&U>S.from&&S.view.colIdx[U-1]===R)&&!(w[R]!==M||!E(P,R))){if(g[P]=R,w[R]=P,C===o-1){if(x=r(g),g[P]=M,w[R]=M,x)break;continue}C++,S=_(C),b[C]=S.from,I=!0;break}}if(x||I)continue;if(C===0)return;C--;const F=d[C];w[g[F]]=M,g[F]=M,S=_(C)}}function fi(t,e,n={}){let r=null;return Jr(t,e,n,o=>(r=o.slice(),!0)),{isomorphic:r!==null,mapping:r}}function hi(t,e,n={}){const r=[];return Jr(t,e,n,o=>(r.push(o.slice()),!1)),r}function Qr(t){if(t.directed)throw $(new Error("k-core requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const e=t.nodeCount,{rowPtr:n,colIdx:r}=t,o=new Uint32Array(e);let s=0;for(let f=0;f<e;f++){let h=0,g=-1;const w=n[f+1];for(let m=n[f];m<w;m++){const A=r[m];A!==f&&A!==g&&h++,g=A}o[f]=h,h>s&&(s=h)}const i=new Uint32Array(s+1);for(let f=0;f<e;f++)i[o[f]]++;let a=0;for(let f=0;f<=s;f++){const h=i[f];i[f]=a,a+=h}const l=new Uint32Array(e),c=new Uint32Array(e);for(let f=0;f<e;f++)c[f]=i[o[f]],l[c[f]]=f,i[o[f]]++;for(let f=s;f>0;f--)i[f]=i[f-1];i[0]=0;let u=0;for(let f=0;f<e;f++){const h=l[f],g=o[h];g>u&&(u=g);const w=n[h+1];let m=-1;for(let A=n[h];A<w;A++){const p=r[A],y=p===h||p===m;if(m=p,y)continue;const v=o[p];if(v>g){const E=c[p],b=i[v],C=l[b];p!==C&&(l[E]=C,l[b]=p,c[p]=b,c[C]=E),i[v]++,o[p]=v-1}}}let d=null;return{coreness:o,maxCore:u,cores(){if(d===null){const f=new Uint32Array(u+1);for(let w=0;w<e;w++)f[o[w]]++;const h=[];for(let w=0;w<=u;w++)h.push(new Uint32Array(f[w]));const g=new Uint32Array(u+1);for(let w=0;w<e;w++){const m=o[w];h[m][g[m]++]=w}d=h}return d}}}function to(t,e={}){const n=t.nodeCount,r=e.alpha??.1,o=e.beta??1,s=e.maxIterations??100,i=e.tolerance??1e-6,a=t.reverse(),l=e.weighted===!0?a.weights:null;let c=new Float64Array(n).fill(o),u=new Float64Array(n),d=0,f=!1;for(;d<s&&!f;d++){let h=0;for(let g=0;g<n;g++){let w=0;const m=a.rowPtr[g+1];for(let p=a.rowPtr[g];p<m;p++)w+=c[a.colIdx[p]]*(l===null?1:l[p]);u[g]=r*w+o;const A=Math.abs(u[g]-c[g]);A>h&&(h=A)}[c,u]=[u,c],f=h<i}if(e.normalized!==!1){let h=1/0,g=-1/0;for(let m=0;m<n;m++)c[m]<h&&(h=c[m]),c[m]>g&&(g=c[m]);const w=g-h;if(w>0)for(let m=0;m<n;m++)c[m]=(c[m]-h)/w}return{scores:c,iterations:d,converged:f}}const gi=.01,wi=3;function eo(t,e){const{nodeCount:n,rowPtr:r,colIdx:o,arcToEdge:s}=t,i=new Float64Array(t.arcCount),a=new Float64Array(n);for(let l=0;l<n;l++)for(let c=r[l],u=r[l+1];c<u;c++){const d=e[s[c]];i[c]=d,a[l]+=o[c]===l?2*d:d}return{s:t,edgeW:e,arcW:i,deg:a}}function no(t,e){const n=new Uint32Array(t);for(let r=0;r<t;r++)n[r]=r;for(let r=t-1;r>=1;r--){const o=Math.floor(e()*(r+1)),s=n[r];n[r]=n[o],n[o]=s}return n}function pi(t,e,n,r,o){const{s,arcW:i,deg:a}=t,{nodeCount:l,rowPtr:c,colIdx:u}=s,d=new Float64Array(l),f=new Uint32Array(l);for(let C=0;C<l;C++)d[e[C]]+=a[C],f[e[C]]++;const h=[];for(let C=l-1;C>=0;C--)f[C]===0&&h.push(C);const g=new Float64Array(l),w=new Int32Array(l).fill(-1),m=new Uint32Array(l),A=l+1,p=new Uint32Array(A);p.set(no(l,n));const y=new Uint8Array(l).fill(1);let v=0,E=l,b=0;for(;v!==E;){const C=p[v];v=v+1===A?0:v+1,y[C]=0,b++;const x=e[C],_=a[C];d[x]-=_,f[x]--;let S=0;for(let F=c[C],U=c[C+1];F<U;F++){const R=u[F];if(R===C)continue;const D=e[R];w[D]!==b&&(w[D]=b,g[D]=0,m[S++]=D),g[D]+=i[F]}let P=x,I=(w[x]===b?g[x]:0)-r*_*d[x]/o;for(let F=0;F<S;F++){const U=m[F],R=g[U]-r*_*d[U]/o;U!==x&&R>I&&(I=R,P=U)}if(f[x]>0&&I<0&&(P=h[h.length-1]),P!==x&&f[P]===0&&h.pop(),d[P]+=_,f[P]++,P!==x){e[C]=P,f[x]===0&&h.push(x);for(let F=c[C],U=c[C+1];F<U;F++){const R=u[F];y[R]===0&&e[R]!==P&&(y[R]=1,p[E]=R,E=E+1===A?0:E+1)}}}}function mi(t,e,n,r,o){const{s,arcW:i,deg:a}=t,{nodeCount:l,rowPtr:c,colIdx:u}=s,d=new Float64Array(l);for(let v=0;v<l;v++)d[e[v]]+=a[v];const f=new Uint32Array(l),h=new Float64Array(l),g=new Uint32Array(l),w=new Float64Array(l);for(let v=0;v<l;v++){f[v]=v,h[v]=a[v],g[v]=1;for(let E=c[v],b=c[v+1];E<b;E++){const C=u[E];C!==v&&e[C]===e[v]&&(w[v]+=i[E])}}const m=new Float64Array(l),A=new Int32Array(l).fill(-1),p=new Uint32Array(l),y=no(l,n);for(let v=0;v<l;v++){const E=y[v],b=e[E],C=a[E];if(g[E]!==1||w[E]<r*C*(d[b]-C)/o)continue;let x=0;for(let D=c[E],k=c[E+1];D<k;D++){const N=u[D];if(N===E||e[N]!==b)continue;const O=f[N];A[O]!==E&&(A[O]=E,m[O]=0,p[x++]=O),m[O]+=i[D]}const _=D=>m[D]-r*C*h[D]/o;let S=-1/0,P=0;for(let D=0;D<x;D++){const k=p[D],N=_(k);N>=0&&w[k]>=r*h[k]*(d[b]-h[k])/o&&(p[P++]=k,S=Math.max(S,N))}if(P===0)continue;const I=D=>hs((_(D)-S)*2/o/gi);let F=0;for(let D=0;D<P;D++)F+=I(p[D]);let U=n()*F,R=p[P-1];for(let D=0;D<P-1;D++)if(U-=I(p[D]),U<0){R=p[D];break}f[E]=R,g[E]=0,g[R]++,h[R]+=C,w[R]+=w[E]-2*m[R]}return f}function yi(t,e,n,r,o){const s=t.s.nodeCount,i=new Uint32Array(s);for(let d=0;d<s;d++)i[d]=d;let a=t,l=e.slice(),c=0;for(;;){pi(a,l,n,r,o),c++;const d=tt(l);if(d.count===a.s.nodeCount)break;let f=mi(a,d.labels,n,r,o);tt(f).count===a.s.nodeCount&&(f=d.labels);const h=a.s.contract(f),{nodeRemap:g,edgeRemap:w}=h,m=h.snapshot,A=new Float64Array(m.edgeCount);for(let y=0;y<a.edgeW.length;y++)A[w===null?y:w[y]]+=a.edgeW[y];const p=new Uint32Array(m.nodeCount);for(let y=0;y<a.s.nodeCount;y++)p[g===null?y:g[y]]=d.labels[y];if(g!==null)for(let y=0;y<s;y++)i[y]=g[i[y]];l=tt(p).labels,a=eo(m,A)}const u=new Uint32Array(s);for(let d=0;d<s;d++)u[d]=l[i[d]];return{comm:u,levels:c}}function Ai(t){const e=ce(t)??t.edgeList().weights,n=e===null?new Float64Array(t.edgeCount).fill(1):Float64Array.from(e);for(let r=0;r<n.length;r++){const o=n[r];if(!(o>=0)||o===1/0)throw $(new RangeError(`edge ${r} has weight ${o}; Leiden needs finite, non-negative weights`),"E_BAD_WEIGHT")}return n}function ro(t,e={}){if(t.directed)throw $(new Error("Leiden requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.resolution??1,r=e.randomSeed??42,o=e.maxIterations??100,s=e.threshold??1e-7;if(!Number.isInteger(r))throw $(new RangeError(`randomSeed must be a finite integer, got ${r}`),"E_BAD_OPTION");const i=t.nodeCount,a=eo(t,Ai(t));let l=0;for(let w=0;w<i;w++)l+=a.deg[w];let c=new Uint32Array(i);for(let w=0;w<i;w++)c[w]=w;if(l===0)return{...K(c,i),modularity:0,iterations:0};const u=w=>un(t,w,{resolution:n,weights:a.arcW}),d=Vt(r);let f=u(c),h=0,g=0;for(let w=0;w<o&&g<wi;w++){const{comm:m,levels:A}=yi(a,c,d,n,l);h+=A;const p=tt(m).labels,y=u(p);y>f?(g=y-f<=s?g+1:0,c=p,f=y):g++}return{...K(c,tt(c).count),modularity:f,iterations:h}}const Pn=2**36;function vi(t){return t>1?Math.round(Pn/fr(t))/Pn:t===1?1:0}function Nn(t,e){const n=t.rowPtr[e],r=t.rowPtr[e+1];let o=0;for(let s=n;s<r;s++)(s===n||t.colIdx[s]!==t.colIdx[s-1])&&o++;return o}function oo(t,e){const n=t.directed&&!e?t.reverse():void 0;return r=>vi(Nn(t,r)+(n===void 0?0:Nn(n,r)))}function Wt(t,e,n){const r=e.directed===!0?t.reverse():t,o=t.nodeCount;return(s,i)=>s<o&&i<o?Ur(t,r,s,i,n):0}function we(t,e){const n=oo(t,e.directed===!0),r=Float64Array.from({length:t.nodeCount},(o,s)=>n(s));return Wt(t,e,o=>r[o])}function so(t,e,n,r){const o=Array.from(n,(s,i)=>i).sort((s,i)=>n[i]-n[s]).slice(0,r);return{sources:Uint32Array.from(o,s=>t[s]),targets:Uint32Array.from(o,s=>e[s]),scores:Float64Array.from(o,s=>n[s])}}function Ei(t,e,n,r,o,s,i){t.push(r),e.push(o),n.push(s),i&&(t.push(o),e.push(r),n.push(s))}function io(t,e,n){const r=[],o=[],s=[],i=e.directed!==!0&&e.uniquePairs!==!0;for(let l=0;l<t.nodeCount;l++)for(let c=l+1;c<t.nodeCount;c++){if(e.includeExisting!==!0&&t.hasArc(l,c))continue;const u=n(l,c);u>0&&Ei(r,o,s,l,c,u,i)}const{topK:a}=e;return so(r,o,s,a!==void 0&&a>0?a:s.length)}function $t(t,e){return Float64Array.from(t.sources,(n,r)=>e(n,t.targets[r]))}function ao(t,e,n,r){const o=t.nodeCount,s=[],i=[],a=[];if(e<o){const l=n.candidates===void 0?Array.from({length:o},(c,u)=>u):fe(t,n.candidates);for(let c=0;c<l.length;c++){const u=l[c];if(u===e||n.includeExisting!==!0&&u<o&&t.hasArc(e,u))continue;const d=r(e,u);d>0&&(s.push(e),i.push(u),a.push(d))}}return so(s,i,a,n.topK??10)}function lo(t,e){const n=[...t,...e],r=Array.from(n,(u,d)=>d).sort((u,d)=>n[d]-n[u]);let o=0,s=0,i=0,a=0,l=0,c=0;for(const u of r){u<t.length?o++:(c+=o,s++);const d=o/(o+s),f=o/t.length,h=d+f>0?2*(d*f)/(d+f):0;h>i&&(i=h,a=d,l=f)}return c=o>0&&s>0?c/(o*s):.5,{precision:a,recall:l,f1Score:i,auc:c}}function co(t,e={}){return io(t,e,Wt(t,e))}function bi(t,e,n={}){return $t(e,Wt(t,n))}function Ci(t,e,n={}){const r=V(t,e);return ao(t,r,n,Wt(t,n))}function Fn(t,e,n,r={}){const o=Wt(t,r);return lo($t(e,o),$t(n,o))}function Ii(t,e,n,r={}){const o=V(t,e),s=V(t,n);return Wt(t,r,oo(t,r.directed===!0))(o,s)}function uo(t,e={}){return io(t,e,we(t,e))}function xi(t,e,n={}){return $t(e,we(t,n))}function Pi(t,e,n={}){const r=V(t,e);return ao(t,r,n,we(t,n))}function kn(t,e,n,r={}){const o=we(t,r);return lo($t(e,o),$t(n,o))}function Ni(t){const e=t.nodeCount,n=new Uint32Array(e+1);for(let r=0;r<e;r++){let o=0;const s=t.rowPtr[r+1];for(let i=t.rowPtr[r];i<s;i++)t.colIdx[i]!==r&&o++;n[r+1]=n[r]+o}return n}function Fi(t){const e=t.nodeCount,n=Ni(t),r=n[e],o=new Uint32Array(r),s=new Float64Array(r),i=new Float64Array(e),a=new Float64Array(e);let l=0;for(let c=0;c<e;c++){let u=0;const d=t.rowPtr[c+1];for(let f=t.rowPtr[c];f<d;f++){const h=t.colIdx[f],g=t.weights===null?1:t.weights[f];h===c?i[c]+=g:(o[l]=h,s[l]=g,l++,u+=g)}a[c]=u+2*i[c]}return{n:e,rowPtr:n,colIdx:o,w:s,loop:i,deg:a}}function Sn(t,e,n,r,o){const s=new Float64Array(n),i=new Float64Array(n);for(let l=0;l<t.n;l++){const c=e[l];i[c]+=t.deg[l],s[c]+=2*t.loop[l];const u=t.rowPtr[l+1];for(let d=t.rowPtr[l];d<u;d++)e[t.colIdx[d]]===c&&(s[c]+=t.w[d])}let a=0;for(let l=0;l<n;l++){const c=i[l]/o;a+=s[l]/o-r*c*c}return a}function ki(t,e,n,r,o){const{n:s}=t,i=new Float64Array(s);for(let y=0;y<s;y++)e[y]=y,i[y]=t.deg[y];const a=new Float64Array(s),l=new Int32Array(s).fill(-1),c=new Uint32Array(s),u=s+1,d=new Uint32Array(u),f=new Uint8Array(s);let h=0,g=s;for(let y=0;y<s;y++)d[y]=y,f[y]=1;let w=0,m=0;const A=o*s;let p=!1;for(;h!==g&&m<A;){const y=d[h];h=h+1===u?0:h+1,f[y]=0,m++;const v=e[y],E=t.deg[y];i[v]-=E,w++;let b=0;const C=t.rowPtr[y+1];for(let S=t.rowPtr[y];S<C;S++){const P=e[t.colIdx[S]];l[P]!==w&&(l[P]=w,a[P]=0,c[b++]=P),a[P]+=t.w[S]}let x=v,_=(l[v]===w?a[v]:0)-n*E*i[v]/r;for(let S=0;S<b;S++){const P=c[S];if(P===v)continue;const I=a[P]-n*E*i[P]/r;I>_&&(_=I,x=P)}if(i[x]+=E,x!==v){e[y]=x,p=!0;for(let S=t.rowPtr[y];S<C;S++){const P=t.colIdx[S];f[P]===0&&e[P]!==x&&(f[P]=1,d[g]=P,g=g+1===u?0:g+1)}}}return p}function Si(t,e,n){const r=new Uint32Array(n+1);for(let i=0;i<t;i++)r[e[i]+1]++;for(let i=0;i<n;i++)r[i+1]+=r[i];const o=new Uint32Array(t),s=r.slice(0,n);for(let i=0;i<t;i++)o[s[e[i]]++]=i;return{start:r,members:o}}function _i(t,e,n,r,o,s){let i=0;for(let a=0;a<e;a++){const l=t[a];r[s+a]=l,o[s+a]=n[l],i+=n[l]}return i}function Mi(t,e,n){const{start:r,members:o}=Si(t.n,e,n),s=new Uint32Array(n+1),i=new Uint32Array(t.colIdx.length),a=new Float64Array(t.colIdx.length),l=new Float64Array(n),c=new Float64Array(n),u=new Float64Array(n),d=new Int32Array(n).fill(-1),f=new Uint32Array(n);let h=0;for(let g=0;g<n;g++){s[g]=h;let w=0,m=0;for(let p=r[g];p<r[g+1];p++){const y=o[p];l[g]+=t.loop[y];const v=t.rowPtr[y+1];for(let E=t.rowPtr[y];E<v;E++){const b=e[t.colIdx[E]];if(b===g){w+=t.w[E];continue}d[b]!==g&&(d[b]=g,u[b]=0,f[m++]=b),u[b]+=t.w[E]}}l[g]+=w/2;const A=_i(f,m,u,i,a,h);h+=m,c[g]=A+2*l[g]}return s[n]=h,{n,rowPtr:s,colIdx:i.subarray(0,h),w:a.subarray(0,h),loop:l,deg:c}}function fo(t,e={}){if(t.directed)throw $(new Error("Louvain requires an undirected graph. Pass s.toUndirected().snapshot."),"E_NEEDS_UNDIRECTED");const n=e.resolution??1,r=e.maxIterations??100,o=e.tolerance??1e-6,s=t.nodeCount,i=new Uint32Array(s);for(let f=0;f<s;f++)i[f]=f;let a=Fi(t),l=0;for(let f=0;f<s;f++)l+=a.deg[f];if(l===0)return{...K(i,s),modularity:0,iterations:0};let c=Sn(a,i,s,n,l),u=0;for(let f=0;f<r;f++){const h=new Uint32Array(a.n);if(!ki(a,h,n,l,r))break;const g=tt(h),{count:w}=g,m=g.labels,A=Sn(a,m,w,n,l);for(let y=0;y<s;y++)i[y]=m[i[y]];u=f+1;const p=A-c;if(c=A,p<o||w===a.n)break;a=Mi(a,m,w)}const d=tt(i);return{...K(d.labels,d.count),modularity:c,iterations:u}}function Ve(t){for(let e=0;e+1<t.ptr.length;e++){let n=0;for(let r=t.ptr[e];r<t.ptr[e+1];r++)n+=t.val[r];if(n>0)for(let r=t.ptr[e];r<t.ptr[e+1];r++)t.val[r]/=n}}function Ui(t,e,n,r){const o=n.length,s=new Uint32Array(o+1),i=[],a=[],l=[];for(let c=0;c<o;c++){l.length=0;for(let u=e.ptr[c];u<e.ptr[c+1];u++){const d=e.row[u],f=e.val[u];for(let h=t.ptr[d];h<t.ptr[d+1];h++){const g=t.row[h];r[g]===0&&(r[g]=1,l.push(g)),n[g]+=t.val[h]*f}}l.sort((u,d)=>u-d);for(const u of l)i.push(u),a.push(n[u]),n[u]=0,r[u]=0;s[c+1]=i.length}return{ptr:s,row:Uint32Array.from(i),val:Float64Array.from(a)}}function Oi(t,e,n){const r={ptr:t.ptr,row:t.row,val:t.val.map(c=>gs(c,e))};Ve(r);const o=t.ptr.length-1,s=new Uint32Array(o+1),i=[],a=[];for(let c=0;c<o;c++){for(let u=r.ptr[c];u<r.ptr[c+1];u++)r.val[u]>=n&&(i.push(r.row[u]),a.push(r.val[u]));s[c+1]=i.length}const l={ptr:s,row:Uint32Array.from(i),val:Float64Array.from(a)};return Ve(l),l}function Ti(t,e,n){for(let r=0;r+1<t.ptr.length;r++){let o=t.ptr[r],s=e.ptr[r];const i=t.ptr[r+1],a=e.ptr[r+1];for(;o<i||s<a;){let l;if(s>=a||o<i&&t.row[o]<e.row[s]?l=t.val[o++]:o>=i||e.row[s]<t.row[o]?l=e.val[s++]:l=t.val[o++]-e.val[s++],Math.abs(l)>n)return!1}}return!0}function Ri(t,e){const n=t===null?1:t[e];if(!(n>=0)||n===1/0)throw $(new RangeError(`arc ${e} has weight ${n}; Markov clustering needs finite, non-negative weights`),"E_BAD_WEIGHT");return n}function Di(t,e,n){const r=t.nodeCount,o=new Uint32Array(r+1);for(let h=0;h<t.arcCount;h++)o[t.colIdx[h]+1]++;if(n)for(let h=0;h<r;h++)o[h+1]++;for(let h=0;h<r;h++)o[h+1]+=o[h];const s=o.slice(0,r),i=new Uint32Array(o[r]),a=new Float64Array(o[r]),l=(h,g,w,m)=>{const A=s[g]-1;A>=o[g]&&i[A]===h?a[A]=m?w:a[A]+w:(i[s[g]]=h,a[s[g]++]=w)};for(let h=0;h<r;h++){for(let g=t.rowPtr[h];g<t.rowPtr[h+1];g++){const w=t.colIdx[g];l(h,w,Ri(e,g),!1)}n&&l(h,h,1,!0)}const c=new Uint32Array(r+1),u=[],d=[];for(let h=0;h<r;h++){for(let g=o[h];g<s[h];g++)u.push(i[g]),d.push(a[g]);c[h+1]=u.length}const f={ptr:c,row:Uint32Array.from(u),val:Float64Array.from(d)};return Ve(f),f}function $i(t,e={}){const n=e.expansion??2,r=e.inflation??2,o=e.maxIterations??100,s=e.tolerance??1e-6,i=e.pruningThreshold??1e-5;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`expansion must be an integer of at least 1, got ${n}`),"E_BAD_OPTION");if(!(r>0)||r===1/0)throw $(new RangeError(`inflation must be finite and above 0, got ${r}`),"E_BAD_OPTION");if(!Number.isInteger(o)||o<0)throw $(new RangeError(`maxIterations must be a non-negative integer, got ${o}`),"E_BAD_OPTION");if(!(s>=0)||!(i>=0))throw $(new RangeError("tolerance and pruningThreshold must be non-negative"),"E_BAD_OPTION");const a=e.weights??t.weights;if(a!==null&&a.length!==t.arcCount)throw $(new RangeError(`weights has ${a.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const l=t.nodeCount;let c=Di(t,a,e.selfLoops??!0);const u=new Float64Array(l),d=new Uint8Array(l);let f=l===0,h=0;for(;!f&&h<o;){let y=c;for(let E=1;E<n;E++)y=Ui(y,c,u,d);const v=Oi(y,r,i);h++,f=Ti(c,v,s),c=v}const g=[],w=new Int32Array(l).fill(-1);for(let y=0;y<l;y++){let v=0;for(let E=c.ptr[y];E<c.ptr[y+1];E++)c.row[E]===y&&c.val[E]>0&&g.push(y),c.val[E]>v&&(v=c.val[E],w[y]=c.row[E])}const m=new Uint32Array(l),A=new Map;for(let y=0;y<l;y++){const v=w[y];if(v>=0){let E=A.get(v);E===void 0&&(E=A.size,A.set(v,E)),m[y]=E}}let p=A.size;for(let y=0;y<l;y++)w[y]<0&&(m[y]=p++);return{...K(m,p),attractors:Uint32Array.from(g),iterations:h,converged:f}}function _n(t,e){return e instanceof Uint32Array?e:hr(t,e)}function ho(t,e){const n=t.directed&&e.arcs!=="out"?[t,t.reverse()]:[t];if(e.left!==void 0&&e.right!==void 0)return{left:_n(t,e.left),right:_n(t,e.right),views:n};const{sides:r}=kr(t,{arcs:e.arcs});if(r===null)throw $(new Error("Graph is not bipartite"),"E_NOT_BIPARTITE");const o=ct(t.nodeCount);for(let i=0;i<o.length;i++)o[i]=~r[i];const s=t.nodeCount%32;return s!==0&&(o[o.length-1]&=(1<<s)-1),{left:o,right:r,views:n}}function zi(t,e,n){const r=new Uint32Array(e+1),o=new Uint32Array(n+1);for(const{rowPtr:u,arcToEdge:d}of t){for(let f=0;f<e;f++)r[f+1]+=u[f+1]-u[f];for(const f of d)o[f+1]++}for(let u=0;u<e;u++)r[u+1]+=r[u];for(let u=0;u<n;u++)o[u+1]+=o[u];const s=r[e],i=new Uint32Array(s),a=new Uint32Array(s);for(const{rowPtr:u,colIdx:d,arcToEdge:f}of t)for(let h=0;h<e;h++)for(let g=u[h];g<u[h+1];g++){const w=o[f[g]]++;i[w]=h,a[w]=d[g]}const l=r.slice(0,e),c=new Uint32Array(s);for(let u=0;u<s;u++)c[l[i[u]]++]=a[u];return{start:r,nbr:c}}function go(t,e={}){const{left:n,right:r,views:o}=ho(t,e),{nodeCount:s}=t,{start:i,nbr:a}=zi(o,s,t.edgeCount),l=new Uint32Array(s),c=new Uint8Array(s);let u=0;for(let p=0;p<s;p++){if(c[p]===1)continue;c[p]=1;let y=u;for(l[u++]=p;y<u;){const v=l[y++];for(let E=i[v];E<i[v+1];E++)c[a[E]]===0&&(c[a[E]]=1,l[u++]=a[E])}}const d=new Uint32Array(s).fill(M),f=new Uint32Array(s).fill(M),h=new Uint32Array(s),g=new Uint32Array(s),w=new Uint32Array(s),m=new Uint32Array(s);let A=0;for(let p=0;p<s;p++){const y=l[p];if(!L(n,y))continue;const v=p+1;let E=0;g[E++]=y,w[y]=i[y];let b=M;t:for(;E>0;){const C=g[E-1];for(;w[C]<i[C+1];){const x=a[w[C]++];if(h[x]===v||!L(r,x))continue;h[x]=v;const _=f[x];if(_===M){b=x;break t}m[C]=x,g[E++]=_,w[_]=i[_];continue t}E--}if(b!==M){for(let C=E-1;C>=0;C--){const x=g[C],_=C===E-1?b:m[x];d[x]=_,f[_]=x}A++}}return{matching:d,size:A}}function Bi(t,e={}){const{left:n,right:r,views:o}=ho(t,e),{nodeCount:s}=t,i=new Uint32Array(s).fill(M),a=new Uint8Array(s);let l=0;for(let c=0;c<s;c++)if(L(n,c))t:for(const{rowPtr:u,colIdx:d}of o){const f=u[c+1];for(let h=u[c];h<f;h++){const g=d[h];if(a[g]===0&&L(r,g)){a[g]=1,i[c]=g,l++;break t}}}return{matching:i,size:l}}function wo(t,e={}){const n=t.nodeCount,r=rn(t,e.weights);if(n<2){const p=ct(n,!0);return{...Dt(p,n),cutValue:0,side:p,cutEdges:new Uint32Array(0)}}const{src:o,dst:s}=t.edgeList(),i=new Uint32Array(n+1);for(let p=0;p<t.edgeCount;p++)i[o[p]+1]++,i[s[p]+1]++;for(let p=0;p<n;p++)i[p+1]+=i[p];const a=i.slice(0,n),l=new Uint32Array(i[n]),c=new Float64Array(i[n]);for(let p=0;p<t.edgeCount;p++)l[a[o[p]]]=s[p],c[a[o[p]]++]=r[p],l[a[s[p]]]=o[p],c[a[s[p]]++]=r[p];const u=new Uint32Array(n),d=new Uint32Array(n).fill(M),f=new Uint32Array(n);for(let p=0;p<n;p++)u[p]=p,f[p]=p;const h=new Float64Array(n),g=new Uint8Array(n),w=new $r(n);let m=1/0,A=ct(n);for(let p=n;p>1;p--){let y=M;for(let b=0;b<n;b++)u[b]===b&&(g[b]=0,h[b]=0,y===M?y=b:w.push(b,0));let v=M,E=y;for(;;){g[E]=1;for(let b=E;b!==M;b=d[b])for(let C=i[b];C<i[b+1];C++){const x=u[l[C]];g[x]===0&&(h[x]+=c[C],w.pushOrIncrease(x,h[x]))}if(w.isEmpty())break;v=E,E=w.pop()}if(h[E]<m){m=h[E],A=ct(n);for(let b=E;b!==M;b=d[b])bt(A,b,!0)}for(let b=E;b!==M;b=d[b])u[b]=v;d[f[v]]=E,f[v]=f[E]}return{...Dt(A,n),cutValue:m,side:A,cutEdges:on(t,A,!1,r,!1)}}function po(t,e={}){const n=e.iterations??100,r=e.randomSeed??42;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`iterations must be a positive integer, got ${n}`),"E_BAD_OPTION");if(!Number.isInteger(r))throw $(new RangeError(`randomSeed must be a finite integer, got ${r}`),"E_BAD_OPTION");const o=t.nodeCount,s=rn(t,e.weights);if(o<2){const f=ct(o);return{...Dt(f,o),cutValue:0,side:f,cutEdges:new Uint32Array(0)}}const{src:i,dst:a}=t.edgeList(),l=Vt(r),c=new Uint32Array(t.edgeCount);let u=1/0,d=ct(o);for(let f=0;f<n;f++){for(let A=0;A<c.length;A++)c[A]=A;for(let A=c.length-1;A>0;A--){const p=Math.floor(l()*(A+1)),y=c[A];c[A]=c[p],c[p]=y}const h=new qt(o);let g=o;for(let A=0;A<c.length&&g>2;A++)h.union(i[c[A]],a[c[A]])&&g--;const w=h.find(0);let m=0;for(let A=0;A<t.edgeCount;A++)h.find(i[A])===w!=(h.find(a[A])===w)&&(m+=s[A]);if(m<u){u=m,d=ct(o);for(let A=0;A<o;A++)bt(d,A,h.find(A)===w)}}return{...Dt(d,o),cutValue:u,side:d,cutEdges:on(t,d,!1,s,!1)}}function mo(t,e={}){const n=t.edgeList(),r=t.edgeCount,o=new Float64Array(r);if(e.weights!==void 0)for(let u=0;u<r;u++)o[u]=e.weights[n.arc[u]];else if(n.weights!==null)for(let u=0;u<r;u++)o[u]=n.weights[u];else o.fill(1);const s=new Array(r);for(let u=0;u<r;u++)s[u]=u;s.sort((u,d)=>o[u]-o[d]||u-d);const i=new qt(t.nodeCount),a=new Uint32Array(Math.max(t.nodeCount-1,0));let l=0,c=0;for(const u of s)i.union(n.src[u],n.dst[u])&&(a[l++]=u,c+=o[u]);return{edges:a.subarray(0,l),totalWeight:c}}function yo(t,e={}){if(t.directed)throw $(new Error("Prim's algorithm requires an undirected graph"),"E_NEEDS_UNDIRECTED");const{nodeCount:n,rowPtr:r,colIdx:o}=t,s=e.weights??t.weights,i=new Float64Array(n).fill(1/0),a=new Uint32Array(n).fill(M),l=new Uint8Array(n),c=new _t(n),u=new Uint32Array(Math.max(n-1,0));let d=0,f=0;const h=g=>{for(c.push(g,0);!c.isEmpty();){const w=c.pop();l[w]=1,a[w]!==M&&(u[d++]=t.arcToEdge[a[w]],f+=i[w]);for(let m=r[w];m<r[w+1];m++){const A=o[m],p=s===null?1:s[m];l[A]===0&&p<i[A]&&(i[A]=p,a[A]=m,c.pushOrDecrease(A,p))}}};if(n>0&&h(e.start===void 0?0:V(t,e.start)),e.forest===!0)for(let g=0;g<n;g++)l[g]===0&&h(g);else if(d<n-1)throw $(new Error("Graph is not connected"),"E_NOT_CONNECTED");return{edges:u.subarray(0,d),totalWeight:f,predArc:a}}function Ao(t,e={}){return Eo(t,null,e)}function vo(t,e,n={}){return Eo(t,bo(e,t.nodeCount,"personalization"),n)}function Eo(t,e,n){const r=t.nodeCount,o=n.dampingFactor??.85,s=n.maxIterations??100,i=n.tolerance??1e-6,a=n.convergenceNorm==="max";if(r===0)return{scores:new Float64Array(0),iterations:0,converged:!0};const l=t.reverse(),c=n.weighted===!0&&l.weights!==null,u=c?t.edges.byRole("weight"):null,d=u?.dtype==="f64"?u.data:null;let f=c?l.weights:null,h=c?t.weightedOutDegree():t.outDegree();d!==null&&(f=Mn(d,l.arcToEdge),h=qi(t,Mn(d,t.arcToEdge)));let g=(n.initialRanks===void 0?null:bo(n.initialRanks,r,"initialRanks"))??new Float64Array(r).fill(n.initialRanks===void 0?1/r:0),w=new Float64Array(r),m=0,A=!1;for(;m<s&&!A;m++){let p=0;for(let E=0;E<r;E++)h[E]===0&&(p+=g[E]);const y=1-o+o*p*(e===null?1:1/r);let v=0;for(let E=0;E<r;E++){let b=0;const C=l.rowPtr[E+1];for(let _=l.rowPtr[E];_<C;_++){const S=l.colIdx[_],P=h[S];P>0&&(b+=g[S]*(f===null?1:f[_])/P)}w[E]=y*(e===null?1/r:e[E])+o*b;const x=Math.abs(w[E]-g[E]);v=a?Math.max(v,x):v+x}[g,w]=[w,g],A=v<i}return{scores:g,iterations:m,converged:A}}function Mn(t,e){const n=new Float64Array(e.length);for(let r=0;r<n.length;r++)n[r]=t[e[r]];return n}function qi(t,e){const n=new Float64Array(t.nodeCount);for(let r=0;r<t.nodeCount;r++)for(let o=t.rowPtr[r];o<t.rowPtr[r+1];o++)n[r]+=e[o];return n}function bo(t,e,n){if(t.length!==e)throw $(new Error(`PageRank: ${n} has ${String(t.length)} entries for ${String(e)} nodes`),"E_BAD_OPTION");let r=0;for(const s of t){if(!Number.isFinite(s)||s<0)throw $(new Error(`PageRank: ${n} must be finite and non-negative, got ${String(s)}`),"E_BAD_OPTION");r+=s}if(r===0)return null;const o=new Float64Array(e);for(let s=0;s<e;s++)o[s]=t[s]/r;return o}const Co={distance:1/0,path:new Uint32Array(0),edges:new Uint32Array(0)};function Vi(t,e,n,r={}){const o=V(t,e),s=V(t,n);if(o===s)return{distance:0,path:Uint32Array.of(o),edges:new Uint32Array(0)};const i=t.nodeCount,a=r.weights??t.weights;if(a?.some(S=>S<0)===!0)throw $(new Error("Bidirectional Dijkstra does not support negative edge weights"),"E_BAD_WEIGHT");const l=t.reverse(),c=S=>a===null?1:a[S],u=new Float64Array(i).fill(1/0),d=new Float64Array(i).fill(1/0),f=new Uint32Array(i).fill(M),h=new Uint32Array(i).fill(M),g=new Uint32Array(i).fill(M),w=new _t(i),m=new _t(i);u[o]=0,d[s]=0,w.push(o,0),m.push(s,0);let A=1/0,p=M,y=M,v=M,E=0;for(;w.peekKey()+m.peekKey()<A;)if(w.peekKey()<=m.peekKey()){const S=w.pop();for(let P=t.rowPtr[S];P<t.rowPtr[S+1];P++){const I=t.colIdx[P],F=c(P),U=u[S]+F;U<u[I]&&(u[I]=U,f[I]=P,w.pushOrDecrease(I,U)),U+d[I]<A&&(A=U+d[I],[p,y,v,E]=[S,I,t.arcToEdge[P],F])}}else{const S=m.pop();for(let P=l.rowPtr[S];P<l.rowPtr[S+1];P++){const I=l.colIdx[P],F=c(l.fwdArc[P]),U=d[S]+F;U<d[I]&&(d[I]=U,h[I]=P,g[I]=S,m.pushOrDecrease(I,U)),u[I]+U<A&&(A=u[I]+U,[p,y,v,E]=[I,S,l.arcToEdge[P],F])}}if(p===M)return Co;const b=[],C=[],x=[];for(let S=p;S!==o;S=t.arcSource(f[S]))b.push(S),C.push(t.arcToEdge[f[S]]),x.push(c(f[S]));b.push(o),b.reverse(),C.reverse(),x.reverse(),C.push(v),x.push(E),b.push(y);for(let S=y;S!==s;S=g[S])C.push(l.arcToEdge[h[S]]),x.push(c(l.fwdArc[h[S]])),b.push(g[S]);let _=0;for(const S of x)_+=S;return{distance:_,path:Uint32Array.from(b),edges:Uint32Array.from(C)}}function Wi(t,e,n,r,o){const{gScore:s,fScore:i,predArc:a,closed:l,heap:c}=o;for(let u=t.rowPtr[e];u<t.rowPtr[e+1];u++){const d=t.colIdx[u];if(l[d]===1)continue;const f=s[e]+(n===null?1:n[u]);f<s[d]&&(s[d]=f,i[d]=f+r(d),a[d]=u,c.pushOrDecrease(d,i[d]))}}function Li(t,e,n,r,o={}){const s=V(t,e),i=V(t,n),a=t.nodeCount;if(!(i>=0&&i<a))throw $(new RangeError(`Target index ${String(i)} is outside the graph's ${String(a)} nodes`),"E_BAD_NODE");const l=o.weights??t.weights,c=new Float64Array(a).fill(1/0),u=new Float64Array(a).fill(1/0),d=new Uint32Array(a).fill(M),f=new Uint8Array(a),h=[],g=new _t(a);for(c[s]=0,u[s]=r(s,i),g.push(s,u[s]);!g.isEmpty();){const A=g.pop();if(A===i)break;f[A]=1,h.push(A),Wi(t,A,l,p=>r(p,i),{gScore:c,fScore:u,predArc:d,closed:f,heap:g})}const w={gScore:c,fScore:u,visited:Uint32Array.from(h)};if(c[i]===1/0)return{...Co,...w};const m=zt(t,d,s,i);return{distance:c[i],path:m,edges:Bt(t,d,s,i),...w}}function dn(t,e={}){const{labels:n,count:r}=ji(t,e);return K(n,r)}function ji(t,e={}){if(!t.directed)throw $(new Error("Strongly connected components require a directed graph. Use connectedComponents for an undirected one."),"E_NEEDS_DIRECTED");const{nodeCount:n,rowPtr:r,colIdx:o}=t,s=ge(t,e.arcOrder),i=new Uint32Array(n).fill(M),a=new Uint32Array(n),l=new Uint32Array(n),c=new Uint8Array(n),u=new Uint32Array(n),d=new Uint32Array(n),f=new Uint32Array(n),h=new Uint32Array(n);let g=0,w=0,m=0,A=0,p=0;const y=v=>{i[v]=w,a[v]=w,w++,l[v]=r[v],d[A++]=v,c[v]=1,u[p++]=v};for(let v=0;v<n;v++)if(i[v]===M)for(y(v);p>0;){const E=u[p-1];if(l[E]<r[E+1]){const b=l[E]++,C=o[s===null?b:s[b]];i[C]===M?y(C):c[C]===1&&i[C]<a[E]&&(a[E]=i[C]);continue}if(p--,a[E]===i[E]){let b;do b=d[--A],c[b]=0,f[b]=m,h[g++]=b;while(b!==E);m++}p>0&&a[E]<a[u[p-1]]&&(a[u[p-1]]=a[E])}return{labels:f,count:m,popped:h}}function Gi(t,e={}){const n=dn(t,e),r=t.contract(n.labels,{selfLoops:"drop",parallel:"merge",weights:"first"});return{components:n,condensed:r}}const Hi=1e-11,Un=3e3;function Ki(t,e){const n=t.nodeCount,r=[{view:t,arcOf:c=>c}];if(t.directed){const c=t.reverse();r.push({view:c,arcOf:u=>c.fwdArc[u]})}const o=new Uint32Array(n+1);for(let c=0;c<n;c++){let u=0;for(const{view:d}of r)for(let f=d.rowPtr[c];f<d.rowPtr[c+1];f++)u+=d.colIdx[f]===c?0:1;o[c+1]=o[c]+u}const s=new Uint32Array(o[n]),i=new Float64Array(o[n]),a=new Float64Array(n);let l=0;for(let c=0;c<n;c++)for(const{view:u,arcOf:d}of r)for(let f=u.rowPtr[c];f<u.rowPtr[c+1];f++){const h=e===null?1:e[d(f)];if(!(h>=0)||h===1/0)throw $(new RangeError(`an arc has weight ${h}; spectral clustering needs finite, non-negative weights`),"E_BAD_WEIGHT");u.colIdx[f]!==c&&(s[l]=u.colIdx[f],i[l++]=h,a[c]+=h)}return{rowPtr:o,colIdx:s,w:i,degree:a}}function On(t,e){for(let n=0;n<t.length;n++){const r=t[n];for(let o=0;;o++){for(let i=0;i<2;i++)for(let a=0;a<n;a++){const l=t[a];let c=0;for(let u=0;u<r.length;u++)c+=l[u]*r[u];for(let u=0;u<r.length;u++)r[u]-=c*l[u]}let s=0;for(let i=0;i<r.length;i++)s+=r[i]*r[i];if(s=Math.sqrt(s),s>1e-10||o===3){for(let i=0;i<r.length;i++)r[i]=s>0?r[i]/s:0;break}for(let i=0;i<r.length;i++)r[i]=e()-.5}}}function Yi(t,e){const n=new Float64Array(e*e);for(let r=0;r<e;r++)n[r*e+r]=1;for(let r=0;r<100;r++){let o=0;for(let s=0;s<e;s++)for(let i=s+1;i<e;i++)o+=t[s*e+i]*t[s*e+i];if(o<1e-30)break;for(let s=0;s<e;s++)for(let i=s+1;i<e;i++){const a=t[s*e+i];if(a===0)continue;const l=(t[i*e+i]-t[s*e+s])/(2*a),c=Math.sign(l||1)/(Math.abs(l)+Math.sqrt(l*l+1)),u=1/Math.sqrt(c*c+1),d=c*u;for(let f=0;f<e;f++){const h=t[f*e+s],g=t[f*e+i];t[f*e+s]=u*h-d*g,t[f*e+i]=d*h+u*g}for(let f=0;f<e;f++){const h=t[s*e+f],g=t[i*e+f];t[s*e+f]=u*h-d*g,t[i*e+f]=d*h+u*g}for(let f=0;f<e;f++){const h=n[f*e+s],g=n[f*e+i];n[f*e+s]=u*h-d*g,n[f*e+i]=d*h+u*g}}}return{values:Float64Array.from({length:e},(r,o)=>t[o*e+o]),vectors:n}}function Xi(t,e,n){const r=t.length,o=new Float64Array(r*r);for(let s=0;s<r;s++)for(let i=s;i<r;i++){let a=0;for(let l=0;l<n;l++)a+=t[s][l]*e[i][l];o[s*r+i]=a,o[i*r+s]=a}return o}function Zi(t,e,n,r,o){const s=Math.min(e,n+Math.max(n,8));let i=Array.from({length:s},()=>Float64Array.from({length:e},()=>o()-.5));On(i,o);const a=new Float64Array(e),l=w=>(t(w,a),w.map((m,A)=>r*m-a[A]));let c=new Float64Array(s),u=[],d=new Float64Array(s*s),f=!1;for(let w=0;w<Un;w++){const m=i.map(l);if({values:c,vectors:d}=Yi(Xi(i,m,e),s),u=Array.from({length:s},(p,y)=>y).sort((p,y)=>c[y]-c[p]),f=u.slice(0,n).every(p=>{let y=0;for(let v=0;v<e;v++){let E=0;for(let b=0;b<s;b++)E+=d[b*s+p]*(m[b][v]-c[p]*i[b][v]);y+=E*E}return Math.sqrt(y)<=Hi*r}),f||w===Un-1)break;i=m,On(i,o)}const h=new Float64Array(n),g=[];for(let w=0;w<n;w++){const m=u[w];h[w]=Math.max(0,r-c[m]);const A=new Float64Array(e);for(let p=0;p<s;p++){const y=d[p*s+m];for(let v=0;v<e;v++)A[v]+=y*i[p][v]}g.push(A)}return{values:h,vectors:g,converged:f}}const Ji=10;function Qi(t,e,n,r,o){const s=new Float64Array(o*r),i=new Float64Array(o);for(let l=0;l<n;l++){i[e[l]]++;for(let c=0;c<r;c++)s[e[l]*r+c]+=t[l*r+c]}let a=0;for(let l=0;l<n;l++){const c=e[l];for(let u=0;u<r;u++){const d=t[l*r+u]-s[c*r+u]/i[c];a+=d*d}}return a}function ta(t,e,n,r,o,s,i){const a=(f,h,g)=>{let w=0;for(let m=0;m<n;m++){const A=t[f*n+m]-h[g*n+m];w+=A*A}return w},l=new Float64Array(r*n),c=new Float64Array(e).fill(1/0);let u=Math.floor(i()*e);for(let f=0;f<r;f++){l.set(t.subarray(u*n,u*n+n),f*n);let h=0;for(let w=0;w<e;w++)c[w]=Math.min(c[w],a(w,l,f)),h+=c[w];let g=i()*h;u=0;for(let w=0;w<e&&h>0;w++)if(g-=c[w],g<0){u=w;break}}const d=new Uint32Array(e);for(let f=0;f<o;f++){let h=f===0;for(let A=0;A<e;A++){let p=0,y=1/0;for(let v=0;v<r;v++){const E=a(A,l,v);E<y&&(y=E,p=v)}h||(h=d[A]!==p),d[A]=p}if(!h)break;const g=new Float64Array(r*n),w=new Float64Array(r);for(let A=0;A<e;A++){w[d[A]]++;for(let p=0;p<n;p++)g[d[A]*n+p]+=t[A*n+p]}let m=0;for(let A=0;A<r;A++){if(w[A]===0)continue;let p=0;for(let y=0;y<n;y++){const v=g[A*n+y]/w[A],E=v-l[A*n+y];p+=E*E,l[A*n+y]=v}m=Math.max(m,Math.sqrt(p))}if(m<s)break}return d}function ea(t,e){const{k:n}=e,r=e.laplacianType??"normalized",o=e.maxIterations??100,s=e.tolerance??1e-4,i=e.seed??42;if(!Number.isInteger(n)||n<1)throw $(new RangeError(`k must be a positive integer, got ${n}`),"E_BAD_OPTION");if(!["unnormalized","normalized","randomWalk"].includes(r))throw $(new RangeError(`unknown laplacianType "${r}"`),"E_BAD_OPTION");if(!Number.isInteger(o)||o<0||!(s>=0)||!Number.isInteger(i))throw $(new RangeError("maxIterations and seed must be integers, maxIterations and tolerance non-negative"),"E_BAD_OPTION");const a=e.weights??t.weights;if(a!==null&&a.length!==t.arcCount)throw $(new RangeError(`weights has ${a.length} entries; the snapshot has ${t.arcCount} arcs`),"E_BAD_OPTION");const l=t.nodeCount;if(n>=l){const E=Uint32Array.from({length:l},(b,C)=>C);return{...K(E,l),eigenvalues:new Float64Array(0),eigenvectors:[],converged:!0}}const c=Ki(t,a),u=c.degree.map(E=>E>0?1/Math.sqrt(E):0),d=r!=="unnormalized",f=(E,b)=>{for(let C=0;C<l;C++){let x=0;for(let _=c.rowPtr[C];_<c.rowPtr[C+1];_++){const S=c.colIdx[_];x+=d?c.w[_]*u[S]*E[S]:c.w[_]*E[S]}d?b[C]=c.degree[C]>0?E[C]-u[C]*x:0:b[C]=c.degree[C]*E[C]-x}};let h=2;if(!d){h=1;for(let E=0;E<l;E++)for(let b=c.rowPtr[E];b<c.rowPtr[E+1];b++)h=Math.max(h,c.degree[E]+c.degree[c.colIdx[b]])}const g=Vt(i),w=Zi(f,l,n,h,g);if(r==="randomWalk")for(const E of w.vectors)for(let b=0;b<l;b++)E[b]*=c.degree[b]>0?u[b]:1;const m=new Float64Array(l*n);for(let E=0;E<n;E++)for(let b=0;b<l;b++)m[b*n+E]=w.vectors[E][b];if(r==="normalized")for(let E=0;E<l;E++){const b=m.subarray(E*n,E*n+n),C=ws(...b);C>0&&b.forEach((x,_)=>b[_]=x/C)}let A=new Uint32Array(l),p=1/0;for(let E=0;E<Ji;E++){const b=ta(m,l,n,n,o,s,g),C=Qi(m,b,l,n,n);C<p&&(p=C,A=b)}const{labels:y,count:v}=tt(A);return{...K(y,v),eigenvalues:w.values,eigenvectors:w.vectors,converged:w.converged}}const na=10;function ue(t,e,n,r,o){let s=0;for(let i=0;i<o;i++){const a=t[e*o+i]-n[r*o+i];s+=a*a}return Math.sqrt(s)}function ra(t,e,n){const r=t.nodeCount,o=new Float64Array(r*n),s=t.directed?t.degree():t.outDegree();for(let i=0;i<r;i++){const a=s[i]/Math.max(1,r-1);for(let l=0;l<n;l++)o[i*n+l]=(e()-.5)*.1+a*.1}return o}function oa(t,e){let n=1/0;for(let r=0;r<t.centreCount;r++)n=Math.min(n,ue(t.emb,e,t.centres,r,t.dim));return n}function sa(t,e,n){const{n:r,dim:o,emb:s,centres:i}=t;for(let a=0;a<r;a++)if(n-=e[a],n<=0){i.set(s.subarray(a*o,(a+1)*o),t.centreCount*o),t.centreCount++;return}}function ia(t,e,n){const{n:r,dim:o,emb:s,centres:i}=t,a=Math.floor(n()*r);i.set(s.subarray(a*o,(a+1)*o),0),t.centreCount=1;const l=new Float64Array(r);for(let c=1;c<e;c++){let u=0;for(let d=0;d<r;d++){const f=oa(t,d);l[d]=f*f,u+=f*f}sa(t,l,n()*u)}}function Tn(t){const{n:e,dim:n,emb:r,centres:o,centreCount:s,labels:i}=t;for(let a=0;a<e;a++){let l=0,c=1/0;for(let u=0;u<s;u++){const d=ue(r,a,o,u,n);d<c&&(c=d,l=u)}i[a]=l}}function aa(t,e,n,r){const{n:o,dim:s,rowPtr:i,colIdx:a,emb:l}=t;e.fill(0);for(let c=0;c<o;c++){const u=c*s;for(let d=i[c];d<i[c+1];d++){const f=a[d]*s;for(let h=0;h<s;h++)e[u+h]+=n*(l[u+h]-l[f+h])}}for(let c=0;c<o;c++){const u=i[c+1]-i[c],d=n===0||u===0?0:r/(n*u);for(let f=c*s;f<(c+1)*s;f++)l[f]-=d*e[f]}}function la(t,e,n){const{n:r,dim:o,emb:s,centres:i,centreCount:a,labels:l}=t;e.fill(0),n.fill(0);for(let c=0;c<r;c++){const u=l[c];for(let d=0;d<o;d++)e[u*o+d]+=s[c*o+d];n[u]++}for(let c=0;c<a;c++)if(n[c]>0)for(let u=0;u<o;u++)i[c*o+u]=e[c*o+u]/n[c]}function ca(t,e){const{n,dim:r,rowPtr:o,colIdx:s,emb:i,centres:a,labels:l}=t;let c=0,u=0,d=0;for(let f=0;f<n;f++){const h=ue(i,f,a,l[f],r);c+=h*h}for(let f=0;f<n;f++)for(let h=o[f];h<o[f+1];h++){const g=ue(i,f,i,s[h],r);u+=g*g}for(const f of i)d+=f*f;return c+e*u+e*d}function ua(t,e){return t.every((n,r)=>n===e[r])}function da(t,e){const{numClusters:n,maxIterations:r=1e3,tolerance:o=1e-6,seed:s=42,learningRate:i=.5,lambda:a=.1}=e,l=t.nodeCount;if(l===0)return{...K(new Uint32Array(0),n),embeddings:new Float64Array(0),dimensions:0,loss:0,previousLoss:1/0,iterations:0,converged:!0};if(!Number.isInteger(n)||n<=0||n>l)throw $(new Error(`Invalid number of clusters: ${String(n)}. Must be between 1 and ${String(l)}`),"E_BAD_OPTION");const c=Vt(s),u=Math.min(64,l),d={n:l,dim:u,rowPtr:t.rowPtr,colIdx:t.colIdx,emb:ra(t,c,u),centres:new Float64Array(n*u),centreCount:0,labels:new Uint32Array(l)};ia(d,n,c);const f=new Float64Array(l*u),h=new Float64Array(n*u),g=new Uint32Array(n);let w=1/0,m=1/0,A=0,p=!1;const y=new Uint32Array(l);let v=0;for(;A<r;)if(A++,Tn(d),v=A>1&&ua(d.labels,y)?v+1:0,y.set(d.labels),aa(d,f,a,i),la(d,h,g),A>1&&(w=m),m=ca(d,a),Math.abs(w-m)<o||v>=na){p=!0;break}return Tn(d),{...K(d.labels,n),embeddings:d.emb,dimensions:u,loss:m,previousLoss:w,iterations:A,converged:p}}const fa=100;function ha(t,e,n){const r=t.nodeCount;if(!e){for(let s=0;s<r;s++)for(let i=s+1;i<r;i++)n[s*r+i]=n[i*r+s]=t.hasArc(s,i)?1:2;return}n.fill(1/0);const o=new Uint32Array(r);for(let s=0;s<r;s++)ga(t,s,n,o)}function ga(t,e,n,r){const o=e*t.nodeCount;n[o+e]=0,r[0]=e;let s=0,i=1;for(;s<i;){const a=r[s++],l=n[o+a]+1;for(let c=t.rowPtr[a];c<t.rowPtr[a+1];c++){const u=t.colIdx[c];n[o+u]===1/0&&(n[o+u]=l,r[i++]=u)}}}function wa(t,e={}){const{linkage:n="average",numClusters:r,distanceThreshold:o,useGraphDistance:s=!0}=e;if(r!==void 0&&(r<1||!Number.isInteger(r)))throw $(new Error("numClusters must be a positive integer"),"E_BAD_OPTION");const i=t.nodeCount;if(i===0)throw $(new Error("Cannot cluster empty graph"),"E_EMPTY_GRAPH");const a=pr("teraHAC",i,e.maxNodes,8*i*i,()=>new Float64Array(i*i));if(ha(t,s,a),n==="ward")for(let N=0;N<a.length;N++)a[N]*=a[N];const c={single:Math.min,complete:Math.max,average:null,ward:null}[n],u=Math.max(0,i-1),d=new Uint32Array(u),f=new Uint32Array(u),h=new Float64Array(u),g=new Uint32Array(u),w=new Uint32Array(i),m=new Uint32Array(i).fill(1),A=new Uint8Array(i).fill(1);for(let N=0;N<i;N++)w[N]=N;const p=(N,O)=>{const T=w[N],B=w[O];return T<i&&B<i?T<B:T>B},y=(N,O)=>{const[T,B]=p(N,O)?[N,O]:[O,N];let z=a[T*i+B];return w[T]<i&&w[B]<i?z=n==="ward"?Math.sqrt(z):z:c||(z/=m[T]*m[B]),z===1/0?fa:z},v=(N,O)=>{const T=w[N],B=w[O],z=Math.min(T,B),q=Math.max(T,B);return q<i?z*i+q:i*i+q*2*i+z},E=new Int32Array(i).fill(-1),b=new Float64Array(i),C=new Float64Array(i),x=(N,O)=>{const T=y(N,O),B=v(N,O);(E[N]===-1||T<b[N]||T===b[N]&&B<C[N])&&(E[N]=O,b[N]=T,C[N]=B)},_=N=>{E[N]=-1;for(let O=0;O<i;O++)A[O]&&O!==N&&x(N,O)};for(let N=0;N<i;N++)_(N);let S=i,P=0;for(;S>1&&(r===void 0||S>r);){let N=-1;for(let z=0;z<i;z++)A[z]&&(N===-1||b[z]<b[N]||b[z]===b[N]&&C[z]<C[N])&&(N=z);const O=b[N];if(o!==void 0&&O>o)break;const[T,B]=p(N,E[N])?[N,E[N]]:[E[N],N];d[P]=w[T],f[P]=w[B],h[P]=O,g[P]=m[T]+m[B];for(let z=0;z<i;z++)if(A[z]&&z!==T&&z!==B){const q=T*i+z,W=B*i+z,Y=z*i+T,et=z*i+B;a[q]=c?c(a[q],a[W]):a[q]+a[W],a[Y]=c?c(a[Y],a[et]):a[Y]+a[et]}w[T]=i+P,m[T]=g[P],A[B]=0,P++,S--,_(T);for(let z=0;z<i;z++)A[z]&&z!==T&&(E[z]===T||E[z]===B?_(z):x(z,T))}const I=Array.from(w.filter((N,O)=>A[O]===1)).sort((N,O)=>N-O);let F=I[0];const U=N=>N<i?1:g[N-i];for(let N=1;N<I.length;N++){const O=P+N-1;d[O]=F,f[O]=I[N],h[O]=1/0,g[O]=U(F)+U(I[N]),F=i+O}const R=pa(i,d,f,F,r??S),{labels:D,count:k}=tt(R);return{...K(D,k),left:d,right:f,distance:h,size:g,merges:P,root:F}}function pa(t,e,n,r,o){const s=[],i=[r];let a=0;for(;a<i.length&&s.length<o;){const c=i[a++];c<t||o===1||s.length+(i.length-a)+1>=o?s.push(c):i.push(e[c-t],n[c-t])}const l=new Uint32Array(t);return s.forEach((c,u)=>{const d=[c];for(;d.length>0;){const f=d.pop()??0;f<t?l[f]=u:d.push(e[f-t],n[f-t])}}),l}function ma(t,e,n){let r=e;for(let o=e;o<n;o++)(o===e||t[o]!==t[o-1])&&(t[r++]=t[o]);return r}function Io(t){const e=t.nodeCount,n=t.directed?[t,t.reverse()]:[t],r=new Uint32Array(e+1),o=new Uint32Array(n.reduce((i,a)=>i+a.colIdx.length,0));let s=0;for(let i=0;i<e;i++){r[i]=s;const a=s;for(const c of n)for(let u=c.rowPtr[i];u<c.rowPtr[i+1];u++)c.colIdx[u]!==i&&(o[s++]=c.colIdx[u]);const l=o.subarray(a,s);n.length>1&&l.sort(),s=ma(o,a,s)}return r[e]=s,{rowPtr:r,colIdx:o.slice(0,s)}}function xo(t,e,n){const r=t.length,o=new Float64Array(r);let s=0;for(let i=0;i<r;i++){const a=n[i+1]-n[i],l=a*(a-1)/2;s+=l,o[i]=l>0?t[i]/l:0}return{coefficient:o,transitivity:s>0?3*e/s:0}}function ya(t,e,n,r,o){let s=0,i=e[n],a=e[r];const l=e[n+1],c=e[r+1];for(;i<l&&a<c;)t[i]<t[a]?i++:(t[i]>t[a]||(o[n]++,o[r]++,o[t[i]]++,s++,i++),a++);return s}function Po(t){const e=t.nodeCount,{rowPtr:n,colIdx:r}=Io(t),o=(u,d)=>{const f=n[u+1]-n[u],h=n[d+1]-n[d];return h>f||h===f&&d>u},s=new Uint32Array(e+1),i=new Uint32Array(r.length/2);let a=0;for(let u=0;u<e;u++){s[u]=a;for(let d=n[u];d<n[u+1];d++)o(u,r[d])&&(i[a++]=r[d])}s[e]=a;const l=new Uint32Array(e);let c=0;for(let u=0;u<e;u++){const d=s[u+1];for(let f=s[u];f<d;f++)c+=ya(i,s,u,i[f],l)}return{perNode:l,total:c,...xo(l,c,n)}}function lt(t){return new Promise(e=>{e(t())})}function Rn(t,e){if(t.sigmaOverflow===!0)throw new ys(e);return t}function Dn(t,e){return{normalized:e?.normalized,sources:Jt(t,e?.sources,e?.k)}}function Aa(t,e,n){const r=n?.sources!==void 0||n?.k!==void 0,o=n?.harmonic===!0;return(!r||!e.directed)&&(!o||t.harmonicCloseness===!0&&!r)&&n?.normalized!==!0&&n?.cutoff===void 0&&n?.weights===void 0}function va(t,e){const n=e?.weighted===!0;return e?.sources===void 0&&e?.k===void 0?e?.harmonic===!0?{weighted:n,harmonic:!0}:{weighted:n}:{weighted:n,sources:Jt(t,e.sources,e.k,"closenessCentrality")}}function Ea(t,e){return e?.weights===void 0&&(e?.method??"auto")==="auto"&&e?.maxNodes===void 0&&e?.paths!==!0&&e?.weighted!==!1&&t.nodeCount<=Er&&t.flags.nonNegativeWeights&&t.flags.finiteWeights}function No(t){return t?.initialRanks!==void 0||t?.convergenceNorm==="max"}function $n(t){return{dampingFactor:t?.dampingFactor,maxIterations:t?.maxIterations,tolerance:t?.tolerance,weighted:t?.weighted===!0}}function ba(t,e,n){const r=n?.weighted===!0&&t.weights!==null?t.weightedOutDegree():t.outDegree();return No(n)||!e.some(o=>o>0)||r.includes(0)}function Ca(t,e){return e?.startVector===void 0&&!t.directed&&!t.flags.multigraph&&!Ia(t)}function Ia(t){const e=t.nodeCount,n=new Int8Array(e).fill(-1),r=new Uint32Array(e);for(let o=0;o<e;o++){if(n[o]!==-1||t.rowPtr[o]===t.rowPtr[o+1])continue;n[o]=0;let s=0;r[s++]=o;let i=!1;for(let a=0;a<s;a++){const l=r[a];for(let c=t.rowPtr[l];c<t.rowPtr[l+1];c++){const u=t.colIdx[c];n[u]===-1?(n[u]=1-n[l],r[s++]=u):n[u]===n[l]&&(i=!0)}}if(!i)return!0}return!1}function xa(t,e){if(!t.converged)throw new mr("eigenvectorCentrality",e?.maxIterations??100,e?.tolerance??1e-6);if(e?.normalized===!1)return t;const n=Float64Array.from(t.scores);return Lr(n),{scores:n,iterations:t.iterations,converged:!0}}function Pa(t,e){if(e?.normalized===!1||e?.alpha===0)return!1;const n=t.reverse(),r=e?.weighted===!0?n.weights:null,o=new Float64Array(t.nodeCount);let s,i=!1;for(let c=0;c<t.nodeCount;c++){let u=0;for(let d=n.rowPtr[c];d<n.rowPtr[c+1];d++)u+=r===null?1:r[d],o[c]+=r===null?1:Math.abs(r[d]);s??(s=u),i||(i=u!==s)}if(!i)return!1;let a=0;for(let c=0;c<t.nodeCount;c++)for(let u=n.rowPtr[c];u<n.rowPtr[c+1];u++)a=Math.max(a,o[c]*o[n.colIdx[u]]);const l=e?.alpha??.1;return l*l*a<1}function Na(t){let e=1/0,n=-1/0;for(const o of t)e=Math.min(e,o),n=Math.max(n,o);const r=n-e;if(r>0)for(let o=0;o<t.length;o++)t[o]=(t[o]-e)/r}function Fa(t){const e=Float64Array.from(t.scores);return Na(e),{scores:e,iterations:t.iterations,converged:t.converged}}function zn(t,e){let n=0;for(const r of t)n=e==="l2"?n+r*r:Math.max(n,r);if(n=e==="l2"?Math.sqrt(n):n,n>0)for(let r=0;r<t.length;r++)t[r]/=n}function ka(t,e){const n=e?.normalized===!1?"max":"l2",r=Float64Array.from(t.hubs),o=Float64Array.from(t.authorities);return zn(r,n),zn(o,n),{hubs:r,authorities:o,iterations:t.iterations,converged:t.converged}}function Bn(t,e,n){const{predArc:r}=n;return{dist:n.dist,predArc:r,pathTo:o=>zt(t,r,e,o),pathEdges:o=>Bt(t,r,e,o)}}function Sa(t,e){const{perNode:n,total:r,coefficient:o,transitivity:s}=e;return o!==void 0&&s!==void 0?{perNode:n,total:r,coefficient:o,transitivity:s}:{perNode:n,total:r,...xo(n,r,Io(t).rowPtr)}}function _a(t){return{accelerator:t??null,pageRank:(e,n)=>t?.pageRank!==void 0&&!No(n)?t.pageRank(e,$n(n)):Promise.resolve(Ao(e,n)),personalizedPageRank:(e,n,r)=>t?.personalizedPageRank!==void 0&&!ba(e,n,r)?t.personalizedPageRank(e,n,$n(r)):Promise.resolve(vo(e,n,r)),eigenvectorCentrality:(e,n)=>t?.eigenvectorCentrality!==void 0&&Ca(e,n)?t.eigenvectorCentrality(e,{maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:!1}).then(r=>xa(r,n)):new Promise(r=>{r(Wr(e,n))}),sssp:(e,n,r)=>{const o=V(e,n);return t?.sssp!==void 0?t.sssp(e,o,r).then(s=>Bn(e,o,s)):Promise.resolve(vr(e,o,r))},bellmanFord:(e,n,r)=>{const o=V(e,n);return t?.bellmanFord!==void 0&&(r?.weights===void 0||r.weights instanceof Float32Array)?t.bellmanFord(e,o,r).then(s=>({...Bn(e,o,s),hasNegativeCycle:s.hasNegativeCycle})):Promise.resolve(Cr(e,o,r))},breadthFirstSearch:(e,n,r)=>{const o=V(e,n);return t?.breadthFirstSearch!==void 0&&r?.target===void 0&&r?.arcOrder===void 0?t.breadthFirstSearch(e,o,r):Promise.resolve(Fr(e,o,r))},connectedComponents:e=>t?.connectedComponents!==void 0?t.connectedComponents(e):Promise.resolve(Tr(e)),weaklyConnectedComponents:e=>t?.weaklyConnectedComponents!==void 0?t.weaklyConnectedComponents(e):Promise.resolve(Rr(e)),minimumSpanningTree:(e,n)=>t?.minimumSpanningTree!==void 0&&n?.weights===void 0?t.minimumSpanningTree(e):Promise.resolve(mo(e,n)),kCoreDecomposition:e=>t?.kCoreDecomposition!==void 0?t.kCoreDecomposition(e):Promise.resolve(Qr(e)),katzCentrality:(e,n)=>t?.katzCentrality!==void 0&&Pa(e,n)?t.katzCentrality(e,{alpha:n?.alpha,beta:n?.beta,maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:n?.weighted===!0}).then(Fa):Promise.resolve(to(e,n)),hits:(e,n)=>t?.hits!==void 0?t.hits(e,{maxIterations:n?.maxIterations,tolerance:n?.tolerance,weighted:n?.weighted===!0}).then(r=>ka(r,n)):Promise.resolve(Zr(e,n)),louvain:(e,n)=>t?.louvain!==void 0?t.louvain(e,n):Promise.resolve(fo(e,n)),allPairsShortestPath:(e,n)=>t?.allPairsShortestPath!==void 0&&Ea(e,n)?t.allPairsShortestPath(e).then(({dist:r,n:o})=>({dist:r,n:o,hasNegativeCycle:!1})):Promise.resolve(br(e,n)),betweennessCentrality:(e,n)=>t?.betweennessCentrality!==void 0&&!e.flags.multigraph&&n?.endpoints!==!0?t.betweennessCentrality(e,Dn(e,n)).then(r=>Rn(r,"betweennessCentrality")):Promise.resolve(Pr(e,n)),edgeBetweennessCentrality:(e,n)=>t?.edgeBetweennessCentrality!==void 0&&!e.flags.multigraph&&n?.alive===void 0?t.edgeBetweennessCentrality(e,Dn(e,n)).then(r=>Rn(r,"edgeBetweennessCentrality")):Promise.resolve(Ze(e,n)),closenessCentrality:(e,n)=>t?.closenessCentrality!==void 0&&Aa(t,e,n)?t.closenessCentrality(e,va(e,n)):Promise.resolve(Mr(e,n)),labelPropagation:(e,n)=>t?.labelPropagation!==void 0&&n?.randomSeed===void 0?t.labelPropagation(e,n):Promise.resolve(Kr(e,n)),labelPropagationSynchronous:(e,n)=>t?.labelPropagation!==void 0?t.labelPropagation(e,{maxIterations:n?.maxIterations,weighted:n?.weighted}):Promise.resolve(Yr(e,n)),triangleCount:e=>t?.triangleCount!==void 0?t.triangleCount(e).then(n=>Sa(e,n)):Promise.resolve(Po(e)),depthFirstSearch:(e,n,r)=>lt(()=>Vr(e,n,r)),degrees:e=>lt(()=>Dr(e)),stronglyConnectedComponents:(e,n)=>lt(()=>dn(e,n)),leiden:(e,n)=>lt(()=>ro(e,n)),girvanNewman:(e,n)=>lt(()=>Xr(e,n)),maxFlow:(e,n,r,o)=>lt(()=>sn(e,n,r,o)),minSTCut:(e,n,r,o)=>lt(()=>jr(e,n,r,o)),stoerWagner:(e,n)=>lt(()=>wo(e,n)),kargerMinCut:(e,n)=>lt(()=>po(e,n)),commonNeighborsPrediction:(e,n)=>lt(()=>co(e,n)),adamicAdarPrediction:(e,n)=>lt(()=>uo(e,n)),primMST:(e,n)=>lt(()=>yo(e,n)),maximumBipartiteMatching:(e,n)=>lt(()=>go(e,n))}}const Ma={graphtyAStar:["directed","goal","gpu","heuristic","root","weight"],graphtyAdamicAdarForPairs:["directed","gpu","pairs"],graphtyAdamicAdarPrediction:["directed","gpu","includeExisting","topK"],graphtyAdamicAdarScore:["directed","gpu","source","target"],graphtyAllPairsShortestPath:["directed","gpu","maxNodes","method","paths","weight"],graphtyBellmanFord:["cutoff","directed","field","gpu","root","weight"],graphtyBetweennessCentrality:["directed","endpoints","field","gpu","k","normalized","sources"],graphtyBidirectionalDijkstra:["directed","goal","gpu","root","weight"],graphtyBreadthFirstSearch:["directed","field","goal","gpu","maxDepth","root"],graphtyClosenessCentrality:["cutoff","directed","field","gpu","harmonic","k","normalized","sources","weight"],graphtyCommonNeighborsForPairs:["directed","gpu","pairs"],graphtyCommonNeighborsPrediction:["directed","gpu","includeExisting","topK"],graphtyCommonNeighborsScore:["directed","gpu","source","target"],graphtyCompareAdamicAdarWithCommonNeighbors:["directed","edges","gpu","nonEdges"],graphtyCondensation:["directed","field","gpu"],graphtyConnectedComponents:["directed","field","gpu"],graphtyDegreeCentrality:["directed","field","gpu","mode","normalized"],graphtyDegrees:["directed","gpu"],graphtyDeltaPageRank:["dampingFactor","deltaThreshold","directed","field","gpu","maxIterations","personalization","priority","tolerance","weight"],graphtyDepthFirstSearch:["directed","field","goal","gpu","order","root"],graphtyDijkstra:["cutoff","directed","field","gpu","root","weight"],graphtyDirectionOptimizedBfs:["alpha","beta","directed","field","gpu","root"],graphtyEdgeBetweennessCentrality:["directed","field","gpu","k","normalized","sources"],graphtyEigenvectorCentrality:["directed","field","gpu","maxIterations","mode","normalized","startVector","tolerance"],graphtyEvaluateAdamicAdar:["directed","edges","gpu","nonEdges"],graphtyEvaluateCommonNeighbors:["directed","edges","gpu","nonEdges"],graphtyFindAllIsomorphisms:["directed","edgeMatch","gpu","nodeMatch","other"],graphtyGirvanNewman:["directed","field","gpu","maxCommunities","maxIterations","minCommunitySize","weight"],graphtyGreedyBipartiteMatching:["arcs","directed","gpu","left","right"],graphtyGrsbm:["directed","field","gpu","maxDepth","maxIterations","minClusterSize","seed","tolerance","weight"],graphtyHasCycle:["directed","gpu"],graphtyHierarchicalClustering:["directed","gpu","linkage","maxNodes"],graphtyHits:["directed","field","gpu","maxIterations","normalized","tolerance","weight"],graphtyIsBipartite:["arcs","directed","field","gpu"],graphtyIsGraphIsomorphic:["directed","edgeMatch","gpu","nodeMatch","other"],graphtyKCoreDecomposition:["directed","field","gpu"],graphtyKargerMinCut:["directed","field","gpu","iterations","randomSeed","weight"],graphtyKatzCentrality:["alpha","beta","directed","field","gpu","maxIterations","normalized","tolerance","weight"],graphtyKruskalMST:["directed","gpu","weight"],graphtyLabelPropagation:["directed","field","gpu","maxIterations","randomSeed","weight"],graphtyLabelPropagationSemiSupervised:["directed","field","gpu","maxIterations","randomSeed","seeds","weight"],graphtyLabelPropagationSynchronous:["directed","field","gpu","maxIterations","weight"],graphtyLeiden:["directed","field","gpu","maxIterations","randomSeed","resolution","threshold","weight"],graphtyLouvain:["directed","field","gpu","maxIterations","resolution","tolerance","weight"],graphtyMarkovClustering:["directed","expansion","field","gpu","inflation","maxIterations","pruningThreshold","selfLoops","tolerance","weight"],graphtyMaxFlow:["algorithm","directed","field","gpu","sink","source","weight"],graphtyMaximumBipartiteMatching:["arcs","directed","gpu","left","right"],graphtyMinSTCut:["algorithm","directed","field","gpu","sink","source","weight"],graphtyModularity:["clusters","directed","gpu","resolution","weight"],graphtyNodeClosenessCentrality:["cutoff","directed","gpu","harmonic","normalized","root","weight"],graphtyPageRank:["convergenceNorm","dampingFactor","directed","field","gpu","initialRanks","maxIterations","tolerance","weight"],graphtyPersonalizedPageRank:["convergenceNorm","dampingFactor","directed","field","gpu","initialRanks","maxIterations","personalization","tolerance","weight"],graphtyPrimMST:["directed","forest","gpu","root","weight"],graphtySpectralClustering:["directed","field","gpu","k","laplacianType","maxIterations","seed","tolerance","weight"],graphtyStoerWagner:["directed","field","gpu","weight"],graphtyStronglyConnectedComponents:["directed","field","gpu"],graphtySyncClustering:["directed","field","gpu","lambda","learningRate","maxIterations","numClusters","seed","tolerance"],graphtyTeraHAC:["directed","distanceThreshold","field","gpu","linkage","maxNodes","numClusters","useGraphDistance"],graphtyTopAdamicAdarCandidatesForNode:["candidates","directed","gpu","includeExisting","root","topK"],graphtyTopCandidatesForNode:["candidates","directed","gpu","includeExisting","root","topK"],graphtyTopologicalSort:["directed","gpu"],graphtyTriangleCount:["directed","field","gpu"],graphtyWeaklyConnectedComponents:["directed","field","gpu"]},Oc={ak:["k"],"balanced-tree":["branching","height","seed","weights"],"barabasi-albert":["m","n","seed","triadProbability","weights"],barbell:["cliqueSize","pathLength","seed","weights"],"bianconi-barabasi":["fitness","m","n","seed","weights"],"bipartite-configuration-model":["leftDegrees","multiEdges","rightDegrees","seed","weights"],caveman:["cliques","seed","size","weights"],"chung-lu":["expectedDegrees","seed","weights"],"circular-ladder":["n","seed","weights"],complete:["n","seed","weights"],"complete-bipartite":["a","b","seed","weights"],"complete-multipartite":["seed","sizes","weights"],"configuration-model":["degrees","multiEdges","seed","selfLoops","weights"],"connected-caveman":["cliques","seed","size","weights"],cycle:["n","seed","weights"],"degree-corrected-sbm":["expectedDegrees","mixing","seed","sizes","weights"],"directed-configuration-model":["inDegrees","multiEdges","outDegrees","seed","selfLoops","weights"],"duplication-divergence":["n","retention","seed","weights"],empty:["n","seed","weights"],"erdos-renyi":["directed","n","p","seed","weights"],"erdos-renyi-gnm":["m","n","seed","weights"],"forest-fire":["backward","forward","maxBurn","n","seed","weights"],genrmf:["a","b","c1","c2","seed"],grid:["cols","diagonals","directed","obstacles","periodic","positions","rows","seed","weights"],"grid-3d":["cols","diagonals","directed","layers","obstacles","periodic","positions","rows","seed","weights"],"grid-flow-network":["cols","maxCapacity","minCapacity","rows","seed"],"hexagonal-lattice":["cols","positions","rows","seed","weights"],hyperbolic:["averageDegree","exponent","n","seed","temperature","weights"],hypercube:["dimension","seed","weights"],knn:["clusters","dimension","directed","k","n","seed","spread","weights"],kronecker:["directed","edges","initiator","multiEdges","permute","power","seed","selfLoops","weights"],ladder:["n","seed","weights"],"layered-flow-network":["layers","maxCapacity","minCapacity","p","seed"],lfr:["communityExponent","degreeExponent","maxCommunity","maxDegree","minCommunity","minDegree","mixing","n","seed","weights"],lollipop:["cliqueSize","pathLength","seed","weights"],"mobius-ladder":["n","seed","weights"],named:["name","seed","weights"],"newman-watts":["k","n","p","seed","weights"],path:["n","seed","weights"],petersen:["seed","weights"],"planted-partition":["groupSize","groups","pIn","pOut","seed","weights"],price:["attractiveness","citations","n","seed","weights"],"random-apollonian":["n","seed","weights"],"random-bipartite":["n1","n2","p","perfectMatching","seed","weights"],"random-dag":["layers","p","seed","weights"],"random-geometric":["dimension","n","periodic","radius","seed","weights"],"random-order-dag":["n","p","seed","weights"],"random-recursive-tree":["n","seed","weights"],"random-regular":["d","n","seed","weights"],"random-tree":["n","seed","weights"],"ring-of-cliques":["cliques","seed","size","weights"],rmat:["a","b","c","d","directed","edgeFactor","multiEdges","permute","scale","seed","selfLoops","weights"],star:["n","seed","weights"],"stochastic-block-model":["probabilities","seed","sizes","weights"],"triangular-lattice":["cols","positions","rows","seed","weights"],"watts-strogatz":["beta","k","n","seed","weights"],waxman:["alpha","beta","n","seed","weights"],wheel:["n","seed","weights"],"wilson-maze":["cols","rows","seed","weights"]},qn=5e3;function Ht(t,e){const n=setTimeout(()=>{console.warn(`graphty: ${e} has waited ${String(qn/1e3)} s for part of @graphty/cytoscape-extensions to load. If a module awaits ${e} at its top level, a Vite 6 or 7 production build never loads it: call it inside an async function, or with .then(), instead. On a slow network the call carries on.`)},qn),r=()=>{clearTimeout(n)};return t.then(r,r),t}const Vn=new WeakMap,Tt=new Map,We=new Map;function pe(t){return t.cy()}function Fo(t){let e=Vn.get(t);if(e===void 0){const n={version:0,entries:[],dropListeners:new Set};t.on("add remove data move",()=>{n.version++,ko(n,n.entries),n.entries=[]}),Vn.set(t,n),e=n}return e}function ko(t,e){const n=[];for(const{value:r}of e)Tt.has(r.snapshot)?We.set(r.snapshot,t):n.push(r.snapshot);So(t,n)}function So(t,e){if(e.length>0)for(const n of t.dropListeners)n(e)}function _o(t){Tt.set(t,(Tt.get(t)??0)+1);let e=!0;return()=>{if(!e)return;e=!1;const n=(Tt.get(t)??1)-1;if(n>0){Tt.set(t,n);return}Tt.delete(t);const r=We.get(t);r!==void 0&&(We.delete(t),So(r,[t]))}}function Ua(t,e){const n=Fo(t);return n.dropListeners.add(e),()=>{n.dropListeners.delete(e)}}function fn(t,e={}){const n=e.directed??!1,r=t;if(typeof e.weight=="function")return Wn(r,n,e.weight);const o=`${String(n)}|${e.weight??""}`,s=Fo(pe(t)),i=s.entries.find(l=>l.key===o&&l.version===s.version&&Oa(l.eles,r));if(i!==void 0)return i.value;const a=Wn(r,n,e.weight);return ko(s,s.entries.filter(l=>l.key===o)),s.entries=[...s.entries.filter(l=>l.key!==o),{key:o,eles:r,version:s.version,value:a}],a}function Oa(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function Wn(t,e,n){const r=t.nodes(),o=[],s=new Map;r.forEach((d,f)=>{o.push(d.id()),s.set(d.id(),f)});const i=t.edges().filter(d=>s.has(d.source().id())&&s.has(d.target().id())),a=new Uint32Array(i.length),l=new Uint32Array(i.length),c=n===void 0?void 0:new Float64Array(i.length);return i.forEach((d,f)=>{if(a[f]=s.get(d.source().id())??0,l[f]=s.get(d.target().id())??0,c!==void 0&&n!==void 0){const h=typeof n=="function"?n(d):d.data(n);c[f]=typeof h=="number"&&Number.isFinite(h)?h:1}}),{snapshot:dr({directed:e,ids:o,src:a,dst:l,weights:c}),nodes:r,edges:i}}function Qt(t,e,n){pe(t).batch(()=>{t.forEach((r,o)=>{r.data(n,e[o])})})}function ft(t,e){return(typeof e=="string"?t.nodes.filter(e):t.nodes.intersection(e)).map(r=>t.snapshot.ids.requireIndex(r.id()))}function Rt(t,e,n){if(e===void 0)return;const[r]=ft(t,e);if(r===void 0)throw new Error(`${n} matches no node of the collection`);return r}class Ta extends Error{constructor(e,n,r=null){super(n),this.name="GpuUnavailableError",this.code=e,this.fix=r}}function Kt(){return Object.assign(new Error("graphty: the Cytoscape core was destroyed"),{code:"E_CORE_DESTROYED"})}const Ra=["auto","off","require"];function hn(t,e){if(e!==void 0&&!Ra.includes(e)){const r=e===null||typeof e=="number"||typeof e=="boolean"?String(e):`a value of type ${typeof e}`,o=typeof e=="string"?JSON.stringify(e):r;throw new TypeError(`${t}: gpu must be "auto", "off" or "require"; got ${o}`)}}const Da=5e3;let Mo={},$a,Ut=null,St=null,Le=!1;const Ln=new WeakMap;function Tc(t={}){Mo={...t},Ut=null,St=null,Le=!1}function Uo(){const t=globalThis.navigator;return t?.gpu!==void 0&&t.gpu!==null?null:globalThis.process?.versions?.node===void 0?"this runtime has no WebGPU (navigator.gpu is undefined)":null}function za(){if(Ut===null){const t=Uo();if(t!==null)St={reason:t??"WebGPU was disabled",fix:null},Ut=Promise.resolve(St);else{const e=Ht(Gt(()=>import("./gpu-platform-browser-DnQwf3s0.js"),[],import.meta.url),"an ...Async method or a simulation layout").then(n=>n.gpuProvider(Mo),n=>({reason:`@graphty/webgpu-graph-algorithms did not load: ${To(n)}`,fix:"check that @graphty/webgpu-graph-algorithms is installed and that your bundler serves its chunk"}));Ut=e,e.then(n=>{Ut===e&&(St=n)})}}return Ut}function Oo(t){return"acquire"in t}function To(t){return t instanceof Error?t.message:String(t)}function Ro(t){t?.then(e=>e.gpu?.accelerator.dispose(),()=>{})}function Ba(t){let e=Ln.get(t);if(e===void 0){const n={decision:null,by:null,destroyed:!1};t.one("destroy",()=>{n.destroyed=!0,Ro(n.decision),n.decision=null}),Ln.set(t,n),e=n}return e}async function qa(t,e,n){let r;try{r=await n.acquire()}catch(i){const a=i instanceof Ta?i.fix:null;return{gpu:null,reason:`no usable WebGPU device: ${To(i)}`,fix:a}}if(e.destroyed)return r.accelerator.dispose(),{gpu:null,reason:"the core was destroyed"};const o=Ua(t,i=>{for(const a of i)r.accelerator.release(a)}),s=e.decision;return r.lost.then(()=>{o(),e.decision===s&&(e.decision=null)}),{gpu:r,reason:null}}function Do(t="auto"){if(t==="off")return{gpu:null,reason:'gpu: "off" was requested',fix:null};if(Ut===null&&$a===void 0){const e=Uo();if(e!==null)return{gpu:null,reason:e,fix:null}}return St===null||Oo(St)?null:{gpu:null,...St}}async function $o(t,e="auto"){let n=Do(e);if(n===null){const r=Ba(t),o=za(),s=St??await o;if(!Oo(s))n={gpu:null,...s};else{if(r.destroyed)throw Kt();(r.decision===null||r.by!==s)&&(Ro(r.decision),r.decision=Promise.resolve().then(()=>qa(t,r,s)),r.by=s),n=await r.decision}}if(e==="require"&&n.gpu===null)throw new Error(`graphty: gpu: "require" but ${n.reason??"no device"}`);return n}function je(t,e){Le||t.gpu!==null||t.fix===void 0||t.fix===null||e<Da||(Le=!0,console.warn(`graphty: a ${e.toLocaleString("en-US")}-node graph ran on the CPU because ${t.reason??"no GPU"}. To use the GPU: ${t.fix}. Pass gpu: "off" to run on the CPU without this warning.`))}function Va(t){let e=!1;return{accelerator:new Proxy(t,{get(r,o,s){const i=Reflect.get(r,o,s);return typeof i!="function"||o==="release"||o==="dispose"?i:(...a)=>(e=!0,i.apply(r,a))}}),used:()=>e}}function Ge(t,e,n){return t.gpu===null?{ran:"cpu",reason:t.reason,device:null}:e?{ran:"gpu",reason:null,device:t.gpu.device}:{ran:"cpu",reason:n,device:t.gpu.device}}const Wa={breadthFirstSearch:Fr,sssp:vr,bellmanFord:Cr,allPairsShortestPath:br,pageRank:Ao,personalizedPageRank:vo,eigenvectorCentrality:Wr,katzCentrality:to,hits:Zr,closenessCentrality:Mr,betweennessCentrality:Pr,edgeBetweennessCentrality:Ze,connectedComponents:Tr,weaklyConnectedComponents:Rr,triangleCount:Po,labelPropagation:Kr,labelPropagationSynchronous:Yr};function X(t,e){return t instanceof Promise?t.then(e):e(t)}const La=new Set(["gpu","directed","weight","field","root","goal","target","source","sink","heuristic","personalization","seeds","left","right","clusters","pairs","edges","nonEdges","nodeMatch","edgeMatch","priority","candidates","sources","alive","startVector","initialRanks"]),ja=new Set(["breadthFirstSearch","directionOptimizedBfs","depthFirstSearch","hasCycle","topologicalSort","degreeCentrality","degrees","eigenvectorCentrality","betweennessCentrality","edgeBetweennessCentrality","connectedComponents","weaklyConnectedComponents","stronglyConnectedComponents","condensation","kCoreDecomposition","triangleCount","isBipartite","isGraphIsomorphic","findAllIsomorphisms","hierarchicalClustering","teraHAC","syncClustering","maximumBipartiteMatching","greedyBipartiteMatching","commonNeighborsScore","adamicAdarScore","commonNeighborsPrediction","adamicAdarPrediction","commonNeighborsForPairs","adamicAdarForPairs","topCandidatesForNode","topAdamicAdarCandidatesForNode","evaluateCommonNeighbors","evaluateAdamicAdar","compareAdamicAdarWithCommonNeighbors"]),Ga=new Set(["hasCycle","topologicalSort","bidirectionalDijkstra","aStar","allPairsShortestPath","degrees","nodeClosenessCentrality","isGraphIsomorphic","findAllIsomorphisms","modularity","hierarchicalClustering","kruskalMST","primMST","maximumBipartiteMatching","greedyBipartiteMatching","commonNeighborsScore","adamicAdarScore","commonNeighborsPrediction","adamicAdarPrediction","commonNeighborsForPairs","adamicAdarForPairs","topCandidatesForNode","topAdamicAdarCandidatesForNode","evaluateCommonNeighbors","evaluateAdamicAdar","compareAdamicAdarWithCommonNeighbors"]),se={maxIterations:100,tolerance:1e-6},xe={...se,dampingFactor:.85},Ha={pageRank:xe,personalizedPageRank:xe,deltaPageRank:xe,eigenvectorCentrality:se,hits:se,katzCentrality:{...se,alpha:.1,beta:1},labelPropagation:{maxIterations:100},labelPropagationSynchronous:{maxIterations:100},labelPropagationSemiSupervised:{maxIterations:100},allPairsShortestPath:{paths:!0},maxFlow:{algorithm:"edmonds-karp"},minSTCut:{algorithm:"ford-fulkerson"}},zo=new Set(["topologicalSort","stronglyConnectedComponents","condensation","deltaPageRank"]);function Yt(t,e){return t.cy.collection(e)}function Pe(t){if(t.s.weights?.some(e=>e<0)===!0)throw Object.assign(new Error(`${t.name}: a weight is negative; use graphtyBellmanFord`),{code:"E_BAD_WEIGHT"})}function G(t,e,n){const r=Rt(t.cs,e,`${t.name}: ${n}`);if(r===void 0)throw new Error(`${t.name}: the ${n} option is required`);return r}function At(t,e,n=!1){const r=n?t.cs.edges:t.cs.nodes,s=(typeof e=="string"?r.filter(e):e)[0];if(s===void 0)return;if(!n){const a=t.s.ids.indexOf(s.id());return a===M?void 0:a}const i=t.cs.edges.toArray().indexOf(s);return i<0?void 0:i}function Q(t,e,n=!1){return r=>{const o=At(t,r,n);return o===void 0?void 0:e[o]}}function Xt(t,e){return Yt(t,Array.from(e,n=>t.cs.nodes[n]))}function He(t,e){return Yt(t,Array.from(e,n=>t.cs.edges[n]))}function de(t,e,n){const r=[];for(let o=0;o<e.length;o++)o>0&&r.push(t.cs.edges[n[o-1]]),r.push(t.cs.nodes[e[o]]);return Yt(t,r)}function Pt(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);return{sources:e.map(r=>G(t,r[0],n)),targets:e.map(r=>G(t,r[1],n))}}function ee(t,e){if(e===void 0)return;const n=ct(t.s.nodeCount);for(const r of ft(t.cs,e))bt(n,r,!0);return n}function gt(t,e,n,r=!1){return t.field!==void 0&&Qt(r?t.cs.edges:t.cs.nodes,e,t.field),{score:Q(t,e,r),...n}}function Z(t,e,n){const r=[];for(let s=0;s<e.length;s++){const i=e[s];if(i!==M){const a=r[i]??[];a.push(s),r[i]=a}}t.field!==void 0&&Qt(t.cs.nodes,e,t.field);const o=Array.from(r,s=>Xt(t,s??[]));return Object.assign(o,n,{cluster:s=>{const i=At(t,s);return i===void 0||e[i]===M?void 0:e[i]}})}function Ne(t,e,n){const r=[];for(let s=0;s<e.visitedCount;s++){const i=e.order[s],a=e.parent[i];if(a!==M){const l=t.cs.nodes[a],c=t.cs.nodes[i],d=(t.s.directed?l.edgesTo(c):l.edgesWith(c)).intersection(t.cs.edges)[0];d!==void 0&&r.push(d)}r.push(t.cs.nodes[i])}const o=n!==void 0&&e.depth[n]!==M;return t.field!==void 0&&Qt(t.cs.nodes,Array.from(e.depth,s=>s===M?Number.NaN:s),t.field),{path:Yt(t,r),found:Yt(t,o?[t.cs.nodes[n]]:[]),depth:s=>{const i=At(t,s);return i===void 0||e.depth[i]===M?void 0:e.depth[i]},parent:s=>{const i=At(t,s);return i===void 0||e.parent[i]===M?void 0:t.cs.nodes[e.parent[i]]}}}function jn(t,e){return t.field!==void 0&&Qt(t.cs.nodes,e.dist,t.field),{distanceTo:n=>{const r=At(t,n);return r===void 0?1/0:e.dist[r]},pathTo:n=>{const r=At(t,n);return r===void 0?t.cy.collection():de(t,e.pathTo(r),e.pathEdges(r))}}}function Ke(t,e,n,r){const o=[],s=[];for(let i=0;i<t.s.nodeCount;i++)(L(e,i)?o:s).push(i);return t.field!==void 0&&Qt(t.cs.nodes,Array.from({length:t.s.nodeCount},(i,a)=>L(e,a)?0:1),t.field),{value:r,cut:He(t,n),partitionFirst:Xt(t,o),partitionSecond:Xt(t,s)}}function Fe(t,e){return Ke(t,e.side,e.cutEdges,e.cutValue)}function ne(t,e){return Array.from(e.scores,(n,r)=>({source:t.cs.nodes[e.sources[r]],target:t.cs.nodes[e.targets[r]],score:n}))}function Gn(t,e,n){return r=>{const o=At(t,r);return o===void 0?void 0:e.nodes[n[o]]}}function Hn(t,e,n){const{nodeMatch:r,edgeMatch:o}=n;return{...t.rest,nodeMatch:r&&((s,i)=>r(t.cs.nodes[s],e.nodes[i])),edgeMatch:o&&((s,i)=>o(t.cs.edges[s],e.edges[i]))}}function Kn(t,e){const n=e.other;if(n===void 0)throw new Error(`${t.name}: the other option (the collection to compare with) is required`);let r=n;if(typeof n=="string"?r=t.cy.$(n):Array.isArray(n)&&(r=t.cy.collection(n)),typeof r?.nodes!="function")throw new TypeError(`${t.name}: the other option must be a Cytoscape collection, a selector or an array of elements`);return fn(r,{directed:t.s.directed})}function ie(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);if(typeof e=="function")return Float64Array.from(t.cs.nodes.toArray(),o=>e(o));const r=new Float64Array(t.s.nodeCount);for(const o of ft(t.cs,e))r[o]=1;return r}function Yn(t,e){const n=ie(t,e,"personalization");if(!n.some(r=>r>0))throw new Error(`${t.name}: personalization ${typeof e=="function"?"gives no node a positive weight":"matches no node"}`);return n}function Xn(t,e,n){if(e===void 0)throw new Error(`${t.name}: the ${n} option is required`);const r=new Uint32Array(t.s.nodeCount).fill(M);if(typeof e=="string"){const o=new Map;t.cs.nodes.forEach((s,i)=>{const a=s.data(e);a!==void 0&&(r[i]=o.get(a)??o.set(a,o.size).size-1)})}else e.forEach((o,s)=>{for(const i of ft(t.cs,o))r[i]=s});if(r.every(o=>o===M))throw new Error(typeof e=="string"?`${t.name}: ${n} names the data field "${e}", which no node has`:`${t.name}: ${n} matches no node`);return r}function Ka(t,e){const n=(r,o)=>{const s=At(t,r),i=At(t,o);return s===void 0||i===void 0?void 0:[s,i]};return{hasNegativeWeightCycle:e.hasNegativeCycle,distance:(r,o)=>{const s=n(r,o);return s===void 0?1/0:e.dist[s[0]*e.n+s[1]]},path:(r,o)=>{const{pathTo:s,pathEdges:i}=e;if(s===void 0||i===void 0)throw new Error("allPairsShortestPath: pass paths: true to walk shortest paths");const a=n(r,o);return a===void 0?t.cy.collection():de(t,s(a[0],a[1]),i(a[0],a[1]))}}}function Zn(t,e){return gt(t,e.scores,{rank:Q(t,e.scores),iterations:e.iterations,converged:e.converged})}function Jn(t,e){return Z(t,e.labels,{iterations:e.iterations,converged:e.converged})}function Qn(t){return t.topK!==void 0&&t.topK<=0?1/0:t.topK}function tr(t,e){const n=new Uint32Array(t.s.nodeCount).fill(M);return e.matching.forEach((r,o)=>{r!==M&&(n[o]=r,n[r]=o)}),{size:e.size,mate:r=>{const o=At(t,r);return o===void 0||n[o]===M?void 0:t.cs.nodes[n[o]]}}}function Ya(t){let e=0;for(let n=1;n<t.length;n++)t[n]>t[e]&&(e=n);return e}const gn={breadthFirstSearch:(t,e)=>{const n=Rt(t.cs,e.goal,`${t.name}: goal`);return X(t.call("breadthFirstSearch",t.s,G(t,e.root,"root"),{...t.rest,target:n}),r=>Ne(t,r,n))},directionOptimizedBfs:(t,e)=>Ne(t,Fs(t.s,G(t,e.root,"root"),t.rest),void 0),depthFirstSearch:(t,e)=>{const n=Rt(t.cs,e.goal,`${t.name}: goal`);if(n!==void 0&&e.order==="post")throw new Error(`${t.name}: goal stops only a pre-order walk; leave it out with order "post"`);return Ne(t,Vr(t.s,G(t,e.root,"root"),{...t.rest,target:n}),n)},hasCycle:(t,e={})=>$s(t.s),topologicalSort:(t,e={})=>{const n=qs(t.s);return n===null?null:Xt(t,n)},dijkstra:(t,e)=>(Pe(t),X(t.call("sssp",t.s,G(t,e.root,"root"),t.rest),n=>jn(t,n))),bellmanFord:(t,e)=>X(t.call("bellmanFord",t.s,G(t,e.root,"root"),t.rest),n=>({...jn(t,n),hasNegativeWeightCycle:n.hasNegativeCycle})),bidirectionalDijkstra:(t,e)=>{Pe(t);const n=Vi(t.s,G(t,e.root,"root"),G(t,e.goal,"goal"),t.rest);return{found:n.path.length>0,distance:n.distance,path:de(t,n.path,n.edges)}},aStar:(t,e)=>{const n=e.heuristic;Pe(t);const r=Li(t.s,G(t,e.root,"root"),G(t,e.goal,"goal"),n===void 0?()=>0:o=>n(t.cs.nodes[o]),t.rest);return{found:r.path.length>0,distance:r.distance,path:de(t,r.path,r.edges)}},allPairsShortestPath:(t,e={})=>X(t.call("allPairsShortestPath",t.s,t.rest),n=>Ka(t,n)),degreeCentrality:(t,e={})=>{const n=_s(t.s,t.rest);return gt(t,n,{degree:Q(t,n)})},degrees:(t,e={})=>{const n=Dr(t.s);if(!t.s.directed){const r=Float64Array.from(n.inDegree,(o,s)=>o+n.outDegree[s]);return{indegree:Q(t,r),outdegree:Q(t,r)}}return{indegree:Q(t,n.inDegree),outdegree:Q(t,n.outDegree)}},pageRank:(t,e={})=>{const n=e.initialRanks&&ie(t,e.initialRanks,"initialRanks");return X(t.call("pageRank",t.s,{...t.rest,initialRanks:n,weighted:t.weighted}),r=>Zn(t,r))},personalizedPageRank:(t,e)=>{const n=Yn(t,e.personalization),r=e.initialRanks&&ie(t,e.initialRanks,"initialRanks");return X(t.call("personalizedPageRank",t.s,n,{...t.rest,initialRanks:r,weighted:t.weighted}),o=>Zn(t,o))},deltaPageRank:(t,e={})=>{const n=e.personalization===void 0?void 0:Yn(t,e.personalization),r=e.priority===!0&&e.maxIterations===void 0?{maxIterations:1/0}:{},o={...t.rest,...r,personalization:n,weighted:t.weighted},s=e.priority===!0?new Ts(t.s).computeWithPriority(o):new Os(t.s).compute(o);if(s.some(i=>!Number.isFinite(i)))throw new RangeError(`${t.name}: the ranks overflowed (maxIterations ${String(t.rest.maxIterations)} is too high for this graph); lower maxIterations, or pass priority: true`);return gt(t,s,{rank:Q(t,s)})},eigenvectorCentrality:(t,e={})=>{const n=e.startVector&&ie(t,e.startVector,"startVector");return X(t.call("eigenvectorCentrality",t.s,{...t.rest,startVector:n}),r=>gt(t,r.scores,{iterations:r.iterations,converged:r.converged}))},katzCentrality:(t,e={})=>X(t.call("katzCentrality",t.s,{...t.rest,weighted:t.weighted}),n=>gt(t,n.scores,{iterations:n.iterations,converged:n.converged})),hits:(t,e={})=>X(t.call("hits",t.s,{...t.rest,weighted:t.weighted}),n=>gt(t,n.authorities,{hub:Q(t,n.hubs),authority:Q(t,n.authorities),iterations:n.iterations,converged:n.converged})),closenessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("closenessCentrality",t.s,{...t.rest,sources:n,weighted:t.weighted}),r=>gt(t,r.scores,{closeness:Q(t,r.scores)}))},nodeClosenessCentrality:(t,e)=>ks(t.s,G(t,e.root,"root"),{...t.rest,weighted:t.weighted}),betweennessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("betweennessCentrality",t.s,{...t.rest,sources:n}),r=>{const o=t.s.nodeCount,s=e.endpoints===!0?o*(o-1):(o-1)*(o-2),i=t.s.directed?1:2,a=e.normalized===!0&&s>0?s:i,l=Float64Array.from(r.scores,u=>u*a),c=l.reduce((u,d)=>Math.max(u,d),0);return gt(t,r.scores,{betweenness:Q(t,l),betweennessNormalized:Q(t,l.map(u=>c===0?0:u/c))})})},edgeBetweennessCentrality:(t,e={})=>{const n=e.sources===void 0?void 0:ft(t.cs,e.sources);return X(t.call("edgeBetweennessCentrality",t.s,{...t.rest,sources:n}),r=>gt(t,r.scores,{},!0))},connectedComponents:(t,e={})=>X(t.call("connectedComponents",t.s),n=>Z(t,n.labels,{})),weaklyConnectedComponents:(t,e={})=>X(t.call("weaklyConnectedComponents",t.s),n=>Z(t,n.labels,{})),stronglyConnectedComponents:(t,e={})=>Z(t,dn(t.s).labels,{}),condensation:(t,e={})=>{const n=Gi(t.s);return Z(t,n.components.labels,{condensed:n.condensed})},kCoreDecomposition:(t,e={})=>{const n=Qr(t.s),r=o=>Xt(t,Array.from(n.coreness.keys()).filter(s=>n.coreness[s]>=o));return gt(t,n.coreness,{maxCore:n.maxCore,core:r})},triangleCount:(t,e={})=>X(t.call("triangleCount",t.s),n=>gt(t,n.perNode,{coefficient:Q(t,n.coefficient),total:n.total,transitivity:n.transitivity})),isBipartite:(t,e={})=>{const n=kr(t.s,t.rest);if(n.sides===null){const s=t.cy.collection();return{bipartite:!1,partitionFirst:s,partitionSecond:s}}const{partitionFirst:r,partitionSecond:o}=Ke(t,n.sides,[],0);return{bipartite:!0,partitionFirst:r,partitionSecond:o}},isGraphIsomorphic:(t,e)=>{const n=Kn(t,e),r=fi(t.s,n.snapshot,Hn(t,n,e)),o=r.mapping;return{isomorphic:r.isomorphic,mapping:o===null?()=>{}:Gn(t,n,o)}},findAllIsomorphisms:(t,e)=>{const n=Kn(t,e);return hi(t.s,n.snapshot,Hn(t,n,e)).map(r=>Gn(t,n,r))},modularity:(t,e)=>un(t.s,Xn(t,e.clusters,"clusters"),t.rest),louvain:(t,e={})=>{const n=fo(t.s,t.rest);return Z(t,n.labels,{modularity:n.modularity,iterations:n.iterations})},leiden:(t,e={})=>{const n=ro(t.s,t.rest);return Z(t,n.labels,{modularity:n.modularity,iterations:n.iterations})},labelPropagation:(t,e={})=>X(t.call("labelPropagation",t.s,{...t.rest,weighted:t.weighted}),n=>Jn(t,n)),labelPropagationSynchronous:(t,e={})=>X(t.call("labelPropagationSynchronous",t.s,{...t.rest,weighted:t.weighted}),n=>Jn(t,n)),labelPropagationSemiSupervised:(t,e)=>{const n=Js(t.s,Xn(t,e.seeds,"seeds"),{...t.rest,weighted:t.weighted});return Z(t,n.labels,{iterations:n.iterations,converged:n.converged})},girvanNewman:(t,e={})=>{const n=Xr(t.s,t.rest),r={...t,field:void 0},o=i=>Z(r,n.levels[i]??[],{}),s=Ya(n.modularity);return Z(t,n.levels[s]??[],{modularity:n.modularity[s]??Number.NaN,levels:n.levels.length,level:o,modularities:Array.from(n.modularity)})},markovClustering:(t,e={})=>{const n=$i(t.s,t.rest);return Z(t,n.labels,{iterations:n.iterations,converged:n.converged})},spectralClustering:(t,e)=>{if(e.k===void 0)throw new Error(`${t.name}: the k option is required`);const n=ea(t.s,t.rest);return Z(t,n.labels,{converged:n.converged,eigenvalues:n.eigenvalues})},hierarchicalClustering:(t,e={})=>{const n=ci(t.s,t.rest),r={...t,field:void 0},o=i=>{const a=new Uint32Array(t.s.nodeCount).fill(M);return n.cut(i).forEach((l,c)=>{for(const u of l)a[u]=c}),Z(r,a,{})},s=i=>Z(r,n.cutAt(i).labels,{});return{merges:n.left.length,cut:o,cutAt:s}},teraHAC:(t,e={})=>{const n=wa(t.s,t.rest);return Z(t,n.labels,{merges:n.merges})},grsbm:(t,e={})=>Z(t,si(t.s,{...t.rest,weighted:t.weighted}).labels,{}),syncClustering:(t,e)=>{if(e.numClusters===void 0)throw new Error(`${t.name}: the numClusters option is required`);const n=da(t.s,t.rest);return Z(t,n.labels,{loss:n.loss,iterations:n.iterations,converged:n.converged})},kruskalMST:(t,e={})=>{const n=mo(t.s);return Object.assign(t.cs.nodes.union(He(t,n.edges)),{totalWeight:n.totalWeight})},primMST:(t,e={})=>{const n=Rt(t.cs,e.root,`${t.name}: root`),r=yo(t.s,{...t.rest,start:n});return Object.assign(t.cs.nodes.union(He(t,r.edges)),{totalWeight:r.totalWeight})},maxFlow:(t,e)=>{const n=sn(t.s,G(t,e.source,"source"),G(t,e.sink,"sink"),t.rest);return{...Ke(t,n.sourceSide,n.cutEdges,n.maxFlow),flow:Q(t,n.flow,!0)}},minSTCut:(t,e)=>Fe(t,jr(t.s,G(t,e.source,"source"),G(t,e.sink,"sink"),t.rest)),stoerWagner:(t,e={})=>Fe(t,wo(t.s)),kargerMinCut:(t,e={})=>Fe(t,po(t.s,t.rest)),maximumBipartiteMatching:(t,e={})=>tr(t,go(t.s,{...t.rest,left:ee(t,e.left),right:ee(t,e.right)})),greedyBipartiteMatching:(t,e={})=>tr(t,Bi(t.s,{...t.rest,left:ee(t,e.left),right:ee(t,e.right)})),commonNeighborsScore:(t,e)=>Ss(t.s,G(t,e.source,"source"),G(t,e.target,"target"),{directed:t.s.directed}),adamicAdarScore:(t,e)=>Ii(t.s,G(t,e.source,"source"),G(t,e.target,"target"),{directed:t.s.directed}),commonNeighborsPrediction:(t,e={})=>ne(t,co(t.s,{...t.rest,directed:t.s.directed,uniquePairs:!0})),adamicAdarPrediction:(t,e={})=>ne(t,uo(t.s,{...t.rest,directed:t.s.directed,uniquePairs:!0})),commonNeighborsForPairs:(t,e)=>Array.from(bi(t.s,Pt(t,e.pairs,"pairs"),{directed:t.s.directed})),adamicAdarForPairs:(t,e)=>Array.from(xi(t.s,Pt(t,e.pairs,"pairs"),{directed:t.s.directed})),topCandidatesForNode:(t,e)=>{const n=e.candidates===void 0?void 0:ft(t.cs,e.candidates);return ne(t,Ci(t.s,G(t,e.root,"root"),{...t.rest,topK:Qn(e),candidates:n,directed:t.s.directed}))},topAdamicAdarCandidatesForNode:(t,e)=>{const n=e.candidates===void 0?void 0:ft(t.cs,e.candidates);return ne(t,Pi(t.s,G(t,e.root,"root"),{...t.rest,topK:Qn(e),candidates:n,directed:t.s.directed}))},evaluateCommonNeighbors:(t,e)=>Fn(t.s,Pt(t,e.edges,"edges"),Pt(t,e.nonEdges,"nonEdges"),{directed:t.s.directed}),evaluateAdamicAdar:(t,e)=>kn(t.s,Pt(t,e.edges,"edges"),Pt(t,e.nonEdges,"nonEdges"),{directed:t.s.directed}),compareAdamicAdarWithCommonNeighbors:(t,e)=>{const n=Pt(t,e.edges,"edges"),r=Pt(t,e.nonEdges,"nonEdges"),o={directed:t.s.directed};return{adamicAdar:kn(t.s,n,r,o),commonNeighbors:Fn(t.s,n,r,o)}}};function Ct(t){return`graphty${t.charAt(0).toUpperCase()}${t.slice(1)}`}const Rc=Object.keys(gn).map(Ct),wn=["breadthFirstSearch","dijkstra","bellmanFord","allPairsShortestPath","pageRank","personalizedPageRank","eigenvectorCentrality","katzCentrality","hits","closenessCentrality","betweennessCentrality","edgeBetweennessCentrality","connectedComponents","weaklyConnectedComponents","triangleCount","labelPropagation","labelPropagationSynchronous"],Dc=wn.map(t=>`${Ct(t)}Async`);function Bo(t,e,n,r){const o=Ct(e);if(typeof n!="object"||n===null||Array.isArray(n))throw new TypeError(`${o}: takes one options object; pass a weight function as { weight: fn }`);if(n.weight!==void 0&&ja.has(e))throw new Error(`${o}: this algorithm reads no edge weights; remove the weight option`);if(n.field!==void 0&&Ga.has(e))throw new Error(`${o}: this algorithm has no per-element value to write; remove the field option`);const s=Ma[o]??[];for(const[h,g]of Object.entries(n))if(g!==void 0&&!s.includes(h))throw new Error(`${o}: unknown option ${h}; the options are ${s.join(", ")}`);const i=n.directed??zo.has(e),{mode:a}=n;if((a==="in"||a==="out")&&!i)throw new Error(`${o}: mode: "${a}" needs directed: true`);const l=fn(t,{directed:i,weight:n.weight}),c={...Ha[e],...Object.fromEntries(Object.entries(n).filter(([h,g])=>!La.has(h)&&g!==void 0))},u={name:o,cy:pe(t),cs:l,s:l.snapshot,field:n.field,weighted:n.weight!==void 0,rest:c,call:(h,...g)=>r[h](...g)},d=h=>{throw Xa(h,e)};let f;try{f=gn[e](u,n)}catch(h){d(h)}return{result:f instanceof Promise?f.catch(d):f,snapshot:l.snapshot}}function Xa(t,e){if(!(t instanceof Error))return t;const n=Ct(e),{code:r}=t;if(r==="E_NEEDS_UNDIRECTED"){const o=e==="connectedComponents"?", or call graphtyWeaklyConnectedComponents":"";t.message=`${n}: needs an undirected graph; leave out directed: true${o}`}else r==="E_NEEDS_DIRECTED"?t.message=`${n}: needs a directed graph; ${zo.has(e)?"leave out directed: false":"pass directed: true"}`:t.message.startsWith(`${n}:`)||(t.message=`${n}: ${t.message}`);return t}function er(t,e,n={}){if(hn(Ct(e),n?.gpu),n.gpu==="require"){const r=Ct(e);throw new Error(wn.includes(e)?`${r}: runs on the CPU; call ${r}Async for the GPU`:`${r}: has no GPU implementation and runs only on the CPU; leave out gpu: "require"`)}return Bo(t,e,n,Wa).result}async function nr(t,e,n={}){hn(`${Ct(e)}Async`,n?.gpu);const r=pe(t),o=await $o(r,n.gpu);if(r.destroyed())throw Kt();const s=o.gpu===null?null:Va(o.gpu.accelerator);je(o,t.nodes().length);const{result:i,snapshot:a}=Bo(t,e,n,_a(s?.accelerator??null)),l=_o(a);let c;try{c=await i}catch(d){throw r.destroyed()?Kt():d}finally{l(),o.gpu!==null&&typeof n.weight=="function"&&o.gpu.accelerator.release(a)}const u=Ge(o,s?.used()??!1,"the options or the graph need the CPU implementation");return Object.assign(c,{backend:u})}function Za(t){for(const e of Object.keys(gn)){const n=Ct(e);t("collection",n,function(r){return er(this,e,r)}),t("core",n,function(r){return er(this.elements(),e,r)})}for(const e of wn){const n=`${Ct(e)}Async`;t("collection",n,function(r){return nr(this,e,r)}),t("core",n,function(r){return nr(this.elements(),e,r)})}}class Ja extends Error{constructor(e){super(`graphty: the core already has an element with id "${e}", so the new graph would merge into it; remove the existing elements (cy.elements().remove()) or add the graph to an empty core`),this.code="E_ID_TAKEN",this.name="IdTakenError",this.id=e}}function ke(t,e){if(t.destroyed())throw Kt();for(const n of e)if(n.group==="nodes"&&n.data.id!==void 0&&t.getElementById(n.data.id).nonempty())throw new Ja(n.data.id);for(const n of e)n.group==="edges"&&n.data.id!==void 0&&t.getElementById(n.data.id).nonempty()&&delete n.data.id;return t.add(e)}function Qa(t){const e=new Set(t.elements("[graphty_import]").map(r=>String(r.data("graphty_import"))));let n=e.size+1;for(;e.has(String(n));)n++;return String(n)}function tl(t,e,n){let r=0;e.forEach((o,s)=>{const i=t[r];if(i?.id()!==o.data.id)return;r++;const a=n[s];a!=null&&i.style(a)})}function el(t){t("core","graphtyGenerate",async function(n,r){const{generateElements:o}=await Ht(Gt(()=>import("./samples-Dja23zzq.js"),[],import.meta.url),"graphtyGenerate"),s=o(n,r);return{elements:ke(this,s.elements),directed:s.directed}}),t("core","graphtyDataset",async function(n,r){const{datasetElements:o}=await Ht(Gt(()=>import("./samples-Dja23zzq.js"),[],import.meta.url),"graphtyDataset"),s=await o(n,r);return r?.signal?.throwIfAborted(),{elements:ke(this,s.elements),directed:s.directed}}),t("core","graphtyImport",async function(n,r,o){const{importElements:s}=await Ht(Gt(()=>import("../io.js"),[],import.meta.url),"graphtyImport"),i=o?.styles!==void 0&&o.styles!==!1,a=await s(n,r,{...o,...i&&o.importId===void 0?{importId:Qa(this)}:{}}),l=o?.applyStyles===!0&&a.stylesheet!==void 0,c=ke(this,a.elements);l&&a.bypass!==void 0&&tl(c,a.elements,a.bypass);const u=this.style();if(l&&a.stylesheet!==void 0&&u!==void 0){let d=u;for(const f of a.stylesheet)d=d.selector(f.selector).style(f.style);d.update()}return{elements:c,directed:a.directed,format:a.format,report:a.report,...a.stylesheet===void 0?{}:{stylesheet:a.stylesheet}}});const e=async(n,r,o)=>{const{exportElements:s}=await Ht(Gt(()=>import("../io.js"),[],import.meta.url),"graphtyExport");return s(n,r,o)};t("core","graphtyExport",function(n,r){return e(this.elements(),n,r)}),t("collection","graphtyExport",function(n,r){return e(this,n,r)})}function nl(t,e,n,r){const{positions:o,dim:s,n:i}=t,a=new Float32Array(3*i),l=[0,0,0];for(let c=0;c<i;c++)for(let u=0;u<3;u++)a[3*c+u]=u<s?o[s*c+u]*e+l[u]:l[u];return a}function rl(t,e,n){const r=new Float64Array(n),o=new Float64Array(n);for(let s=0;s<e*n;s++){const i=t[s];Number.isNaN(i)||(r[s%n]+=i,o[s%n]++)}for(let s=0;s<n;s++)r[s]=o[s]>0?r[s]/o[s]:0;return r}function ol(t,e,n,r){let o=0;for(let s=0;s<e;s++){let i=0;for(let a=0;a<n;a++){const l=t[n*s+a]-r[a];Number.isNaN(l)||(i+=l*l)}o=Math.max(o,Math.sqrt(i))}return o}function Mt(t,e,n=1,r){const o=Math.floor(t.length/e),s=rl(t,o,e),i=ol(t,o,e,s),a=i>0?n/i:0;for(let l=0;l<o*e;l++){const c=t[l],u=r?.[l%e]??0;Number.isNaN(c)||(t[l]=a===0?u:(c-s[l%e])*a+u)}return t}const Se=34359738337,sl=185852,il=1;class qo{constructor(e){this.seed=e===null||e===0||Number.isNaN(e)?Math.floor(Math.random()*1e6):e,this.state=this.seed%Se}next(){return this.state=(sl*this.state+il)%Se,this.state/Se}}function al(t){const e=[0,0,0];if(t===null)return e;for(let n=0;n<3&&n<t.length;n++){const r=t[n];if(!Number.isFinite(r))throw new RangeError(`center[${n}] is not finite`);e[n]=r}return e}function me(t,e,n,r,o,s,i){const a=t.nodeCount;if(r!==2&&r!==3)throw new RangeError(`dim must be 2 or 3, got ${String(r)}`);if(e.length!==3*a)throw new RangeError(`positions has ${e.length} entries, expected ${3*a}`);if(!Number.isFinite(o)||o<=0)throw new RangeError(`scale must be a finite number > 0, got ${o}`);const l=al(s),c=[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY],u=[Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY];let d=0,f=0;for(let p=0;p<a;p++){let y=!0;for(let v=0;v<r;v++){const E=e[3*p+v];Number.isFinite(E)?(E<c[v]&&(c[v]=E),E>u[v]&&(u[v]=E)):y=!1}y?d++:f++}if(f===0)return;const h=i==="fr"?0:-1,g=1,w=[h,h,h],m=[g,g,g];if(d>0){for(let p=0;p<r;p++)if(Number.isFinite(c[p])&&Number.isFinite(u[p])&&(w[p]=(c[p]-l[p])/o,m[p]=(u[p]-l[p])/o,w[p]===m[p])){const y=(g-h)/2;w[p]-=y,m[p]+=y}}const A=new qo(n);for(let p=0;p<a;p++){let y=!1;for(let v=0;v<r;v++)Number.isFinite(e[3*p+v])||(y=!0);if(y){for(let v=0;v<r;v++){const E=3*p+v;if(!Number.isFinite(e[E])){const b=A.next(),C=w[v]+b*(m[v]-w[v]);e[E]=C*o+l[v]}}r===2&&(e[3*p+2]=l[2])}}}const rr=new WeakMap;function ll(t){if(t!=null)return typeof t=="number"?t:typeof t=="string"?Number(t):Number.NaN}function Lt(t,e=null){if(ps(t)){if(!t.directed)return t;const r=rr.get(t);if(r!==void 0)return r;const o=t.toUndirected().snapshot;return rr.set(t,o),o}const n=new ms({directed:!1,weighted:"auto"});if(Array.isArray(t))n.addNodes(t);else{n.addNodes(t.nodes());for(const[r,o]of t.edges()){const s=e!==null&&t.getEdgeData!==void 0?t.getEdgeData(r,o,e):void 0;n.addEdge(r,o,ll(s))}}return n.freeze()}function H(t,e,n){return Object.assign(t,{code:e,params:n})}function pt(t,e){const n=e.dim??2;if(n!==2&&n!==3)throw H(new Error(`dim must be 2 or 3, got ${String(n)}`),"E_BAD_OPTION",{option:"dim",value:n});const r=Array.from({length:n},(o,s)=>e.center?.[s]??0);return{n:t.nodeCount,dim:n,scale:e.scale??1,center:r}}function ht(t,e,n){return{positions:Float32Array.from(t),dim:e,n}}function It(t,e,n){if(e===2)return t;const r=t.length/2,o=new Float64Array(3*r);for(let s=0;s<r;s++)o[3*s]=t[2*s],o[3*s+1]=t[2*s+1],o[3*s+2]=Number.isNaN(t[2*s])?Number.NaN:n[2];return o}function Vo(t){const e=t??2;if(e!==2&&e!==3)throw H(new RangeError(`dim must be 2 or 3, got ${String(e)}`),"E_BAD_OPTION",{option:"dim",value:e});return e}function cl(t,e,n){const r=t.nodeCount;if(e==null)return new Float32Array(3*r).fill(Number.NaN);if(e.length!==n*r)throw H(new RangeError(`pos has ${e.length} values, expected ${n*r} (dim * nodeCount)`),"E_BAD_OPTION",{option:"pos",length:e.length,expected:n*r});return nl({positions:e,dim:n,n:r},1)}function ul(t,e={}){const{scaling:n=1,a:r=1.1,maxIter:o=1e3}=e;if(r<=1)throw H(new Error("The parameter a should be larger than 1"),"E_BAD_OPTION",{option:"a",value:r});const s=Lt(t),i=Vo(e.dim),a=s.nodeCount,l=Float64Array.from(cl(s,e.pos,i));me(s,l,e.seed??null,i,1,null,"fr");const c=new Float64Array(i*a);for(let y=0;y<a;y++)for(let v=0;v<i;v++)c[i*y+v]=l[3*y+v];const{rowPtr:u,colIdx:d}=s,f=n*Math.sqrt(a),h=.001,g=1e-6,w=new Float64Array(i*a),m=new Float64Array(i),A=new Int32Array(a);let p=g+1;for(let y=0;p>g&&y<o;y++){w.fill(0);for(let v=0;v<a;v++){for(let E=u[v];E<u[v+1];E++)A[d[E]]=v+1;for(let E=0;E<a;E++){if(v===E)continue;let b=0;for(let _=0;_<i;_++)m[_]=c[i*v+_]-c[i*E+_],b+=m[_]*m[_];const C=Math.sqrt(b)||.01,x=A[E]===v+1?r:1;for(let _=0;_<i;_++)w[i*v+_]+=f/C*m[_]-x*m[_]}}p=0;for(let v=0;v<a;v++){let E=0;for(let b=0;b<i;b++)c[i*v+b]+=w[i*v+b]*h,E+=w[i*v+b]*w[i*v+b];p+=Math.sqrt(E)}}return(e.scale!==void 0||e.center!==void 0)&&Mt(c,i,e.scale??1,e.center),{positions:Float32Array.from(c),dim:i,n:a}}function pn(t,e,n,r,o,s,i="subsets"){if(r!=="vertical"&&r!=="horizontal")throw H(new Error("align must be either vertical or horizontal"),"E_BAD_OPTION",{option:"align",value:r});const a=new Float64Array(2*t).fill(Number.NaN);if(e.forEach((l,c)=>{for(let u=0;u<l.length;u++){const d=l[u];if(!Number.isInteger(d)||d<0||d>=t)throw H(new Error(`layer node index ${String(d)} is not a node of the graph`),"E_BAD_NODE",{option:i,index:d});[a[2*d],a[2*d+1]]=n(c,e.length,u,l.length)}}),r==="vertical")return Mt(a,2,o,s);Mt(a,2,o,[s[1],s[0]]);for(let l=0;l<t;l++)[a[2*l],a[2*l+1]]=[a[2*l+1],a[2*l]];return a}const Wo=(t,e,n,r)=>[t-(e-1)/2,n-(r-1)/2];function Lo(t,e,n){const r=t.nodes.get(e);if(r===null)throw H(new Error(`${n} names node column "${e}", which the snapshot does not hold`),"E_UNKNOWN_COLUMN",{option:n,column:e});if(r.dtype!=="u32"&&r.dtype!=="dict")throw H(new Error(`${n} column "${e}" must be u32 or dict, not ${r.dtype}`),"E_COLUMN_TYPE",{option:n,column:e,dtype:r.dtype,expected:["u32","dict"]});const o=r.dtype==="u32"?r.data:r.codes,s=new Map;for(let i=0;i<t.nodeCount;i++)if(r.isSet(i)){const a=s.get(o[i]);a===void 0?s.set(o[i],[i]):a.push(i)}return[...s.keys()].sort((i,a)=>i-a).map(i=>s.get(i)??[])}function dl(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{subsets:i="subset",align:a="vertical"}=e,l=typeof i=="string"?Lo(t,i,"subsets"):i.map(c=>fe(t,c));return ht(It(pn(n,l,Wo,a,o,s),r,s),r,n)}function fl(t,e){const n=t.nodeCount;if(!Number.isInteger(e)||e<0||e>=n)throw H(new Error(`start node ${String(e)} is not in the graph`),"E_BAD_NODE",{option:"start",index:e});const r=new Uint8Array(n);r[e]=1;let o=1;const s=[[e]];for(let i=s[0];;){const a=[];for(const l of i)for(let c=t.rowPtr[l];c<t.rowPtr[l+1];c++){const u=t.colIdx[c];r[u]===0&&(r[u]=1,a.push(u))}if(a.length===0)break;o+=a.length,s.push(a),i=a}if(o<n)throw Object.assign(new Error("bfs_layout didn't include all nodes. Graph may be disconnected."),{code:"E_NOT_CONNECTED",params:{nodeCount:n,reached:o}});return s}function hl(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{start:i=0,align:a="vertical"}=e,l=n===0?[]:fl(Lt(t),V(t,i));return ht(It(pn(n,l,Wo,a,o,s),r,s),r,n)}function gl(t,e,n,r,o,s,i){const a=r;return pn(t,[e,n],(l,c,u,d)=>[l*a-a/2,u/d-1/2],o,s,i)}function wl(t,e){const n=typeof e=="string"||e instanceof Uint32Array?e:hr(t,e);if(typeof n!="string"){if(n.length<Math.ceil(t.nodeCount/32))throw H(new Error(`top mask has ${n.length} words; ${t.nodeCount} nodes need ${Math.ceil(t.nodeCount/32)}`),"E_BAD_OPTION",{option:"top",length:n.length,expected:Math.ceil(t.nodeCount/32)});return o=>(n[o>>>5]>>>(o&31)&1)===1}const r=t.nodes.get(n);if(r===null)throw H(new Error(`top names node column "${n}", which the snapshot does not hold`),"E_UNKNOWN_COLUMN",{option:"top",column:n});if(r.dtype!=="bool")throw H(new Error(`top column "${n}" must be bool, not ${r.dtype}`),"E_COLUMN_TYPE",{option:"top",column:n,dtype:r.dtype,expected:["bool"]});return o=>r.isSet(o)&&(r.data[o>>>5]>>>(o&31)&1)===1}function or(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{top:i,align:a="vertical",aspectRatio:l=4/3}=e,c=i===void 0?f=>f%2===0:wl(t,i),u=[],d=[];for(let f=0;f<n;f++)(c(f)?u:d).push(f);return ht(It(gl(n,u,d,l,a,o,s),r,s),r,n)}const Zt={zeros:function(t){return typeof t=="number"?new Array(t).fill(0):t.length===1?new Array(t[0]).fill(0):new Array(t[0]).fill(0).map(()=>this.zeros(t.slice(1)))},ones:function(t){return typeof t=="number"?new Array(t).fill(1):t.length===1?new Array(t[0]).fill(1):new Array(t[0]).fill(1).map(()=>this.ones(t.slice(1)))},linspace:function(t,e,n){if(n===1)return[t];const r=(e-t)/(n-1);return Array.from({length:n},(o,s)=>t+s*r)},array:function(t){return Array.isArray(t)?[...t]:[t]},repeat:function(t,e){const n=[];for(let r=0;r<e;r++)n.push(...Zt.array(t));return n},mean:function(t,e=null){if(e===null){const n=Array.isArray(t[0])?t.flat(1/0):t;return n.reduce((o,s)=>o+s,0)/n.length}if(e===0){const n=[],r=t;for(let o=0;o<r[0].length;o++){let s=0;for(let i=0;i<r.length;i++)s+=r[i][o];n.push(s/r.length)}return n}return t.map(n=>Zt.mean(n))},add:function(t,e){return!Array.isArray(t)&&!Array.isArray(e)?t+e:Array.isArray(t)?Array.isArray(e)?t.map((n,r)=>n+e[r]):t.map(n=>n+e):e.map(n=>t+n)},subtract:function(t,e){return!Array.isArray(t)&&!Array.isArray(e)?t-e:Array.isArray(t)?Array.isArray(e)?t.map((n,r)=>n-e[r]):t.map(n=>n-e):e.map(n=>t-n)},max:function(t){return Array.isArray(t)?Math.max(...t.flat(1/0)):t},min:function(t){return Array.isArray(t)?Math.min(...t.flat(1/0)):t},norm:function(t){return Math.sqrt(t.reduce((e,n)=>e+n*n,0))}};function pl(t,e,n,r){const o=new Float64Array(t*e);if(t===1)o.set(r);else if(e===2){const s=Zt.linspace(0,2*Math.PI,t+1);for(let i=0;i<t;i++)o[2*i]=yt(s[i])*n+r[0],o[2*i+1]=wt(s[i])*n+r[1]}else{const s=(1+Math.sqrt(5))/2;for(let i=0;i<t;i++){const a=2*Math.PI*i/s,l=gr(1-2*(i+.5)/t);o[3*i]=wt(l)*yt(a)*n+r[0],o[3*i+1]=wt(l)*wt(a)*n+r[1],o[3*i+2]=yt(l)*n+r[2]}}return o}function ml(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(pl(n,r,o,s),r,n)}const J=Object.freeze({maxIter:100,jitterTolerance:1,scalingRatio:2,gravity:1,strongGravity:!1,distributedAction:!1,linlog:!1,dissuadeHubs:!1,dim:2,scale:1,settleThreshold:.001,settleWindow:10,iterationsPerStep:1}),ae=.01,yl=1e-4,jo=1e-8,Ot=256,sr=.9,Al=5,vl=1e4;function ir(t,e,n){if(n.meta.components!==1)throw new RangeError(`node column "${e}" has ${n.meta.components} components; a node vector has one`);switch(n.dtype){case"f32":case"f64":{const r=t.nodes.gpuView(e);return r instanceof Float32Array?r:new Float32Array(r)}case"u32":case"i32":return new Float32Array(t.nodes.gpuView(e));case"u8":return new Float32Array(n.data);default:throw new TypeError(`node column "${e}" is ${n.dtype}, not numeric`)}}function El(t,e,n){const r=e.nodeCount;if(t==null){const s=e.nodes.byRole("mass");if(s!==null)return ir(e,s.meta.name,s);const i=new Float32Array(r);for(let a=0;a<r;a++)i[a]=n(a);return i}if(t instanceof Float32Array){if(t.length!==r)throw new RangeError(`the node vector has ${t.length} values, expected ${r}`);return t}if(typeof t=="string"){const s=e.nodes.get(t);if(s===null)throw new RangeError(`the option names node column "${t}", which the snapshot does not hold`);return ir(e,t,s)}const o=new Float32Array(r);for(let s=0;s<r;s++){const i=t[e.ids.idOf(s)];o[s]=i===void 0?n(s):i}return o}function Go(t,e){if(t===!0)return e.weights;if(t===!1||t===null||t===void 0)return null;const n=e.edges.get(t);if(n===null)throw new RangeError(`weight names edge column "${t}", which the snapshot does not hold`);if(n.meta.components!==1)throw new RangeError(`edge column "${t}" has ${n.meta.components} components; a weight column has one`);switch(n.dtype){case"f32":case"f64":case"u32":case"i32":{const r=wr(e,e.edges.gpuView(t));return r instanceof Float32Array?r:new Float32Array(r)}default:throw new TypeError(`edge column "${t}" is ${n.dtype}; a weight column is f32, f64, u32 or i32`)}}function Nt(t,e,n,r,o){if(e===void 0)return n;if(typeof e!="number"||!r(e))throw new RangeError(`ForceAtlas2Simulation: ${t} must be ${o}, got ${String(e)}`);return e}function _e(t,e,n){if(e===void 0)return n;if(typeof e!="boolean")throw new TypeError(`ForceAtlas2Simulation: ${t} must be a boolean, got ${String(e)}`);return e}function bl(t){const e=[0,0,0];if(t===void 0)return e;if(t.length>3)throw new RangeError(`ForceAtlas2Simulation: center has ${t.length} components, at most 3 expected`);for(let n=0;n<t.length;n++){const r=t[n];if(typeof r!="number"||!Number.isFinite(r))throw new RangeError(`ForceAtlas2Simulation: center[${n}] is not finite`);e[n]=r}return e}function le(t){return Number.isInteger(t)&&t>=1}function Cl(t){const e=t??{},n=e.dim??J.dim;if(n!==2&&n!==3)throw new RangeError(`ForceAtlas2Simulation: dim must be 2 or 3, got ${String(n)}`);const r=e.compat??"paper";if(r!=="paper"&&r!=="networkx")throw new RangeError(`ForceAtlas2Simulation: compat must be "paper" or "networkx", got ${String(r)}`);return{maxIter:Nt("maxIter",e.maxIter,J.maxIter,le,"an integer >= 1"),jitterTolerance:Nt("jitterTolerance",e.jitterTolerance,J.jitterTolerance,o=>o>0,"> 0"),scalingRatio:Nt("scalingRatio",e.scalingRatio,J.scalingRatio,o=>o>0,"> 0"),gravity:Nt("gravity",e.gravity,J.gravity,o=>o>=0,">= 0"),strongGravity:_e("strongGravity",e.strongGravity,J.strongGravity),distributedAction:_e("distributedAction",e.distributedAction,J.distributedAction),linlog:_e("linlog",e.linlog,J.linlog),nodeMass:e.nodeMass??null,weight:e.weight??null,dim:n,scale:Nt("scale",e.scale,J.scale,o=>Number.isFinite(o)&&o>0,"a finite number > 0"),center:bl(e.center),settleThreshold:Nt("settleThreshold",e.settleThreshold,J.settleThreshold,o=>o>=0,">= 0"),settleWindow:Nt("settleWindow",e.settleWindow,J.settleWindow,le,"an integer >= 1"),iterationsPerStep:Nt("iterationsPerStep",e.iterationsPerStep,J.iterationsPerStep,le,"an integer >= 1"),compat:r,seed:e.seed??null}}function Me(t){return{attraction:new Float64Array(3*t),force:new Float64Array(3*t),swing:new Float64Array(t),traction:new Float64Array(t),sumX:new Float64Array(t),sumY:new Float64Array(t),sumZ:new Float64Array(t),sumW:new Float64Array(t),disp:new Float64Array(t),free:new Float64Array(t)}}function Ho(t){for(let e=Ot/2;e>0;e=Math.floor(e/2))for(let n=0;n<e;n++)t[n]=t[n]+t[n+e];return t[0]}function Ft(t,e){const n=Math.ceil(e/Ot),r=new Float64Array(n),o=new Float64Array(Ot);for(let s=0;s<n;s++){for(let i=0;i<Ot;i++){const a=s*Ot+i;o[i]=a<e?t[a]:0}r[s]=Ho(o)}return r}function kt(t){const e=new Float64Array(Ot);for(let n=0;n<t.length;n++){const r=n%Ot;e[r]=e[r]+t[n]}return Ho(e)}function vt(t,e,n){return Math.sqrt(t*t+e*e+n*n)}function Ko(t){let e=t>>>0;return e=(e^e>>>16)>>>0,e=Math.imul(e,2146121005)>>>0,e=(e^e>>>15)>>>0,e=Math.imul(e,2221713035)>>>0,e=(e^e>>>16)>>>0,e}function Il(t,e){const n=Math.min(t,e),r=Math.max(t,e);return Ko((Math.imul(n,2654435769)>>>0^r)>>>0)}function ar(t){return(t>>>8)*(1/16777216)}function Yo(t,e,n){const r=Il(t,e),o=6.283185307179586*ar(r);let s;if(n===2)s=[yt(o),wt(o),0];else{const i=2*ar(Ko((r^1540483477)>>>0))-1,a=Math.sqrt(Math.max(0,1-i*i));s=[a*yt(o),a*wt(o),i]}return t>e?[-s[0],-s[1],-s[2]]:s}function xl(t,e,n,r,o,s){const i=t,a=s,l=.05*Math.sqrt(i),c=Math.sqrt(l),u=10,d=Math.max(n,1e-30),f=Math.min(u,l*n/(i*i));let h=a*Math.max(c,f),g=o;e>2*d&&(g>.05&&(g=g*.5),h=Math.max(h,a));const w=e===0?1e30:h*g*n/e;return e>h*n?g>.05&&(g=g*.7):r<1e3&&(g=g*1.3),{speed:r+Math.min(w-r,.5*r),speedEfficiency:g}}function Pl(t,e,n,r){for(let o=0;o<n;o++){if(t[o]===e[o])continue;const s=Math.min(r,o*32+32);for(let i=o*32;i<s;i++)if(L(t,i)&&!L(e,i))return!0}return!1}class Nl{constructor(e){this.state="created",this.n=0,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.weights=null,this.mass=new Float64Array(0),this.owner=new Float32Array(0),this.positions=new Float64Array(0),this.oldForce=new Float64Array(0),this.fixed=null,this.scratch=Me(0),this.speedValue=1,this.speedEfficiencyValue=1,this.swingValue=1,this.tractionValue=1,this.settledCountValue=0,this.iterationsDoneValue=0,this.centroid=[0,0,0],this.pending=null,this.options=Cl(e),this.dim=this.options.dim,this.swingMode=this.options.compat==="networkx"?1:0,this.gravityCenter=this.options.compat==="networkx"?1:0}get settled(){return this.state!=="loaded"?!1:this.n===0||this.iterationsDoneValue>=this.options.maxIter||this.settledCountValue>=this.options.settleWindow}get iterationsDone(){return this.iterationsDoneValue}load(e,n){if(this.assertNotDisposed(),e.directed)throw new Error("ForceAtlas2Simulation: the snapshot must be undirected (pass toUndirected().snapshot)");const r=e.nodeCount;if(n.length!==3*r)throw new Error(`ForceAtlas2Simulation: positions.length ${n.length} !== 3 x ${r}`);const o=e.outDegree(),s=El(this.options.nodeMass,e,m=>o[m]+1),i=Go(this.options.weight,e),a=new Float64Array(r);for(let m=0;m<r;m++)if(a[m]=s[m],!(a[m]>0))throw new Error(`ForceAtlas2Simulation: mass[${m}] must be positive`);const l=i===null?null:Float64Array.from(i);r!==this.n&&(this.fixed=null),this.n=r,this.rowPtr=e.rowPtr,this.colIdx=e.colIdx,this.mass=a,this.weights=l,this.owner=n;const{scale:c,center:u,seed:d}=this.options;me(e,n,d,this.dim,c,u,"fa2");const[f,h,g]=u,w=new Float64Array(3*r);for(let m=0;m<r;m++)w[3*m]=(n[3*m]-f)/c,w[3*m+1]=(n[3*m+1]-h)/c,w[3*m+2]=this.dim===2?0:(n[3*m+2]-g)/c;this.positions=w,this.oldForce=new Float64Array(3*r),this.scratch=Me(r),this.speedValue=1,this.speedEfficiencyValue=1,this.swingValue=1,this.tractionValue=1,this.pending=null,this.writeInitialStatistics(),this.state="loaded",this.reheat()}step(e=this.options.iterationsPerStep){if(this.state==="disposed")throw new Error("ForceAtlas2Simulation: disposed");if(this.state==="created")throw new Error("ForceAtlas2Simulation: not loaded (call load() first)");if(!le(e))throw new RangeError(`ForceAtlas2Simulation: iterations must be an integer >= 1, got ${String(e)}`);if(this.settled)return;let n=0;for(;n<e&&!this.settled;)this.iterate(),this.iterationsDoneValue+=1,n+=1;this.writeBack()}setFixed(e){this.assertLoaded();const{n}=this,r=Math.ceil(n/32);if(e.length<r)throw new RangeError(`ForceAtlas2Simulation: setFixed: the mask has ${e.length} words, ${r} needed for ${n} nodes`);const o=this.fixed,s=o!==null&&Pl(o,e,r,n),i=new Uint32Array(r);i.set(e.subarray(0,r)),this.fixed=i,s&&this.reheat()}setPosition(e,n,r,o){this.assertLoaded();const{n:s,owner:i}=this;if(!Number.isInteger(e)||e<0||e>=s)throw new RangeError(`ForceAtlas2Simulation: setPosition(${e}): index out of range [0, ${s})`);if(!Number.isFinite(n)||!Number.isFinite(r)||!Number.isFinite(o))throw new RangeError("ForceAtlas2Simulation: setPosition: coordinates must be finite");i[3*e]=n,i[3*e+1]=r,i[3*e+2]=o;const{scale:a}=this.options,[l,c,u]=this.options.center;this.positions[3*e]=(i[3*e]-l)/a,this.positions[3*e+1]=(i[3*e+1]-c)/a,this.positions[3*e+2]=this.dim===2?0:(i[3*e+2]-u)/a,this.reheat()}reheat(){this.assertNotDisposed(),this.iterationsDoneValue=0,this.settledCountValue=0,this.swingMode===1&&(this.swingValue=1,this.tractionValue=1)}dispose(){this.state="disposed",this.owner=new Float32Array(0),this.scratch=Me(0),this.pending=null,this.positions=new Float64Array(0),this.oldForce=new Float64Array(0),this.mass=new Float64Array(0),this.weights=null,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.fixed=null,this.n=0}assertNotDisposed(){if(this.state==="disposed")throw new Error("ForceAtlas2Simulation: disposed")}assertLoaded(){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("ForceAtlas2Simulation: not loaded (call load() first)")}isFixed(e){return this.fixed!==null&&L(this.fixed,e)}writeBack(){const{n:e,owner:n,positions:r}=this,{scale:o}=this.options,[s,i,a]=this.options.center;for(let l=0;l<e;l++)this.isFixed(l)||(n[3*l]=r[3*l]*o+s,n[3*l+1]=r[3*l+1]*o+i,n[3*l+2]=this.dim===2?a:r[3*l+2]*o+a)}writeInitialStatistics(){const{n:e,positions:n}=this,r=[0,0,0];for(let o=0;o<e;o++)for(let s=0;s<3;s++)r[s]+=n[3*o+s];e===0?this.centroid=[0,0,0]:this.centroid=[r[0]/e,r[1]/e,r[2]/e],this.settledCountValue=0}foldTotals(e){return{sum:[kt(e.sumX),kt(e.sumY),kt(e.sumZ)],sumSq:kt(e.sumW),disp:kt(e.disp),free:kt(e.free)}}fold(e){const n=this.foldTotals(e),r=this.n,o=Math.sqrt(Math.max(n.sumSq,0)/r),s=n.free===0?0:n.disp/n.free;this.centroid=[n.sum[0]/r,n.sum[1]/r,n.sum[2]/r],this.settledCountValue=s<=this.options.settleThreshold*o?this.settledCountValue+1:0}iterate(){const{n:e,scratch:n}=this;this.pending!==null&&this.fold(this.pending);const{positions:r,mass:o,rowPtr:s,colIdx:i,weights:a,oldForce:l}=this,{scalingRatio:c,gravity:u,linlog:d,distributedAction:f,strongGravity:h}=this.options,{attraction:g,force:w,swing:m,traction:A,sumX:p,sumY:y,sumZ:v,sumW:E,disp:b,free:C}=n;for(let k=0;k<e;k++){let N=0,O=0,T=0;const B=r[3*k],z=r[3*k+1],q=r[3*k+2];for(let W=s[k];W<s[k+1];W++){const Y=i[W];if(Y===k)continue;const et=a===null?1:a[W],nt=r[3*Y]-B,ut=r[3*Y+1]-z,rt=r[3*Y+2]-q;let ot=et;if(d){const st=Math.max(vt(nt,ut,rt),ae);ot=et*fr(1+st)/st}N=N+nt*ot,O=O+ut*ot,T=T+rt*ot}f&&(N=N/o[k],O=O/o[k],T=T/o[k]),g[3*k]=N,g[3*k+1]=O,g[3*k+2]=T}m.fill(0),A.fill(0);const x=this.centroid[0],_=this.centroid[1],S=this.centroid[2];for(let k=0;k<e;k++){const N=r[3*k],O=r[3*k+1],T=r[3*k+2],B=o[k];let z=0,q=0,W=0;for(let at=0;at<e;at++){if(at===k)continue;const An=o[at],ye=N-r[3*at],Ae=O-r[3*at+1],ve=T-r[3*at+2];let te=ye*ye+Ae*Ae+ve*ve;if(te<jo){const be=Yo(k,at,this.dim),Ce=c*B*An/ae;z=z+be[0]*Ce,q=q+be[1]*Ce,W=W+be[2]*Ce;continue}te=Math.max(te,yl);const Ee=c*B*An/te;z=z+ye*Ee,q=q+Ae*Ee,W=W+ve*Ee}let Y=0,et=0,nt=0;const ut=this.gravityCenter===0?N-x:N,rt=this.gravityCenter===0?O-_:O,ot=this.gravityCenter===0?T-S:T,st=-u*B;if(h)Y=st*ut,et=st*rt,nt=st*ot;else{const at=vt(ut,rt,ot);at>ae&&(Y=st*ut/at,et=st*rt/at,nt=st*ot/at)}z=z+Y,q=q+et,W=W+nt;const mt=g[3*k]+z,dt=g[3*k+1]+q,xt=g[3*k+2]+W;w[3*k]=mt,w[3*k+1]=dt,w[3*k+2]=xt;const j=l[3*k],it=l[3*k+1],yn=l[3*k+2];this.swingMode===1?(m[k]=B*vt(N-mt,O-dt,T-xt),A[k]=.5*B*vt(N+mt,O+dt,T+xt)):this.isFixed(k)||(m[k]=B*vt(mt-j,dt-it,xt-yn),A[k]=.5*B*vt(mt+j,dt+it,xt+yn))}const P=kt(Ft(m,e)),I=kt(Ft(A,e));let F=P,U=I;this.swingMode===1&&(F=this.swingValue+P,U=this.tractionValue+I);const R=xl(e,F,U,this.speedValue,this.speedEfficiencyValue,this.options.jitterTolerance);this.speedValue=R.speed,this.speedEfficiencyValue=R.speedEfficiency,this.swingValue=F,this.tractionValue=U;const D=this.speedValue;b.fill(0),C.fill(0);for(let k=0;k<e;k++){const N=w[3*k],O=w[3*k+1],T=w[3*k+2],B=o[k];let z=B*vt(N,O,T);this.swingMode===0&&(z=B*vt(N-l[3*k],O-l[3*k+1],T-l[3*k+2]));const q=D/(1+Math.sqrt(D*z)),W=this.isFixed(k),Y=W?0:N*q,et=W?0:O*q;let nt=W?0:T*q;this.dim===2&&(nt=0);const ut=r[3*k]+Y,rt=r[3*k+1]+et,ot=r[3*k+2]+nt;r[3*k]=ut,r[3*k+1]=rt,r[3*k+2]=ot,this.swingMode===0&&(l[3*k]=N,l[3*k+1]=O,l[3*k+2]=T);const st=ut-x,mt=rt-_,dt=ot-S;p[k]=ut,y[k]=rt,v[k]=ot,E[k]=st*st+mt*mt+dt*dt,W||(b[k]=vt(Y,et,nt),C[k]=1)}this.pending={sumX:Ft(p,e),sumY:Ft(y,e),sumZ:Ft(v,e),sumW:Ft(E,e),disp:Ft(b,e),free:Ft(C,e)}}}const jt=.1,Fl=50,kl=.1,Sl=.7;function _l(t){const e=[0,0,0];if(t===void 0)return e;for(let n=0;n<3&&n<t.length;n++){const r=t[n];if(!Number.isFinite(r))throw new RangeError(`center[${n}] is not finite`);e[n]=r}return e}function re(t,e,n,r,o){if(!Number.isFinite(e)||e<n||e>r||o&&!Number.isInteger(e))throw new RangeError(`FruchtermanReingoldSimulation: ${t} must be ${o?"an integer":"a finite number"} in [${n}, ${r}], got ${e}`);return e}class Ml{constructor(e={}){this.state="created",this.n=0,this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.owner=new Float32Array(0),this.positions=new Float64Array(0),this.displacement=new Float64Array(0),this.fixed=null,this.k=0,this.dt=0,this.t=jt,this.previousEnergy=Number.POSITIVE_INFINITY,this.progress=0,this.pendingEnergy=null,this.iterationsDoneValue=0,this.settledCountValue=0,this.settledValue=!1,this.centroid=[0,0,0],this.rmsRadiusValue=0,this.meanDisplacementValue=0;const n=e.dim??J.dim;if(n!==2&&n!==3)throw new RangeError(`FruchtermanReingoldSimulation: dim must be 2 or 3, got ${String(n)}`);this.dim=n;const r=e.scale??J.scale;if(!Number.isFinite(r)||r<=0)throw new RangeError(`FruchtermanReingoldSimulation: scale must be a finite number > 0, got ${r}`);this.scale=r,this.center=_l(e.center),this.seed=e.seed??null;const o=e.k??null;if(o===null||Number.isNaN(o)||o===0)this.kOption=null;else{if(!Number.isFinite(o)||o<0)throw new RangeError(`FruchtermanReingoldSimulation: k must be null, 0 or a finite number > 0, got ${o}`);this.kOption=o}const s=e.cooling??"linear";if(s!=="linear"&&s!=="adaptive")throw new RangeError(`FruchtermanReingoldSimulation: cooling must be "linear" or "adaptive", got ${String(s)}`);this.adaptive=s==="adaptive",this.iterations=re("iterations",e.iterations??(this.adaptive?vl:Fl),0,Number.MAX_SAFE_INTEGER,!0),this.settleThreshold=re("settleThreshold",e.settleThreshold??J.settleThreshold,0,Number.POSITIVE_INFINITY,!1),this.settleWindow=re("settleWindow",e.settleWindow??J.settleWindow,1,Number.MAX_SAFE_INTEGER,!0),this.iterationsPerStep=re("iterationsPerStep",e.iterationsPerStep??J.iterationsPerStep,1,Number.MAX_SAFE_INTEGER,!0),this.fixedOption=e.fixed??null}get settled(){return this.settledValue}get iterationsDone(){return this.iterationsDoneValue}get settledCount(){return this.settledCountValue}get meanDisplacement(){return this.meanDisplacementValue}get rmsRadius(){return this.rmsRadiusValue}get temperature(){return this.t}load(e,n){if(this.assertNotDisposed(),e.directed)throw new Error("FruchtermanReingoldSimulation: the snapshot must be undirected (pass toUndirected().snapshot)");const r=e.nodeCount;if(n.length!==3*r)throw new RangeError(`FruchtermanReingoldSimulation: positions has ${n.length} entries, expected ${3*r}`);const o=this.resolveFixed(e,r);r!==this.n&&(this.positions=new Float64Array(3*r),this.displacement=new Float64Array(3*r)),this.n=r,this.rowPtr=e.rowPtr,this.colIdx=e.colIdx,this.owner=n,this.fixed=o,me(e,n,this.seed,this.dim,this.scale,this.center,"fr");const s=this.positions,[i,a,l]=this.center,{scale:c}=this;for(let u=0;u<r;u++)s[3*u]=(n[3*u]-i)/c,s[3*u+1]=(n[3*u+1]-a)/c,s[3*u+2]=this.dim===2?0:(n[3*u+2]-l)/c;this.k=this.kOption??1/Math.sqrt(r),this.dt=jt/(this.iterations+1),this.t=jt,this.resetAdaptive(),this.pendingEnergy=null,this.writeInitialStatistics(),this.iterationsDoneValue=0,this.settledCountValue=0,this.state="loaded",this.settledValue=r===0||this.computeSettled()}step(e=this.iterationsPerStep){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("FruchtermanReingoldSimulation: not loaded");if(!Number.isInteger(e)||e<1)throw new RangeError(`FruchtermanReingoldSimulation: step(${e}): an integer >= 1 is required`);if(this.settledValue)return;let n=0;for(;n<e&&!this.settledValue;)this.iterate(),n++,this.iterationsDoneValue++,this.settledValue=this.computeSettled();this.writeBack()}setFixed(e){this.assertLoaded();const{n}=this,r=Math.ceil(n/32);if(e.length<r)throw new RangeError(`FruchtermanReingoldSimulation: setFixed: the mask has ${e.length} words, ${r} needed for ${n} nodes`);let o=!1;const s=this.fixed;if(s!==null)for(let i=0;i<n&&!o;i++)L(s,i)&&!L(e,i)&&(o=!0);this.fixed=new Uint32Array(e.subarray(0,r)),o&&this.reheat()}setPosition(e,n,r,o){this.assertLoaded();const{n:s}=this;if(!Number.isInteger(e)||e<0||e>=s)throw new RangeError(`FruchtermanReingoldSimulation: setPosition(${e}): index out of range [0, ${s})`);if(!Number.isFinite(n)||!Number.isFinite(r)||!Number.isFinite(o))throw new RangeError("FruchtermanReingoldSimulation: setPosition: coordinates must be finite");const{owner:i,positions:a,scale:l}=this,[c,u,d]=this.center;i[3*e]=n,i[3*e+1]=r,i[3*e+2]=o,a[3*e]=(n-c)/l,a[3*e+1]=(r-u)/l,a[3*e+2]=this.dim===2?0:(o-d)/l,this.reheat()}reheat(){this.assertNotDisposed(),this.settledCountValue=0,this.adaptive?(this.iterationsDoneValue=0,this.t=jt,this.resetAdaptive()):(this.iterationsDoneValue=Math.floor(Sl*this.iterations),this.t=jt-this.dt*this.iterationsDoneValue),this.state==="loaded"&&(this.settledValue=this.n===0||this.computeSettled())}dispose(){this.state="disposed",this.n=0,this.positions=new Float64Array(0),this.displacement=new Float64Array(0),this.owner=new Float32Array(0),this.rowPtr=new Uint32Array(1),this.colIdx=new Uint32Array(0),this.fixed=null}iterate(){this.adaptive&&this.pendingEnergy!==null&&this.updateAdaptive(this.pendingEnergy);const{n:e,k:n,t:r,positions:o,displacement:s,rowPtr:i,colIdx:a,fixed:l}=this;s.fill(0);for(let y=0;y<e;y++){const v=o[3*y],E=o[3*y+1],b=o[3*y+2];for(let C=y+1;C<e;C++){const x=v-o[3*C],_=E-o[3*C+1],S=b-o[3*C+2],P=x*x+_*_+S*S;let I,F,U;if(P<jo){const R=Yo(y,C,this.dim),D=n*n/ae;I=R[0]*D,F=R[1]*D,U=R[2]*D}else{const R=Math.sqrt(P),D=n*n/R;I=x/R*D,F=_/R*D,U=S/R*D}s[3*y]+=I,s[3*y+1]+=F,s[3*y+2]+=U,s[3*C]-=I,s[3*C+1]-=F,s[3*C+2]-=U}}for(let y=0;y<e;y++){const v=o[3*y],E=o[3*y+1],b=o[3*y+2];for(let C=i[y];C<i[y+1];C++){const x=a[C],_=v-o[3*x],S=E-o[3*x+1],P=b-o[3*x+2],I=Math.sqrt(_*_+S*S+P*P)||kl,F=I*I/n;s[3*y]-=_/I*F,s[3*y+1]-=S/I*F,s[3*y+2]-=P/I*F}}const[c,u,d]=this.centroid;let f=0,h=0,g=0,w=0,m=0,A=0,p=0;for(let y=0;y<e;y++){if(l===null||!L(l,y)){const S=s[3*y],P=s[3*y+1],I=s[3*y+2],F=S*S+P*P+I*I,U=Math.sqrt(F);p+=F;const R=Math.min(U,r);U!==0&&(o[3*y]+=S/U*R,o[3*y+1]+=P/U*R,o[3*y+2]+=I/U*R),m+=R,A++}const v=o[3*y],E=o[3*y+1],b=o[3*y+2];f+=v,h+=E,g+=b;const C=v-c,x=E-u,_=b-d;w+=C*C+x*x+_*_}this.adaptive?this.pendingEnergy=p:this.t-=this.dt,this.centroid=[f/e,h/e,g/e],this.rmsRadiusValue=Math.sqrt(Math.max(w,0)/e),this.meanDisplacementValue=A===0?0:m/A,this.settledCountValue=this.meanDisplacementValue<=this.settleThreshold*this.rmsRadiusValue?this.settledCountValue+1:0}resetAdaptive(){this.previousEnergy=Number.POSITIVE_INFINITY,this.progress=0}updateAdaptive(e){e<this.previousEnergy?(this.progress++,this.progress>=Al&&(this.progress=0,this.t/=sr)):(this.progress=0,this.t*=sr),this.previousEnergy=e}writeBack(){const{n:e,owner:n,positions:r,scale:o,fixed:s}=this,[i,a,l]=this.center;for(let c=0;c<e;c++)s!==null&&L(s,c)||(n[3*c]=r[3*c]*o+i,n[3*c+1]=r[3*c+1]*o+a,n[3*c+2]=r[3*c+2]*o+l)}writeInitialStatistics(){const{n:e,positions:n}=this;if(e===0){this.centroid=[0,0,0],this.rmsRadiusValue=0,this.meanDisplacementValue=0;return}let r=0,o=0,s=0;for(let a=0;a<e;a++)r+=n[3*a],o+=n[3*a+1],s+=n[3*a+2];r/=e,o/=e,s/=e;let i=0;for(let a=0;a<e;a++){const l=n[3*a]-r,c=n[3*a+1]-o,u=n[3*a+2]-s;i+=l*l+c*c+u*u}this.centroid=[r,o,s],this.rmsRadiusValue=Math.sqrt(i/e),this.meanDisplacementValue=0}computeSettled(){return this.iterationsDoneValue>=this.iterations||this.settledCountValue>=this.settleWindow}resolveFixed(e,n){const r=Math.ceil(n/32),o=this.fixedOption;if(typeof o=="string"){const i=e.nodes.get(o);if(i===null)throw new RangeError(`FruchtermanReingoldSimulation: fixed names node column "${o}", which the snapshot does not hold`);if(i.dtype!=="bool")throw new TypeError(`FruchtermanReingoldSimulation: fixed names node column "${o}", which is ${i.dtype}, not bool`);return new Uint32Array(i.data.subarray(0,r))}if(o!==null){if(o.length<r)throw new RangeError(`FruchtermanReingoldSimulation: the fixed mask has ${o.length} words, ${r} needed for ${n} nodes`);return new Uint32Array(o.subarray(0,r))}const s=e.nodes.byRole("fixed");return s!==null&&s.dtype==="bool"?new Uint32Array(s.data.subarray(0,r)):n===this.n?this.fixed:null}assertNotDisposed(){if(this.state==="disposed")throw new Error("FruchtermanReingoldSimulation: disposed")}assertLoaded(){if(this.assertNotDisposed(),this.state!=="loaded")throw new Error("FruchtermanReingoldSimulation: not loaded")}}function Ul(t,e,n,r){if(e!==null&&(!Number.isInteger(e)||e<1))throw H(new Error("columns must be a positive integer"),"E_BAD_OPTION",{option:"columns",value:e});const o=new Float64Array(2*t);if(t===0)return o;const s=Math.min(e??Math.ceil(Math.sqrt(t)),t),i=Math.ceil(t/s),a=Math.max(s-1,i-1),l=a===0?0:2*n/a;for(let c=0;c<t;c++)o[2*c]=r[0]+(c%s-(s-1)/2)*l,o[2*c+1]=r[1]+(Math.floor(c/s)-(i-1)/2)*l;return o}function Ol(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(It(Ul(n,e.columns??null,o,s),r,s),r,n)}function Tl(t,e,n,r){if(e.length===0)return t.map(c=>-c);const o=t.slice(),s=new Array(e.length).fill(0),i=[];for(let c=0;c<e.length;c++){const u=e[c],d=n[c];i.push(1/d.reduce((f,h,g)=>f+h*u[g],0))}for(let c=e.length-1;c>=0;c--){const u=e[c];s[c]=i[c]*u.reduce((d,f,h)=>d+f*o[h],0);for(let d=0;d<o.length;d++)o[d]-=s[c]*n[c][d]}let a=1;if(e.length>0&&n.length>0){const c=n[n.length-1];a=e[e.length-1].reduce((d,f,h)=>d+f*c[h],0)/c.reduce((d,f)=>d+f*f,0)}const l=o.map(c=>a*c);for(let c=0;c<e.length;c++){const u=e[c],d=n[c],f=i[c]*d.reduce((h,g,w)=>h+g*l[w],0);for(let h=0;h<l.length;h++)l[h]+=u[h]*(s[c]-f)}return l.map(c=>-c)}function Rl(t,e,n,r,o,s){const l=r.reduce((d,f,h)=>d+f*e[h],0);if(l>=0)return 0;let c=s;const u=20;for(let d=0;d<u;d++){const f=t.map((g,w)=>g+c*e[w]);if(o(f)<=n+1e-4*c*l)return c;c*=.5}return 0}function Dl(t,e,n){const r=e.length,o=.001,s=t.map(h=>h.map(g=>g===0?0:1/g)),i=e.flat(),a=500,l=1e-5,c=10,u=[],d=[];for(let h=0;h<a;h++){const[g,w]=Ue(i,s,o,n),m=Tl(w,u,d),A=Rl(i,m,g,w,C=>Ue(C,s,o,n)[0],1);if(A===0){if(u.length===0)break;u.length=0,d.length=0;continue}const p=[...i];for(let C=0;C<i.length;C++)i[C]+=A*m[C];const[,y]=Ue(i,s,o,n),v=i.map((C,x)=>C-p[x]),E=y.map((C,x)=>C-w[x]);if(v.reduce((C,x,_)=>C+x*E[_],0)>1e-10&&(u.push(v),d.push(E)),u.length>c&&(u.shift(),d.shift()),Math.sqrt(y.reduce((C,x)=>C+x*x,0))<l)break}const f=[];for(let h=0;h<r;h++)f.push(i.slice(h*n,(h+1)*n));return f}function $l(t,e,n){const r=n.length;for(let o=0;o<r;o++)for(let s=o+1;s<r;s++){const i=e[o].map((u,d)=>u-e[s][d]),a=Math.sqrt(i.reduce((u,d)=>u+d*d,0)),l=n[o][s],c=a*l-1;t+=c*c}return t}function zl(t,e,n,r){const o=n.length;for(let s=0;s<o;s++)for(let i=s+1;i<o;i++){const a=e[s].map((f,h)=>f-e[i][h]),l=Math.sqrt(a.reduce((f,h)=>f+h*h,0))||1e-10,c=a.map(f=>f/l),u=n[s][i],d=l*u-1;for(let f=0;f<r;f++){const h=2*u*d*c[f];t[s*r+f]+=h,t[i*r+f]-=h}}}function Ue(t,e,n,r){const o=e.length,s=[];for(let c=0;c<o;c++)s.push(t.slice(c*r,(c+1)*r));let i=0;const a=new Array(r).fill(0);for(let c=0;c<o;c++)for(let u=0;u<r;u++)a[u]+=s[c][u];i+=.5*n*a.reduce((c,u)=>c+u*u,0),i=$l(i,s,e);const l=new Array(t.length).fill(0);for(let c=0;c<o;c++)for(let u=0;u<r;u++)l[c*r+u]+=n*a[u];return zl(l,s,e,r),[i,l]}const Bl=1e6,ql=42,Vl=24;function Xo(t,e){const n=Vl*t*t,r=e===null?`kamadaKawai: ${t} nodes need about ${n} bytes of n x n memory, more than this runtime could allocate. Pass a maxNodes to refuse such graphs up front.`:`kamadaKawai: ${t} nodes exceeds maxNodes ${e}; it would allocate about ${n} bytes. Pass a larger maxNodes to allow it.`;return Object.assign(new RangeError(r),{code:"E_TOO_LARGE",params:{nodeCount:t,maxNodes:e,bytes:n}})}function Wl(t,e){let n=null;return e===!0?n=t.edges.byRole("weight"):typeof e=="string"&&(n=t.edges.get(e)),n?.dtype==="f64"&&n.meta.components===1?wr(t,n.data):Go(e,t)}function Ll(t,e){const n=t.nodeCount;let r;if(e.dist!==null&&e.dist!==void 0){if(e.dist.length!==n*n)throw H(new RangeError(`kamadaKawai: dist has ${e.dist.length} values, expected ${n*n} (n * n)`),"E_BAD_OPTION",{option:"dist",length:e.dist.length,expected:n*n});if(n>=2&&!e.dist.some((o,s)=>Number.isFinite(o)&&s%(n+1)!==0))throw H(new RangeError("kamadaKawai: dist has no finite distance between two different nodes; was it computed from a graph without edges?"),"E_BAD_OPTION",{option:"dist"});r=e.dist}else{const o=Wl(t,e.weight??!0);let s;try{s=new Float64Array(n*n).fill(Number.POSITIVE_INFINITY)}catch(l){throw l instanceof RangeError?Xo(n,null):l}const{rowPtr:i,colIdx:a}=t;for(let l=0;l<n;l++){s[l*n+l]=0;for(let c=i[l];c<i[l+1];c++){const u=o===null?1:o[c];if(!(u>=0))throw H(new RangeError(`kamadaKawai: weights are distances and must be >= 0, got ${u}`),"E_BAD_WEIGHT",{weight:u});const d=l*n+a[c];u<s[d]&&(s[d]=u)}}for(let l=0;l<n;l++)for(let c=0;c<n;c++){const u=s[c*n+l];if(u!==Number.POSITIVE_INFINITY)for(let d=0;d<n;d++){const f=u+s[l*n+d];f<s[c*n+d]&&(s[c*n+d]=f)}}r=s}return Array.from({length:n},(o,s)=>Array.from({length:n},(i,a)=>{const l=r[s*n+a];return s===a?0:Number.isFinite(l)?l:Bl}))}function jl(t,e,n){const r=t.nodeCount,{pos:o}=e;if(o!=null){if(o.length!==n*r)throw H(new RangeError(`kamadaKawai: pos has ${o.length} values, expected ${n*r} (dim * nodeCount)`),"E_BAD_OPTION",{option:"pos",length:o.length,expected:n*r});return Array.from({length:r},(i,a)=>Array.from(o.subarray(n*a,n*a+n),l=>Number.isNaN(l)?0:l))}if(n===2){const i=Zt.linspace(0,2*Math.PI,r+1);return Array.from({length:r},(a,l)=>[yt(i[l]),wt(i[l])])}const s=new Float64Array(3*r).fill(Number.NaN);return me(t,s,e.seed===void 0?ql:e.seed,3,1,null,"fr"),Array.from({length:r},(i,a)=>Array.from(s.subarray(3*a,3*a+3)))}function Gl(t,e={}){const n=Lt(t),r=Vo(e.dim),o=n.nodeCount;if(e.maxNodes!==void 0&&(o>e.maxNodes||Number.isNaN(e.maxNodes)))throw Xo(o,e.maxNodes);if(o<=1){const a=new Float32Array(r*o);return o===1&&a.set(Array.from({length:r},(l,c)=>e.center?.[c]??0)),{positions:a,dim:r,n:o}}const s=Dl(Ll(n,e),jl(n,e,r),r),i=Float64Array.from(s.flat());return Mt(i,r,e.scale??1,e.center),{positions:Float32Array.from(i),dim:r,n:o}}class Hl{constructor(e){this.n=e,this.cw=Array.from({length:e},()=>new Map),this.ccw=Array.from({length:e},()=>new Map),this.first=new Int32Array(e).fill(-1)}has(e,n){return this.cw[e].has(n)}addCw(e,n,r){if(r<0){this.cw[e].set(n,n),this.ccw[e].set(n,n),this.first[e]=n;return}const o=this.cw[e].get(r);if(o===void 0)throw new Error("Cannot add edge. Reference neighbor does not exist");this.cw[e].set(r,n),this.cw[e].set(n,o),this.ccw[e].set(o,n),this.ccw[e].set(n,r)}addCcw(e,n,r){if(r<0){this.addCw(e,n,-1);return}this.addCw(e,n,this.ccw[e].get(r)??-1),r===this.first[e]&&(this.first[e]=n)}addFirst(e,n){this.addCcw(e,n,this.first[e])}nextFace(e,n){return[n,this.ccw[n].get(e)??-1]}neighboursCw(e){const n=this.first[e];if(n<0)return[];const r=[n];for(let o=this.cw[e].get(n)??n;o!==n;o=this.cw[e].get(o)??n)r.push(o);return r}}const oe=(t=-1,e=-1)=>({low:t,high:e}),Et=t=>t.low<0&&t.high<0;class Kl{constructor(e){this.adj=e,this.lowpt=new Map,this.lowpt2=new Map,this.nesting=new Map,this.oriented=new Set,this.ordered=[],this.ref=new Map,this.side=new Map,this.stack=[],this.stackBottom=new Map,this.lowptEdge=new Map,this.leftRef=new Map,this.rightRef=new Map,this.roots=[],this.n=e.length,this.height=new Int32Array(this.n).fill(-1),this.parentEdge=new Float64Array(this.n).fill(-1),this.out=Array.from({length:this.n},()=>[])}key(e,n){return e*this.n+n}target(e){return e%this.n}low(e){return this.lowpt.get(e)??0}run(){const{n:e}=this,n=this.adj.reduce((s,i)=>s+i.length,0)/2;if(e>2&&n>3*e-6)return null;for(let s=0;s<e;s++)this.height[s]<0&&(this.height[s]=0,this.roots.push(s),this.orient(s));const r=s=>[...this.out[s]].sort((i,a)=>(this.nesting.get(this.key(s,i))??0)-(this.nesting.get(this.key(s,a))??0));this.ordered=Array.from({length:e},(s,i)=>r(i));for(const s of this.roots)if(!this.test(s))return null;for(const s of this.oriented)this.nesting.set(s,this.sign(s)*(this.nesting.get(s)??0));const o=new Hl(e);for(let s=0;s<e;s++){this.ordered[s]=r(s);let i=-1;for(const a of this.ordered[s])o.addCw(s,a,i),i=a}for(const s of this.roots)this.embed(s,o);return o}orient(e){const n=[e],r=new Int32Array(this.n),o=new Set;for(;n.length>0;){const s=n.pop()??0,i=this.parentEdge[s];for(;r[s]<this.adj[s].length;){const a=this.adj[s][r[s]],l=this.key(s,a),c=this.key(a,s);if(!o.has(l)){if(this.oriented.has(l)||this.oriented.has(c)){r[s]++;continue}if(this.oriented.add(l),this.out[s].push(a),this.lowpt.set(l,this.height[s]),this.lowpt2.set(l,this.height[s]),this.height[a]<0){this.parentEdge[a]=l,this.height[a]=this.height[s]+1,n.push(s,a),o.add(l);break}this.lowpt.set(l,this.height[a])}if(this.nesting.set(l,2*this.low(l)+((this.lowpt2.get(l)??0)<this.height[s]?1:0)),i>=0){const u=this.low(i),d=this.lowpt2.get(i)??0,f=this.low(l),h=this.lowpt2.get(l)??0;f<u?(this.lowpt2.set(i,Math.min(u,h)),this.lowpt.set(i,f)):f>u?this.lowpt2.set(i,Math.min(d,f)):this.lowpt2.set(i,Math.min(d,h))}r[s]++}}}conflicting(e,n){return!Et(e)&&this.low(e.high)>this.low(n)}lowest(e){return Et(e.left)?this.low(e.right.low):Et(e.right)?this.low(e.left.low):Math.min(this.low(e.left.low),this.low(e.right.low))}top(){return this.stack.length===0?null:this.stack[this.stack.length-1]}test(e){const n=[e],r=new Int32Array(this.n),o=new Set;for(;n.length>0;){const s=n.pop()??0,i=this.parentEdge[s];let a=!1;const l=this.ordered[s];for(;r[s]<l.length;){const c=l[r[s]],u=this.key(s,c);if(!o.has(u)){if(this.stackBottom.set(u,this.top()),u===this.parentEdge[c]){n.push(s,c),o.add(u),a=!0;break}this.lowptEdge.set(u,u),this.stack.push({left:oe(),right:oe(u,u)})}if(this.low(u)<this.height[s]){if(c===l[0])this.lowptEdge.set(i,this.lowptEdge.get(u)??-1);else if(!this.addConstraints(u,i))return!1}r[s]++}!a&&i>=0&&this.removeBackEdges(i)}return!0}addConstraints(e,n){const r={left:oe(),right:oe()};for(;;){const o=this.stack.pop();if(Et(o.left)||([o.left,o.right]=[o.right,o.left]),!Et(o.left))return!1;if(this.low(o.right.low)>this.low(n)?(Et(r.right)?r.right={...o.right}:this.ref.set(r.right.low,o.right.high),r.right.low=o.right.low):this.ref.set(o.right.low,this.lowptEdge.get(n)??-1),this.top()===(this.stackBottom.get(e)??null))break}for(let o=this.top();o!==null&&(this.conflicting(o.left,e)||this.conflicting(o.right,e));o=this.top()){const s=this.stack.pop();if(this.conflicting(s.right,e)&&([s.left,s.right]=[s.right,s.left]),this.conflicting(s.right,e))return!1;this.ref.set(r.right.low,s.right.high),s.right.low>=0&&(r.right.low=s.right.low),Et(r.left)?r.left={...s.left}:this.ref.set(r.left.low,s.left.high),r.left.low=s.left.low}return Et(r.left)&&Et(r.right)||this.stack.push(r),!0}removeBackEdges(e){const n=Math.floor(e/this.n),r=o=>this.ref.get(o)??-1;for(;this.stack.length>0&&this.lowest(this.top())===this.height[n];){const o=this.stack.pop();o.left.low>=0&&this.side.set(o.left.low,-1)}if(this.stack.length>0){const o=this.stack.pop();for(;o.left.high>=0&&this.target(o.left.high)===n;)o.left.high=r(o.left.high);for(o.left.high<0&&o.left.low>=0&&(this.ref.set(o.left.low,o.right.low),this.side.set(o.left.low,-1),o.left.low=-1);o.right.high>=0&&this.target(o.right.high)===n;)o.right.high=r(o.right.high);o.right.high<0&&o.right.low>=0&&(this.ref.set(o.right.low,o.left.low),this.side.set(o.right.low,-1),o.right.low=-1),this.stack.push(o)}if(this.low(e)<this.height[n]){const o=this.top(),s=o.left.high,i=o.right.high;this.ref.set(e,s>=0&&(i<0||this.low(s)>this.low(i))?s:i)}}sign(e){const n=[e],r=new Map;for(;n.length>0;){const o=n.pop()??0,s=this.ref.get(o)??-1;if(s>=0)n.push(o,s),r.set(o,s),this.ref.set(o,-1);else{const i=r.get(o)??-1;this.side.set(o,(this.side.get(o)??1)*(i>=0?this.side.get(i)??1:1))}}return this.side.get(e)??1}embed(e,n){const r=[e],o=new Int32Array(this.n);for(;r.length>0;){const s=r.pop()??0,i=this.ordered[s];for(;o[s]<i.length;){const a=i[o[s]++],l=this.key(s,a);if(l===this.parentEdge[a]){n.addFirst(a,s),this.leftRef.set(s,a),this.rightRef.set(s,a),r.push(s,a);break}(this.side.get(l)??1)===1?n.addCw(a,s,this.rightRef.get(a)??-1):(n.addCcw(a,s,this.leftRef.get(a)??-1),this.leftRef.set(a,s))}}}}function Yl(t,e,n,r){const{n:o}=t;if(r.has(e*o+n))return[];r.add(e*o+n);let s=e,i=n;const a=[e],l=new Set(a);let[,c]=t.nextFace(s,i);for(;i!==e||c!==n;)l.has(i)?(t.addCw(s,c,i),t.addCcw(c,s,i),r.add(i*o+c),r.add(c*o+s),i=s):(l.add(i),a.push(i)),s=i,[i,c]=t.nextFace(i,c),r.add(s*o+i);return a}function Xl(t,e,n){let[,r]=t.nextFace(e,n),[,o]=t.nextFace(n,r);if(!(e===n||e===r))for(;e!==o;)t.has(e,r)?[e,n,r]=[n,r,o]:(t.addCw(e,r,n),t.addCcw(r,e,n),[n,r]=[r,o]),[,o]=t.nextFace(n,r)}function Zl(t){const{n:e}=t,n=new Uint8Array(e),r=[];for(let a=0;a<e;a++)if(n[a]!==1){r.push(a),n[a]=1;for(const l=[a];l.length>0;)for(const c of t.neighboursCw(l.pop()??0))n[c]===0&&(n[c]=1,l.push(c))}for(let a=0;a+1<r.length;a++)t.addFirst(r[a],r[a+1]),t.addFirst(r[a+1],r[a]);let o=[];const s=[],i=new Set;for(let a=0;a<e;a++){const l=t.first[a];if(l<0)continue;let c=l;do{const u=Yl(t,a,c,i);u.length>0&&(s.push(u),u.length>o.length&&(o=u)),c=t.cw[a].get(c)??l}while(c!==l)}for(const a of s)a!==o&&Xl(t,a[0],a[1]);return o}class Jl{constructor(e,n){this.embedding=e,this.ccwNbr=new Map,this.cwNbr=new Map;const{n:r}=e;this.v1=n[0],this.v2=n[1],this.chords=new Int32Array(r),this.marked=new Uint8Array(r),this.ready=new Set(n);let o=this.v2;for(let s=2;s<n.length;s++)this.ccwNbr.set(o,n[s]),o=n[s];this.ccwNbr.set(o,this.v1),o=this.v1;for(let s=n.length-1;s>0;s--)this.cwNbr.set(o,n[s]),o=n[s];for(const s of n)this.countChords(s,null)}run(){const{n:e}=this.embedding,n=new Array(e);n[0]=[this.v1,[]],n[1]=[this.v2,[]],this.ready.delete(this.v1),this.ready.delete(this.v2);for(let r=e-1;r>1;r--){const o=this.ready.values().next().value;this.ready.delete(o),this.marked[o]=1;const[s,i]=this.contourEnds(o),a=this.replaceContour(o,s,i);a.length===2?(this.releaseChord(s),this.releaseChord(i)):this.addInner(new Set(a.slice(1,-1))),n[r]=[o,a]}return n}isOuterNbr(e,n){return this.ccwNbr.get(e)===n||this.cwNbr.get(e)===n}isOnOuter(e){return this.marked[e]===0&&(this.ccwNbr.has(e)||e===this.v1)}countChords(e,n){for(const r of this.embedding.neighboursCw(e))this.isOnOuter(r)&&!this.isOuterNbr(e,r)&&(this.chords[e]++,this.ready.delete(e),n!==null&&!n.has(r)&&(this.chords[r]++,this.ready.delete(r)))}contourEnds(e){let n=-1,r=-1;for(const o of this.embedding.neighboursCw(e))if(this.isOnOuter(o)&&(o===this.v1?n=this.v1:o===this.v2?r=this.v2:this.cwNbr.get(o)===e?n=o:r=o),n>=0&&r>=0)break;return[n,r]}replaceContour(e,n,r){const o=[n];for(let s=n;s!==r;){const i=this.embedding.ccw[e].get(s)??r;o.push(i),this.cwNbr.set(s,i),this.ccwNbr.set(i,s),s=i}return o}releaseChord(e){--this.chords[e]===0&&this.ready.add(e)}addInner(e){for(const n of e)this.ready.add(n),this.countChords(n,e)}}function Ql(t,e){return new Jl(t,e).run()}function tc(t){const{n:e}=t,n=new Float64Array(2*e);if(e<4){const d=[0,0,2,0,1,1];return n.set(d.slice(0,2*e)),n}const r=Ql(t,Zl(t)),o=new Int32Array(e).fill(-1),s=new Int32Array(e).fill(-1),i=new Float64Array(e),a=new Float64Array(e),[l,c,u]=[r[0][0],r[1][0],r[2][0]];s[l]=u,i[c]=1,i[u]=1,a[u]=1,s[u]=c;for(let d=3;d<e;d++){const[f,h]=r[d],g=h[0],w=h[1],m=h[h.length-1],A=h[h.length-2],p=h.length>2;i[w]+=1,i[m]+=1;let y=0;for(let v=1;v<h.length;v++)y+=i[h[v]];i[f]=Math.floor((-a[g]+y+a[m])/2),a[f]=Math.floor((a[g]+y+a[m])/2),i[m]=y-i[f],p&&(i[w]-=i[f]),s[g]=f,s[f]=m,p?(o[f]=w,s[A]=-1):o[f]=-1}n[2*l+1]=a[l];for(const d=[l];d.length>0;){const f=d.pop()??0;for(const h of[o[f],s[f]])h>=0&&(n[2*h]=n[2*f]+i[h],n[2*h+1]=a[h],d.push(h))}return n}function ec(t){const e=[];for(let n=0;n<t.nodeCount;n++){const r=[];for(let o=t.rowPtr[n];o<t.rowPtr[n+1];o++){const s=t.colIdx[o];s!==n&&s!==r[r.length-1]&&r.push(s)}e.push(r)}return e}function nc(t,e,n){const r=new Kl(ec(t)).run();if(r===null)throw Object.assign(new Error("G is not planar."),{code:"E_NOT_PLANAR",params:{nodeCount:t.nodeCount,edgeCount:t.edgeCount}});return Mt(tc(r),2,e,n)}function rc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(It(nc(Lt(t),o,s),r,s),r,n)}function Zo(t,e,n,r,o=0){const s=new Float64Array(2*t).fill(Number.NaN);if(e.length===0)return s;const i=o==="networkx"?Math.PI/e.length:o;if(!Number.isFinite(i))throw H(new TypeError(`rotate must be a finite number of radians or "networkx", not ${String(o)}`),"E_BAD_OPTION",{option:"rotate",value:o});const a=n/e.length;let l=a;return e[0].length===1&&(s[2*e[0][0]]=r[0],s[2*e[0][0]+1]=r[1]),e.forEach((c,u)=>{if(c.length===0||u===0&&c.length===1)return;const d=Zt.linspace(0,2*Math.PI,c.length+1),f=(u+1)*i;for(let h=0;h<c.length;h++)s[2*c[h]]=yt(d[h]+f)*l+r[0],s[2*c[h]+1]=wt(d[h]+f)*l+r[1];l+=a}),s}function oc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),{nlist:i}=e,a=typeof i=="string"?Lo(t,i,"nlist"):i?.map(l=>fe(t,l))??[Array.from({length:n},(l,c)=>c)];for(const l of a)for(let c=0;c<l.length;c++)if(!Number.isInteger(l[c])||l[c]<0||l[c]>=n)throw H(new Error(`shell node index ${String(l[c])} is not a node of the graph`),"E_BAD_NODE",{option:"nlist",index:l[c]});return ht(It(Zo(n,a,o,s,e.rotate),r,s),r,n)}function sc(t,e){const n=new Map;for(let r=t.rowPtr[e];r<t.rowPtr[e+1];r++)n.has(t.colIdx[r])||n.set(t.colIdx[r],t.arcToEdge[r]);return[...n.keys()].sort((r,o)=>(n.get(r)??0)-(n.get(o)??0))}function ic(t,e){const n=t.nodeCount,r=new Int32Array(n).fill(-1);if(n===0)return{rings:[],parent:r};if(e===null){let a=0,l=-1;for(let c=0;c<n;c++){let u=0;for(let d=t.rowPtr[c];d<t.rowPtr[c+1];d++)(d===t.rowPtr[c]||t.colIdx[d]!==t.colIdx[d-1])&&u++;u>l&&(a=c,l=u)}e=a}else if(!Number.isInteger(e)||e<0||e>=n)throw H(new Error(`root node ${String(e)} is not in the graph`),"E_BAD_NODE",{option:"root",index:e});const o=new Uint8Array(n);o[e]=1;const s=[[e]];for(let a=s[0];a.length>0;){const l=[];for(const c of a)for(const u of sc(t,c))o[u]===0&&(o[u]=1,r[u]=c,l.push(u));l.length>0&&s.push(l),a=l}const i=[];for(let a=0;a<n;a++)o[a]===0&&i.push(a);return i.length>0&&s.push(i),{rings:s,parent:r}}function ac(t,e,n,r){const o=new Float64Array(t);for(let s=r-1;s>=0;s--)for(const i of e[s])o[i]||=1,s>0&&(o[n[i]]+=o[i]);return o}function lc(t,e,n,r,o){const s=new Float64Array(2*t);if(t===0)return s;const i=e.length>1?r/(e.length-1):0,a=e.at(-1)??[],l=e.length>1&&n[a[0]]===-1?e.length-1:e.length,c=ac(t,e,n,l),u=new Float64Array(t),d=new Float64Array(t),f=new Float64Array(t);d[e[0][0]]=2*Math.PI,l>1&&e[1].length===1&&(u[e[0][0]]=Math.PI/4-Math.PI);const h=(g,w,m)=>{s[2*g]=yt(m)*w+o[0],s[2*g+1]=wt(m)*w+o[1]};h(e[0][0],0,0);for(let g=1;g<l;g++){const w=2*gr(g/(g+1));for(const m of e[g]){const A=n[m],p=d[A]*c[m]/c[A],y=u[A]+f[A]+p/2;f[A]+=p,h(m,g*i,y),d[m]=Math.min(p,w),u[m]=y-d[m]/2}}return l<e.length&&a.forEach((g,w)=>{h(g,r,2*Math.PI*w/a.length)}),s}function cc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),i=e.root??null,{arrangement:a="rings",rotate:l}=e;if(a!=="rings"&&a!=="sectors")throw H(new Error(`arrangement must be "rings" or "sectors", not ${String(a)}`),"E_BAD_OPTION",{option:"arrangement",value:a});if(a==="sectors"&&l!==void 0&&l!==0)throw H(new Error('rotate turns each ring separately, which only arrangement: "rings" allows'),"E_BAD_OPTION",{option:"rotate",value:l});const{rings:c,parent:u}=ic(Lt(t),i===null?null:V(t,i));if(a==="sectors")return ht(It(lc(n,c,u,o,s),r,s),r,n);const d=c.length>1?c.length/(c.length-1):1;return ht(It(Zo(n,c,o*d,s,l),r,s),r,n)}class Jo{constructor(e){this.seed=e??Math.floor(Math.random()*1e6),this.m=2**35-31,this.a=185852,this.c=1,this._state=this.seed%this.m}_next(){return this._state=(this.a*this._state+this.c)%this.m,this._state/this.m}rand(e=null){if(e===null)return this._next();if(typeof e=="number"){const r=[];for(let o=0;o<e;o++)r.push(this._next());return r}if(e.length===1){const r=[];for(let o=0;o<e[0];o++)r.push(this._next());return r}const n=[];for(let r=0;r<e[0];r++)n.push(this.rand(e.slice(1)));return n}}function uc(t,e,n,r,o){const s=new Jo(o??void 0),i=new Float64Array(t*e);for(let a=0;a<t*e;a++)i[a]=s.rand()*n+r[a%e];return i}function dc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(uc(n,r,o,s,e.seed??null),r,n)}const fc=1e-9,hc=5e3;function gc(t,e,n,r,o){const s=t.nodeCount,i=new Float64Array(s*e);if(s<=2){for(let w=0;w<s;w++){const m=w===0?-n:n;for(let A=0;A<e;A++)i[e*w+A]=s===1?r[A]:r[A]+m}return i}const{rowPtr:a,colIdx:l}=t,c=(w,m)=>l[m]!==w&&(m===a[w]||l[m]!==l[m-1]),u=new Float64Array(s);for(let w=0;w<s;w++)for(let m=a[w];m<a[w+1];m++)u[w]+=c(w,m)?1:0;const d=w=>{const m=new Float64Array(s);for(let A=0;A<s;A++){let p=u[A]*w[A];for(let y=a[A];y<a[A+1];y++)c(A,y)&&(p-=w[l[y]]);m[A]=p}return m},f=fc*2*Math.max(1,...u),h=new Jo(o??void 0),g=[new Float64Array(s).fill(1/Math.sqrt(s))];for(let w=0;w<e;w++){const m=Float64Array.from({length:s},()=>h.rand()-.5),A=Qo(m,g)?wc(d,m,g,f):m.fill(0);g.push(A);for(let p=0;p<s;p++)i[e*p+w]=A[p]}Mt(i,e,n);for(let w=0;w<s*e;w++)i[w]+=r[w%e];return i}function wc(t,e,n,r){let o=t(e),s=null;for(let i=0;i<hc;i++){const a=Ye(e,o),l=o.map((m,A)=>m-a*e[A]);if(Xe(l)<r)break;const c=[e];for(const m of s===null?[l]:[l,s])Qo(m,[...n,...c])&&c.push(m);const u=[o,...c.slice(1).map(t)],d=c.map(m=>u.map(A=>Ye(m,A))),f=pc(d),h=new Float64Array(e.length),g=new Float64Array(e.length),w=new Float64Array(e.length);c.forEach((m,A)=>{for(let p=0;p<e.length;p++)h[p]+=f[A]*m[p],g[p]+=f[A]*u[A][p],A>0&&(w[p]+=f[A]*m[p])}),e.set(h),o=g,s=w}return e}function pc(t){const e=t.length,n=t.map((o,s)=>t.map((i,a)=>s===a?1:0));for(let o=0;o<50;o++)for(let s=0;s<e;s++)for(let i=s+1;i<e;i++)mc(t,n,s,i);let r=0;for(let o=1;o<e;o++)t[o][o]<t[r][r]&&(r=o);return n.map(o=>o[r])}function mc(t,e,n,r){if(Math.abs(t[n][r])<1e-300)return;const o=(t[r][r]-t[n][n])/(2*t[n][r]),s=Math.sign(o||1)/(Math.abs(o)+Math.sqrt(o*o+1)),i=1/Math.sqrt(s*s+1),a=s*i;for(const l of[t,e])for(const c of l)[c[n],c[r]]=[i*c[n]-a*c[r],a*c[n]+i*c[r]];[t[n],t[r]]=[t[n].map((l,c)=>i*l-a*t[r][c]),t[n].map((l,c)=>a*l+i*t[r][c])]}function Qo(t,e){const n=Xe(t);for(let o=0;o<2;o++)for(const s of e){const i=Ye(t,s);for(let a=0;a<t.length;a++)t[a]-=i*s[a]}const r=Xe(t);if(r<=1e-8*n)return!1;for(let o=0;o<t.length;o++)t[o]/=r;return!0}function Ye(t,e){let n=0;for(let r=0;r<t.length;r++)n+=t[r]*e[r];return n}function Xe(t){let e=0;for(const n of t)e+=n*n;return Math.sqrt(e)}function yc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e);return ht(gc(Lt(t),r,o,s,e.seed??null),r,n)}function Ac(t,e,n,r,o){const s=new Float64Array(2*t);if(o){let l=r;l+=1/(.5*l);for(let c=0;c<t;c++){const u=.5*l;l+=1/u,s[2*c]=yt(l)*u,s[2*c+1]=wt(l)*u}}else for(let i=0;i<t;i++)s[2*i]=yt(r*i)*i,s[2*i+1]=wt(r*i)*i;return Mt(s,2,e,n)}function vc(t,e={}){const{n,dim:r,scale:o,center:s}=pt(t,e),i=Ac(n,o,s,e.resolution??.35,e.equidistant??!1);return ht(It(i,r,s),r,n)}function Ec(t,e,n){switch(t){case"forceatlas2":return n?.forceAtlas2!==void 0?n.forceAtlas2(e):new Nl(e);case"fruchtermanReingold":case"spring":return n?.fruchtermanReingold!==void 0?n.fruchtermanReingold(e):new Ml(e);case"spring-electrical":if(n?.springElectrical===void 0)throw new Error('createSimulation: "spring-electrical" has no CPU simulation; inject an accelerator that implements springElectrical');return n.springElectrical(e);default:{const r=t;throw new Error(`createSimulation: unknown simulation type ${String(r)}`)}}}const bc={animate:!1,animationDuration:500,fit:!0,padding:30,randomize:!0,refresh:1};function Cc(t,e){const n=new Map;return t.nodes.forEach((o,s)=>{const i=o.data(e);if(typeof i=="number"||typeof i=="string"){const a=n.get(i);a===void 0?n.set(i,[s]):a.push(s)}}),[...n.keys()].sort((o,s)=>typeof o=="number"&&typeof s=="number"?o-s:String(o).localeCompare(String(s))).map(o=>n.get(o)??[])}const ts={random:(t,e)=>dc(t,e),circular:(t,e)=>ml(t,e),spiral:(t,e)=>vc(t,e),grid:(t,e)=>Ol(t,e),spectral:(t,e)=>yc(t,e),planar:(t,e)=>rc(t,e),arf:(t,e)=>ul(t,e),"kamada-kawai":(t,e)=>Gl(t,e),shell:(t,e,n)=>{const r=e.nlist;return oc(t,{...e,nlist:r?.map(o=>ft(n,o))})},multipartite:(t,e,n)=>{const r=e.subsets??"subset",o=typeof r=="string"?Cc(n,r):r.map(s=>ft(n,s));return dl(t,{...e,subsets:o})},bipartite:(t,e,n)=>{if(e.top===void 0)return or(t,e);const r=ct(t.nodeCount);for(const o of ft(n,e.top))bt(r,o,!0);return or(t,{...e,top:r})},bfs:(t,{root:e,...n},r)=>hl(t,{...n,start:Rt(r,e,"graphty layout: root")}),radial:(t,e,n)=>cc(t,{...e,root:Rt(n,e.root,"graphty layout: root")})},Ic={forceatlas2:{maxIter:100},fruchtermanReingold:{iterations:50}},es={forceatlas2:"forceatlas2","fruchterman-reingold":"fruchtermanReingold","spring-electrical":"spring-electrical"},$c=[...Object.keys(ts),...Object.keys(es)];function ns(t,e){if(t===void 0){const n=r=>Number.isFinite(r)?r:1;return{x1:0,y1:0,w:n(e.width()),h:n(e.height())}}return"w"in t?t:{x1:t.x1,y1:t.y1,w:t.x2-t.x1,h:t.y2-t.y1}}function mn(t){return{radius:Math.max(Math.min(t.w,t.h)/2,1),center:[t.x1+t.w/2,t.y1+t.h/2,0]}}function rs(t,e,n,r){const o=new Float64Array(2*n);for(let a=0;a<n;a++)o[2*a]=t[e*a],o[2*a+1]=t[e*a+1];const{radius:s,center:i}=mn(r);return Mt(o,2,s,i)}function xc(t,e,n,r){let o=0,s=0,i=0;for(let f=0;f<n;f++)L(e,f)&&(o+=t[3*f],s+=t[3*f+1],i++);o/=i,s/=i;const a=o>=r.x1&&o<=r.x1+r.w&&s>=r.y1&&s<=r.y1+r.h,l=(f,h,g,w)=>h===0?1/0:((h>0?w:g)-f)/h;let c=1/0,u=0;for(let f=0;f<n;f++)if(!L(e,f)){const h=t[3*f]-o,g=t[3*f+1]-s;c=Math.min(c,l(o,h,r.x1,r.x1+r.w),l(s,g,r.y1,r.y1+r.h)),u=Math.max(u,Math.hypot(h,g))}a?Number.isFinite(c)||(c=1):c=u>0?mn(r).radius/u:1;const d=new Float64Array(2*n);for(let f=0;f<n;f++){const h=L(e,f)?1:c;d[2*f]=o+h*(t[3*f]-o),d[2*f+1]=s+h*(t[3*f+1]-s)}return d}function Pc(t,e){const n={};for(const r of["nodeMass","nodeSize"]){const o=e[r];if(Array.isArray(o)||ArrayBuffer.isView(o)&&!(o instanceof Float32Array))n[r]=Float32Array.from(o);else if(typeof o=="string"){const s={};t.nodes.forEach(i=>{const a=i.data(o);typeof a=="number"&&Number.isFinite(a)&&(s[i.id()]=a)}),n[r]=s}}return n}function os(t){return(e,n)=>Number.isNaN(t[2*n])?e.position():{x:t[2*n],y:t[2*n+1]}}function ss(t,e,n){const r=os(n);e.layoutPositions(t,t.options,r)}function is(t){return{...t,dim:t.dim??2,scale:1,center:void 0,seed:t.seed??null,weight:t.weight!==void 0}}function as(t){return fn(t.eles.not(":parent"),{weight:t.weight})}function ls(t,e){if((e?t.animate!==!0&&!!t.animate:!!t.animate)&&!t.cy.styleEnabled())throw new Error(`${t.name}: animate needs a core that renders; create a headless core with styleEnabled: true, or pass animate: false`)}function Nc(t,e){const n=t.options;ls(n,!1);const r=as(n),o=e(r.snapshot,is(n),r);ss(t,r.nodes,rs(o.positions,o.dim,o.n,ns(n.boundingBox,n.cy)))}function cs(t,e){for(;!t.settled&&!e.stopped&&!e.options.cy.destroyed();){const n=t.step(50);if(n instanceof Promise)return n.then(()=>cs(t,e))}}function Fc(t,e){const n=t.options;if(ls(n,!0),hn(n.name,n.gpu),t.stopped=!1,n.accelerator!==void 0){t.backend=n.accelerator===null?{ran:"cpu",reason:"accelerator: null was passed",device:null}:{ran:"gpu",reason:null,device:n.accelerator.kind},Te(t,e,n.accelerator);return}const r=n.gpu==="require"?null:Do(n.gpu);if(r!==null){t.backend=Ge(r,!1,""),je(r,n.eles.nodes().length),Te(t,e,null);return}t.looping=!0,$o(n.cy,n.gpu).then(o=>{if(t.looping=!1,t.backend=Ge(o,o.gpu!==null,""),je(o,n.eles.nodes().length),t.stopped||n.cy.destroyed()){t.emit({type:"layoutstop",layout:t});return}Te(t,e,o.gpu?.accelerator??null)}).catch(o=>{t.looping=!1,n.cy.destroyed()||t.emit("layouterror",[o]),t.emit({type:"layoutstop",layout:t})})}const Oe=new WeakMap;function lr(t,e){let n=0,r=0;for(let o=0;o<e;o++)n+=t[3*o],r+=t[3*o+1];return e===0?[0,0,0]:[n/e,r/e,0]}function Te(t,e,n){const r=t.options;if(n===null&&e==="spring-electrical"){const P=t.backend?.reason??"none was available";t.backend=void 0;const I=new Error(`graphty-spring-electrical has no CPU simulation and runs only on the GPU; no GPU ran because ${P}`);us(t,I);return}const o=as(r),s=o.snapshot,i=s.nodeCount,a=ns(r.boundingBox,r.cy),{radius:l,center:c}=mn(a),u=r.dim??2,d=new Float32Array(3*i),f=ct(i),h=new qo(r.seed??null);let g=!1;o.nodes.forEach((P,I)=>{const F=P.locked();g||=F,bt(f,I,F);const U=P.position(),R=F||r.randomize===!1;d[3*I]=R?U.x:a.x1+h.next()*a.w,d[3*I+1]=R?U.y:a.y1+h.next()*a.h,d[3*I+2]=R||u===2?0:(h.next()*2-1)*l});const w=r.randomize===!1&&!g?Oe.get(r.cy)?.get(e):void 0,m=w??l,A=Ec(e,{...Ic[e],...is(r),...Pc(o,r),scale:m,center:w===void 0?c:lr(d,i),iterationsPerStep:1},n);A.load(s,d),g&&A.setFixed(f);const p=_o(s),y=()=>{if(A.dispose(),p(),!g){const[P,I]=lr(d,i);let F=0;for(let U=0;U<i;U++)F=Math.max(F,Math.hypot(d[3*U]-P,d[3*U+1]-I));if(F>0){const U=Oe.get(r.cy)??new Map;U.set(e,m*l/F),Oe.set(r.cy,U)}}},v=()=>g?xc(d,f,i,a):rs(d,3,i,a),E=P=>{t.looping=!1,y(),r.cy.destroyed()||t.emit("layouterror",[P]),t.emit({type:"layoutstop",layout:t})};if(r.animate!==!0){const P=()=>{t.looping=!1,y(),r.cy.destroyed()?t.emit({type:"layoutstop",layout:t}):ss(t,o.nodes,v())},I=cs(A,t);I===void 0?P():(t.looping=!0,I.then(P,E));return}t.looping=!0,t.emit({type:"layoutstart",layout:t});let b=!0;const C=typeof requestAnimationFrame=="function"?requestAnimationFrame:P=>setTimeout(P,16),x=()=>{t.looping=!1,y(),r.stop!==void 0&&t.one("layoutstop",r.stop),t.emit({type:"layoutstop",layout:t})},_=()=>{if(r.cy.destroyed()){x();return}o.nodes.positions(os(v())),r.fit!==!1&&r.cy.fit(r.eles,r.padding),b&&(b=!1,r.ready!==void 0&&t.one("layoutready",r.ready),t.emit({type:"layoutready",layout:t})),A.settled||t.stopped?x():C(S)},S=()=>{if(t.stopped||r.cy.destroyed()){_();return}let P;try{P=A.step(r.refresh??1)}catch(I){E(I);return}P instanceof Promise?P.then(_,E):_()};S()}function us(t,e){t.looping=!0,Promise.resolve().then(()=>{t.looping=!1,t.emit("layouterror",[e]),t.emit({type:"layoutstop",layout:t})})}const ds=new WeakMap;function kc(){return this.options.cy.destroyed()?us(this,Kt()):ds.get(this)?.(this),this}function Sc(){return this.looping?this.stopped=!0:this.emit({type:"layoutstop",layout:this}),this}function cr(t,e){function n(o){this.options={...bc,...o},this.stopped=!1,this.looping=!1,ds.set(this,t)}const r={run:kc};return e&&(r.stop=Sc),Object.assign(n.prototype,r),n}function _c(t){for(const[e,n]of Object.entries(ts))t("layout",`graphty-${e}`,cr(r=>{Nc(r,n)},!1));for(const[e,n]of Object.entries(es))t("layout",`graphty-${e}`,cr(r=>{Fc(r,n)},!0))}const ur=Symbol.for("@graphty/cytoscape-extensions");function zc(t){const e=t;e[ur]!==!0&&(e[ur]=!0,_c(t),Za(t),el(t))}export{Rc as A,Oc as G,Ja as I,$c as L,Ta as a,Dc as b,Da as c,Tc as d,zc as g,fn as t,Qt as w};