@eramux/graph-structure
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -14,6 +14,8 @@ export declare type NodeId = string | number; | ||
} | ||
export default class Graph { | ||
private edges; | ||
private edgeWeights; | ||
export declare class Graph { | ||
protected _directed: boolean; | ||
private _edges; | ||
private _edgeWeights; | ||
constructor(serialized?: Serialized); | ||
get edges(): Map<NodeId, NodeId[]>; | ||
get nodes(): NodeId[]; | ||
@@ -45,1 +47,4 @@ get entryNodes(): NodeId[]; | ||
} | ||
export declare class UndirectedGraph extends Graph { | ||
constructor(serialized?: Serialized); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UndirectedGraph = exports.Graph = void 0; | ||
class CycleError extends Error { | ||
@@ -11,4 +12,5 @@ constructor(message) { | ||
constructor(serialized) { | ||
this.edges = new Map(); | ||
this.edgeWeights = new Map(); | ||
this._directed = true; | ||
this._edges = new Map(); | ||
this._edgeWeights = new Map(); | ||
if (serialized) { | ||
@@ -18,9 +20,9 @@ this.deserialize(serialized); | ||
} | ||
get edges() { | ||
return this._edges; | ||
} | ||
get nodes() { | ||
const nodeSet = new Set(); | ||
this.edges.forEach((targetNodes, sourceNode) => { | ||
this._edges.forEach((targetNodes, sourceNode) => { | ||
nodeSet.add(sourceNode); | ||
targetNodes.forEach((targetNode) => { | ||
nodeSet.add(targetNode); | ||
}); | ||
}); | ||
@@ -31,3 +33,3 @@ return [...nodeSet.values()]; | ||
const nodeSet = new Set(); | ||
this.edges.forEach((targetNodes, sourceNode) => { | ||
this._edges.forEach((targetNodes, sourceNode) => { | ||
const inboundNodes = this.inbound(sourceNode); | ||
@@ -42,3 +44,3 @@ if (inboundNodes.length === 0) { | ||
const nodeSet = new Set(); | ||
this.edges.forEach((targetNodes, sourceNode) => { | ||
this._edges.forEach((targetNodes, sourceNode) => { | ||
const outboundNodes = this.outbound(sourceNode); | ||
@@ -52,7 +54,7 @@ if (outboundNodes.length === 0) { | ||
addNode(node) { | ||
this.edges.set(node, this.adjacent(node)); | ||
this._edges.set(node, this.adjacent(node)); | ||
return this; | ||
} | ||
removeNode(node) { | ||
this.edges.forEach((targetNodes, sourceNode) => { | ||
this._edges.forEach((targetNodes, sourceNode) => { | ||
targetNodes.forEach((targetNode) => { | ||
@@ -64,7 +66,7 @@ if (targetNode === node) { | ||
}); | ||
this.edges.delete(node); | ||
this._edges.delete(node); | ||
return this; | ||
} | ||
adjacent(node) { | ||
return this.edges.get(node) || []; | ||
return this._edges.get(node) || []; | ||
} | ||
@@ -75,3 +77,3 @@ encodeEdge(sourceNode, targetNode) { | ||
setEdgeWeight(sourceNode, targetNode, weight) { | ||
this.edgeWeights.set(this.encodeEdge(sourceNode, targetNode), weight); | ||
this._edgeWeights.set(this.encodeEdge(sourceNode, targetNode), weight); | ||
return this; | ||
@@ -81,3 +83,3 @@ } | ||
var _a; | ||
return (_a = this.edgeWeights.get(this.encodeEdge(sourceNode, targetNode))) !== null && _a !== void 0 ? _a : 1; | ||
return (_a = this._edgeWeights.get(this.encodeEdge(sourceNode, targetNode))) !== null && _a !== void 0 ? _a : 1; | ||
} | ||
@@ -91,10 +93,21 @@ addEdge(sourceNode, targetNode, weight) { | ||
} | ||
if (!this._directed && targetNode !== sourceNode) { | ||
this.adjacent(targetNode).push(sourceNode); | ||
if (weight !== undefined) { | ||
this.setEdgeWeight(targetNode, sourceNode, weight); | ||
} | ||
} | ||
return this; | ||
} | ||
removeEdge(sourceNode, targetNode) { | ||
if (this.edges.get(sourceNode)) { | ||
this.edges.set(sourceNode, this.adjacent(sourceNode).filter((targetNodes) => { | ||
if (this._edges.get(sourceNode)) { | ||
this._edges.set(sourceNode, this.adjacent(sourceNode).filter((targetNodes) => { | ||
return targetNodes !== targetNode; | ||
})); | ||
} | ||
if (this._edges.get(targetNode) && !this._directed) { | ||
this._edges.set(targetNode, this.adjacent(targetNode).filter((sourceNodes) => { | ||
return sourceNodes !== sourceNode; | ||
})); | ||
} | ||
return this; | ||
@@ -107,3 +120,3 @@ } | ||
const inboundNodes = new Set(); | ||
this.edges.forEach((targetNodes, sourceNode) => { | ||
this._edges.forEach((targetNodes, sourceNode) => { | ||
targetNodes.forEach((targetNode) => { | ||
@@ -119,3 +132,3 @@ if (targetNode === node) { | ||
var _a; | ||
return (_a = this.edges.get(node)) !== null && _a !== void 0 ? _a : []; | ||
return (_a = this._edges.get(node)) !== null && _a !== void 0 ? _a : []; | ||
} | ||
@@ -290,3 +303,3 @@ depthFirstSearch(sourceNodes, includeSourceNodes = true, errorOnCycle = false) { | ||
if (!visitedMap.has(node)) { | ||
const subNodes = this.depthFirstSearch([node]); | ||
const subNodes = this.depthFirstSearch([node], true); | ||
for (const subNode of subNodes) { | ||
@@ -320,3 +333,3 @@ visitedMap.add(subNode); | ||
reset() { | ||
this.edges.clear(); | ||
this._edges.clear(); | ||
} | ||
@@ -335,3 +348,10 @@ deserialize(serialized, reset = false) { | ||
} | ||
exports.default = Graph; | ||
exports.Graph = Graph; | ||
class UndirectedGraph extends Graph { | ||
constructor(serialized) { | ||
super(serialized); | ||
this._directed = false; | ||
} | ||
} | ||
exports.UndirectedGraph = UndirectedGraph; | ||
//# sourceMappingURL=Graph.js.map |
{ | ||
"name": "@eramux/graph-structure", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Typescript graph structure library for common graph operations", | ||
@@ -5,0 +5,0 @@ "main": "dist/Graph.js", |
Sorry, the diff of this file is not supported yet
39860
390