@dagrejs/graphlib
Version:
A directed and undirected multi-graph library
3 lines (2 loc) • 13 kB
JavaScript
"use strict";var v=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var F=(s,e)=>{for(var t in e)v(s,t,{get:e[t],enumerable:!0})},K=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of U(e))!Y.call(s,i)&&i!==t&&v(s,i,{get:()=>e[i],enumerable:!(r=H(e,i))||r.enumerable});return s};var z=s=>K(v({},"__esModule",{value:!0}),s);var oe={};F(oe,{Graph:()=>l,alg:()=>G,json:()=>m,version:()=>P});module.exports=z(oe);var l=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(r=>{t!==void 0?this.setNode(r,t):this.setNode(r)}),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=r=>this.removeEdge(this._edgeObjs[r]);delete this._nodes[e],this._isCompound&&(this._removeFromParentsChildList(e),delete this._parent[e],this.children(e).forEach(r=>{this.setParent(r)}),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 r=t;r!==void 0;r=this.parent(r))if(r===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 r=new Set(t);for(let i of this.successors(e))r.add(i);return Array.from(r.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(([n,o])=>{e(n)&&t.setNode(n,o)}),Object.values(this._edgeObjs).forEach(n=>{t.hasNode(n.v)&&t.hasNode(n.w)&&t.setEdge(n,this.edge(n))});let r={},i=n=>{let o=this.parent(n);return!o||t.hasNode(o)?(r[n]=o!=null?o:void 0,o!=null?o:void 0):o in r?r[o]:i(o)};return this._isCompound&&t.nodes().forEach(n=>t.setParent(n,i(n))),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((r,i)=>(t!==void 0?this.setEdge(r,i,t):this.setEdge(r,i),i)),this}setEdge(e,t,r,i){let n,o,d,a,c=!1;typeof e=="object"&&e!==null&&"v"in e?(n=e.v,o=e.w,d=e.name,arguments.length===2&&(a=t,c=!0)):(n=e,o=t,d=i,arguments.length>2&&(a=r,c=!0)),n=""+n,o=""+o,d!==void 0&&(d=""+d);let h=b(this._isDirected,n,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(n),this.setNode(o),this._edgeLabels[h]=c?a:this._defaultEdgeLabelFn(n,o,d);let u=Q(this._isDirected,n,o,d);return n=u.v,o=u.w,Object.freeze(u),this._edgeObjs[h]=u,x(this._preds[o],n),x(this._sucs[n],o),this._in[o][h]=u,this._out[n][h]=u,this._edgeCount++,this}edge(e,t,r){let i=arguments.length===1?k(this._isDirected,e):b(this._isDirected,e,t,r);return this._edgeLabels[i]}edgeAsObj(e,t,r){let i=arguments.length===1?this.edge(e):this.edge(e,t,r);return typeof i!="object"?{label:i}:i}hasEdge(e,t,r){return(arguments.length===1?k(this._isDirected,e):b(this._isDirected,e,t,r))in this._edgeLabels}removeEdge(e,t,r){let i=arguments.length===1?k(this._isDirected,e):b(this._isDirected,e,t,r),n=this._edgeObjs[i];if(n){let o=n.v,d=n.w;delete this._edgeLabels[i],delete this._edgeObjs[i],R(this._preds[d],o),R(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,r){if(!e)return;let i=Object.values(e);return r?i.filter(n=>n.v===t&&n.w===r||n.v===r&&n.w===t):i}};function x(s,e){s[e]?s[e]++:s[e]=1}function R(s,e){s[e]!==void 0&&!--s[e]&&delete s[e]}function b(s,e,t,r){let i=""+e,n=""+t;if(!s&&i>n){let o=i;i=n,n=o}return i+""+n+""+(r===void 0?"\0":r)}function Q(s,e,t,r){let i=""+e,n=""+t;if(!s&&i>n){let d=i;i=n,n=d}let o={v:i,w:n};return r&&(o.name=r),o}function k(s,e){return b(s,e.v,e.w,e.name)}var P="4.0.1";var m={};F(m,{read:()=>X,write:()=>$});function $(s){let e={options:{directed:s.isDirected(),multigraph:s.isMultigraph(),compound:s.isCompound()},nodes:q(s),edges:B(s)},t=s.graph();return t!==void 0&&(e.value=structuredClone(t)),e}function q(s){return s.nodes().map(e=>{let t=s.node(e),r=s.parent(e),i={v:e};return t!==void 0&&(i.value=t),r!==void 0&&(i.parent=r),i})}function B(s){return s.edges().map(e=>{let t=s.edge(e),r={v:e.v,w:e.w};return e.name!==void 0&&(r.name=e.name),t!==void 0&&(r.value=t),r})}function X(s){let e=new l(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 G={};F(G,{CycleException:()=>p,bellmanFord:()=>y,components:()=>I,dijkstra:()=>E,dijkstraAll:()=>D,findCycles:()=>O,floydWarshall:()=>j,isAcyclic:()=>C,postorder:()=>W,preorder:()=>S,prim:()=>M,shortestPaths:()=>V,tarjan:()=>L,topsort:()=>w});var Z=()=>1;function y(s,e,t,r){return ee(s,String(e),t||Z,r||function(i){return s.outEdges(i)})}function ee(s,e,t,r){let i={},n,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},n=!0)},c=function(){d.forEach(function(u){r(u).forEach(function(g){let f=g.v===u?g.v:g.w,J=f===g.v?g.w:g.v;a({v:f,w:J})})})};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&&(n=!1,o++,c(),!!n);u++);if(o===h-1&&(n=!1,c(),n))throw new Error("The graph contains a negative weight cycle");return i}function I(s){let e={},t=[],r;function i(n){n in e||(e[n]=!0,r.push(n),s.successors(n).forEach(i),s.predecessors(n).forEach(i))}return s.nodes().forEach(function(n){r=[],i(n),r.length&&t.push(r)}),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 r=this._keyIndices,i=String(e);if(!(i in r)){let n=this._arr,o=n.length;return r[i]=o,n.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 r=this._keyIndices[e];if(r===void 0)throw new Error(`Key not found: ${e}`);let i=this._arr[r].priority;if(t>i)throw new Error(`New priority is greater than current priority. Key: ${e} Old: ${i} New: ${t}`);this._arr[r].priority=t,this._decrease(r)}_heapify(e){let t=this._arr,r=2*e,i=r+1,n=e;r<t.length&&(n=t[r].priority<t[n].priority?r:n,i<t.length&&(n=t[i].priority<t[n].priority?i:n),n!==e&&(this._swap(e,n),this._heapify(n)))}_decrease(e){let t=this._arr,r=t[e].priority,i;for(;e!==0&&(i=e>>1,!(t[i].priority<r));)this._swap(e,i),e=i}_swap(e,t){let r=this._arr,i=this._keyIndices,n=r[e],o=r[t];r[e]=o,r[t]=n,i[o.key]=e,i[n.key]=t}};var te=()=>1;function E(s,e,t,r){let i=function(n){return s.outEdges(n)};return re(s,String(e),t||te,r||i)}function re(s,e,t,r){let i={},n=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,n.decrease(h,f))};for(s.nodes().forEach(function(c){let h=c===e?0:Number.POSITIVE_INFINITY;i[c]={distance:h,predecessor:""},n.add(c,h)});n.size()>0&&(o=n.removeMin(),d=i[o],d.distance!==Number.POSITIVE_INFINITY);)r(o).forEach(a);return i}function D(s,e,t){return s.nodes().reduce(function(r,i){return r[i]=E(s,i,e,t),r},{})}function L(s){let e=0,t=[],r={},i=[];function n(o){let d=r[o]={onStack:!0,lowlink:e,index:e++};if(t.push(o),s.successors(o).forEach(function(a){a in r?r[a].onStack&&(d.lowlink=Math.min(d.lowlink,r[a].index)):(n(a),d.lowlink=Math.min(d.lowlink,r[a].lowlink))}),d.lowlink===d.index){let a=[],c;do c=t.pop(),r[c].onStack=!1,a.push(c);while(o!==c);i.push(a)}}return s.nodes().forEach(function(o){o in r||n(o)}),i}function O(s){return L(s).filter(function(e){return e.length>1||e.length===1&&s.hasEdge(e[0],e[0])})}var ne=()=>1;function j(s,e,t){return ie(s,e||ne,t||function(r){return s.outEdges(r)})}function ie(s,e,t){let r={},i=s.nodes();return i.forEach(function(n){r[n]={},r[n][n]={distance:0,predecessor:""},i.forEach(function(o){n!==o&&(r[n][o]={distance:Number.POSITIVE_INFINITY,predecessor:""})}),t(n).forEach(function(o){let d=o.v===n?o.w:o.v,a=e(o);r[n][d]={distance:a,predecessor:n}})}),i.forEach(function(n){let o=r[n];i.forEach(function(d){let a=r[d];i.forEach(function(c){let h=a[n],u=o[c],g=a[c],f=h.distance+u.distance;f<g.distance&&(g.distance=f,g.predecessor=u.predecessor)})})}),r}var p=class extends Error{constructor(...e){super(...e)}};function w(s){let e={},t={},r=[];function i(n){if(n in t)throw new p;n in e||(t[n]=!0,e[n]=!0,s.predecessors(n).forEach(i),delete t[n],r.push(n))}if(s.sinks().forEach(i),Object.keys(e).length!==s.nodeCount())throw new p;return r}function C(s){try{w(s)}catch(e){if(e instanceof p)return!1;throw e}return!0}function T(s,e,t,r,i){Array.isArray(e)||(e=[e]);let n=(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=A(s,d,t==="post",o,n,r,i)}),i}function A(s,e,t,r,i,n,o){return e in r||(r[e]=!0,t||(o=n(o,e)),i(e).forEach(function(d){o=A(s,d,t,r,i,n,o)}),t&&(o=n(o,e))),o}function N(s,e,t){return T(s,e,t,function(r,i){return r.push(i),r},[])}function W(s,e){return N(s,e,"post")}function S(s,e){return N(s,e,"pre")}function M(s,e){let t=new l,r={},i=new _,n;function o(a){let c=a.v===n?a.w:a.v,h=i.priority(c);if(h!==void 0){let u=e(a);u<h&&(r[c]=n,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(n=i.removeMin(),n in r)t.setEdge(n,r[n]);else{if(d)throw new Error("Input graph is not connected: "+s);d=!0}s.nodeEdges(n).forEach(o)}return t}function V(s,e,t,r){return se(s,e,t,r!=null?r:(i=>{let n=s.outEdges(i);return n!=null?n:[]}))}function se(s,e,t,r){if(t===void 0)return E(s,e,t,r);let i=!1,n=s.nodes();for(let o=0;o<n.length;o++){let d=r(n[o]);for(let a=0;a<d.length;a++){let c=d[a],h=c.v===n[o]?c.v:c.w,u=h===c.v?c.w:c.v;t({v:h,w:u})<0&&(i=!0)}if(i)return y(s,e,t,r)}return E(s,e,t,r)}0&&(module.exports={Graph,alg,json,version});
//# sourceMappingURL=graphlib.cjs.js.map