@dagrejs/graphlib
Version:
A directed and undirected multi-graph library
3 lines (2 loc) • 12.6 kB
JavaScript
var V=Object.defineProperty;var F=(s,e)=>{for(var t in e)V(s,t,{get:e[t],enumerable:!0})};var p=class{constructor(e){this._isDirected=!0;this._isMultigraph=!1;this._isCompound=!1;this._nodes={};this._in={};this._preds={};this._out={};this._sucs={};this._edgeObjs={};this._edgeLabels={};this._nodeCount=0;this._edgeCount=0;this._defaultNodeLabelFn=()=>{};this._defaultEdgeLabelFn=()=>{};e&&(this._isDirected="directed"in e?e.directed:!0,this._isMultigraph="multigraph"in e?e.multigraph:!1,this._isCompound="compound"in e?e.compound:!1),this._isCompound&&(this._parent={},this._children={},this._children["\0"]={})}isDirected(){return this._isDirected}isMultigraph(){return this._isMultigraph}isCompound(){return this._isCompound}setGraph(e){return this._label=e,this}graph(){return this._label}setDefaultNodeLabel(e){return typeof e!="function"?this._defaultNodeLabelFn=()=>e:this._defaultNodeLabelFn=e,this}nodeCount(){return this._nodeCount}nodes(){return Object.keys(this._nodes)}sources(){return this.nodes().filter(e=>Object.keys(this._in[e]).length===0)}sinks(){return this.nodes().filter(e=>Object.keys(this._out[e]).length===0)}setNodes(e,t){return e.forEach(n=>{t!==void 0?this.setNode(n,t):this.setNode(n)}),this}setNode(e,t){return e in this._nodes?(arguments.length>1&&(this._nodes[e]=t),this):(this._nodes[e]=arguments.length>1?t:this._defaultNodeLabelFn(e),this._isCompound&&(this._parent[e]="\0",this._children[e]={},this._children["\0"][e]=!0),this._in[e]={},this._preds[e]={},this._out[e]={},this._sucs[e]={},++this._nodeCount,this)}node(e){return this._nodes[e]}hasNode(e){return e in this._nodes}removeNode(e){if(e in this._nodes){let t=n=>this.removeEdge(this._edgeObjs[n]);delete this._nodes[e],this._isCompound&&(this._removeFromParentsChildList(e),delete this._parent[e],this.children(e).forEach(n=>{this.setParent(n)}),delete this._children[e]),Object.keys(this._in[e]).forEach(t),delete this._in[e],delete this._preds[e],Object.keys(this._out[e]).forEach(t),delete this._out[e],delete this._sucs[e],--this._nodeCount}return this}setParent(e,t){if(!this._isCompound)throw new Error("Cannot set parent in a non-compound graph");if(t===void 0)t="\0";else{t+="";for(let n=t;n!==void 0;n=this.parent(n))if(n===e)throw new Error("Setting "+t+" as parent of "+e+" would create a cycle");this.setNode(t)}return this.setNode(e),this._removeFromParentsChildList(e),this._parent[e]=t,this._children[t][e]=!0,this}parent(e){if(this._isCompound){let t=this._parent[e];if(t!=="\0")return t}}children(e="\0"){if(this._isCompound){let t=this._children[e];if(t)return Object.keys(t)}else{if(e==="\0")return this.nodes();if(this.hasNode(e))return[]}return[]}predecessors(e){let t=this._preds[e];if(t)return Object.keys(t)}successors(e){let t=this._sucs[e];if(t)return Object.keys(t)}neighbors(e){let t=this.predecessors(e);if(t){let n=new Set(t);for(let i of this.successors(e))n.add(i);return Array.from(n.values())}}isLeaf(e){let t;return this.isDirected()?t=this.successors(e):t=this.neighbors(e),t.length===0}filterNodes(e){let t=new this.constructor({directed:this._isDirected,multigraph:this._isMultigraph,compound:this._isCompound});t.setGraph(this.graph()),Object.entries(this._nodes).forEach(([r,o])=>{e(r)&&t.setNode(r,o)}),Object.values(this._edgeObjs).forEach(r=>{t.hasNode(r.v)&&t.hasNode(r.w)&&t.setEdge(r,this.edge(r))});let n={},i=r=>{let o=this.parent(r);return!o||t.hasNode(o)?(n[r]=o!=null?o:void 0,o!=null?o:void 0):o in n?n[o]:i(o)};return this._isCompound&&t.nodes().forEach(r=>t.setParent(r,i(r))),t}setDefaultEdgeLabel(e){return typeof e!="function"?this._defaultEdgeLabelFn=()=>e:this._defaultEdgeLabelFn=e,this}edgeCount(){return this._edgeCount}edges(){return Object.values(this._edgeObjs)}setPath(e,t){return e.reduce((n,i)=>(t!==void 0?this.setEdge(n,i,t):this.setEdge(n,i),i)),this}setEdge(e,t,n,i){let r,o,d,a,c=!1;typeof e=="object"&&e!==null&&"v"in e?(r=e.v,o=e.w,d=e.name,arguments.length===2&&(a=t,c=!0)):(r=e,o=t,d=i,arguments.length>2&&(a=n,c=!0)),r=""+r,o=""+o,d!==void 0&&(d=""+d);let h=b(this._isDirected,r,o,d);if(h in this._edgeLabels)return c&&(this._edgeLabels[h]=a),this;if(d!==void 0&&!this._isMultigraph)throw new Error("Cannot set a named edge when isMultigraph = false");this.setNode(r),this.setNode(o),this._edgeLabels[h]=c?a:this._defaultEdgeLabelFn(r,o,d);let u=J(this._isDirected,r,o,d);return r=u.v,o=u.w,Object.freeze(u),this._edgeObjs[h]=u,k(this._preds[o],r),k(this._sucs[r],o),this._in[o][h]=u,this._out[r][h]=u,this._edgeCount++,this}edge(e,t,n){let i=arguments.length===1?N(this._isDirected,e):b(this._isDirected,e,t,n);return this._edgeLabels[i]}edgeAsObj(e,t,n){let i=arguments.length===1?this.edge(e):this.edge(e,t,n);return typeof i!="object"?{label:i}:i}hasEdge(e,t,n){return(arguments.length===1?N(this._isDirected,e):b(this._isDirected,e,t,n))in this._edgeLabels}removeEdge(e,t,n){let i=arguments.length===1?N(this._isDirected,e):b(this._isDirected,e,t,n),r=this._edgeObjs[i];if(r){let o=r.v,d=r.w;delete this._edgeLabels[i],delete this._edgeObjs[i],x(this._preds[d],o),x(this._sucs[o],d),delete this._in[d][i],delete this._out[o][i],this._edgeCount--}return this}inEdges(e,t){return this.isDirected()?this.filterEdges(this._in[e],e,t):this.nodeEdges(e,t)}outEdges(e,t){return this.isDirected()?this.filterEdges(this._out[e],e,t):this.nodeEdges(e,t)}nodeEdges(e,t){if(e in this._nodes)return this.filterEdges({...this._in[e],...this._out[e]},e,t)}_removeFromParentsChildList(e){delete this._children[this._parent[e]][e]}filterEdges(e,t,n){if(!e)return;let i=Object.values(e);return n?i.filter(r=>r.v===t&&r.w===n||r.v===n&&r.w===t):i}};function k(s,e){s[e]?s[e]++:s[e]=1}function x(s,e){s[e]!==void 0&&!--s[e]&&delete s[e]}function b(s,e,t,n){let i=""+e,r=""+t;if(!s&&i>r){let o=i;i=r,r=o}return i+""+r+""+(n===void 0?"\0":n)}function J(s,e,t,n){let i=""+e,r=""+t;if(!s&&i>r){let d=i;i=r,r=d}let o={v:i,w:r};return n&&(o.name=n),o}function N(s,e){return b(s,e.v,e.w,e.name)}var H="4.0.1";var G={};F(G,{read:()=>z,write:()=>U});function U(s){let e={options:{directed:s.isDirected(),multigraph:s.isMultigraph(),compound:s.isCompound()},nodes:Y(s),edges:K(s)},t=s.graph();return t!==void 0&&(e.value=structuredClone(t)),e}function Y(s){return s.nodes().map(e=>{let t=s.node(e),n=s.parent(e),i={v:e};return t!==void 0&&(i.value=t),n!==void 0&&(i.parent=n),i})}function K(s){return s.edges().map(e=>{let t=s.edge(e),n={v:e.v,w:e.w};return e.name!==void 0&&(n.name=e.name),t!==void 0&&(n.value=t),n})}function z(s){let e=new p(s.options);return s.value!==void 0&&e.setGraph(s.value),s.nodes.forEach(t=>{e.setNode(t.v,t.value),t.parent&&e.setParent(t.v,t.parent)}),s.edges.forEach(t=>{e.setEdge({v:t.v,w:t.w,name:t.name},t.value)}),e}var v={};F(v,{CycleException:()=>l,bellmanFord:()=>m,components:()=>R,dijkstra:()=>E,dijkstraAll:()=>P,findCycles:()=>I,floydWarshall:()=>D,isAcyclic:()=>O,postorder:()=>T,preorder:()=>A,prim:()=>W,shortestPaths:()=>S,tarjan:()=>y,topsort:()=>L});var Q=()=>1;function m(s,e,t,n){return $(s,String(e),t||Q,n||function(i){return s.outEdges(i)})}function $(s,e,t,n){let i={},r,o=0,d=s.nodes(),a=function(u){let g=t(u);i[u.v].distance+g<i[u.w].distance&&(i[u.w]={distance:i[u.v].distance+g,predecessor:u.v},r=!0)},c=function(){d.forEach(function(u){n(u).forEach(function(g){let f=g.v===u?g.v:g.w,M=f===g.v?g.w:g.v;a({v:f,w:M})})})};d.forEach(function(u){let g=u===e?0:Number.POSITIVE_INFINITY;i[u]={distance:g,predecessor:""}});let h=d.length;for(let u=1;u<h&&(r=!1,o++,c(),!!r);u++);if(o===h-1&&(r=!1,c(),r))throw new Error("The graph contains a negative weight cycle");return i}function R(s){let e={},t=[],n;function i(r){r in e||(e[r]=!0,n.push(r),s.successors(r).forEach(i),s.predecessors(r).forEach(i))}return s.nodes().forEach(function(r){n=[],i(r),n.length&&t.push(n)}),t}var _=class{constructor(){this._arr=[];this._keyIndices={}}size(){return this._arr.length}keys(){return this._arr.map(e=>e.key)}has(e){return e in this._keyIndices}priority(e){let t=this._keyIndices[e];if(t!==void 0)return this._arr[t].priority}min(){if(this.size()===0)throw new Error("Queue underflow");return this._arr[0].key}add(e,t){let n=this._keyIndices,i=String(e);if(!(i in n)){let r=this._arr,o=r.length;return n[i]=o,r.push({key:i,priority:t}),this._decrease(o),!0}return!1}removeMin(){this._swap(0,this._arr.length-1);let e=this._arr.pop();return delete this._keyIndices[e.key],this._heapify(0),e.key}decrease(e,t){let n=this._keyIndices[e];if(n===void 0)throw new Error(`Key not found: ${e}`);let i=this._arr[n].priority;if(t>i)throw new Error(`New priority is greater than current priority. Key: ${e} Old: ${i} New: ${t}`);this._arr[n].priority=t,this._decrease(n)}_heapify(e){let t=this._arr,n=2*e,i=n+1,r=e;n<t.length&&(r=t[n].priority<t[r].priority?n:r,i<t.length&&(r=t[i].priority<t[r].priority?i:r),r!==e&&(this._swap(e,r),this._heapify(r)))}_decrease(e){let t=this._arr,n=t[e].priority,i;for(;e!==0&&(i=e>>1,!(t[i].priority<n));)this._swap(e,i),e=i}_swap(e,t){let n=this._arr,i=this._keyIndices,r=n[e],o=n[t];n[e]=o,n[t]=r,i[o.key]=e,i[r.key]=t}};var q=()=>1;function E(s,e,t,n){let i=function(r){return s.outEdges(r)};return B(s,String(e),t||q,n||i)}function B(s,e,t,n){let i={},r=new _,o,d,a=function(c){let h=c.v!==o?c.v:c.w,u=i[h],g=t(c),f=d.distance+g;if(g<0)throw new Error("dijkstra does not allow negative edge weights. Bad edge: "+c+" Weight: "+g);f<u.distance&&(u.distance=f,u.predecessor=o,r.decrease(h,f))};for(s.nodes().forEach(function(c){let h=c===e?0:Number.POSITIVE_INFINITY;i[c]={distance:h,predecessor:""},r.add(c,h)});r.size()>0&&(o=r.removeMin(),d=i[o],d.distance!==Number.POSITIVE_INFINITY);)n(o).forEach(a);return i}function P(s,e,t){return s.nodes().reduce(function(n,i){return n[i]=E(s,i,e,t),n},{})}function y(s){let e=0,t=[],n={},i=[];function r(o){let d=n[o]={onStack:!0,lowlink:e,index:e++};if(t.push(o),s.successors(o).forEach(function(a){a in n?n[a].onStack&&(d.lowlink=Math.min(d.lowlink,n[a].index)):(r(a),d.lowlink=Math.min(d.lowlink,n[a].lowlink))}),d.lowlink===d.index){let a=[],c;do c=t.pop(),n[c].onStack=!1,a.push(c);while(o!==c);i.push(a)}}return s.nodes().forEach(function(o){o in n||r(o)}),i}function I(s){return y(s).filter(function(e){return e.length>1||e.length===1&&s.hasEdge(e[0],e[0])})}var X=()=>1;function D(s,e,t){return Z(s,e||X,t||function(n){return s.outEdges(n)})}function Z(s,e,t){let n={},i=s.nodes();return i.forEach(function(r){n[r]={},n[r][r]={distance:0,predecessor:""},i.forEach(function(o){r!==o&&(n[r][o]={distance:Number.POSITIVE_INFINITY,predecessor:""})}),t(r).forEach(function(o){let d=o.v===r?o.w:o.v,a=e(o);n[r][d]={distance:a,predecessor:r}})}),i.forEach(function(r){let o=n[r];i.forEach(function(d){let a=n[d];i.forEach(function(c){let h=a[r],u=o[c],g=a[c],f=h.distance+u.distance;f<g.distance&&(g.distance=f,g.predecessor=u.predecessor)})})}),n}var l=class extends Error{constructor(...e){super(...e)}};function L(s){let e={},t={},n=[];function i(r){if(r in t)throw new l;r in e||(t[r]=!0,e[r]=!0,s.predecessors(r).forEach(i),delete t[r],n.push(r))}if(s.sinks().forEach(i),Object.keys(e).length!==s.nodeCount())throw new l;return n}function O(s){try{L(s)}catch(e){if(e instanceof l)return!1;throw e}return!0}function j(s,e,t,n,i){Array.isArray(e)||(e=[e]);let r=(d=>{var a;return(a=s.isDirected()?s.successors(d):s.neighbors(d))!=null?a:[]}),o={};return e.forEach(function(d){if(!s.hasNode(d))throw new Error("Graph does not have node: "+d);i=C(s,d,t==="post",o,r,n,i)}),i}function C(s,e,t,n,i,r,o){return e in n||(n[e]=!0,t||(o=r(o,e)),i(e).forEach(function(d){o=C(s,d,t,n,i,r,o)}),t&&(o=r(o,e))),o}function w(s,e,t){return j(s,e,t,function(n,i){return n.push(i),n},[])}function T(s,e){return w(s,e,"post")}function A(s,e){return w(s,e,"pre")}function W(s,e){let t=new p,n={},i=new _,r;function o(a){let c=a.v===r?a.w:a.v,h=i.priority(c);if(h!==void 0){let u=e(a);u<h&&(n[c]=r,i.decrease(c,u))}}if(s.nodeCount()===0)return t;s.nodes().forEach(function(a){i.add(a,Number.POSITIVE_INFINITY),t.setNode(a)}),i.decrease(s.nodes()[0],0);let d=!1;for(;i.size()>0;){if(r=i.removeMin(),r in n)t.setEdge(r,n[r]);else{if(d)throw new Error("Input graph is not connected: "+s);d=!0}s.nodeEdges(r).forEach(o)}return t}function S(s,e,t,n){return ee(s,e,t,n!=null?n:(i=>{let r=s.outEdges(i);return r!=null?r:[]}))}function ee(s,e,t,n){if(t===void 0)return E(s,e,t,n);let i=!1,r=s.nodes();for(let o=0;o<r.length;o++){let d=n(r[o]);for(let a=0;a<d.length;a++){let c=d[a],h=c.v===r[o]?c.v:c.w,u=h===c.v?c.w:c.v;t({v:h,w:u})<0&&(i=!0)}if(i)return m(s,e,t,n)}return E(s,e,t,n)}export{p as Graph,v as alg,G as json,H as version};
//# sourceMappingURL=graphlib.esm.js.map