From 86fee221646de94fac60eba933c40b6a5cead45e Mon Sep 17 00:00:00 2001 From: David Newell Date: Sun, 2 Aug 2026 20:05:24 +0200 Subject: [PATCH] Strengthening the types --- dist/graphlib.cjs.js | 2 +- dist/graphlib.cjs.js.map | 6 +- dist/graphlib.esm.js | 2 +- dist/graphlib.esm.js.map | 6 +- dist/graphlib.js | 126 ++++++++++++++------ dist/graphlib.js.map | 4 +- dist/graphlib.min.js | 2 +- dist/graphlib.min.js.map | 6 +- dist/types/index.d.ts | 2 +- dist/types/index.d.ts.map | 2 +- dist/types/lib/alg/dfs.d.ts | 2 +- dist/types/lib/alg/dfs.d.ts.map | 2 +- dist/types/lib/alg/extract-path.d.ts.map | 2 +- dist/types/lib/alg/find-cycles.d.ts.map | 2 +- dist/types/lib/alg/prim.d.ts.map | 2 +- dist/types/lib/alg/reduce.d.ts | 2 +- dist/types/lib/alg/reduce.d.ts.map | 2 +- dist/types/lib/alg/shortest-paths.d.ts.map | 2 +- dist/types/lib/alg/tarjan.d.ts.map | 2 +- dist/types/lib/alg/topsort.d.ts | 2 +- dist/types/lib/alg/topsort.d.ts.map | 2 +- dist/types/lib/data/priority-queue.d.ts.map | 2 +- dist/types/lib/graph.d.ts | 18 +-- dist/types/lib/graph.d.ts.map | 2 +- dist/types/lib/json.d.ts | 4 +- dist/types/lib/json.d.ts.map | 2 +- dist/types/lib/types.d.ts | 2 + dist/types/lib/types.d.ts.map | 2 +- dist/types/lib/version.d.ts | 2 +- dist/types/lib/version.d.ts.map | 2 +- index.ts | 2 +- lib/alg/bellman-ford.ts | 9 +- lib/alg/components.ts | 4 +- lib/alg/dfs.ts | 2 +- lib/alg/dijkstra.ts | 12 +- lib/alg/extract-path.ts | 17 ++- lib/alg/find-cycles.ts | 8 +- lib/alg/floyd-warshall.ts | 33 ++--- lib/alg/prim.ts | 9 +- lib/alg/reduce.ts | 2 +- lib/alg/shortest-paths.ts | 12 +- lib/alg/tarjan.ts | 19 ++- lib/alg/topsort.ts | 7 +- lib/data/priority-queue.ts | 3 + lib/graph.ts | 44 ++++--- lib/json.ts | 4 +- lib/types.ts | 3 + 47 files changed, 250 insertions(+), 156 deletions(-) diff --git a/dist/graphlib.cjs.js b/dist/graphlib.cjs.js index 9b16aefc..c7495cbb 100644 --- a/dist/graphlib.cjs.js +++ b/dist/graphlib.cjs.js @@ -1,2 +1,2 @@ -"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.3";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+ge.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>1,!(t[i].priority1;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);f0&&(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.outEdges(e[0],e[0]).length>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{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);u0;){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{for(var t in e)F(s,t,{get:e[t],enumerable:!0})},z=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of U(e))!Y.call(s,i)&&i!==t&&F(s,i,{get:()=>e[i],enumerable:!(r=H(e,i))||r.enumerable});return s};var K=s=>z(F({},"__esModule",{value:!0}),s);var oe={};x(oe,{Graph:()=>l,alg:()=>v,json:()=>y,version:()=>I});module.exports=K(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),i=this.successors(e);if(i)for(let n of i)r.add(n);return Array.from(r.values())}}isLeaf(e){var r;let t;return this.isDirected()?t=this.successors(e):t=this.neighbors(e),((r=t==null?void 0:t.length)!=null?r:0)===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,o):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,h,c=!1;typeof e=="object"&&e!==null&&"v"in e?(n=e.v,o=e.w,d=e.name,arguments.length===2&&(h=t,c=!0)):(n=e,o=t,d=i,arguments.length>2&&(h=r,c=!0)),n=""+n,o=""+o,d!==void 0&&(d=""+d);let a=_(this._isDirected,n,o,d);if(a in this._edgeLabels)return c&&(this._edgeLabels[a]=h),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[a]=c?h: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[a]=u,k(this._preds[o],n),k(this._sucs[n],o),this._in[o][a]=u,this._out[n][a]=u,this._edgeCount++,this}edge(e,t,r){let i=arguments.length===1?R(this._isDirected,e):_(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"||i===null?{label:i}:i}hasEdge(e,t,r){return(arguments.length===1?R(this._isDirected,e):_(this._isDirected,e,t,r))in this._edgeLabels}removeEdge(e,t,r){let i=arguments.length===1?R(this._isDirected,e):_(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],P(this._preds[d],o),P(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 k(s,e){s[e]?s[e]++:s[e]=1}function P(s,e){s[e]!==void 0&&!--s[e]&&delete s[e]}function _(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 R(s,e){return _(s,e.v,e.w,e.name)}var I="4.0.4-pre";var y={};x(y,{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 v={};x(v,{CycleException:()=>p,bellmanFord:()=>L,components:()=>D,dijkstra:()=>E,dijkstraAll:()=>O,findCycles:()=>j,floydWarshall:()=>C,isAcyclic:()=>T,postorder:()=>S,preorder:()=>M,prim:()=>V,shortestPaths:()=>J,tarjan:()=>w,topsort:()=>N});var Z=()=>1;function L(s,e,t,r){return ee(s,String(e),t||Z,r||function(i){var n;return(n=s.outEdges(i))!=null?n:[]})}function ee(s,e,t,r){let i={},n,o=0,d=s.nodes(),h=function(u){let g=i[u.v],f=i[u.w];if(!g||!f)return;let m=t(u);g.distance+me.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(){if(this.size()===0)throw new Error("Queue underflow");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>1,!(t[i].priority1;function E(s,e,t,r){let i=function(n){var o;return(o=s.outEdges(n))!=null?o:[]};return re(s,String(e),t||te,r||i)}function re(s,e,t,r){let i={},n=new b,o,d,h=function(c){let a=c.v!==o?c.v:c.w,u=i[a];if(!u)return;let 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);f0;){o=n.removeMin();let c=i[o];if(!c||c.distance===Number.POSITIVE_INFINITY)break;d=c,r(o).forEach(h)}return i}function O(s,e,t){return s.nodes().reduce(function(r,i){return r[i]=E(s,i,e,t),r},{})}function w(s){let e=0,t=[],r={},i=[];function n(o){var h;let d=r[o]={onStack:!0,lowlink:e,index:e++};if(t.push(o),(h=s.successors(o))==null||h.forEach(function(c){if(c in r){let a=r[c];a!=null&&a.onStack&&(d.lowlink=Math.min(d.lowlink,a.index))}else{n(c);let a=r[c];a&&(d.lowlink=Math.min(d.lowlink,a.lowlink))}}),d.lowlink===d.index){let c=[],a;do{a=t.pop();let u=r[a];u&&(u.onStack=!1),c.push(a)}while(o!==a);i.push(c)}}return s.nodes().forEach(function(o){o in r||n(o)}),i}function j(s){return w(s).filter(function(e){var r;let t=e[0];return t?e.length>1||e.length===1&&((r=s.outEdges(t,t))!=null?r:[]).length>0:!1})}var ne=()=>1;function C(s,e,t){return ie(s,e||ne,t||function(r){var i;return(i=s.outEdges(r))!=null?i:[]})}function ie(s,e,t){let r={},i=s.nodes();return i.forEach(function(n){let o={};r[n]=o,o[n]={distance:0,predecessor:""},i.forEach(function(d){n!==d&&(o[d]={distance:Number.POSITIVE_INFINITY,predecessor:""})}),t(n).forEach(function(d){let h=d.v===n?d.w:d.v,c=e(d);o[h]={distance:c,predecessor:n}})}),i.forEach(function(n){let o=r[n];o&&i.forEach(function(d){let h=r[d];h&&i.forEach(function(c){let a=h[n],u=o[c],g=h[c];if(a&&u&&g){let f=a.distance+u.distance;f{var h;return(h=s.isDirected()?s.successors(d):s.neighbors(d))!=null?h:[]}),o={};return e.forEach(function(d){if(!s.hasNode(d))throw new Error("Graph does not have node: "+d);i=W(s,d,t==="post",o,n,r,i)}),i}function W(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=W(s,d,t,r,i,n,o)}),t&&(o=n(o,e))),o}function G(s,e,t){return A(s,e,t,function(r,i){return r.push(i),r},[])}function S(s,e){return G(s,e,"post")}function M(s,e){return G(s,e,"pre")}function V(s,e){var c;let t=new l,r={},i=new b,n;function o(a){let u=a.v===n?a.w:a.v,g=i.priority(u);if(g!==void 0){let f=e(a);f0;){if(n=i.removeMin(),n in r)t.setEdge(n,r[n]);else{if(h)throw new Error("Input graph is not connected: "+s);h=!0}(c=s.nodeEdges(n))==null||c.forEach(o)}return t}function J(s,e,t,r){return se(s,e,t,r!=null?r:(i=>{var n;return(n=s.outEdges(i))!=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 private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined | void = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | void {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): void | string[] {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): void | string[] {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): void | string[] {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n for (const succ of this.successors(v)!) {\n union.add(succ);\n }\n\n return Array.from(union.values());\n }\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | void;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return neighbors!.length === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent ?? undefined;\n return parent ?? undefined;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\") {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): void | Edge[] {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.3';\n", "import {Graph} from './graph';\nimport type {GraphOptions} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v)!;\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const edgeWeight = weightFn(edge);\n if (results[edge.v]!.distance + edgeWeight < results[edge.w]!.distance) {\n results[edge.w] = {\n distance: results[edge.v]!.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)!.forEach(dfs);\n graph.predecessors(v)!.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v)!;\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w]!;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n vEntry = results[v]!;\n if (vEntry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)!.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.lowlink);\n } else if (visited[w]!.onStack) {\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.index);\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n visited[w]!.onStack = false;\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {Edge} from '../types';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n // A single-node component is a cycle iff the node has a self-loop. We check via outEdges\n // rather than hasEdge(v, v) because the latter only matches the default (unnamed) edge and\n // would miss a named self-loop edge in a multigraph.\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(cmpt[0]!, cmpt[0]!) as Edge[]).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v)!;\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n results[v] = {};\n results[v]![v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n results[v]![w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n results[v]![w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k]!;\n nodes.forEach(function (i) {\n const rowI = results[i]!;\n nodes.forEach(function (j) {\n const ik = rowI[k]!;\n const kj = rowK[j]!;\n const ij = rowI[j]!;\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(...args: unknown[]) {\n super(...args as ConstructorParameters);\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)!.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: string,\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: string): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n pq.decrease(graph.nodes()[0]!, 0);\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)!.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n const edges = g.outEdges(v);\n return edges ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const adjList = edgeFn(nodes[i]!);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j]!;\n const inVertex = edge.v === nodes[i] ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], - "mappings": "yaAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,QAAAC,EAAA,SAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAN,ICgBO,IAAMO,EAAN,KAAgE,CA4BnE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAAyB,GACjC,KAAQ,YAAuB,GAK/B,KAAQ,OAAoC,CAAC,EAE7C,KAAQ,IAA4C,CAAC,EAErD,KAAQ,OAAiD,CAAC,EAE1D,KAAQ,KAA6C,CAAC,EAEtD,KAAQ,MAAgD,CAAC,EAEzD,KAAQ,UAAkC,CAAC,EAE3C,KAAQ,YAAyC,CAAC,EAElD,KAAQ,WAAqB,EAE7B,KAAQ,WAAqB,EAgvB7B,KAAQ,oBAAmD,IAAG,GAG9D,KAAQ,oBAAmD,IAAG,GA9uBtDA,IACA,KAAK,YAAc,aAAcA,EAAOA,EAAK,SAAY,GACzD,KAAK,cAAgB,eAAgBA,EAAOA,EAAK,WAAc,GAC/D,KAAK,YAAc,aAAcA,EAAOA,EAAK,SAAY,IAGzD,KAAK,cAEL,KAAK,QAAU,CAAC,EAGhB,KAAK,UAAY,CAAC,EAClB,KAAK,UAAU,IAAU,EAAI,CAAC,EAEtC,CAOA,YAAsB,CAClB,OAAO,KAAK,WAChB,CAOA,cAAwB,CACpB,OAAO,KAAK,aAChB,CASA,YAAsB,CAClB,OAAO,KAAK,WAChB,CAQA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,EAAK,EAAIA,EAAK,EAElDX,EAAS,CAAC,EAAGY,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCP,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOQ,EAAYhB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", - "names": ["index_exports", "__export", "Graph", "alg_exports", "json_exports", "version", "__toCommonJS", "Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "succ", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "entry", "w", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "results", "nodes", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "args", "topsort", "graph", "visited", "stack", "results", "visit", "node", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "edges", "dijkstra", "negativeEdgeExists", "nodes", "i", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] + "sourcesContent": ["/**\n * Copyright (c) 2014, Chris Pettitt\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n * 1. Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * 2. Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * 3. Neither the name of the copyright holder nor the names of its contributors\n * may be used to endorse or promote products derived from this software without\n * specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\nexport {Graph} from './lib/graph';\nexport {version} from './lib/version';\nexport * as json from './lib/json';\nexport * as alg from './lib/alg/index';\n\nexport type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction, Label} from './lib/types.js';\n", "import type {Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory} from './types';\n\nconst DEFAULT_EDGE_NAME = \"\\x00\";\nconst GRAPH_NODE = \"\\x00\";\nconst EDGE_KEY_DELIM = \"\\x01\";\n\n// Implementation notes:\n//\n// * Node id query functions should return string ids for the nodes\n// * Edge id query functions should return an \"edgeObj\", edge object, that is\n// composed of enough information to uniquely identify an edge: {v, w, name}.\n// * Internally we use an \"edgeId\", a stringified form of the edgeObj, to\n// reference edges. This is because we need a performant way to look these\n// edges up and, object properties, which have string keys, are the closest\n// we're going to get to a performant hashtable in JavaScript.\n\nexport class Graph {\n private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | undefined {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n return undefined;\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): string[] | undefined {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): string[] | undefined {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): string[] | undefined {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n const sucs = this.successors(v);\n if (sucs) {\n for (const succ of sucs) {\n union.add(succ);\n }\n }\n\n return Array.from(union.values());\n }\n return undefined;\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | undefined;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return (neighbors?.length ?? 0) === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent;\n return parent;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\" || edgeLabel === null) {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as unknown as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): Edge[] | undefined {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n return undefined;\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.4-pre';\n", "import {Graph} from './graph';\nimport type {GraphOptions, Label} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v) ?? [];\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const uEntry = results[edge.v];\n const wEntry = results[edge.w];\n if (!uEntry || !wEntry) return;\n const edgeWeight = weightFn(edge);\n if (uEntry.distance + edgeWeight < wEntry.distance) {\n results[edge.w] = {\n distance: uEntry.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)?.forEach(dfs);\n graph.predecessors(v)?.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v) ?? [];\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w];\n if (!wEntry) return;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin();\n const entry = results[v];\n if (!entry || entry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n vEntry = entry;\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)?.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n const wEntry = visited[w];\n if (wEntry) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink);\n }\n } else {\n const wEntry = visited[w];\n if (wEntry?.onStack) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.index);\n }\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n const wEntry = visited[w];\n if (wEntry) {\n wEntry.onStack = false;\n }\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n const firstNode = cmpt[0];\n if (!firstNode) return false;\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(firstNode, firstNode) ?? []).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v) ?? [];\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n const rowV: Record = {};\n results[v] = rowV;\n rowV[v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n rowV[w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n rowV[w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k];\n if (!rowK) return;\n nodes.forEach(function (i) {\n const rowI = results[i];\n if (!rowI) return;\n nodes.forEach(function (j) {\n const ik = rowI[k];\n const kj = rowK[j];\n const ij = rowI[j];\n if (ik && kj && ij) {\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(message?: string) {\n super(message);\n this.name = \"CycleException\";\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)?.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: \"pre\" | \"post\",\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: \"pre\" | \"post\"): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n const firstNode = graph.nodes()[0];\n if (firstNode !== undefined) {\n pq.decrease(firstNode, 0);\n }\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin();\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)?.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n return g.outEdges(v) ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node === undefined) continue;\n const adjList = edgeFn(node);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j];\n if (!edge) continue;\n const inVertex = edge.v === node ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], + "mappings": "yaAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,QAAAC,EAAA,SAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAN,ICgBO,IAAMO,EAAN,KAAsE,CA4BzE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAAyB,GACjC,KAAQ,YAAuB,GAK/B,KAAQ,OAAoC,CAAC,EAE7C,KAAQ,IAA4C,CAAC,EAErD,KAAQ,OAAiD,CAAC,EAE1D,KAAQ,KAA6C,CAAC,EAEtD,KAAQ,MAAgD,CAAC,EAEzD,KAAQ,UAAkC,CAAC,EAE3C,KAAQ,YAAyC,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C,IAAMC,EAAOF,EAAQC,CAAC,EACtB,GAAI,CAACC,EAAM,SACX,IAAMC,EAAWD,EAAK,IAAMH,EAAOG,EAAK,EAAIA,EAAK,EAC3CE,EAAYD,IAAaD,EAAK,EAAIA,EAAK,EAAIA,EAAK,EAElDZ,EAAS,CAAC,EAAGa,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCR,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOS,EAAYjB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", + "names": ["index_exports", "__export", "Graph", "alg_exports", "json_exports", "version", "__toCommonJS", "Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "sucs", "succ", "_a", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "_a", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "uEntry", "wEntry", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "_a", "_b", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "_a", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "entry", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "_a", "entry", "w", "wEntry", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "_a", "firstNode", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "_a", "results", "nodes", "rowV", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "message", "topsort", "graph", "visited", "stack", "results", "visit", "node", "_a", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "_a", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "firstNode", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "_a", "dijkstra", "negativeEdgeExists", "nodes", "i", "node", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] } diff --git a/dist/graphlib.esm.js b/dist/graphlib.esm.js index 19c0c354..76be1f38 100644 --- a/dist/graphlib.esm.js +++ b/dist/graphlib.esm.js @@ -1,2 +1,2 @@ -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.3";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+ge.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>1,!(t[i].priority1;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);f0&&(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.outEdges(e[0],e[0]).length>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{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);u0;){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{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(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),i=this.successors(e);if(i)for(let n of i)r.add(n);return Array.from(r.values())}}isLeaf(e){var r;let t;return this.isDirected()?t=this.successors(e):t=this.neighbors(e),((r=t==null?void 0:t.length)!=null?r:0)===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,o):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,h,c=!1;typeof e=="object"&&e!==null&&"v"in e?(n=e.v,o=e.w,d=e.name,arguments.length===2&&(h=t,c=!0)):(n=e,o=t,d=i,arguments.length>2&&(h=r,c=!0)),n=""+n,o=""+o,d!==void 0&&(d=""+d);let a=_(this._isDirected,n,o,d);if(a in this._edgeLabels)return c&&(this._edgeLabels[a]=h),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[a]=c?h:this._defaultEdgeLabelFn(n,o,d);let u=J(this._isDirected,n,o,d);return n=u.v,o=u.w,Object.freeze(u),this._edgeObjs[a]=u,R(this._preds[o],n),R(this._sucs[n],o),this._in[o][a]=u,this._out[n][a]=u,this._edgeCount++,this}edge(e,t,r){let i=arguments.length===1?G(this._isDirected,e):_(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"||i===null?{label:i}:i}hasEdge(e,t,r){return(arguments.length===1?G(this._isDirected,e):_(this._isDirected,e,t,r))in this._edgeLabels}removeEdge(e,t,r){let i=arguments.length===1?G(this._isDirected,e):_(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],k(this._preds[d],o),k(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 R(s,e){s[e]?s[e]++:s[e]=1}function k(s,e){s[e]!==void 0&&!--s[e]&&delete s[e]}function _(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 J(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 G(s,e){return _(s,e.v,e.w,e.name)}var H="4.0.4-pre";var v={};x(v,{read:()=>K,write:()=>U});function U(s){let e={options:{directed:s.isDirected(),multigraph:s.isMultigraph(),compound:s.isCompound()},nodes:Y(s),edges:z(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),r=s.parent(e),i={v:e};return t!==void 0&&(i.value=t),r!==void 0&&(i.parent=r),i})}function z(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 K(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 F={};x(F,{CycleException:()=>l,bellmanFord:()=>y,components:()=>P,dijkstra:()=>E,dijkstraAll:()=>I,findCycles:()=>D,floydWarshall:()=>O,isAcyclic:()=>j,postorder:()=>A,preorder:()=>W,prim:()=>S,shortestPaths:()=>M,tarjan:()=>L,topsort:()=>w});var Q=()=>1;function y(s,e,t,r){return $(s,String(e),t||Q,r||function(i){var n;return(n=s.outEdges(i))!=null?n:[]})}function $(s,e,t,r){let i={},n,o=0,d=s.nodes(),h=function(u){let g=i[u.v],f=i[u.w];if(!g||!f)return;let m=t(u);g.distance+me.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(){if(this.size()===0)throw new Error("Queue underflow");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>1,!(t[i].priority1;function E(s,e,t,r){let i=function(n){var o;return(o=s.outEdges(n))!=null?o:[]};return B(s,String(e),t||q,r||i)}function B(s,e,t,r){let i={},n=new b,o,d,h=function(c){let a=c.v!==o?c.v:c.w,u=i[a];if(!u)return;let 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);f0;){o=n.removeMin();let c=i[o];if(!c||c.distance===Number.POSITIVE_INFINITY)break;d=c,r(o).forEach(h)}return i}function I(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){var h;let d=r[o]={onStack:!0,lowlink:e,index:e++};if(t.push(o),(h=s.successors(o))==null||h.forEach(function(c){if(c in r){let a=r[c];a!=null&&a.onStack&&(d.lowlink=Math.min(d.lowlink,a.index))}else{n(c);let a=r[c];a&&(d.lowlink=Math.min(d.lowlink,a.lowlink))}}),d.lowlink===d.index){let c=[],a;do{a=t.pop();let u=r[a];u&&(u.onStack=!1),c.push(a)}while(o!==a);i.push(c)}}return s.nodes().forEach(function(o){o in r||n(o)}),i}function D(s){return L(s).filter(function(e){var r;let t=e[0];return t?e.length>1||e.length===1&&((r=s.outEdges(t,t))!=null?r:[]).length>0:!1})}var X=()=>1;function O(s,e,t){return Z(s,e||X,t||function(r){var i;return(i=s.outEdges(r))!=null?i:[]})}function Z(s,e,t){let r={},i=s.nodes();return i.forEach(function(n){let o={};r[n]=o,o[n]={distance:0,predecessor:""},i.forEach(function(d){n!==d&&(o[d]={distance:Number.POSITIVE_INFINITY,predecessor:""})}),t(n).forEach(function(d){let h=d.v===n?d.w:d.v,c=e(d);o[h]={distance:c,predecessor:n}})}),i.forEach(function(n){let o=r[n];o&&i.forEach(function(d){let h=r[d];h&&i.forEach(function(c){let a=h[n],u=o[c],g=h[c];if(a&&u&&g){let f=a.distance+u.distance;f{var h;return(h=s.isDirected()?s.successors(d):s.neighbors(d))!=null?h:[]}),o={};return e.forEach(function(d){if(!s.hasNode(d))throw new Error("Graph does not have node: "+d);i=T(s,d,t==="post",o,n,r,i)}),i}function T(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=T(s,d,t,r,i,n,o)}),t&&(o=n(o,e))),o}function N(s,e,t){return C(s,e,t,function(r,i){return r.push(i),r},[])}function A(s,e){return N(s,e,"post")}function W(s,e){return N(s,e,"pre")}function S(s,e){var c;let t=new p,r={},i=new b,n;function o(a){let u=a.v===n?a.w:a.v,g=i.priority(u);if(g!==void 0){let f=e(a);f0;){if(n=i.removeMin(),n in r)t.setEdge(n,r[n]);else{if(h)throw new Error("Input graph is not connected: "+s);h=!0}(c=s.nodeEdges(n))==null||c.forEach(o)}return t}function M(s,e,t,r){return ee(s,e,t,r!=null?r:(i=>{var n;return(n=s.outEdges(i))!=null?n:[]}))}function ee(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 private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined | void = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | void {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): void | string[] {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): void | string[] {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): void | string[] {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n for (const succ of this.successors(v)!) {\n union.add(succ);\n }\n\n return Array.from(union.values());\n }\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | void;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return neighbors!.length === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent ?? undefined;\n return parent ?? undefined;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\") {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): void | Edge[] {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.3';\n", "import {Graph} from './graph';\nimport type {GraphOptions} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v)!;\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const edgeWeight = weightFn(edge);\n if (results[edge.v]!.distance + edgeWeight < results[edge.w]!.distance) {\n results[edge.w] = {\n distance: results[edge.v]!.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)!.forEach(dfs);\n graph.predecessors(v)!.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v)!;\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w]!;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n vEntry = results[v]!;\n if (vEntry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)!.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.lowlink);\n } else if (visited[w]!.onStack) {\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.index);\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n visited[w]!.onStack = false;\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {Edge} from '../types';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n // A single-node component is a cycle iff the node has a self-loop. We check via outEdges\n // rather than hasEdge(v, v) because the latter only matches the default (unnamed) edge and\n // would miss a named self-loop edge in a multigraph.\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(cmpt[0]!, cmpt[0]!) as Edge[]).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v)!;\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n results[v] = {};\n results[v]![v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n results[v]![w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n results[v]![w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k]!;\n nodes.forEach(function (i) {\n const rowI = results[i]!;\n nodes.forEach(function (j) {\n const ik = rowI[k]!;\n const kj = rowK[j]!;\n const ij = rowI[j]!;\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(...args: unknown[]) {\n super(...args as ConstructorParameters);\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)!.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: string,\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: string): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n pq.decrease(graph.nodes()[0]!, 0);\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)!.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n const edges = g.outEdges(v);\n return edges ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const adjList = edgeFn(nodes[i]!);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j]!;\n const inVertex = edge.v === nodes[i] ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], - "mappings": "0FAgBO,IAAMA,EAAN,KAAgE,CA4BnE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAAyB,GACjC,KAAQ,YAAuB,GAK/B,KAAQ,OAAoC,CAAC,EAE7C,KAAQ,IAA4C,CAAC,EAErD,KAAQ,OAAiD,CAAC,EAE1D,KAAQ,KAA6C,CAAC,EAEtD,KAAQ,MAAgD,CAAC,EAEzD,KAAQ,UAAkC,CAAC,EAE3C,KAAQ,YAAyC,CAAC,EAElD,KAAQ,WAAqB,EAE7B,KAAQ,WAAqB,EAgvB7B,KAAQ,oBAAmD,IAAG,GAG9D,KAAQ,oBAAmD,IAAG,GA9uBtDA,IACA,KAAK,YAAc,aAAcA,EAAOA,EAAK,SAAY,GACzD,KAAK,cAAgB,eAAgBA,EAAOA,EAAK,WAAc,GAC/D,KA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,EAAM,OAAQC,IAAK,CACnC,IAAMC,EAAUR,EAAOM,EAAMC,CAAC,CAAE,EAEhC,QAASE,EAAI,EAAGA,EAAID,EAAQ,OAAQC,IAAK,CACrC,IAAMC,EAAOF,EAAQC,CAAC,EAChBE,EAAWD,EAAK,IAAMJ,EAAMC,CAAC,EAAIG,EAAK,EAAIA,EAAK,EAC/CE,EAAYD,IAAaD,EAAK,EAAIA,EAAK,EAAIA,EAAK,EAElDX,EAAS,CAAC,EAAGY,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCP,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOQ,EAAYhB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", - "names": ["Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "succ", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "entry", "w", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "results", "nodes", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "args", "topsort", "graph", "visited", "stack", "results", "visit", "node", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "edges", "dijkstra", "negativeEdgeExists", "nodes", "i", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] + "sourcesContent": ["import type {Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory} from './types';\n\nconst DEFAULT_EDGE_NAME = \"\\x00\";\nconst GRAPH_NODE = \"\\x00\";\nconst EDGE_KEY_DELIM = \"\\x01\";\n\n// Implementation notes:\n//\n// * Node id query functions should return string ids for the nodes\n// * Edge id query functions should return an \"edgeObj\", edge object, that is\n// composed of enough information to uniquely identify an edge: {v, w, name}.\n// * Internally we use an \"edgeId\", a stringified form of the edgeObj, to\n// reference edges. This is because we need a performant way to look these\n// edges up and, object properties, which have string keys, are the closest\n// we're going to get to a performant hashtable in JavaScript.\n\nexport class Graph {\n private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | undefined {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n return undefined;\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): string[] | undefined {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): string[] | undefined {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): string[] | undefined {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n const sucs = this.successors(v);\n if (sucs) {\n for (const succ of sucs) {\n union.add(succ);\n }\n }\n\n return Array.from(union.values());\n }\n return undefined;\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | undefined;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return (neighbors?.length ?? 0) === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent;\n return parent;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\" || edgeLabel === null) {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as unknown as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): Edge[] | undefined {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n return undefined;\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.4-pre';\n", "import {Graph} from './graph';\nimport type {GraphOptions, Label} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v) ?? [];\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const uEntry = results[edge.v];\n const wEntry = results[edge.w];\n if (!uEntry || !wEntry) return;\n const edgeWeight = weightFn(edge);\n if (uEntry.distance + edgeWeight < wEntry.distance) {\n results[edge.w] = {\n distance: uEntry.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)?.forEach(dfs);\n graph.predecessors(v)?.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v) ?? [];\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w];\n if (!wEntry) return;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin();\n const entry = results[v];\n if (!entry || entry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n vEntry = entry;\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)?.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n const wEntry = visited[w];\n if (wEntry) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink);\n }\n } else {\n const wEntry = visited[w];\n if (wEntry?.onStack) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.index);\n }\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n const wEntry = visited[w];\n if (wEntry) {\n wEntry.onStack = false;\n }\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n const firstNode = cmpt[0];\n if (!firstNode) return false;\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(firstNode, firstNode) ?? []).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v) ?? [];\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n const rowV: Record = {};\n results[v] = rowV;\n rowV[v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n rowV[w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n rowV[w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k];\n if (!rowK) return;\n nodes.forEach(function (i) {\n const rowI = results[i];\n if (!rowI) return;\n nodes.forEach(function (j) {\n const ik = rowI[k];\n const kj = rowK[j];\n const ij = rowI[j];\n if (ik && kj && ij) {\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(message?: string) {\n super(message);\n this.name = \"CycleException\";\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)?.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: \"pre\" | \"post\",\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: \"pre\" | \"post\"): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n const firstNode = graph.nodes()[0];\n if (firstNode !== undefined) {\n pq.decrease(firstNode, 0);\n }\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin();\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)?.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n return g.outEdges(v) ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node === undefined) continue;\n const adjList = edgeFn(node);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j];\n if (!edge) continue;\n const inVertex = edge.v === node ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], + "mappings": "0FAgBO,IAAMA,EAAN,KAAsE,CA4BzE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAA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E,MAAM,EAEtB,QAASU,EAAI,EAAGA,EAAID,EAAM,OAAQC,IAAK,CACnC,IAAMC,EAAOF,EAAMC,CAAC,EACpB,GAAIC,IAAS,OAAW,SACxB,IAAMC,EAAUT,EAAOQ,CAAI,EAE3B,QAASE,EAAI,EAAGA,EAAID,EAAQ,OAAQC,IAAK,CACrC,IAAMC,EAAOF,EAAQC,CAAC,EACtB,GAAI,CAACC,EAAM,SACX,IAAMC,EAAWD,EAAK,IAAMH,EAAOG,EAAK,EAAIA,EAAK,EAC3CE,EAAYD,IAAaD,EAAK,EAAIA,EAAK,EAAIA,EAAK,EAElDZ,EAAS,CAAC,EAAGa,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCR,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOS,EAAYjB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", + "names": ["Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "sucs", "succ", "_a", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "_a", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "uEntry", "wEntry", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "_a", "_b", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "_a", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "entry", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "_a", "entry", "w", "wEntry", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "_a", "firstNode", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "_a", "results", "nodes", "rowV", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "message", "topsort", "graph", "visited", "stack", "results", "visit", "node", "_a", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "_a", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "firstNode", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "_a", "dijkstra", "negativeEdgeExists", "nodes", "i", "node", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] } diff --git a/dist/graphlib.js b/dist/graphlib.js index 9525735d..76fb1479 100644 --- a/dist/graphlib.js +++ b/dist/graphlib.js @@ -309,6 +309,7 @@ var graphlib = (() => { return parent; } } + return void 0; } /** * Gets list of direct children of node v. @@ -343,6 +344,7 @@ var graphlib = (() => { if (predsV) { return Object.keys(predsV); } + return void 0; } /** * Return all nodes that are successors of the specified node or undefined if node v is not in @@ -357,6 +359,7 @@ var graphlib = (() => { if (sucsV) { return Object.keys(sucsV); } + return void 0; } /** * Return all nodes that are predecessors or successors of the specified node or undefined if @@ -370,20 +373,25 @@ var graphlib = (() => { const preds = this.predecessors(v); if (preds) { const union = new Set(preds); - for (const succ of this.successors(v)) { - union.add(succ); + const sucs = this.successors(v); + if (sucs) { + for (const succ of sucs) { + union.add(succ); + } } return Array.from(union.values()); } + return void 0; } isLeaf(v) { + var _a; let neighbors; if (this.isDirected()) { neighbors = this.successors(v); } else { neighbors = this.neighbors(v); } - return neighbors.length === 0; + return ((_a = neighbors == null ? void 0 : neighbors.length) != null ? _a : 0) === 0; } /** * Creates new graph with nodes filtered via filter. Edges incident to rejected node @@ -415,8 +423,8 @@ var graphlib = (() => { const findParent = (v) => { const parent = this.parent(v); if (!parent || copy.hasNode(parent)) { - parents[v] = parent != null ? parent : void 0; - return parent != null ? parent : void 0; + parents[v] = parent; + return parent; } else if (parent in parents) { return parents[parent]; } else { @@ -543,7 +551,7 @@ var graphlib = (() => { } edgeAsObj(v, w, name) { const edgeLabel = arguments.length === 1 ? this.edge(v) : this.edge(v, w, name); - if (typeof edgeLabel !== "object") { + if (typeof edgeLabel !== "object" || edgeLabel === null) { return { label: edgeLabel }; } return edgeLabel; @@ -611,6 +619,7 @@ var graphlib = (() => { if (v in this._nodes) { return this.filterEdges({ ...this._in[v], ...this._out[v] }, v, w); } + return void 0; } _removeFromParentsChildList(v) { delete this._children[this._parent[v]][v]; @@ -669,7 +678,7 @@ var graphlib = (() => { } // lib/version.ts - var version = "4.0.3"; + var version = "4.0.4-pre"; // lib/json.ts var json_exports = {}; @@ -764,7 +773,8 @@ var graphlib = (() => { String(source), weightFn || DEFAULT_WEIGHT_FUNC, edgeFn || function(v) { - return g.outEdges(v); + var _a; + return (_a = g.outEdges(v)) != null ? _a : []; } ); } @@ -774,10 +784,13 @@ var graphlib = (() => { let iterations = 0; const nodes = g.nodes(); const relaxEdge = function(edge) { + const uEntry = results[edge.v]; + const wEntry = results[edge.w]; + if (!uEntry || !wEntry) return; const edgeWeight = weightFn(edge); - if (results[edge.v].distance + edgeWeight < results[edge.w].distance) { + if (uEntry.distance + edgeWeight < wEntry.distance) { results[edge.w] = { - distance: results[edge.v].distance + edgeWeight, + distance: uEntry.distance + edgeWeight, predecessor: edge.v }; didADistanceUpgrade = true; @@ -821,11 +834,12 @@ var graphlib = (() => { const cmpts = []; let cmpt; function dfs2(v) { + var _a, _b; if (v in visited) return; visited[v] = true; cmpt.push(v); - graph.successors(v).forEach(dfs2); - graph.predecessors(v).forEach(dfs2); + (_a = graph.successors(v)) == null ? void 0 : _a.forEach(dfs2); + (_b = graph.predecessors(v)) == null ? void 0 : _b.forEach(dfs2); } graph.nodes().forEach(function(v) { cmpt = []; @@ -904,6 +918,9 @@ var graphlib = (() => { * Removes and returns the smallest key in the queue. Takes `O(log n)` time. */ removeMin() { + if (this.size() === 0) { + throw new Error("Queue underflow"); + } this._swap(0, this._arr.length - 1); const min = this._arr.pop(); delete this._keyIndices[min.key]; @@ -973,7 +990,8 @@ var graphlib = (() => { var DEFAULT_WEIGHT_FUNC2 = () => 1; function dijkstra(graph, source, weightFn, edgeFn) { const defaultEdgeFn = function(v) { - return graph.outEdges(v); + var _a; + return (_a = graph.outEdges(v)) != null ? _a : []; }; return runDijkstra( graph, @@ -989,6 +1007,7 @@ var graphlib = (() => { const updateNeighbors = function(edge) { const w = edge.v !== v ? edge.v : edge.w; const wEntry = results[w]; + if (!wEntry) return; const weight = weightFn(edge); const distance = vEntry.distance + weight; if (weight < 0) { @@ -1007,10 +1026,11 @@ var graphlib = (() => { }); while (pq.size() > 0) { v = pq.removeMin(); - vEntry = results[v]; - if (vEntry.distance === Number.POSITIVE_INFINITY) { + const entry = results[v]; + if (!entry || entry.distance === Number.POSITIVE_INFINITY) { break; } + vEntry = entry; edgeFn(v).forEach(updateNeighbors); } return results; @@ -1031,18 +1051,25 @@ var graphlib = (() => { const visited = {}; const results = []; function dfs2(v) { + var _a; const entry = visited[v] = { onStack: true, lowlink: index, index: index++ }; stack.push(v); - graph.successors(v).forEach(function(w) { + (_a = graph.successors(v)) == null ? void 0 : _a.forEach(function(w) { if (!(w in visited)) { dfs2(w); - entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink); - } else if (visited[w].onStack) { - entry.lowlink = Math.min(entry.lowlink, visited[w].index); + const wEntry = visited[w]; + if (wEntry) { + entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink); + } + } else { + const wEntry = visited[w]; + if (wEntry == null ? void 0 : wEntry.onStack) { + entry.lowlink = Math.min(entry.lowlink, wEntry.index); + } } }); if (entry.lowlink === entry.index) { @@ -1050,7 +1077,10 @@ var graphlib = (() => { let w; do { w = stack.pop(); - visited[w].onStack = false; + const wEntry = visited[w]; + if (wEntry) { + wEntry.onStack = false; + } cmpt.push(w); } while (v !== w); results.push(cmpt); @@ -1067,7 +1097,10 @@ var graphlib = (() => { // lib/alg/find-cycles.ts function findCycles(graph) { return tarjan(graph).filter(function(cmpt) { - return cmpt.length > 1 || cmpt.length === 1 && graph.outEdges(cmpt[0], cmpt[0]).length > 0; + var _a; + const firstNode = cmpt[0]; + if (!firstNode) return false; + return cmpt.length > 1 || cmpt.length === 1 && ((_a = graph.outEdges(firstNode, firstNode)) != null ? _a : []).length > 0; }); } @@ -1078,7 +1111,8 @@ var graphlib = (() => { graph, weightFn || DEFAULT_WEIGHT_FUNC3, edgeFn || function(v) { - return graph.outEdges(v); + var _a; + return (_a = graph.outEdges(v)) != null ? _a : []; } ); } @@ -1086,31 +1120,36 @@ var graphlib = (() => { const results = {}; const nodes = graph.nodes(); nodes.forEach(function(v) { - results[v] = {}; - results[v][v] = { distance: 0, predecessor: "" }; + const rowV = {}; + results[v] = rowV; + rowV[v] = { distance: 0, predecessor: "" }; nodes.forEach(function(w) { if (v !== w) { - results[v][w] = { distance: Number.POSITIVE_INFINITY, predecessor: "" }; + rowV[w] = { distance: Number.POSITIVE_INFINITY, predecessor: "" }; } }); edgeFn(v).forEach(function(edge) { const w = edge.v === v ? edge.w : edge.v; const d = weightFn(edge); - results[v][w] = { distance: d, predecessor: v }; + rowV[w] = { distance: d, predecessor: v }; }); }); nodes.forEach(function(k) { const rowK = results[k]; + if (!rowK) return; nodes.forEach(function(i) { const rowI = results[i]; + if (!rowI) return; nodes.forEach(function(j) { const ik = rowI[k]; const kj = rowK[j]; const ij = rowI[j]; - const altDistance = ik.distance + kj.distance; - if (altDistance < ij.distance) { - ij.distance = altDistance; - ij.predecessor = kj.predecessor; + if (ik && kj && ij) { + const altDistance = ik.distance + kj.distance; + if (altDistance < ij.distance) { + ij.distance = altDistance; + ij.predecessor = kj.predecessor; + } } }); }); @@ -1120,8 +1159,9 @@ var graphlib = (() => { // lib/alg/topsort.ts var CycleException = class extends Error { - constructor(...args) { - super(...args); + constructor(message) { + super(message); + this.name = "CycleException"; } }; function topsort(graph) { @@ -1129,13 +1169,14 @@ var graphlib = (() => { const stack = {}; const results = []; function visit(node) { + var _a; if (node in stack) { throw new CycleException(); } if (!(node in visited)) { stack[node] = true; visited[node] = true; - graph.predecessors(node).forEach(visit); + (_a = graph.predecessors(node)) == null ? void 0 : _a.forEach(visit); delete stack[node]; results.push(node); } @@ -1214,6 +1255,7 @@ var graphlib = (() => { // lib/alg/prim.ts function prim(graph, weightFn) { + var _a; const result = new Graph(); const parents = {}; const pq = new PriorityQueue(); @@ -1236,7 +1278,10 @@ var graphlib = (() => { pq.add(v2, Number.POSITIVE_INFINITY); result.setNode(v2); }); - pq.decrease(graph.nodes()[0], 0); + const firstNode = graph.nodes()[0]; + if (firstNode !== void 0) { + pq.decrease(firstNode, 0); + } let init = false; while (pq.size() > 0) { v = pq.removeMin(); @@ -1247,7 +1292,7 @@ var graphlib = (() => { } else { init = true; } - graph.nodeEdges(v).forEach(updateNeighbors); + (_a = graph.nodeEdges(v)) == null ? void 0 : _a.forEach(updateNeighbors); } return result; } @@ -1259,8 +1304,8 @@ var graphlib = (() => { source, weightFn, edgeFn != null ? edgeFn : ((v) => { - const edges = g.outEdges(v); - return edges != null ? edges : []; + var _a; + return (_a = g.outEdges(v)) != null ? _a : []; }) ); } @@ -1271,10 +1316,13 @@ var graphlib = (() => { let negativeEdgeExists = false; const nodes = g.nodes(); for (let i = 0; i < nodes.length; i++) { - const adjList = edgeFn(nodes[i]); + const node = nodes[i]; + if (node === void 0) continue; + const adjList = edgeFn(node); for (let j = 0; j < adjList.length; j++) { const edge = adjList[j]; - const inVertex = edge.v === nodes[i] ? edge.v : edge.w; + if (!edge) continue; + const inVertex = edge.v === node ? edge.v : edge.w; const outVertex = inVertex === edge.v ? edge.w : edge.v; if (weightFn({ v: inVertex, w: outVertex }) < 0) { negativeEdgeExists = true; diff --git a/dist/graphlib.js.map b/dist/graphlib.js.map index b0024c34..41fefa54 100644 --- a/dist/graphlib.js.map +++ b/dist/graphlib.js.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["../index.ts", "../lib/graph.ts", "../lib/version.ts", "../lib/json.ts", "../lib/alg/index.ts", "../lib/alg/bellman-ford.ts", "../lib/alg/components.ts", "../lib/data/priority-queue.ts", "../lib/alg/dijkstra.ts", "../lib/alg/dijkstra-all.ts", "../lib/alg/tarjan.ts", "../lib/alg/find-cycles.ts", "../lib/alg/floyd-warshall.ts", "../lib/alg/topsort.ts", "../lib/alg/is-acyclic.ts", "../lib/alg/reduce.ts", "../lib/alg/dfs.ts", "../lib/alg/postorder.ts", "../lib/alg/preorder.ts", "../lib/alg/prim.ts", "../lib/alg/shortest-paths.ts"], - "sourcesContent": ["/**\n * Copyright (c) 2014, Chris Pettitt\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n * 1. Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * 2. Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * 3. Neither the name of the copyright holder nor the names of its contributors\n * may be used to endorse or promote products derived from this software without\n * specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\nexport {Graph} from './lib/graph';\nexport {version} from './lib/version';\nexport * as json from './lib/json';\nexport * as alg from './lib/alg/index';\n\nexport type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction} from './lib/types.js';\n", "import type {Edge, EdgeLabelFactory, GraphOptions, NodeLabelFactory} from './types';\n\nconst DEFAULT_EDGE_NAME = \"\\x00\";\nconst GRAPH_NODE = \"\\x00\";\nconst EDGE_KEY_DELIM = \"\\x01\";\n\n// Implementation notes:\n//\n// * Node id query functions should return string ids for the nodes\n// * Edge id query functions should return an \"edgeObj\", edge object, that is\n// composed of enough information to uniquely identify an edge: {v, w, name}.\n// * Internally we use an \"edgeId\", a stringified form of the edgeObj, to\n// reference edges. This is because we need a performant way to look these\n// edges up and, object properties, which have string keys, are the closest\n// we're going to get to a performant hashtable in JavaScript.\n\nexport class Graph {\n private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined | void = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | void {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): void | string[] {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): void | string[] {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): void | string[] {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n for (const succ of this.successors(v)!) {\n union.add(succ);\n }\n\n return Array.from(union.values());\n }\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | void;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return neighbors!.length === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent ?? undefined;\n return parent ?? undefined;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\") {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): void | Edge[] {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.3';\n", "import {Graph} from './graph';\nimport type {GraphOptions} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v)!;\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const edgeWeight = weightFn(edge);\n if (results[edge.v]!.distance + edgeWeight < results[edge.w]!.distance) {\n results[edge.w] = {\n distance: results[edge.v]!.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)!.forEach(dfs);\n graph.predecessors(v)!.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v)!;\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w]!;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n vEntry = results[v]!;\n if (vEntry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)!.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.lowlink);\n } else if (visited[w]!.onStack) {\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.index);\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n visited[w]!.onStack = false;\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {Edge} from '../types';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n // A single-node component is a cycle iff the node has a self-loop. We check via outEdges\n // rather than hasEdge(v, v) because the latter only matches the default (unnamed) edge and\n // would miss a named self-loop edge in a multigraph.\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(cmpt[0]!, cmpt[0]!) as Edge[]).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v)!;\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n results[v] = {};\n results[v]![v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n results[v]![w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n results[v]![w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k]!;\n nodes.forEach(function (i) {\n const rowI = results[i]!;\n nodes.forEach(function (j) {\n const ik = rowI[k]!;\n const kj = rowK[j]!;\n const ij = rowI[j]!;\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(...args: unknown[]) {\n super(...args as ConstructorParameters);\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)!.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: string,\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: string): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n pq.decrease(graph.nodes()[0]!, 0);\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)!.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n const edges = g.outEdges(v);\n return edges ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const adjList = edgeFn(nodes[i]!);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j]!;\n const inVertex = edge.v === nodes[i] ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], - "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,MAAM,oBAAoB;AAC1B,MAAM,aAAa;AACnB,MAAM,iBAAiB;AAYhB,MAAM,QAAN,MAAgE;AAAA,IA4BnE,YAAY,MAAqB;AA3BjC,WAAQ,cAAuB;AAC/B,WAAQ,gBAAyB;AACjC,WAAQ,cAAuB;AAK/B;AAAA,WAAQ,SAAoC,CAAC;AAE7C;AAAA,WAAQ,MAA4C,CAAC;AAErD;AAAA,WAAQ,SAAiD,CAAC;AAE1D;AA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sourcesContent": ["/**\n * Copyright (c) 2014, Chris Pettitt\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n * 1. Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * 2. Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * 3. Neither the name of the copyright holder nor the names of its contributors\n * may be used to endorse or promote products derived from this software without\n * specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\nexport {Graph} from './lib/graph';\nexport {version} from './lib/version';\nexport * as json from './lib/json';\nexport * as alg from './lib/alg/index';\n\nexport type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction, Label} from './lib/types.js';\n", "import type {Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory} from './types';\n\nconst DEFAULT_EDGE_NAME = \"\\x00\";\nconst GRAPH_NODE = \"\\x00\";\nconst EDGE_KEY_DELIM = \"\\x01\";\n\n// Implementation notes:\n//\n// * Node id query functions should return string ids for the nodes\n// * Edge id query functions should return an \"edgeObj\", edge object, that is\n// composed of enough information to uniquely identify an edge: {v, w, name}.\n// * Internally we use an \"edgeId\", a stringified form of the edgeObj, to\n// reference edges. This is because we need a performant way to look these\n// edges up and, object properties, which have string keys, are the closest\n// we're going to get to a performant hashtable in JavaScript.\n\nexport class Graph {\n private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | undefined {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n return undefined;\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): string[] | undefined {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): string[] | undefined {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): string[] | undefined {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n const sucs = this.successors(v);\n if (sucs) {\n for (const succ of sucs) {\n union.add(succ);\n }\n }\n\n return Array.from(union.values());\n }\n return undefined;\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | undefined;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return (neighbors?.length ?? 0) === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent;\n return parent;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\" || edgeLabel === null) {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as unknown as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): Edge[] | undefined {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n return undefined;\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.4-pre';\n", "import {Graph} from './graph';\nimport type {GraphOptions, Label} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v) ?? [];\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const uEntry = results[edge.v];\n const wEntry = results[edge.w];\n if (!uEntry || !wEntry) return;\n const edgeWeight = weightFn(edge);\n if (uEntry.distance + edgeWeight < wEntry.distance) {\n results[edge.w] = {\n distance: uEntry.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)?.forEach(dfs);\n graph.predecessors(v)?.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v) ?? [];\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w];\n if (!wEntry) return;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin();\n const entry = results[v];\n if (!entry || entry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n vEntry = entry;\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)?.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n const wEntry = visited[w];\n if (wEntry) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink);\n }\n } else {\n const wEntry = visited[w];\n if (wEntry?.onStack) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.index);\n }\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n const wEntry = visited[w];\n if (wEntry) {\n wEntry.onStack = false;\n }\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n const firstNode = cmpt[0];\n if (!firstNode) return false;\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(firstNode, firstNode) ?? []).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v) ?? [];\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n const rowV: Record = {};\n results[v] = rowV;\n rowV[v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n rowV[w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n rowV[w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k];\n if (!rowK) return;\n nodes.forEach(function (i) {\n const rowI = results[i];\n if (!rowI) return;\n nodes.forEach(function (j) {\n const ik = rowI[k];\n const kj = rowK[j];\n const ij = rowI[j];\n if (ik && kj && ij) {\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(message?: string) {\n super(message);\n this.name = \"CycleException\";\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)?.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: \"pre\" | \"post\",\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: \"pre\" | \"post\"): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n const firstNode = graph.nodes()[0];\n if (firstNode !== undefined) {\n pq.decrease(firstNode, 0);\n }\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin();\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)?.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n return g.outEdges(v) ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node === undefined) continue;\n const adjList = edgeFn(node);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j];\n if (!edge) continue;\n const inVertex = edge.v === node ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return b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AQ,UAAU,MAAM;AAAA,EAC/C;", "names": ["dfs", "DEFAULT_WEIGHT_FUNC", "v", "dfs", "DEFAULT_WEIGHT_FUNC", "postorder", "v"] } diff --git a/dist/graphlib.min.js b/dist/graphlib.min.js index ec0070a5..07456403 100644 --- a/dist/graphlib.min.js +++ b/dist/graphlib.min.js @@ -1,2 +1,2 @@ -"use strict";var graphlib=(()=>{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});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.3";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+ge.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>1,!(t[i].priority1;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);f0&&(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.outEdges(e[0],e[0]).length>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{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);u0;){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{var F=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var x=(s,e)=>{for(var t in e)F(s,t,{get:e[t],enumerable:!0})},z=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of U(e))!Y.call(s,i)&&i!==t&&F(s,i,{get:()=>e[i],enumerable:!(r=H(e,i))||r.enumerable});return s};var K=s=>z(F({},"__esModule",{value:!0}),s);var oe={};x(oe,{Graph:()=>l,alg:()=>v,json:()=>y,version:()=>I});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),i=this.successors(e);if(i)for(let n of i)r.add(n);return Array.from(r.values())}}isLeaf(e){var r;let t;return this.isDirected()?t=this.successors(e):t=this.neighbors(e),((r=t==null?void 0:t.length)!=null?r:0)===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,o):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,h,c=!1;typeof e=="object"&&e!==null&&"v"in e?(n=e.v,o=e.w,d=e.name,arguments.length===2&&(h=t,c=!0)):(n=e,o=t,d=i,arguments.length>2&&(h=r,c=!0)),n=""+n,o=""+o,d!==void 0&&(d=""+d);let a=_(this._isDirected,n,o,d);if(a in this._edgeLabels)return c&&(this._edgeLabels[a]=h),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[a]=c?h: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[a]=u,k(this._preds[o],n),k(this._sucs[n],o),this._in[o][a]=u,this._out[n][a]=u,this._edgeCount++,this}edge(e,t,r){let i=arguments.length===1?R(this._isDirected,e):_(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"||i===null?{label:i}:i}hasEdge(e,t,r){return(arguments.length===1?R(this._isDirected,e):_(this._isDirected,e,t,r))in this._edgeLabels}removeEdge(e,t,r){let i=arguments.length===1?R(this._isDirected,e):_(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],P(this._preds[d],o),P(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 k(s,e){s[e]?s[e]++:s[e]=1}function P(s,e){s[e]!==void 0&&!--s[e]&&delete s[e]}function _(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 R(s,e){return _(s,e.v,e.w,e.name)}var I="4.0.4-pre";var y={};x(y,{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 v={};x(v,{CycleException:()=>p,bellmanFord:()=>L,components:()=>D,dijkstra:()=>E,dijkstraAll:()=>O,findCycles:()=>j,floydWarshall:()=>C,isAcyclic:()=>T,postorder:()=>S,preorder:()=>M,prim:()=>V,shortestPaths:()=>J,tarjan:()=>w,topsort:()=>N});var Z=()=>1;function L(s,e,t,r){return ee(s,String(e),t||Z,r||function(i){var n;return(n=s.outEdges(i))!=null?n:[]})}function ee(s,e,t,r){let i={},n,o=0,d=s.nodes(),h=function(u){let g=i[u.v],f=i[u.w];if(!g||!f)return;let m=t(u);g.distance+me.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(){if(this.size()===0)throw new Error("Queue underflow");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>1,!(t[i].priority1;function E(s,e,t,r){let i=function(n){var o;return(o=s.outEdges(n))!=null?o:[]};return re(s,String(e),t||te,r||i)}function re(s,e,t,r){let i={},n=new b,o,d,h=function(c){let a=c.v!==o?c.v:c.w,u=i[a];if(!u)return;let 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);f0;){o=n.removeMin();let c=i[o];if(!c||c.distance===Number.POSITIVE_INFINITY)break;d=c,r(o).forEach(h)}return i}function O(s,e,t){return s.nodes().reduce(function(r,i){return r[i]=E(s,i,e,t),r},{})}function w(s){let e=0,t=[],r={},i=[];function n(o){var h;let d=r[o]={onStack:!0,lowlink:e,index:e++};if(t.push(o),(h=s.successors(o))==null||h.forEach(function(c){if(c in r){let a=r[c];a!=null&&a.onStack&&(d.lowlink=Math.min(d.lowlink,a.index))}else{n(c);let a=r[c];a&&(d.lowlink=Math.min(d.lowlink,a.lowlink))}}),d.lowlink===d.index){let c=[],a;do{a=t.pop();let u=r[a];u&&(u.onStack=!1),c.push(a)}while(o!==a);i.push(c)}}return s.nodes().forEach(function(o){o in r||n(o)}),i}function j(s){return w(s).filter(function(e){var r;let t=e[0];return t?e.length>1||e.length===1&&((r=s.outEdges(t,t))!=null?r:[]).length>0:!1})}var ne=()=>1;function C(s,e,t){return ie(s,e||ne,t||function(r){var i;return(i=s.outEdges(r))!=null?i:[]})}function ie(s,e,t){let r={},i=s.nodes();return i.forEach(function(n){let o={};r[n]=o,o[n]={distance:0,predecessor:""},i.forEach(function(d){n!==d&&(o[d]={distance:Number.POSITIVE_INFINITY,predecessor:""})}),t(n).forEach(function(d){let h=d.v===n?d.w:d.v,c=e(d);o[h]={distance:c,predecessor:n}})}),i.forEach(function(n){let o=r[n];o&&i.forEach(function(d){let h=r[d];h&&i.forEach(function(c){let a=h[n],u=o[c],g=h[c];if(a&&u&&g){let f=a.distance+u.distance;f{var h;return(h=s.isDirected()?s.successors(d):s.neighbors(d))!=null?h:[]}),o={};return e.forEach(function(d){if(!s.hasNode(d))throw new Error("Graph does not have node: "+d);i=W(s,d,t==="post",o,n,r,i)}),i}function W(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=W(s,d,t,r,i,n,o)}),t&&(o=n(o,e))),o}function G(s,e,t){return A(s,e,t,function(r,i){return r.push(i),r},[])}function S(s,e){return G(s,e,"post")}function M(s,e){return G(s,e,"pre")}function V(s,e){var c;let t=new l,r={},i=new b,n;function o(a){let u=a.v===n?a.w:a.v,g=i.priority(u);if(g!==void 0){let f=e(a);f0;){if(n=i.removeMin(),n in r)t.setEdge(n,r[n]);else{if(h)throw new Error("Input graph is not connected: "+s);h=!0}(c=s.nodeEdges(n))==null||c.forEach(o)}return t}function J(s,e,t,r){return se(s,e,t,r!=null?r:(i=>{var n;return(n=s.outEdges(i))!=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 private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined | void = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | void {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): void | string[] {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): void | string[] {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): void | string[] {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n for (const succ of this.successors(v)!) {\n union.add(succ);\n }\n\n return Array.from(union.values());\n }\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | void;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return neighbors!.length === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent ?? undefined;\n return parent ?? undefined;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\") {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): void | Edge[] {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): void | Edge[] {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.3';\n", "import {Graph} from './graph';\nimport type {GraphOptions} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v)!;\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const edgeWeight = weightFn(edge);\n if (results[edge.v]!.distance + edgeWeight < results[edge.w]!.distance) {\n results[edge.w] = {\n distance: results[edge.v]!.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)!.forEach(dfs);\n graph.predecessors(v)!.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v)!;\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w]!;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n vEntry = results[v]!;\n if (vEntry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)!.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.lowlink);\n } else if (visited[w]!.onStack) {\n entry.lowlink = Math.min(entry.lowlink, visited[w]!.index);\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n visited[w]!.onStack = false;\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {Edge} from '../types';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n // A single-node component is a cycle iff the node has a self-loop. We check via outEdges\n // rather than hasEdge(v, v) because the latter only matches the default (unnamed) edge and\n // would miss a named self-loop edge in a multigraph.\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(cmpt[0]!, cmpt[0]!) as Edge[]).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v)!;\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n results[v] = {};\n results[v]![v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n results[v]![w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n results[v]![w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k]!;\n nodes.forEach(function (i) {\n const rowI = results[i]!;\n nodes.forEach(function (j) {\n const ik = rowI[k]!;\n const kj = rowK[j]!;\n const ij = rowI[j]!;\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(...args: unknown[]) {\n super(...args as ConstructorParameters);\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)!.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: string,\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: string): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n pq.decrease(graph.nodes()[0]!, 0);\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin()!;\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)!.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n const edges = g.outEdges(v);\n return edges ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const adjList = edgeFn(nodes[i]!);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j]!;\n const inVertex = edge.v === nodes[i] ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], - "mappings": "4bAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,QAAAC,EAAA,SAAAC,EAAA,YAAAC,ICgBO,IAAMC,EAAN,KAAgE,CA4BnE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAAyB,GACjC,KAAQ,YAAuB,GAK/B,KAAQ,OAAoC,CAAC,EAE7C,KAAQ,IAA4C,CAAC,EAErD,KAAQ,OAAiD,CAAC,EAE1D,KAAQ,KAA6C,CAAC,EAEtD,KAAQ,MAAgD,CAAC,EAEzD,KAAQ,UAAkC,CAAC,EAE3C,KAAQ,YAAyC,CAAC,EAElD,KAAQ,WAAqB,EAE7B,KAAQ,WAAqB,EAgvB7B,KAAQ,oBAAmD,IAAG,GAG9D,KAAQ,oBAAmD,IAAG,GA9uBtDA,IACA,KAAK,YAAc,aAAcA,EAAOA,EAAK,SAAY,GACzD,KAAK,cAAgB,eAAgBA,EAAOA,EAAK,WAAc,GAC/D,KAAK,YAAc,aAAcA,EAAOA,EAAK,SAAY,IAGzD,KAAK,cAEL,KAAK,QAAU,CAAC,EAGhB,KAAK,UAAY,CAAC,EAClB,KAAK,UAAU,IAAU,EAAI,CAAC,EAEtC,CAOA,YAAsB,CAClB,OAAO,KAAK,WAChB,CAOA,cAAwB,CACp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EAAIG,EAAK,EAAIA,EAAK,EAC/CE,EAAYD,IAAaD,EAAK,EAAIA,EAAK,EAAIA,EAAK,EAElDX,EAAS,CAAC,EAAGY,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCP,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOQ,EAAYhB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", - "names": ["index_exports", "__export", "Graph", "alg_exports", "json_exports", "version", "Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "succ", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "entry", "w", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "results", "nodes", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "args", "topsort", "graph", "visited", "stack", "results", "visit", "node", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "edges", "dijkstra", "negativeEdgeExists", "nodes", "i", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] + "sourcesContent": ["/**\n * Copyright (c) 2014, Chris Pettitt\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n * 1. Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * 2. Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * 3. Neither the name of the copyright holder nor the names of its contributors\n * may be used to endorse or promote products derived from this software without\n * specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\nexport {Graph} from './lib/graph';\nexport {version} from './lib/version';\nexport * as json from './lib/json';\nexport * as alg from './lib/alg/index';\n\nexport type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction, Label} from './lib/types.js';\n", "import type {Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory} from './types';\n\nconst DEFAULT_EDGE_NAME = \"\\x00\";\nconst GRAPH_NODE = \"\\x00\";\nconst EDGE_KEY_DELIM = \"\\x01\";\n\n// Implementation notes:\n//\n// * Node id query functions should return string ids for the nodes\n// * Edge id query functions should return an \"edgeObj\", edge object, that is\n// composed of enough information to uniquely identify an edge: {v, w, name}.\n// * Internally we use an \"edgeId\", a stringified form of the edgeObj, to\n// reference edges. This is because we need a performant way to look these\n// edges up and, object properties, which have string keys, are the closest\n// we're going to get to a performant hashtable in JavaScript.\n\nexport class Graph {\n private _isDirected: boolean = true;\n private _isMultigraph: boolean = false;\n private _isCompound: boolean = false;\n\n // Label for the graph itself\n private _label!: GraphLabel;\n // v -> label\n private _nodes: Record = {};\n // v -> edgeObj\n private _in: Record> = {};\n // u -> v -> Number\n private _preds: Record> = {};\n // v -> edgeObj\n private _out: Record> = {};\n // v -> w -> Number\n private _sucs: Record> = {};\n // e -> edgeObj\n private _edgeObjs: Record = {};\n // e -> label\n private _edgeLabels: Record = {};\n /* Number of nodes in the graph. Should only be changed by the implementation. */\n private _nodeCount: number = 0;\n /* Number of edges in the graph. Should only be changed by the implementation. */\n private _edgeCount: number = 0;\n private _parent?: Record;\n private _children?: Record>;\n\n constructor(opts?: GraphOptions) {\n if (opts) {\n this._isDirected = \"directed\" in opts ? opts.directed! : true;\n this._isMultigraph = \"multigraph\" in opts ? opts.multigraph! : false;\n this._isCompound = \"compound\" in opts ? opts.compound! : false;\n }\n\n if (this._isCompound) {\n // v -> parent\n this._parent = {};\n\n // v -> children\n this._children = {};\n this._children[GRAPH_NODE] = {};\n }\n }\n\n /**\n * Whether graph was created with 'directed' flag set to true or not.\n *\n * @returns whether the graph edges have an orientation.\n */\n isDirected(): boolean {\n return this._isDirected;\n }\n\n /**\n * Whether graph was created with 'multigraph' flag set to true or not.\n *\n * @returns whether the pair of nodes of the graph can have multiple edges.\n */\n isMultigraph(): boolean {\n return this._isMultigraph;\n }\n\n /* === Graph functions ========= */\n\n /**\n * Whether graph was created with 'compound' flag set to true or not.\n *\n * @returns whether a node of the graph can have subnodes.\n */\n isCompound(): boolean {\n return this._isCompound;\n }\n\n /**\n * Sets the label of the graph.\n *\n * @param label - label value.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setGraph(label: GraphLabel): this {\n this._label = label;\n return this;\n }\n\n /**\n * Gets the graph label.\n *\n * @returns currently assigned label for the graph or undefined if no label assigned.\n */\n graph(): GraphLabel {\n // TODO: This should return undefined if no label was assigned, but that would be a breaking change.\n return this._label;\n }\n\n /**\n * Sets the default node label. This label will be assigned as default label\n * in case if no label was specified while setting a node.\n * Complexity: O(1).\n *\n * @param labelOrFn - default node label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultNodeLabel(labelOrFn: NodeLabel | NodeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultNodeLabelFn = () => labelOrFn;\n } else {\n this._defaultNodeLabelFn = labelOrFn as NodeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of nodes in the graph.\n * Complexity: O(1).\n *\n * @returns nodes count.\n */\n nodeCount(): number {\n return this._nodeCount;\n }\n\n\n /* === Node functions ========== */\n\n /**\n * Gets all nodes of the graph. Note, the in case of compound graph subnodes are\n * not included in list.\n * Complexity: O(1).\n *\n * @returns list of graph nodes.\n */\n nodes(): string[] {\n return Object.keys(this._nodes);\n }\n\n /**\n * Gets list of nodes without in-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph source nodes.\n */\n sources(): string[] {\n return this.nodes().filter(v => Object.keys(this._in[v]!).length === 0);\n }\n\n /**\n * Gets list of nodes without out-edges.\n * Complexity: O(|V|).\n *\n * @returns the graph sink nodes.\n */\n sinks(): string[] {\n return this.nodes().filter(v => Object.keys(this._out[v]!).length === 0);\n }\n\n /**\n * Invokes setNode method for each node in names list.\n * Complexity: O(|names|).\n *\n * @param names - list of nodes names to be set.\n * @param label - value to set for each node in list.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNodes(names: string[], label?: NodeLabel): this {\n names.forEach((v) => {\n if (label !== undefined) {\n this.setNode(v, label);\n } else {\n this.setNode(v);\n }\n });\n return this;\n }\n\n /**\n * Creates or updates the value for the node v in the graph. If label is supplied\n * it is set as the value for the node. If label is not supplied and the node was\n * created by this call then the default node label will be assigned.\n * Complexity: O(1).\n *\n * @param name - node name.\n * @param label - value to set for node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setNode(name: string, label?: NodeLabel): this {\n if (name in this._nodes) {\n if (arguments.length > 1) {\n this._nodes[name] = label!;\n }\n return this;\n }\n\n this._nodes[name] = arguments.length > 1 ? label! : this._defaultNodeLabelFn(name);\n if (this._isCompound) {\n this._parent![name] = GRAPH_NODE;\n this._children![name] = {};\n this._children![GRAPH_NODE]![name] = true;\n }\n this._in[name] = {};\n this._preds[name] = {};\n this._out[name] = {};\n this._sucs[name] = {};\n ++this._nodeCount;\n return this;\n }\n\n /**\n * Gets the label of node with specified name.\n * Complexity: O(|V|).\n *\n * @param name - node name.\n * @returns label value of the node.\n */\n node(name: string): NodeLabel {\n // TODO: This should return undefined if the node doesn't exist, but that would be a breaking change.\n return this._nodes[name]!;\n }\n\n /**\n * Detects whether graph has a node with specified name or not.\n *\n * @param name - name of the node.\n * @returns true if graph has node with specified name, false - otherwise.\n */\n hasNode(name: string): boolean {\n return name in this._nodes;\n }\n\n /**\n * Remove the node with the name from the graph or do nothing if the node is not in\n * the graph. If the node was removed this function also removes any incident\n * edges.\n * Complexity: O(1).\n *\n * @param name - name of the node.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeNode(name: string): this {\n if (name in this._nodes) {\n const removeEdge = (e: string) => this.removeEdge(this._edgeObjs[e]!);\n delete this._nodes[name];\n if (this._isCompound) {\n this._removeFromParentsChildList(name);\n delete this._parent![name];\n this.children(name).forEach((child) => {\n this.setParent(child);\n });\n delete this._children![name];\n }\n Object.keys(this._in[name]!).forEach(removeEdge);\n delete this._in[name];\n delete this._preds[name];\n Object.keys(this._out[name]!).forEach(removeEdge);\n delete this._out[name];\n delete this._sucs[name];\n --this._nodeCount;\n }\n return this;\n }\n\n /**\n * Sets node parent for node v if it is defined, or removes the\n * parent for v if p is undefined. Method throws an exception in case of\n * invoking it in context of noncompound graph.\n * Average-case complexity: O(1).\n *\n * @param v - node to be child for p.\n * @param parent - node to be parent for v.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setParent(v: string, parent?: string): this {\n if (!this._isCompound) {\n throw new Error(\"Cannot set parent in a non-compound graph\");\n }\n\n if (parent === undefined) {\n parent = GRAPH_NODE;\n } else {\n // Coerce parent to string\n parent += \"\";\n for (let ancestor: string | undefined = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {\n if (ancestor === v) {\n throw new Error(\"Setting \" + parent + \" as parent of \" + v +\n \" would create a cycle\");\n }\n }\n\n this.setNode(parent);\n }\n\n this.setNode(v);\n this._removeFromParentsChildList(v);\n this._parent![v] = parent;\n this._children![parent]![v] = true;\n return this;\n }\n\n /**\n * Gets parent node for node v.\n * Complexity: O(1).\n *\n * @param v - node to get parent of.\n * @returns parent node name or void if v has no parent.\n */\n parent(v: string): string | undefined {\n if (this._isCompound) {\n const parent = this._parent![v];\n if (parent !== GRAPH_NODE) {\n return parent;\n }\n }\n return undefined;\n }\n\n /**\n * Gets list of direct children of node v.\n * Complexity: O(1).\n *\n * @param v - node to get children of.\n * @returns children nodes names list.\n */\n children(v: string = GRAPH_NODE): string[] {\n if (this._isCompound) {\n const children = this._children![v];\n if (children) {\n return Object.keys(children);\n }\n } else if (v === GRAPH_NODE) {\n return this.nodes();\n } else if (this.hasNode(v)) {\n return [];\n }\n return [];\n }\n\n /**\n * Return all nodes that are predecessors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n predecessors(v: string): string[] | undefined {\n const predsV = this._preds[v];\n if (predsV) {\n return Object.keys(predsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are successors of the specified node or undefined if node v is not in\n * the graph. Behavior is undefined for undirected graphs - use neighbors instead.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n successors(v: string): string[] | undefined {\n const sucsV = this._sucs[v];\n if (sucsV) {\n return Object.keys(sucsV);\n }\n return undefined;\n }\n\n /**\n * Return all nodes that are predecessors or successors of the specified node or undefined if\n * node v is not in the graph.\n * Complexity: O(|V|).\n *\n * @param v - node identifier.\n * @returns node identifiers list or undefined if v is not in the graph.\n */\n neighbors(v: string): string[] | undefined {\n const preds = this.predecessors(v);\n if (preds) {\n const union = new Set(preds);\n const sucs = this.successors(v);\n if (sucs) {\n for (const succ of sucs) {\n union.add(succ);\n }\n }\n\n return Array.from(union.values());\n }\n return undefined;\n }\n\n isLeaf(v: string): boolean {\n let neighbors: string[] | undefined;\n if (this.isDirected()) {\n neighbors = this.successors(v);\n } else {\n neighbors = this.neighbors(v);\n }\n return (neighbors?.length ?? 0) === 0;\n }\n\n /**\n * Creates new graph with nodes filtered via filter. Edges incident to rejected node\n * are also removed. In case of compound graph, if parent is rejected by filter,\n * than all its children are rejected too.\n * Average-case complexity: O(|E|+|V|).\n *\n * @param filter - filtration function detecting whether the node should stay or not.\n * @returns new graph made from current and nodes filtered.\n */\n filterNodes(filter: (v: string) => boolean): this {\n const copy = new (this.constructor as typeof Graph)({\n directed: this._isDirected,\n multigraph: this._isMultigraph,\n compound: this._isCompound\n });\n\n copy.setGraph(this.graph()!);\n\n Object.entries(this._nodes).forEach(([v, value]) => {\n if (filter(v)) {\n copy.setNode(v, value);\n }\n });\n\n Object.values(this._edgeObjs).forEach((e) => {\n if (copy.hasNode(e.v) && copy.hasNode(e.w)) {\n copy.setEdge(e, this.edge(e));\n }\n });\n\n const parents: Record = {};\n const findParent = (v: string): string | undefined => {\n const parent = this.parent(v);\n if (!parent || copy.hasNode(parent)) {\n parents[v] = parent;\n return parent;\n } else if (parent in parents) {\n return parents[parent];\n } else {\n return findParent(parent);\n }\n };\n\n if (this._isCompound) {\n copy.nodes().forEach(v => copy.setParent(v, findParent(v)));\n }\n\n return copy as this;\n }\n\n /**\n * Sets the default edge label. This label will be assigned as default label\n * in case if no label was specified while setting an edge.\n * Complexity: O(1).\n *\n * @param labelOrFn - default edge label or label factory function.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setDefaultEdgeLabel(labelOrFn: EdgeLabel | EdgeLabelFactory): this {\n if (typeof labelOrFn !== 'function') {\n this._defaultEdgeLabelFn = () => labelOrFn;\n } else {\n this._defaultEdgeLabelFn = labelOrFn as EdgeLabelFactory;\n }\n\n return this;\n }\n\n /**\n * Gets the number of edges in the graph.\n * Complexity: O(1).\n *\n * @returns edges count.\n */\n edgeCount(): number {\n return this._edgeCount;\n }\n\n /**\n * Gets edges of the graph. In case of compound graph subgraphs are not considered.\n * Complexity: O(|E|).\n *\n * @returns graph edges list.\n */\n edges(): Edge[] {\n return Object.values(this._edgeObjs);\n }\n\n /* === Edge functions ========== */\n\n /**\n * Establish an edges path over the nodes in nodes list. If some edge is already\n * exists, it will update its label, otherwise it will create an edge between pair\n * of nodes with label provided or default label if no label provided.\n * Complexity: O(|nodes|).\n *\n * @param nodes - list of nodes to be connected in series.\n * @param label - value to set for each edge between pairs of nodes.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setPath(nodes: string[], label?: EdgeLabel): this {\n nodes.reduce((v, w) => {\n if (label !== undefined) {\n this.setEdge(v, w, label);\n } else {\n this.setEdge(v, w);\n }\n return w;\n });\n return this;\n }\n\n /**\n * Creates or updates the label for the edge (v, w) with the optionally supplied\n * name. If label is supplied it is set as the value for the edge. If label is not\n * supplied and the edge was created by this call then the default edge label will\n * be assigned. The name parameter is only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param label - value to associate with the edge.\n * @param name - unique name of the edge in order to identify it in multigraph.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(v: string, w: string, label?: EdgeLabel, name?: string): this;\n\n /**\n * Creates or updates the label for the specified edge. If label is supplied it is\n * set as the value for the edge. If label is not supplied and the edge was created\n * by this call then the default edge label will be assigned. The name parameter is\n * only useful with multigraphs.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @param label - value to associate with the edge.\n * @returns the graph, allowing this to be chained with other functions.\n */\n setEdge(edge: Edge, label?: EdgeLabel): this;\n\n setEdge(v: string | Edge, w?: string | EdgeLabel, value?: EdgeLabel, name?: string): this {\n let vStr: string;\n let wStr: string;\n let nameStr: string | undefined;\n let edgeValue: EdgeLabel | undefined;\n let valueSpecified = false;\n\n if (typeof v === \"object\" && v !== null && \"v\" in v) {\n vStr = v.v;\n wStr = v.w;\n nameStr = v.name;\n if (arguments.length === 2) {\n edgeValue = w as EdgeLabel;\n valueSpecified = true;\n }\n } else {\n vStr = v;\n wStr = w as string;\n nameStr = name;\n if (arguments.length > 2) {\n edgeValue = value;\n valueSpecified = true;\n }\n }\n\n vStr = \"\" + vStr;\n wStr = \"\" + wStr;\n if (nameStr !== undefined) {\n nameStr = \"\" + nameStr;\n }\n\n const e = edgeArgsToId(this._isDirected, vStr, wStr, nameStr);\n if (e in this._edgeLabels) {\n if (valueSpecified) {\n this._edgeLabels[e] = edgeValue!;\n }\n return this;\n }\n\n if (nameStr !== undefined && !this._isMultigraph) {\n throw new Error(\"Cannot set a named edge when isMultigraph = false\");\n }\n\n // It didn't exist, so we need to create it.\n // First ensure the nodes exist.\n this.setNode(vStr);\n this.setNode(wStr);\n\n this._edgeLabels[e] = valueSpecified ? edgeValue! : this._defaultEdgeLabelFn(vStr, wStr, nameStr);\n\n // Ensure we add undirected edges in a consistent way.\n const edgeObj = edgeArgsToObj(this._isDirected, vStr, wStr, nameStr);\n\n vStr = edgeObj.v;\n wStr = edgeObj.w;\n\n Object.freeze(edgeObj);\n this._edgeObjs[e] = edgeObj;\n incrementOrInitEntry(this._preds[wStr]!, vStr);\n incrementOrInitEntry(this._sucs[vStr]!, wStr);\n this._in[wStr]![e] = edgeObj;\n this._out[vStr]![e] = edgeObj;\n this._edgeCount++;\n return this;\n }\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edge(v: string, w: string, name?: string): EdgeLabel;\n\n /**\n * Gets the label for the specified edge.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edge(edge: Edge): EdgeLabel;\n\n edge(v: string | Edge, w?: string, name?: string): EdgeLabel {\n // TODO: This should return undefined if the edge doesn't exist, but that would be a breaking change.\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return this._edgeLabels[e]!;\n }\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns value associated with specified edge.\n */\n edgeAsObj(v: string, w: string, name?: string): { label: EdgeLabel };\n\n /**\n * Gets the label for the specified edge and converts it to an object.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns value associated with specified edge.\n */\n edgeAsObj(edge: Edge): { label: EdgeLabel };\n\n edgeAsObj(v: string | Edge, w?: string, name?: string): { label: EdgeLabel } {\n const edgeLabel = arguments.length === 1\n ? this.edge(v as Edge)\n : this.edge(v as string, w!, name);\n\n if (typeof edgeLabel !== \"object\" || edgeLabel === null) {\n return {label: edgeLabel as EdgeLabel};\n }\n\n return edgeLabel as unknown as { label: EdgeLabel };\n }\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(v: string, w: string, name?: string): boolean;\n\n /**\n * Detects whether the graph contains specified edge or not. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns whether the graph contains the specified edge or not.\n */\n hasEdge(edge: Edge): boolean;\n\n hasEdge(v: string | Edge, w?: string, name?: string): boolean {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n return e in this._edgeLabels;\n }\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @param name - name of the edge (actual for multigraph).\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(v: string, w: string, name?: string): this;\n\n /**\n * Removes the specified edge from the graph. No subgraphs are considered.\n * Complexity: O(1).\n *\n * @param edge - edge descriptor.\n * @returns the graph, allowing this to be chained with other functions.\n */\n removeEdge(edge: Edge): this;\n\n removeEdge(v: string | Edge, w?: string, name?: string): this {\n const e = (arguments.length === 1\n ? edgeObjToId(this._isDirected, v as Edge)\n : edgeArgsToId(this._isDirected, v as string, w!, name));\n const edge = this._edgeObjs[e];\n if (edge) {\n const vStr = edge.v;\n const wStr = edge.w;\n delete this._edgeLabels[e];\n delete this._edgeObjs[e];\n decrementOrRemoveEntry(this._preds[wStr]!, vStr);\n decrementOrRemoveEntry(this._sucs[vStr]!, wStr);\n delete this._in[wStr]![e];\n delete this._out[vStr]![e];\n this._edgeCount--;\n }\n return this;\n }\n\n /**\n * Return all edges that point to the node v. Optionally filters those edges down to just those\n * coming from node u. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge sink node.\n * @param w - edge source node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n inEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._in[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Return all edges that are pointed at by node v. Optionally filters those edges down to just\n * those point to w. Behavior is void for undirected graphs - use nodeEdges instead.\n * Complexity: O(|E|).\n *\n * @param v - edge source node.\n * @param w - edge sink node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n outEdges(v: string, w?: string): Edge[] | undefined {\n if (this.isDirected()) {\n return this.filterEdges(this._out[v], v, w);\n }\n return this.nodeEdges(v, w);\n }\n\n /**\n * Returns all edges to or from node v regardless of direction. Optionally filters those edges\n * down to just those between nodes v and w regardless of direction.\n * Complexity: O(|E|).\n *\n * @param v - edge adjacent node.\n * @param w - edge adjacent node.\n * @returns edges descriptors list if v is in the graph, or void otherwise.\n */\n nodeEdges(v: string, w?: string): Edge[] | undefined {\n if (v in this._nodes) {\n return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w);\n }\n return undefined;\n }\n\n // Defaults to be set when creating a new node\n private _defaultNodeLabelFn: NodeLabelFactory = () => undefined as NodeLabel;\n\n // Defaults to be set when creating a new edge\n private _defaultEdgeLabelFn: EdgeLabelFactory = () => undefined as EdgeLabel;\n\n private _removeFromParentsChildList(v: string): void {\n delete this._children![this._parent![v]!]![v];\n }\n\n private filterEdges(setV: Record | undefined, localEdge: string, remoteEdge?: string): Edge[] | undefined {\n if (!setV) {\n return;\n }\n const edges = Object.values(setV);\n if (!remoteEdge) {\n return edges;\n }\n return edges.filter((edge) => {\n return edge.v === localEdge && edge.w === remoteEdge\n || edge.v === remoteEdge && edge.w === localEdge;\n });\n }\n}\n\n\nfunction incrementOrInitEntry(map: Record, k: string): void {\n if (map[k]) {\n map[k]++;\n } else {\n map[k] = 1;\n }\n}\n\nfunction decrementOrRemoveEntry(map: Record, k: string): void {\n if (map[k] !== undefined && !--map[k]) {\n delete map[k];\n }\n}\n\nfunction edgeArgsToId(isDirected: boolean, v_: string, w_: string, name?: string): string {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +\n (name === undefined ? DEFAULT_EDGE_NAME : name);\n}\n\nfunction edgeArgsToObj(isDirected: boolean, v_: string, w_: string, name?: string): Edge {\n let v = \"\" + v_;\n let w = \"\" + w_;\n if (!isDirected && v > w) {\n const tmp = v;\n v = w;\n w = tmp;\n }\n const edgeObj: Edge = {v: v, w: w};\n if (name) {\n edgeObj.name = name;\n }\n return edgeObj;\n}\n\nfunction edgeObjToId(isDirected: boolean, edgeObj: Edge): string {\n return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);\n}\n", "export const version = '4.0.4-pre';\n", "import {Graph} from './graph';\nimport type {GraphOptions, Label} from './types';\n\ninterface JsonGraph {\n options: GraphOptions;\n nodes: JsonNode[];\n edges: JsonEdge[];\n value?: unknown;\n}\n\ninterface JsonNode {\n v: string;\n value?: unknown;\n parent?: string;\n}\n\ninterface JsonEdge {\n v: string;\n w: string;\n name?: string;\n value?: unknown;\n}\n\n/**\n * Creates a JSON representation of the graph that can be serialized to a string with\n * JSON.stringify. The graph can later be restored using json.read.\n *\n * @param graph - target to create JSON representation of.\n * @returns JSON serializable graph representation\n */\nexport function write(graph: Graph): JsonGraph {\n const json: JsonGraph = {\n options: {\n directed: graph.isDirected(),\n multigraph: graph.isMultigraph(),\n compound: graph.isCompound()\n },\n nodes: writeNodes(graph),\n edges: writeEdges(graph)\n };\n\n const graphLabel = graph.graph();\n if (graphLabel !== undefined) {\n json.value = structuredClone(graphLabel);\n }\n\n return json;\n}\n\nfunction writeNodes(g: Graph): JsonNode[] {\n return g.nodes().map(v => {\n const nodeValue = g.node(v);\n const parent = g.parent(v);\n const node: JsonNode = {v};\n\n if (nodeValue !== undefined) {\n node.value = nodeValue;\n }\n if (parent !== undefined) {\n node.parent = parent;\n }\n\n return node;\n });\n}\n\nfunction writeEdges(g: Graph): JsonEdge[] {\n return g.edges().map(e => {\n const edgeValue = g.edge(e);\n const edge: JsonEdge = {v: e.v, w: e.w};\n\n if (e.name !== undefined) {\n edge.name = e.name;\n }\n if (edgeValue !== undefined) {\n edge.value = edgeValue;\n }\n\n return edge;\n });\n}\n\n/**\n * Takes JSON as input and returns the graph representation.\n *\n * @param json - JSON serializable graph representation\n * @returns graph constructed according to specified representation\n *\n * @example\n * var g2 = graphlib.json.read(JSON.parse(str));\n * g2.nodes();\n * // ['a', 'b']\n * g2.edges()\n * // [ { v: 'a', w: 'b' } ]\n */\nexport function read(\n json: JsonGraph\n): Graph {\n const g = new Graph(json.options);\n\n if (json.value !== undefined) {\n g.setGraph(json.value as GraphLabel);\n }\n\n json.nodes.forEach(entry => {\n g.setNode(entry.v, entry.value as NodeLabel);\n if (entry.parent) {\n g.setParent(entry.v, entry.parent);\n }\n });\n\n json.edges.forEach(entry => {\n g.setEdge({v: entry.v, w: entry.w, name: entry.name}, entry.value as EdgeLabel);\n });\n\n return g;\n}\n", "export {bellmanFord} from './bellman-ford';\nexport {components} from './components';\nexport {dijkstra} from './dijkstra';\nexport {dijkstraAll} from './dijkstra-all';\nexport {findCycles} from './find-cycles';\nexport {floydWarshall} from './floyd-warshall';\nexport {isAcyclic} from './is-acyclic';\nexport {postorder} from './postorder';\nexport {preorder} from './preorder';\nexport {prim} from './prim';\nexport {shortestPaths} from './shortest-paths';\nexport {tarjan} from './tarjan';\nexport {topsort, CycleException} from './topsort';\n", "import {Graph} from '../graph';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\nexport function bellmanFord(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runBellmanFord(\n g,\n String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return g.outEdges(v) ?? [];\n }\n );\n}\n\nfunction runBellmanFord(\n g: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n let didADistanceUpgrade: boolean;\n let iterations = 0;\n const nodes = g.nodes();\n\n const relaxEdge = function (edge: Edge): void {\n const uEntry = results[edge.v];\n const wEntry = results[edge.w];\n if (!uEntry || !wEntry) return;\n const edgeWeight = weightFn(edge);\n if (uEntry.distance + edgeWeight < wEntry.distance) {\n results[edge.w] = {\n distance: uEntry.distance + edgeWeight,\n predecessor: edge.v\n };\n didADistanceUpgrade = true;\n }\n };\n\n const relaxAllEdges = function (): void {\n nodes.forEach(function (vertex) {\n edgeFn(vertex).forEach(function (edge) {\n // If the vertex on which the edgeFun in called is\n // the edge.w, then we treat the edge as if it was reversed\n const inVertex = edge.v === vertex ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n relaxEdge({v: inVertex, w: outVertex});\n });\n });\n };\n\n // Initialization\n nodes.forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n });\n\n const numberOfNodes = nodes.length;\n\n // Relax all edges in |V|-1 iterations\n for (let i = 1; i < numberOfNodes; i++) {\n didADistanceUpgrade = false;\n iterations++;\n relaxAllEdges();\n if (!didADistanceUpgrade) {\n // \u0399f no update was made in an iteration, Bellman-Ford has finished\n break;\n }\n }\n\n // Detect if the graph contains a negative weight cycle\n if (iterations === numberOfNodes - 1) {\n didADistanceUpgrade = false;\n relaxAllEdges();\n if (didADistanceUpgrade) {\n throw new Error(\"The graph contains a negative weight cycle\");\n }\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\n\n/**\n * Finds all connected components in a graph and returns an array of these components.\n * Each component is itself an array that contains the ids of nodes in the component.\n * Complexity: O(|V|).\n *\n * @param graph - graph to find components in.\n * @returns array of nodes list representing components\n */\nexport function components(graph: Graph): string[][] {\n const visited: Record = {};\n const cmpts: string[][] = [];\n let cmpt: string[];\n\n function dfs(v: string): void {\n if (v in visited) return;\n visited[v] = true;\n cmpt.push(v);\n graph.successors(v)?.forEach(dfs);\n graph.predecessors(v)?.forEach(dfs);\n }\n\n graph.nodes().forEach(function (v) {\n cmpt = [];\n dfs(v);\n if (cmpt.length) {\n cmpts.push(cmpt);\n }\n });\n\n return cmpts;\n}\n", "/**\n * A min-priority queue data structure. This algorithm is derived from Cormen,\n * et al., \"Introduction to Algorithms\". The basic idea of a min-priority\n * queue is that you can efficiently (in O(1) time) get the smallest key in\n * the queue. Adding and removing elements takes O(log n) time. A key can\n * have its priority decreased in O(log n) time.\n */\n\ninterface PriorityQueueEntry {\n key: string;\n priority: number;\n}\n\nexport class PriorityQueue {\n private _arr: PriorityQueueEntry[] = [];\n private _keyIndices: Record = {};\n\n /**\n * Returns the number of elements in the queue. Takes `O(1)` time.\n */\n size(): number {\n return this._arr.length;\n }\n\n /**\n * Returns the keys that are in the queue. Takes `O(n)` time.\n */\n keys(): string[] {\n return this._arr.map(x => x.key);\n }\n\n /**\n * Returns `true` if **key** is in the queue and `false` if not.\n */\n has(key: string): boolean {\n return key in this._keyIndices;\n }\n\n /**\n * Returns the priority for **key**. If **key** is not present in the queue\n * then this function returns `undefined`. Takes `O(1)` time.\n */\n priority(key: string): number | undefined {\n const index = this._keyIndices[key];\n if (index !== undefined) {\n return this._arr[index]!.priority;\n }\n return undefined;\n }\n\n /**\n * Returns the key for the minimum element in this queue. If the queue is\n * empty this function throws an Error. Takes `O(1)` time.\n */\n min(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n return this._arr[0]!.key;\n }\n\n /**\n * Inserts a new key into the priority queue. If the key already exists in\n * the queue this function returns `false`; otherwise it will return `true`.\n * Takes `O(n)` time.\n */\n add(key: string, priority: number): boolean {\n const keyIndices = this._keyIndices;\n const keyStr = String(key);\n\n if (!(keyStr in keyIndices)) {\n const arr = this._arr;\n const index = arr.length;\n keyIndices[keyStr] = index;\n arr.push({key: keyStr, priority});\n this._decrease(index);\n return true;\n }\n return false;\n }\n\n /**\n * Removes and returns the smallest key in the queue. Takes `O(log n)` time.\n */\n removeMin(): string {\n if (this.size() === 0) {\n throw new Error(\"Queue underflow\");\n }\n this._swap(0, this._arr.length - 1);\n const min = this._arr.pop()!;\n delete this._keyIndices[min.key];\n this._heapify(0);\n return min.key;\n }\n\n /**\n * Decreases the priority for **key** to **priority**. If the new priority is\n * greater than the previous priority, this function will throw an Error.\n */\n decrease(key: string, priority: number): void {\n const index = this._keyIndices[key];\n if (index === undefined) {\n throw new Error(`Key not found: ${key}`);\n }\n\n const currentPriority = this._arr[index]!.priority;\n if (priority > currentPriority) {\n throw new Error(\n `New priority is greater than current priority. Key: ${key} Old: ${currentPriority} New: ${priority}`\n );\n }\n this._arr[index]!.priority = priority;\n this._decrease(index);\n }\n\n private _heapify(i: number): void {\n const arr = this._arr;\n const l = 2 * i;\n const r = l + 1;\n let largest = i;\n\n if (l < arr.length) {\n largest = arr[l]!.priority < arr[largest]!.priority ? l : largest;\n if (r < arr.length) {\n largest = arr[r]!.priority < arr[largest]!.priority ? r : largest;\n }\n if (largest !== i) {\n this._swap(i, largest);\n this._heapify(largest);\n }\n }\n }\n\n private _decrease(index: number): void {\n const arr = this._arr;\n const priority = arr[index]!.priority;\n let parent: number;\n\n while (index !== 0) {\n parent = index >> 1;\n if (arr[parent]!.priority < priority) {\n break;\n }\n this._swap(index, parent);\n index = parent;\n }\n }\n\n private _swap(i: number, j: number): void {\n const arr = this._arr;\n const keyIndices = this._keyIndices;\n const origArrI = arr[i]!;\n const origArrJ = arr[j]!;\n\n arr[i] = origArrJ;\n arr[j] = origArrI;\n keyIndices[origArrJ.key] = i;\n keyIndices[origArrI.key] = j;\n }\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of Dijkstra's algorithm which finds the shortest\n * path from source to all other nodes in graph. This function returns a map of\n * v -> { distance, predecessor }. The distance property holds the sum of the weights\n * from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path\n * from source. The predecessor property can be used to walk the individual elements of the\n * path from source to v in reverse order.\n * Complexity: O((|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param source - node to start paths from.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map that starts from node source\n */\nexport function dijkstra(\n graph: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n const defaultEdgeFn: EdgeFunction = function (v) {\n return graph.outEdges(v) ?? [];\n };\n\n return runDijkstra(graph, String(source),\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || defaultEdgeFn);\n}\n\nfunction runDijkstra(\n graph: Graph,\n source: string,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record {\n const results: Record = {};\n const pq = new PriorityQueue();\n let v: string, vEntry: Path;\n\n const updateNeighbors = function (edge: Edge): void {\n const w = edge.v !== v ? edge.v : edge.w;\n const wEntry = results[w];\n if (!wEntry) return;\n const weight = weightFn(edge);\n const distance = vEntry.distance + weight;\n\n if (weight < 0) {\n throw new Error(\"dijkstra does not allow negative edge weights. \" +\n \"Bad edge: \" + edge + \" Weight: \" + weight);\n }\n\n if (distance < wEntry.distance) {\n wEntry.distance = distance;\n wEntry.predecessor = v;\n pq.decrease(w, distance);\n }\n };\n\n graph.nodes().forEach(function (v) {\n const distance = v === source ? 0 : Number.POSITIVE_INFINITY;\n results[v] = {distance: distance, predecessor: ''};\n pq.add(v, distance);\n });\n\n while (pq.size() > 0) {\n v = pq.removeMin();\n const entry = results[v];\n if (!entry || entry.distance === Number.POSITIVE_INFINITY) {\n break;\n }\n vEntry = entry;\n\n edgeFn(v).forEach(updateNeighbors);\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\nimport {dijkstra} from './dijkstra';\n\n/**\n * This function finds the shortest path from each node to every other reachable node in\n * the graph. It is similar to alg.dijkstra, but instead of returning a single-source\n * array, it returns a mapping of source -> alg.dijkstra(g, source, weightFn, edgeFn).\n * Complexity: O(|V| * (|E| + |V|) * log |V|).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function dijkstraAll(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return graph.nodes().reduce(function (acc, v) {\n acc[v] = dijkstra(graph, v, weightFn, edgeFn);\n return acc;\n }, {} as Record>);\n}\n", "import {Graph} from '../graph';\n\ninterface VisitedEntry {\n onStack: boolean;\n lowlink: number;\n index: number;\n}\n\n/**\n * This function is an implementation of Tarjan's algorithm which finds all strongly connected\n * components in the directed graph g. Each strongly connected component is composed of nodes that\n * can reach all other nodes in the component via directed edges. A strongly connected component\n * can consist of a single node if that node cannot both reach and be reached by any other\n * specific node in the graph. Components of more than one node are guaranteed to have at least\n * one cycle.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to find all strongly connected components of.\n * @returns an array of components. Each component is itself an array that contains\n * the ids of all nodes in the component.\n */\nexport function tarjan(graph: Graph): string[][] {\n let index = 0;\n const stack: string[] = [];\n const visited: Record = {}; // node id -> { onStack, lowlink, index }\n const results: string[][] = [];\n\n function dfs(v: string): void {\n const entry = visited[v] = {\n onStack: true,\n lowlink: index,\n index: index++\n };\n stack.push(v);\n\n graph.successors(v)?.forEach(function (w) {\n if (!(w in visited)) {\n dfs(w);\n const wEntry = visited[w];\n if (wEntry) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink);\n }\n } else {\n const wEntry = visited[w];\n if (wEntry?.onStack) {\n entry.lowlink = Math.min(entry.lowlink, wEntry.index);\n }\n }\n });\n\n if (entry.lowlink === entry.index) {\n const cmpt: string[] = [];\n let w: string;\n do {\n w = stack.pop()!;\n const wEntry = visited[w];\n if (wEntry) {\n wEntry.onStack = false;\n }\n cmpt.push(w);\n } while (v !== w);\n results.push(cmpt);\n }\n }\n\n graph.nodes().forEach(function (v) {\n if (!(v in visited)) {\n dfs(v);\n }\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {tarjan} from './tarjan';\n\n/**\n * Given a Graph, graph, this function returns all nodes that are part of a cycle. As there\n * may be more than one cycle in a graph this function return an array of these cycles,\n * where each cycle is itself represented by an array of ids for each node involved in\n * that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a\n * cycle or not.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph where to search cycles.\n * @returns cycles list.\n */\nexport function findCycles(graph: Graph): string[][] {\n return tarjan(graph).filter(function (cmpt) {\n const firstNode = cmpt[0];\n if (!firstNode) return false;\n return cmpt.length > 1\n || (cmpt.length === 1 && (graph.outEdges(firstNode, firstNode) ?? []).length > 0);\n });\n}\n", "import {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nconst DEFAULT_WEIGHT_FUNC: WeightFunction = () => 1;\n\n/**\n * This function is an implementation of the Floyd-Warshall algorithm, which finds the\n * shortest path from each node to every other reachable node in the graph. It is similar\n * to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types\n * of graphs. This function returns a map of source -> { target -> { distance, predecessor }.\n * The distance property holds the sum of the weights from source to target along the shortest\n * path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property\n * can be used to walk the individual elements of the path from source to target in reverse\n * order.\n * Complexity: O(|V|^3).\n *\n * @param graph - graph where to search paths.\n * @param weightFn - function which takes edge e and returns the weight of it. If no weightFn\n * is supplied then each edge is assumed to have a weight of 1. This function throws an\n * Error if any of the traversed edges have a negative edge weight.\n * @param edgeFn - function which takes a node v and returns the ids of all edges incident to it\n * for the purposes of shortest path traversal. By default this function uses the graph.outEdges.\n * @returns shortest paths map.\n */\nexport function floydWarshall(\n graph: Graph,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record> {\n return runFloydWarshall(graph,\n weightFn || DEFAULT_WEIGHT_FUNC,\n edgeFn || function (v) {\n return graph.outEdges(v) ?? [];\n });\n}\n\nfunction runFloydWarshall(\n graph: Graph,\n weightFn: WeightFunction,\n edgeFn: EdgeFunction\n): Record> {\n const results: Record> = {};\n const nodes = graph.nodes();\n\n nodes.forEach(function (v) {\n const rowV: Record = {};\n results[v] = rowV;\n rowV[v] = {distance: 0, predecessor: ''};\n nodes.forEach(function (w) {\n if (v !== w) {\n rowV[w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''};\n }\n });\n edgeFn(v).forEach(function (edge) {\n const w = edge.v === v ? edge.w : edge.v;\n const d = weightFn(edge);\n rowV[w] = {distance: d, predecessor: v};\n });\n });\n\n nodes.forEach(function (k) {\n const rowK = results[k];\n if (!rowK) return;\n nodes.forEach(function (i) {\n const rowI = results[i];\n if (!rowI) return;\n nodes.forEach(function (j) {\n const ik = rowI[k];\n const kj = rowK[j];\n const ij = rowI[j];\n if (ik && kj && ij) {\n const altDistance = ik.distance + kj.distance;\n if (altDistance < ij.distance) {\n ij.distance = altDistance;\n ij.predecessor = kj.predecessor;\n }\n }\n });\n });\n });\n\n return results;\n}\n", "import {Graph} from '../graph';\n\nexport class CycleException extends Error {\n constructor(message?: string) {\n super(message);\n this.name = \"CycleException\";\n }\n}\n\n/**\n * Given a graph this function applies topological sorting to it.\n * If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.\n * Complexity: O(|V| + |E|).\n *\n * @param graph - graph to apply topological sorting to.\n * @returns an array of nodes such that for each edge u -> v, u appears before v in the array.\n */\nexport function topsort(graph: Graph): string[] {\n const visited: Record = {};\n const stack: Record = {};\n const results: string[] = [];\n\n function visit(node: string): void {\n if (node in stack) {\n throw new CycleException();\n }\n\n if (!(node in visited)) {\n stack[node] = true;\n visited[node] = true;\n graph.predecessors(node)?.forEach(visit);\n delete stack[node];\n results.push(node);\n }\n }\n\n graph.sinks().forEach(visit);\n\n if (Object.keys(visited).length !== graph.nodeCount()) {\n throw new CycleException();\n }\n\n return results;\n}\n", "import {Graph} from '../graph';\nimport {CycleException, topsort} from './topsort';\n\n/**\n * Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it\n * does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles\n * to get the actual list of cycles in the graph.\n *\n * @param graph - graph to detect whether it acyclic or not.\n * @returns whether graph contain cycles or not.\n */\nexport function isAcyclic(graph: Graph): boolean {\n try {\n topsort(graph);\n } catch (e) {\n if (e instanceof CycleException) {\n return false;\n }\n throw e;\n }\n return true;\n}\n", "import {Graph} from '../graph';\n\n/*\n * A helper that preforms a pre- or post-order traversal on the input graph\n * and processes the nodes in the order they are visited. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * Order must be one of \"pre\" or \"post\".\n */\nexport function reduce(\n g: Graph,\n vs: string | string[],\n order: \"pre\" | \"post\",\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!Array.isArray(vs)) {\n vs = [vs];\n }\n\n const navigation = ((v: string) => (g.isDirected() ? g.successors(v) : g.neighbors(v)) ?? []);\n\n const visited: Record = {};\n vs.forEach(function (v) {\n if (!g.hasNode(v)) {\n throw new Error(\"Graph does not have node: \" + v);\n }\n\n acc = doReduce(g, v, order === \"post\", visited, navigation, fn, acc);\n });\n return acc;\n}\n\nfunction doReduce(\n g: Graph,\n v: string,\n postorder: boolean,\n visited: Record,\n navigation: (v: string) => string[],\n fn: (acc: T, v: string) => T,\n acc: T\n): T {\n if (!(v in visited)) {\n visited[v] = true;\n\n if (!postorder) {\n acc = fn(acc, v);\n }\n navigation(v).forEach(function (w) {\n acc = doReduce(g, w, postorder, visited, navigation, fn, acc);\n });\n if (postorder) {\n acc = fn(acc, v);\n }\n }\n return acc;\n}\n", "import {Graph} from '../graph';\nimport {reduce} from './reduce';\n\n/*\n * Pre- or post-order traversal on the input graph.\n * Returns an array of the nodes in the order they were visited.\n *\n * If the order is not \"post\", it will be treated as \"pre\".\n */\nexport function dfs(g: Graph, vs: string | string[], order: \"pre\" | \"post\"): string[] {\n return reduce(g, vs, order, function (acc, v) {\n acc.push(v);\n return acc;\n }, [] as string[]);\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs post-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function postorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"post\");\n}\n", "import {Graph} from '../graph';\nimport {dfs} from './dfs';\n\n/**\n * Performs pre-order depth first traversal on the input graph. If the graph is\n * undirected then this algorithm will navigate using neighbors. If the graph\n * is directed then this algorithm will navigate using successors.\n *\n * @param graph - depth first traversal target.\n * @param vs - nodes list to traverse.\n * @returns the nodes in the order they were visited as a list of their names.\n */\nexport function preorder(graph: Graph, vs: string | string[]): string[] {\n return dfs(graph, vs, \"pre\");\n}\n", "import {Graph} from '../graph';\nimport {PriorityQueue} from '../data/priority-queue';\nimport type {Edge, WeightFunction} from '../types';\n\n/**\n * Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This\n * function returns the minimum spanning tree as an undirected graph. This algorithm is derived\n * from the description in \"Introduction to Algorithms\", Third Edition, Cormen, et al., Pg 634.\n * Complexity: O(|E| * log |V|);\n *\n * @param graph - graph to generate a minimum spanning tree of.\n * @param weightFn - function which takes edge e and returns the weight of it. It throws an Error if\n * the graph is not connected.\n * @returns minimum spanning tree of graph.\n */\nexport function prim(graph: Graph, weightFn: WeightFunction): Graph {\n const result = new Graph();\n const parents: Record = {};\n const pq = new PriorityQueue();\n let v: string;\n\n function updateNeighbors(edge: Edge): void {\n const w = edge.v === v ? edge.w : edge.v;\n const pri = pq.priority(w);\n if (pri !== undefined) {\n const edgeWeight = weightFn(edge);\n if (edgeWeight < pri) {\n parents[w] = v;\n pq.decrease(w, edgeWeight);\n }\n }\n }\n\n if (graph.nodeCount() === 0) {\n return result;\n }\n\n graph.nodes().forEach(function (v) {\n pq.add(v, Number.POSITIVE_INFINITY);\n result.setNode(v);\n });\n\n // Start from an arbitrary node\n const firstNode = graph.nodes()[0];\n if (firstNode !== undefined) {\n pq.decrease(firstNode, 0);\n }\n\n let init = false;\n while (pq.size() > 0) {\n v = pq.removeMin();\n if (v in parents) {\n result.setEdge(v, parents[v]!);\n } else if (init) {\n throw new Error(\"Input graph is not connected: \" + graph);\n } else {\n init = true;\n }\n\n graph.nodeEdges(v)?.forEach(updateNeighbors);\n }\n\n return result;\n}\n", "import {dijkstra} from './dijkstra';\nimport {bellmanFord} from './bellman-ford';\nimport {Graph} from '../graph';\nimport type {EdgeFunction, Path, WeightFunction} from '../types';\n\nexport function shortestPaths(\n g: Graph,\n source: string,\n weightFn?: WeightFunction,\n edgeFn?: EdgeFunction\n): Record {\n return runShortestPaths(\n g,\n source,\n weightFn,\n edgeFn ?? ((v: string) => {\n return g.outEdges(v) ?? [];\n })\n );\n}\n\nfunction runShortestPaths(\n g: Graph,\n source: string,\n weightFn: WeightFunction | undefined,\n edgeFn: EdgeFunction\n): Record {\n if (weightFn === undefined) {\n return dijkstra(g, source, weightFn, edgeFn);\n }\n\n let negativeEdgeExists = false;\n const nodes = g.nodes();\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n if (node === undefined) continue;\n const adjList = edgeFn(node);\n\n for (let j = 0; j < adjList.length; j++) {\n const edge = adjList[j];\n if (!edge) continue;\n const inVertex = edge.v === node ? edge.v : edge.w;\n const outVertex = inVertex === edge.v ? edge.w : edge.v;\n\n if (weightFn({v: inVertex, w: outVertex}) < 0) {\n negativeEdgeExists = true;\n }\n }\n\n if (negativeEdgeExists) {\n return bellmanFord(g, source, weightFn, edgeFn);\n }\n }\n\n return dijkstra(g, source, weightFn, edgeFn);\n}\n"], + "mappings": "4bAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,QAAAC,EAAA,SAAAC,EAAA,YAAAC,ICgBO,IAAMC,EAAN,KAAsE,CA4BzE,YAAYC,EAAqB,CA3BjC,KAAQ,YAAuB,GAC/B,KAAQ,cAAyB,GACjC,KAAQ,YAAuB,GAK/B,KAAQ,OAAoC,CAAC,EAE7C,KAAQ,IAA4C,CAAC,EAErD,KAAQ,OAAiD,CAAC,EAE1D,KAAQ,KAA6C,CAAC,EAEtD,KAAQ,MAAgD,CAAC,EAEzD,KAAQ,UAAk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EAAID,EAAQ,OAAQC,IAAK,CACrC,IAAMC,EAAOF,EAAQC,CAAC,EACtB,GAAI,CAACC,EAAM,SACX,IAAMC,EAAWD,EAAK,IAAMH,EAAOG,EAAK,EAAIA,EAAK,EAC3CE,EAAYD,IAAaD,EAAK,EAAIA,EAAK,EAAIA,EAAK,EAElDZ,EAAS,CAAC,EAAGa,EAAU,EAAGC,CAAS,CAAC,EAAI,IACxCR,EAAqB,GAE7B,CAEA,GAAIA,EACA,OAAOS,EAAYjB,EAAGC,EAAQC,EAAUC,CAAM,CAEtD,CAEA,OAAOI,EAASP,EAAGC,EAAQC,EAAUC,CAAM,CAC/C", + "names": ["index_exports", "__export", "Graph", "alg_exports", "json_exports", "version", "Graph", "opts", "label", "labelOrFn", "v", "names", "name", "removeEdge", "e", "child", "parent", "ancestor", "children", "predsV", "sucsV", "preds", "union", "sucs", "succ", "_a", "neighbors", "filter", "copy", "value", "parents", "findParent", "nodes", "w", "vStr", "wStr", "nameStr", "edgeValue", "valueSpecified", "edgeArgsToId", "edgeObj", "edgeArgsToObj", "incrementOrInitEntry", "edgeObjToId", "edgeLabel", "edge", "decrementOrRemoveEntry", "setV", "localEdge", "remoteEdge", "edges", "map", "k", "isDirected", "v_", "w_", "tmp", "version", "json_exports", "__export", "read", "write", "write", "graph", "json", "writeNodes", "writeEdges", "graphLabel", "g", "v", "nodeValue", "parent", "node", "edgeValue", "edge", "read", "Graph", "entry", "alg_exports", "__export", "CycleException", "bellmanFord", "components", "dijkstra", "dijkstraAll", "findCycles", "floydWarshall", "isAcyclic", "postorder", "preorder", "prim", "shortestPaths", "tarjan", "topsort", "DEFAULT_WEIGHT_FUNC", "bellmanFord", "g", "source", "weightFn", "edgeFn", "runBellmanFord", "v", "_a", "results", "didADistanceUpgrade", "iterations", "nodes", "relaxEdge", "edge", "uEntry", "wEntry", "edgeWeight", "relaxAllEdges", "vertex", "inVertex", "outVertex", "distance", "numberOfNodes", "i", "components", "graph", "visited", "cmpts", "cmpt", "dfs", "v", "_a", "_b", "PriorityQueue", "x", "key", "index", "priority", "keyIndices", "keyStr", "arr", "min", "currentPriority", "i", "l", "r", "largest", "parent", "j", "origArrI", "origArrJ", "DEFAULT_WEIGHT_FUNC", "dijkstra", "graph", "source", "weightFn", "edgeFn", "defaultEdgeFn", "v", "_a", "runDijkstra", "results", "pq", "PriorityQueue", "vEntry", "updateNeighbors", "edge", "w", "wEntry", "weight", "distance", "entry", "dijkstraAll", "graph", "weightFn", "edgeFn", "acc", "v", "dijkstra", "tarjan", "graph", "index", "stack", "visited", "results", "dfs", "v", "_a", "entry", "w", "wEntry", "cmpt", "findCycles", "graph", "tarjan", "cmpt", "_a", "firstNode", "DEFAULT_WEIGHT_FUNC", "floydWarshall", "graph", "weightFn", "edgeFn", "runFloydWarshall", "v", "_a", "results", "nodes", "rowV", "w", "edge", "d", "k", "rowK", "i", "rowI", "j", "ik", "kj", "ij", "altDistance", "CycleException", "message", "topsort", "graph", "visited", "stack", "results", "visit", "node", "_a", "isAcyclic", "graph", "topsort", "CycleException", "reduce", "g", "vs", "order", "fn", "acc", "navigation", "v", "_a", "visited", "doReduce", "postorder", "w", "dfs", "g", "vs", "order", "reduce", "acc", "v", "postorder", "graph", "vs", "dfs", "preorder", "graph", "vs", "dfs", "prim", "graph", "weightFn", "_a", "result", "Graph", "parents", "pq", "PriorityQueue", "v", "updateNeighbors", "edge", "w", "pri", "edgeWeight", "firstNode", "init", "shortestPaths", "g", "source", "weightFn", "edgeFn", "runShortestPaths", "v", "_a", "dijkstra", "negativeEdgeExists", "nodes", "i", "node", "adjList", "j", "edge", "inVertex", "outVertex", "bellmanFord"] } diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index f7aa912b..30b792f0 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -31,5 +31,5 @@ export { Graph } from './lib/graph'; export { version } from './lib/version'; export * as json from './lib/json'; export * as alg from './lib/alg/index'; -export type { GraphOptions, Edge, Path, WeightFunction, EdgeFunction } from './lib/types.js'; +export type { GraphOptions, Edge, Path, WeightFunction, EdgeFunction, Label } from './lib/types.js'; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/types/index.d.ts.map b/dist/types/index.d.ts.map index 12ace885..07d4d26a 100644 --- a/dist/types/index.d.ts.map +++ b/dist/types/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AAClC,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,GAAG,MAAM,iBAAiB,CAAC;AAEvC,YAAY,EAAC,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAC,MAAM,gBAAgB,CAAC"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AAClC,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,GAAG,MAAM,iBAAiB,CAAC;AAEvC,YAAY,EAAC,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC"} \ No newline at end of file diff --git a/dist/types/lib/alg/dfs.d.ts b/dist/types/lib/alg/dfs.d.ts index b4930dc7..2c457305 100644 --- a/dist/types/lib/alg/dfs.d.ts +++ b/dist/types/lib/alg/dfs.d.ts @@ -1,3 +1,3 @@ import { Graph } from '../graph'; -export declare function dfs(g: Graph, vs: string | string[], order: string): string[]; +export declare function dfs(g: Graph, vs: string | string[], order: "pre" | "post"): string[]; //# sourceMappingURL=dfs.d.ts.map \ No newline at end of file diff --git a/dist/types/lib/alg/dfs.d.ts.map b/dist/types/lib/alg/dfs.d.ts.map index 3ce7dcf9..0b800a43 100644 --- a/dist/types/lib/alg/dfs.d.ts.map +++ b/dist/types/lib/alg/dfs.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"dfs.d.ts","sourceRoot":"","sources":["../../../../lib/alg/dfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAS/B,wBAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAK5E"} \ No newline at end of file +{"version":3,"file":"dfs.d.ts","sourceRoot":"","sources":["../../../../lib/alg/dfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAS/B,wBAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,EAAE,CAKpF"} \ No newline at end of file diff --git a/dist/types/lib/alg/extract-path.d.ts.map b/dist/types/lib/alg/extract-path.d.ts.map index 93005641..9970574e 100644 --- a/dist/types/lib/alg/extract-path.d.ts.map +++ b/dist/types/lib/alg/extract-path.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"extract-path.d.ts","sourceRoot":"","sources":["../../../../lib/alg/extract-path.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,UAAU,CAAC;AAEnC,UAAU,aAAa;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,wBAAgB,WAAW,CACvB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,EACnC,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GACpB,aAAa,CAYf"} \ No newline at end of file +{"version":3,"file":"extract-path.d.ts","sourceRoot":"","sources":["../../../../lib/alg/extract-path.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,UAAU,CAAC;AAEnC,UAAU,aAAa;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,wBAAgB,WAAW,CACvB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,EACnC,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GACpB,aAAa,CAiBf"} \ No newline at end of file diff --git a/dist/types/lib/alg/find-cycles.d.ts.map b/dist/types/lib/alg/find-cycles.d.ts.map index 8ad50096..370c6e3d 100644 --- a/dist/types/lib/alg/find-cycles.d.ts.map +++ b/dist/types/lib/alg/find-cycles.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"find-cycles.d.ts","sourceRoot":"","sources":["../../../../lib/alg/find-cycles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAI/B;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,CAQnD"} \ No newline at end of file +{"version":3,"file":"find-cycles.d.ts","sourceRoot":"","sources":["../../../../lib/alg/find-cycles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAG/B;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,CAOnD"} \ No newline at end of file diff --git a/dist/types/lib/alg/prim.d.ts.map b/dist/types/lib/alg/prim.d.ts.map index d77dbb52..42a76e2d 100644 --- a/dist/types/lib/alg/prim.d.ts.map +++ b/dist/types/lib/alg/prim.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"prim.d.ts","sourceRoot":"","sources":["../../../../lib/alg/prim.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAE/B,OAAO,KAAK,EAAO,cAAc,EAAC,MAAM,UAAU,CAAC;AAEnD;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,cAAc,GAAG,KAAK,CA6ClE"} \ No newline at end of file +{"version":3,"file":"prim.d.ts","sourceRoot":"","sources":["../../../../lib/alg/prim.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAE/B,OAAO,KAAK,EAAO,cAAc,EAAC,MAAM,UAAU,CAAC;AAEnD;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,cAAc,GAAG,KAAK,CAgDlE"} \ No newline at end of file diff --git a/dist/types/lib/alg/reduce.d.ts b/dist/types/lib/alg/reduce.d.ts index 3b198e07..f52226c7 100644 --- a/dist/types/lib/alg/reduce.d.ts +++ b/dist/types/lib/alg/reduce.d.ts @@ -1,3 +1,3 @@ import { Graph } from '../graph'; -export declare function reduce(g: Graph, vs: string | string[], order: string, fn: (acc: T, v: string) => T, acc: T): T; +export declare function reduce(g: Graph, vs: string | string[], order: "pre" | "post", fn: (acc: T, v: string) => T, acc: T): T; //# sourceMappingURL=reduce.d.ts.map \ No newline at end of file diff --git a/dist/types/lib/alg/reduce.d.ts.map b/dist/types/lib/alg/reduce.d.ts.map index 79b6f342..ce1c5cb1 100644 --- a/dist/types/lib/alg/reduce.d.ts.map +++ b/dist/types/lib/alg/reduce.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"reduce.d.ts","sourceRoot":"","sources":["../../../../lib/alg/reduce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAU/B,wBAAgB,MAAM,CAAC,CAAC,EACpB,CAAC,EAAE,KAAK,EACR,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,EACrB,KAAK,EAAE,MAAM,EACb,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC,EAC5B,GAAG,EAAE,CAAC,GACP,CAAC,CAgBH"} \ No newline at end of file +{"version":3,"file":"reduce.d.ts","sourceRoot":"","sources":["../../../../lib/alg/reduce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAU/B,wBAAgB,MAAM,CAAC,CAAC,EACpB,CAAC,EAAE,KAAK,EACR,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,EACrB,KAAK,EAAE,KAAK,GAAG,MAAM,EACrB,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC,EAC5B,GAAG,EAAE,CAAC,GACP,CAAC,CAgBH"} \ No newline at end of file diff --git a/dist/types/lib/alg/shortest-paths.d.ts.map b/dist/types/lib/alg/shortest-paths.d.ts.map index ec207472..f4dcce79 100644 --- a/dist/types/lib/alg/shortest-paths.d.ts.map +++ b/dist/types/lib/alg/shortest-paths.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"shortest-paths.d.ts","sourceRoot":"","sources":["../../../../lib/alg/shortest-paths.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAC/B,OAAO,KAAK,EAAC,YAAY,EAAE,IAAI,EAAE,cAAc,EAAC,MAAM,UAAU,CAAC;AAEjE,wBAAgB,aAAa,CACzB,CAAC,EAAE,KAAK,EACR,MAAM,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,cAAc,EACzB,MAAM,CAAC,EAAE,YAAY,GACtB,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAUtB"} \ No newline at end of file +{"version":3,"file":"shortest-paths.d.ts","sourceRoot":"","sources":["../../../../lib/alg/shortest-paths.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAC/B,OAAO,KAAK,EAAC,YAAY,EAAE,IAAI,EAAE,cAAc,EAAC,MAAM,UAAU,CAAC;AAEjE,wBAAgB,aAAa,CACzB,CAAC,EAAE,KAAK,EACR,MAAM,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,cAAc,EACzB,MAAM,CAAC,EAAE,YAAY,GACtB,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAStB"} \ No newline at end of file diff --git a/dist/types/lib/alg/tarjan.d.ts.map b/dist/types/lib/alg/tarjan.d.ts.map index 23999f4a..7bce0b64 100644 --- a/dist/types/lib/alg/tarjan.d.ts.map +++ b/dist/types/lib/alg/tarjan.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"tarjan.d.ts","sourceRoot":"","sources":["../../../../lib/alg/tarjan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAQ/B;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,CA0C/C"} \ No newline at end of file +{"version":3,"file":"tarjan.d.ts","sourceRoot":"","sources":["../../../../lib/alg/tarjan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAQ/B;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,CAmD/C"} \ No newline at end of file diff --git a/dist/types/lib/alg/topsort.d.ts b/dist/types/lib/alg/topsort.d.ts index dfe221d2..80c4420b 100644 --- a/dist/types/lib/alg/topsort.d.ts +++ b/dist/types/lib/alg/topsort.d.ts @@ -1,6 +1,6 @@ import { Graph } from '../graph'; export declare class CycleException extends Error { - constructor(...args: unknown[]); + constructor(message?: string); } /** * Given a graph this function applies topological sorting to it. diff --git a/dist/types/lib/alg/topsort.d.ts.map b/dist/types/lib/alg/topsort.d.ts.map index 4f63293e..ee60eebd 100644 --- a/dist/types/lib/alg/topsort.d.ts.map +++ b/dist/types/lib/alg/topsort.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"topsort.d.ts","sourceRoot":"","sources":["../../../../lib/alg/topsort.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAE/B,qBAAa,cAAe,SAAQ,KAAK;gBACzB,GAAG,IAAI,EAAE,OAAO,EAAE;CAGjC;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,CA0B9C"} \ No newline at end of file +{"version":3,"file":"topsort.d.ts","sourceRoot":"","sources":["../../../../lib/alg/topsort.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAE/B,qBAAa,cAAe,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAI/B;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE,CA0B9C"} \ No newline at end of file diff --git a/dist/types/lib/data/priority-queue.d.ts.map b/dist/types/lib/data/priority-queue.d.ts.map index f7279f65..3e120be1 100644 --- a/dist/types/lib/data/priority-queue.d.ts.map +++ b/dist/types/lib/data/priority-queue.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../../../lib/data/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAOH,qBAAa,aAAa;IACtB,OAAO,CAAC,IAAI,CAA4B;IACxC,OAAO,CAAC,WAAW,CAA8B;IAEjD;;OAEG;IACH,IAAI,IAAI,MAAM;IAId;;OAEG;IACH,IAAI,IAAI,MAAM,EAAE;IAIhB;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAQzC;;;OAGG;IACH,GAAG,IAAI,MAAM;IAOb;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;IAe3C;;OAEG;IACH,SAAS,IAAI,MAAM;IAQnB;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAgB7C,OAAO,CAAC,QAAQ;IAkBhB,OAAO,CAAC,SAAS;IAejB,OAAO,CAAC,KAAK;CAWhB"} \ No newline at end of file +{"version":3,"file":"priority-queue.d.ts","sourceRoot":"","sources":["../../../../lib/data/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAOH,qBAAa,aAAa;IACtB,OAAO,CAAC,IAAI,CAA4B;IACxC,OAAO,CAAC,WAAW,CAA8B;IAEjD;;OAEG;IACH,IAAI,IAAI,MAAM;IAId;;OAEG;IACH,IAAI,IAAI,MAAM,EAAE;IAIhB;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAQzC;;;OAGG;IACH,GAAG,IAAI,MAAM;IAOb;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;IAe3C;;OAEG;IACH,SAAS,IAAI,MAAM;IAWnB;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAgB7C,OAAO,CAAC,QAAQ;IAkBhB,OAAO,CAAC,SAAS;IAejB,OAAO,CAAC,KAAK;CAWhB"} \ No newline at end of file diff --git a/dist/types/lib/graph.d.ts b/dist/types/lib/graph.d.ts index 89b03dd8..2a375dc9 100644 --- a/dist/types/lib/graph.d.ts +++ b/dist/types/lib/graph.d.ts @@ -1,5 +1,5 @@ -import type { Edge, EdgeLabelFactory, GraphOptions, NodeLabelFactory } from './types'; -export declare class Graph { +import type { Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory } from './types'; +export declare class Graph { private _isDirected; private _isMultigraph; private _isCompound; @@ -148,7 +148,7 @@ export declare class Graph { * @param v - node to get parent of. * @returns parent node name or void if v has no parent. */ - parent(v: string): string | void; + parent(v: string): string | undefined; /** * Gets list of direct children of node v. * Complexity: O(1). @@ -165,7 +165,7 @@ export declare class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - predecessors(v: string): void | string[]; + predecessors(v: string): string[] | undefined; /** * Return all nodes that are successors of the specified node or undefined if node v is not in * the graph. Behavior is undefined for undirected graphs - use neighbors instead. @@ -174,7 +174,7 @@ export declare class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - successors(v: string): void | string[]; + successors(v: string): string[] | undefined; /** * Return all nodes that are predecessors or successors of the specified node or undefined if * node v is not in the graph. @@ -183,7 +183,7 @@ export declare class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - neighbors(v: string): void | string[]; + neighbors(v: string): string[] | undefined; isLeaf(v: string): boolean; /** * Creates new graph with nodes filtered via filter. Edges incident to rejected node @@ -340,7 +340,7 @@ export declare class Graph { * @param w - edge source node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - inEdges(v: string, w?: string): void | Edge[]; + inEdges(v: string, w?: string): Edge[] | undefined; /** * Return all edges that are pointed at by node v. Optionally filters those edges down to just * those point to w. Behavior is void for undirected graphs - use nodeEdges instead. @@ -350,7 +350,7 @@ export declare class Graph { * @param w - edge sink node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - outEdges(v: string, w?: string): void | Edge[]; + outEdges(v: string, w?: string): Edge[] | undefined; /** * Returns all edges to or from node v regardless of direction. Optionally filters those edges * down to just those between nodes v and w regardless of direction. @@ -360,7 +360,7 @@ export declare class Graph { * @param w - edge adjacent node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - nodeEdges(v: string, w?: string): void | Edge[]; + nodeEdges(v: string, w?: string): Edge[] | undefined; private _defaultNodeLabelFn; private _defaultEdgeLabelFn; private _removeFromParentsChildList; diff --git a/dist/types/lib/graph.d.ts.map b/dist/types/lib/graph.d.ts.map index 0f457c95..923a62ba 100644 --- a/dist/types/lib/graph.d.ts.map +++ b/dist/types/lib/graph.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../../lib/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAE,gBAAgB,EAAE,YAAY,EAAE,gBAAgB,EAAC,MAAM,SAAS,CAAC;AAgBpF,qBAAa,KAAK,CAAC,UAAU,GAAG,GAAG,EAAE,SAAS,GAAG,GAAG,EAAE,SAAS,GAAG,GAAG;IACjE,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,aAAa,CAAkB;IACvC,OAAO,CAAC,WAAW,CAAkB;IAGrC,OAAO,CAAC,MAAM,CAAc;IAE5B,OAAO,CAAC,MAAM,CAAiC;IAE/C,OAAO,CAAC,GAAG,CAA4C;IAEvD,OAAO,CAAC,MAAM,CAA8C;IAE5D,OAAO,CAAC,IAAI,CAA4C;IAExD,OAAO,CAAC,KAAK,CAA8C;IAE3D,OAAO,CAAC,SAAS,CAA4B;IAE7C,OAAO,CAAC,WAAW,CAAiC;IAEpD,OAAO,CAAC,UAAU,CAAa;IAE/B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,OAAO,CAAC,CAAyB;IACzC,OAAO,CAAC,SAAS,CAAC,CAA0C;gBAEhD,IAAI,CAAC,EAAE,YAAY;IAiB/B;;;;OAIG;IACH,UAAU,IAAI,OAAO;IAIrB;;;;OAIG;IACH,Y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E,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,SAAS;IAQpD,OAAO,CAAC,mBAAmB,CAA6D;IAGxF,OAAO,CAAC,mBAAmB,CAA6D;IAExF,OAAO,CAAC,2BAA2B;IAInC,OAAO,CAAC,WAAW;CAatB"} \ No newline at end of file diff --git a/dist/types/lib/json.d.ts b/dist/types/lib/json.d.ts index 7436d222..67fce658 100644 --- a/dist/types/lib/json.d.ts +++ b/dist/types/lib/json.d.ts @@ -1,5 +1,5 @@ import { Graph } from './graph'; -import type { GraphOptions } from './types'; +import type { GraphOptions, Label } from './types'; interface JsonGraph { options: GraphOptions; nodes: JsonNode[]; @@ -38,6 +38,6 @@ export declare function write(graph: Graph): JsonGraph; * g2.edges() * // [ { v: 'a', w: 'b' } ] */ -export declare function read(json: JsonGraph): Graph; +export declare function read(json: JsonGraph): Graph; export {}; //# sourceMappingURL=json.d.ts.map \ No newline at end of file diff --git a/dist/types/lib/json.d.ts.map b/dist/types/lib/json.d.ts.map index 566c426c..1a430413 100644 --- a/dist/types/lib/json.d.ts.map +++ b/dist/types/lib/json.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../lib/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,SAAS,CAAC;AAE1C,UAAU,SAAS;IACf,OAAO,EAAE,YAAY,CAAC;IACtB,KAAK,EAAE,QAAQ,EAAE,CAAC;IAClB,KAAK,EAAE,QAAQ,EAAE,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,UAAU,QAAQ;IACd,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,QAAQ;IACd,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAA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AAK,CAAC,UAAU,EAAE,SAAS,EAAE,SAAS,CAAC,CAmBzC"} \ No newline at end of file diff --git a/dist/types/lib/types.d.ts b/dist/types/lib/types.d.ts index 57e05e4f..d4d4c875 100644 --- a/dist/types/lib/types.d.ts +++ b/dist/types/lib/types.d.ts @@ -33,6 +33,8 @@ export interface Path { export type WeightFunction = (e: Edge) => number; /** Function that takes a node and returns the edges incident to it */ export type EdgeFunction = (v: string) => Edge[]; +/** Standard label type for node, edge, or graph labels */ +export type Label = string | number | boolean | Record; /** Factory function that creates a label for a node */ export type NodeLabelFactory = (v: string) => NodeLabel; /** Factory function that creates a label for an edge */ diff --git a/dist/types/lib/types.d.ts.map b/dist/types/lib/types.d.ts.map index 78ed1dd1..874c94d0 100644 --- a/dist/types/lib/types.d.ts.map +++ b/dist/types/lib/types.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../lib/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,qFAAqF;IACrF,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,6BAA6B;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,6BAA6B;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,0EAA0E;IAC1E,QAAQ,EAAE,MAAM,CAAC;IACjB,iFAAiF;IACjF,WAAW,EAAE,MAAM,CAAC;CACvB;AAED,yDAAyD;AACzD,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,EAAE,IAAI,KAAK,MAAM,CAAC;AAEjD,sEAAsE;AACtE,MAAM,MAAM,YAAY,GAA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,gBAAgB,CAAC,SAAS,IAAI,CAAC,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC;AAEnE,wDAAwD;AACxD,MAAM,MAAM,gBAAgB,CAAC,SAAS,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,SAAS,CAAC"} \ No newline at end of file diff --git a/dist/types/lib/version.d.ts b/dist/types/lib/version.d.ts index a535a75b..124e4c44 100644 --- a/dist/types/lib/version.d.ts +++ b/dist/types/lib/version.d.ts @@ -1,2 +1,2 @@ -export declare const version = "4.0.3"; +export declare const version = "4.0.4-pre"; //# sourceMappingURL=version.d.ts.map \ No newline at end of file diff --git a/dist/types/lib/version.d.ts.map b/dist/types/lib/version.d.ts.map index bd9d3636..711232be 100644 --- a/dist/types/lib/version.d.ts.map +++ b/dist/types/lib/version.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../../lib/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"} \ No newline at end of file +{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../../lib/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,cAAc,CAAC"} \ No newline at end of file diff --git a/index.ts b/index.ts index f841aadd..9089f8f9 100644 --- a/index.ts +++ b/index.ts @@ -33,4 +33,4 @@ export {version} from './lib/version'; export * as json from './lib/json'; export * as alg from './lib/alg/index'; -export type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction} from './lib/types.js'; +export type {GraphOptions, Edge, Path, WeightFunction, EdgeFunction, Label} from './lib/types.js'; diff --git a/lib/alg/bellman-ford.ts b/lib/alg/bellman-ford.ts index 83a770e4..8b4273f7 100644 --- a/lib/alg/bellman-ford.ts +++ b/lib/alg/bellman-ford.ts @@ -14,7 +14,7 @@ export function bellmanFord( String(source), weightFn || DEFAULT_WEIGHT_FUNC, edgeFn || function (v) { - return g.outEdges(v)!; + return g.outEdges(v) ?? []; } ); } @@ -31,10 +31,13 @@ function runBellmanFord( const nodes = g.nodes(); const relaxEdge = function (edge: Edge): void { + const uEntry = results[edge.v]; + const wEntry = results[edge.w]; + if (!uEntry || !wEntry) return; const edgeWeight = weightFn(edge); - if (results[edge.v]!.distance + edgeWeight < results[edge.w]!.distance) { + if (uEntry.distance + edgeWeight < wEntry.distance) { results[edge.w] = { - distance: results[edge.v]!.distance + edgeWeight, + distance: uEntry.distance + edgeWeight, predecessor: edge.v }; didADistanceUpgrade = true; diff --git a/lib/alg/components.ts b/lib/alg/components.ts index 05c56f69..9f2fd447 100644 --- a/lib/alg/components.ts +++ b/lib/alg/components.ts @@ -17,8 +17,8 @@ export function components(graph: Graph): string[][] { if (v in visited) return; visited[v] = true; cmpt.push(v); - graph.successors(v)!.forEach(dfs); - graph.predecessors(v)!.forEach(dfs); + graph.successors(v)?.forEach(dfs); + graph.predecessors(v)?.forEach(dfs); } graph.nodes().forEach(function (v) { diff --git a/lib/alg/dfs.ts b/lib/alg/dfs.ts index d2e289bb..d3721497 100644 --- a/lib/alg/dfs.ts +++ b/lib/alg/dfs.ts @@ -7,7 +7,7 @@ import {reduce} from './reduce'; * * If the order is not "post", it will be treated as "pre". */ -export function dfs(g: Graph, vs: string | string[], order: string): string[] { +export function dfs(g: Graph, vs: string | string[], order: "pre" | "post"): string[] { return reduce(g, vs, order, function (acc, v) { acc.push(v); return acc; diff --git a/lib/alg/dijkstra.ts b/lib/alg/dijkstra.ts index 4d7d71d3..f78be251 100644 --- a/lib/alg/dijkstra.ts +++ b/lib/alg/dijkstra.ts @@ -29,7 +29,7 @@ export function dijkstra( edgeFn?: EdgeFunction ): Record { const defaultEdgeFn: EdgeFunction = function (v) { - return graph.outEdges(v)!; + return graph.outEdges(v) ?? []; }; return runDijkstra(graph, String(source), @@ -49,7 +49,8 @@ function runDijkstra( const updateNeighbors = function (edge: Edge): void { const w = edge.v !== v ? edge.v : edge.w; - const wEntry = results[w]!; + const wEntry = results[w]; + if (!wEntry) return; const weight = weightFn(edge); const distance = vEntry.distance + weight; @@ -72,11 +73,12 @@ function runDijkstra( }); while (pq.size() > 0) { - v = pq.removeMin()!; - vEntry = results[v]!; - if (vEntry.distance === Number.POSITIVE_INFINITY) { + v = pq.removeMin(); + const entry = results[v]; + if (!entry || entry.distance === Number.POSITIVE_INFINITY) { break; } + vEntry = entry; edgeFn(v).forEach(updateNeighbors); } diff --git a/lib/alg/extract-path.ts b/lib/alg/extract-path.ts index 1f8b1672..fba4b938 100644 --- a/lib/alg/extract-path.ts +++ b/lib/alg/extract-path.ts @@ -17,8 +17,13 @@ export function extractPath( throw new Error("Invalid destination vertex"); } + const destPath = shortestPaths[destination]; + if (!destPath) { + throw new Error("Invalid destination vertex"); + } + return { - weight: shortestPaths[destination]!.distance, + weight: destPath.distance, path: runExtractPath(shortestPaths, source, destination) }; } @@ -29,12 +34,14 @@ function runExtractPath( destination: string ): string[] { const path: string[] = []; - let currentNode = destination; + let currentNode: string | undefined = destination; - while (currentNode !== source) { + while (currentNode !== undefined && currentNode !== source) { path.push(currentNode); - currentNode = shortestPaths[currentNode]!.predecessor!; + currentNode = shortestPaths[currentNode]?.predecessor; + } + if (currentNode === source) { + path.push(source); } - path.push(source); return path.reverse(); } diff --git a/lib/alg/find-cycles.ts b/lib/alg/find-cycles.ts index 43891d8b..56549700 100644 --- a/lib/alg/find-cycles.ts +++ b/lib/alg/find-cycles.ts @@ -1,5 +1,4 @@ import {Graph} from '../graph'; -import type {Edge} from '../types'; import {tarjan} from './tarjan'; /** @@ -15,10 +14,9 @@ import {tarjan} from './tarjan'; */ export function findCycles(graph: Graph): string[][] { return tarjan(graph).filter(function (cmpt) { - // A single-node component is a cycle iff the node has a self-loop. We check via outEdges - // rather than hasEdge(v, v) because the latter only matches the default (unnamed) edge and - // would miss a named self-loop edge in a multigraph. + const firstNode = cmpt[0]; + if (!firstNode) return false; return cmpt.length > 1 - || (cmpt.length === 1 && (graph.outEdges(cmpt[0]!, cmpt[0]!) as Edge[]).length > 0); + || (cmpt.length === 1 && (graph.outEdges(firstNode, firstNode) ?? []).length > 0); }); } diff --git a/lib/alg/floyd-warshall.ts b/lib/alg/floyd-warshall.ts index 06c37488..038f0b0e 100644 --- a/lib/alg/floyd-warshall.ts +++ b/lib/alg/floyd-warshall.ts @@ -30,7 +30,7 @@ export function floydWarshall( return runFloydWarshall(graph, weightFn || DEFAULT_WEIGHT_FUNC, edgeFn || function (v) { - return graph.outEdges(v)!; + return graph.outEdges(v) ?? []; }); } @@ -43,32 +43,37 @@ function runFloydWarshall( const nodes = graph.nodes(); nodes.forEach(function (v) { - results[v] = {}; - results[v]![v] = {distance: 0, predecessor: ''}; + const rowV: Record = {}; + results[v] = rowV; + rowV[v] = {distance: 0, predecessor: ''}; nodes.forEach(function (w) { if (v !== w) { - results[v]![w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''}; + rowV[w] = {distance: Number.POSITIVE_INFINITY, predecessor: ''}; } }); edgeFn(v).forEach(function (edge) { const w = edge.v === v ? edge.w : edge.v; const d = weightFn(edge); - results[v]![w] = {distance: d, predecessor: v}; + rowV[w] = {distance: d, predecessor: v}; }); }); nodes.forEach(function (k) { - const rowK = results[k]!; + const rowK = results[k]; + if (!rowK) return; nodes.forEach(function (i) { - const rowI = results[i]!; + const rowI = results[i]; + if (!rowI) return; nodes.forEach(function (j) { - const ik = rowI[k]!; - const kj = rowK[j]!; - const ij = rowI[j]!; - const altDistance = ik.distance + kj.distance; - if (altDistance < ij.distance) { - ij.distance = altDistance; - ij.predecessor = kj.predecessor; + const ik = rowI[k]; + const kj = rowK[j]; + const ij = rowI[j]; + if (ik && kj && ij) { + const altDistance = ik.distance + kj.distance; + if (altDistance < ij.distance) { + ij.distance = altDistance; + ij.predecessor = kj.predecessor; + } } }); }); diff --git a/lib/alg/prim.ts b/lib/alg/prim.ts index 89defc9f..887059b9 100644 --- a/lib/alg/prim.ts +++ b/lib/alg/prim.ts @@ -41,11 +41,14 @@ export function prim(graph: Graph, weightFn: WeightFunction): Graph { }); // Start from an arbitrary node - pq.decrease(graph.nodes()[0]!, 0); + const firstNode = graph.nodes()[0]; + if (firstNode !== undefined) { + pq.decrease(firstNode, 0); + } let init = false; while (pq.size() > 0) { - v = pq.removeMin()!; + v = pq.removeMin(); if (v in parents) { result.setEdge(v, parents[v]!); } else if (init) { @@ -54,7 +57,7 @@ export function prim(graph: Graph, weightFn: WeightFunction): Graph { init = true; } - graph.nodeEdges(v)!.forEach(updateNeighbors); + graph.nodeEdges(v)?.forEach(updateNeighbors); } return result; diff --git a/lib/alg/reduce.ts b/lib/alg/reduce.ts index 4d7d619f..a2930373 100644 --- a/lib/alg/reduce.ts +++ b/lib/alg/reduce.ts @@ -11,7 +11,7 @@ import {Graph} from '../graph'; export function reduce( g: Graph, vs: string | string[], - order: string, + order: "pre" | "post", fn: (acc: T, v: string) => T, acc: T ): T { diff --git a/lib/alg/shortest-paths.ts b/lib/alg/shortest-paths.ts index 41669936..5e31690b 100644 --- a/lib/alg/shortest-paths.ts +++ b/lib/alg/shortest-paths.ts @@ -14,8 +14,7 @@ export function shortestPaths( source, weightFn, edgeFn ?? ((v: string) => { - const edges = g.outEdges(v); - return edges ?? []; + return g.outEdges(v) ?? []; }) ); } @@ -34,11 +33,14 @@ function runShortestPaths( const nodes = g.nodes(); for (let i = 0; i < nodes.length; i++) { - const adjList = edgeFn(nodes[i]!); + const node = nodes[i]; + if (node === undefined) continue; + const adjList = edgeFn(node); for (let j = 0; j < adjList.length; j++) { - const edge = adjList[j]!; - const inVertex = edge.v === nodes[i] ? edge.v : edge.w; + const edge = adjList[j]; + if (!edge) continue; + const inVertex = edge.v === node ? edge.v : edge.w; const outVertex = inVertex === edge.v ? edge.w : edge.v; if (weightFn({v: inVertex, w: outVertex}) < 0) { diff --git a/lib/alg/tarjan.ts b/lib/alg/tarjan.ts index 3567c07c..d041c966 100644 --- a/lib/alg/tarjan.ts +++ b/lib/alg/tarjan.ts @@ -33,12 +33,18 @@ export function tarjan(graph: Graph): string[][] { }; stack.push(v); - graph.successors(v)!.forEach(function (w) { + graph.successors(v)?.forEach(function (w) { if (!(w in visited)) { dfs(w); - entry.lowlink = Math.min(entry.lowlink, visited[w]!.lowlink); - } else if (visited[w]!.onStack) { - entry.lowlink = Math.min(entry.lowlink, visited[w]!.index); + const wEntry = visited[w]; + if (wEntry) { + entry.lowlink = Math.min(entry.lowlink, wEntry.lowlink); + } + } else { + const wEntry = visited[w]; + if (wEntry?.onStack) { + entry.lowlink = Math.min(entry.lowlink, wEntry.index); + } } }); @@ -47,7 +53,10 @@ export function tarjan(graph: Graph): string[][] { let w: string; do { w = stack.pop()!; - visited[w]!.onStack = false; + const wEntry = visited[w]; + if (wEntry) { + wEntry.onStack = false; + } cmpt.push(w); } while (v !== w); results.push(cmpt); diff --git a/lib/alg/topsort.ts b/lib/alg/topsort.ts index 715cfac7..1e601b84 100644 --- a/lib/alg/topsort.ts +++ b/lib/alg/topsort.ts @@ -1,8 +1,9 @@ import {Graph} from '../graph'; export class CycleException extends Error { - constructor(...args: unknown[]) { - super(...args as ConstructorParameters); + constructor(message?: string) { + super(message); + this.name = "CycleException"; } } @@ -27,7 +28,7 @@ export function topsort(graph: Graph): string[] { if (!(node in visited)) { stack[node] = true; visited[node] = true; - graph.predecessors(node)!.forEach(visit); + graph.predecessors(node)?.forEach(visit); delete stack[node]; results.push(node); } diff --git a/lib/data/priority-queue.ts b/lib/data/priority-queue.ts index 2813b262..374e0f87 100644 --- a/lib/data/priority-queue.ts +++ b/lib/data/priority-queue.ts @@ -83,6 +83,9 @@ export class PriorityQueue { * Removes and returns the smallest key in the queue. Takes `O(log n)` time. */ removeMin(): string { + if (this.size() === 0) { + throw new Error("Queue underflow"); + } this._swap(0, this._arr.length - 1); const min = this._arr.pop()!; delete this._keyIndices[min.key]; diff --git a/lib/graph.ts b/lib/graph.ts index ef88c517..60067ac0 100644 --- a/lib/graph.ts +++ b/lib/graph.ts @@ -1,4 +1,4 @@ -import type {Edge, EdgeLabelFactory, GraphOptions, NodeLabelFactory} from './types'; +import type {Edge, EdgeLabelFactory, GraphOptions, Label, NodeLabelFactory} from './types'; const DEFAULT_EDGE_NAME = "\x00"; const GRAPH_NODE = "\x00"; @@ -14,7 +14,7 @@ const EDGE_KEY_DELIM = "\x01"; // edges up and, object properties, which have string keys, are the closest // we're going to get to a performant hashtable in JavaScript. -export class Graph { +export class Graph { private _isDirected: boolean = true; private _isMultigraph: boolean = false; private _isCompound: boolean = false; @@ -296,7 +296,7 @@ export class Graph { } else { // Coerce parent to string parent += ""; - for (let ancestor: string | undefined | void = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) { + for (let ancestor: string | undefined = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) { if (ancestor === v) { throw new Error("Setting " + parent + " as parent of " + v + " would create a cycle"); @@ -320,13 +320,14 @@ export class Graph { * @param v - node to get parent of. * @returns parent node name or void if v has no parent. */ - parent(v: string): string | void { + parent(v: string): string | undefined { if (this._isCompound) { const parent = this._parent![v]; if (parent !== GRAPH_NODE) { return parent; } } + return undefined; } /** @@ -358,11 +359,12 @@ export class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - predecessors(v: string): void | string[] { + predecessors(v: string): string[] | undefined { const predsV = this._preds[v]; if (predsV) { return Object.keys(predsV); } + return undefined; } /** @@ -373,11 +375,12 @@ export class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - successors(v: string): void | string[] { + successors(v: string): string[] | undefined { const sucsV = this._sucs[v]; if (sucsV) { return Object.keys(sucsV); } + return undefined; } /** @@ -388,26 +391,30 @@ export class Graph { * @param v - node identifier. * @returns node identifiers list or undefined if v is not in the graph. */ - neighbors(v: string): void | string[] { + neighbors(v: string): string[] | undefined { const preds = this.predecessors(v); if (preds) { const union = new Set(preds); - for (const succ of this.successors(v)!) { - union.add(succ); + const sucs = this.successors(v); + if (sucs) { + for (const succ of sucs) { + union.add(succ); + } } return Array.from(union.values()); } + return undefined; } isLeaf(v: string): boolean { - let neighbors: string[] | void; + let neighbors: string[] | undefined; if (this.isDirected()) { neighbors = this.successors(v); } else { neighbors = this.neighbors(v); } - return neighbors!.length === 0; + return (neighbors?.length ?? 0) === 0; } /** @@ -444,8 +451,8 @@ export class Graph { const findParent = (v: string): string | undefined => { const parent = this.parent(v); if (!parent || copy.hasNode(parent)) { - parents[v] = parent ?? undefined; - return parent ?? undefined; + parents[v] = parent; + return parent; } else if (parent in parents) { return parents[parent]; } else { @@ -669,11 +676,11 @@ export class Graph { ? this.edge(v as Edge) : this.edge(v as string, w!, name); - if (typeof edgeLabel !== "object") { + if (typeof edgeLabel !== "object" || edgeLabel === null) { return {label: edgeLabel as EdgeLabel}; } - return edgeLabel as { label: EdgeLabel }; + return edgeLabel as unknown as { label: EdgeLabel }; } /** @@ -751,7 +758,7 @@ export class Graph { * @param w - edge source node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - inEdges(v: string, w?: string): void | Edge[] { + inEdges(v: string, w?: string): Edge[] | undefined { if (this.isDirected()) { return this.filterEdges(this._in[v], v, w); } @@ -767,7 +774,7 @@ export class Graph { * @param w - edge sink node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - outEdges(v: string, w?: string): void | Edge[] { + outEdges(v: string, w?: string): Edge[] | undefined { if (this.isDirected()) { return this.filterEdges(this._out[v], v, w); } @@ -783,10 +790,11 @@ export class Graph { * @param w - edge adjacent node. * @returns edges descriptors list if v is in the graph, or void otherwise. */ - nodeEdges(v: string, w?: string): void | Edge[] { + nodeEdges(v: string, w?: string): Edge[] | undefined { if (v in this._nodes) { return this.filterEdges({...this._in[v]!, ...this._out[v]!}, v, w); } + return undefined; } // Defaults to be set when creating a new node diff --git a/lib/json.ts b/lib/json.ts index 209c787b..34af61a4 100644 --- a/lib/json.ts +++ b/lib/json.ts @@ -1,5 +1,5 @@ import {Graph} from './graph'; -import type {GraphOptions} from './types'; +import type {GraphOptions, Label} from './types'; interface JsonGraph { options: GraphOptions; @@ -93,7 +93,7 @@ function writeEdges(g: Graph): JsonEdge[] { * g2.edges() * // [ { v: 'a', w: 'b' } ] */ -export function read( +export function read( json: JsonGraph ): Graph { const g = new Graph(json.options); diff --git a/lib/types.ts b/lib/types.ts index 5c7f6487..8608b297 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -38,6 +38,9 @@ export type WeightFunction = (e: Edge) => number; /** Function that takes a node and returns the edges incident to it */ export type EdgeFunction = (v: string) => Edge[]; +/** Standard label type for node, edge, or graph labels */ +export type Label = string | number | boolean | Record; + /** Factory function that creates a label for a node */ export type NodeLabelFactory = (v: string) => NodeLabel;