Socket
Socket
Sign inDemoInstall

graphs-for-js

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphs-for-js - npm Package Compare versions

Comparing version 0.4.2 to 1.0.0-beta.0

dist/src/util/functional/CreateEmptyGraphInstance.d.ts

52

dist/index.d.ts
import * as GraphUtility from './src/GraphUtil';
import { ReadonlyWeightedGraph, ReadonlyUnweightedGraph } from './src/system/ReadonlyGraphs';
import { MutableUnweightedGraph, MutableWeightedGraph } from './src/system/MutableGraphs';
/**
* A builder tool for constructing graph data structures. Returns to callback functions,
* either to build to graph with a key function or without a key function.
* @constructor
*/
export declare const GraphBuilder: <V, E = unknown>() => {
withKeyFunction: (fn: (v: V) => string) => {
readonly: (nodes: V[]) => {
directed: {
weighted: (edges: [V, V, E][]) => ReadonlyWeightedGraph<V, E>;
unweighted: (edges: [V, V][]) => ReadonlyUnweightedGraph<V, E>;
};
undirected: {
weighted: (edges: [V, V, E][]) => ReadonlyWeightedGraph<V, E>;
unweighted: (edges: [V, V][]) => ReadonlyUnweightedGraph<V, E>;
};
};
import { MutableWeightedGraph, ReadonlyWeightedGraph, MutableUnweightedGraph, ReadonlyUnweightedGraph } from './src/types/GraphSystem';
declare type UnweightedGraphInit<V, E> = [V, V];
declare type WeightedGraphInit<V, E> = [V, V, E];
export declare class Graph<V, E = never> {
keyFn(fn: (v: V) => string): {
directed: {

@@ -29,14 +15,14 @@ weighted: () => MutableWeightedGraph<V, E>;

};
};
withoutKeyFunction: () => {
readonly: (nodes: V[]) => {
readonly: {
directed: {
weighted: (edges: [V, V, E][]) => ReadonlyWeightedGraph<V, E>;
unweighted: (edges: [V, V][]) => ReadonlyUnweightedGraph<V, E>;
weighted: (init: WeightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyWeightedGraph<V, E>;
unweighted: (init: UnweightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyUnweightedGraph<V, E>;
};
undirected: {
weighted: (edges: [V, V, E][]) => ReadonlyWeightedGraph<V, E>;
unweighted: (edges: [V, V][]) => ReadonlyUnweightedGraph<V, E>;
weighted: (init: WeightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyWeightedGraph<V, E>;
unweighted: (init: UnweightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyUnweightedGraph<V, E>;
};
};
};
noKey(): {
directed: {

@@ -50,4 +36,14 @@ weighted: () => MutableWeightedGraph<V, E>;

};
readonly: {
directed: {
weighted: (init: WeightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyWeightedGraph<V, E>;
unweighted: (init: UnweightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyUnweightedGraph<V, E>;
};
undirected: {
weighted: (init: WeightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyWeightedGraph<V, E>;
unweighted: (init: UnweightedGraphInit<V, E>[], nodes?: V[] | undefined) => ReadonlyUnweightedGraph<V, E>;
};
};
};
};
}
/**

@@ -58,3 +54,3 @@ * A utility object which contains graph algorithm implementations and other graph tools,

export declare const GraphUtil: typeof GraphUtility;
export { GraphType } from './src/types/GraphType';
export { MutableWeightedGraph, ReadonlyGraph, MutableUnweightedGraph, MutableGraph, ValueEdge, BasicEdge, ReadonlyWeightedGraph, ReadonlyUnweightedGraph } from './src/types/GraphSystem';
//# sourceMappingURL=index.d.ts.map

@@ -22,53 +22,59 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphType = exports.GraphUtil = exports.GraphBuilder = void 0;
exports.GraphUtil = exports.Graph = void 0;
var GraphUtility = __importStar(require("./src/GraphUtil"));
var ReadonlyGraphs_1 = require("./src/system/ReadonlyGraphs");
var MutableGraphs_1 = require("./src/system/MutableGraphs");
var builder = function (fn) {
var generator = function (fn) {
return {
readonly: function (nodes) {
return {
directed: {
weighted: function (edges) {
return new ReadonlyGraphs_1.ReadonlyWeightedGraph(nodes, edges, false, fn);
},
unweighted: function (edges) {
return new ReadonlyGraphs_1.ReadonlyUnweightedGraph(nodes, edges, false, true, fn);
}
},
undirected: {
weighted: function (edges) {
return new ReadonlyGraphs_1.ReadonlyWeightedGraph(nodes, edges, true, fn);
},
unweighted: function (edges) {
return new ReadonlyGraphs_1.ReadonlyUnweightedGraph(nodes, edges, true, true, fn);
}
}
};
},
directed: {
weighted: function () { return new MutableGraphs_1.MutableWeightedGraph(false, fn); },
unweighted: function () { return new MutableGraphs_1.MutableUnweightedGraph(false, true, fn); }
weighted: function () { return new MutableGraphs_1.WeightedGraph(false, fn); },
unweighted: function () { return new MutableGraphs_1.UnweightedGraph(false, true, fn); }
},
undirected: {
weighted: function () { return new MutableGraphs_1.MutableWeightedGraph(true, fn); },
unweighted: function () { return new MutableGraphs_1.MutableUnweightedGraph(true, true, fn); }
weighted: function () { return new MutableGraphs_1.WeightedGraph(true, fn); },
unweighted: function () { return new MutableGraphs_1.UnweightedGraph(true, true, fn); }
},
readonly: {
directed: {
weighted: function (init, nodes) {
var g = new MutableGraphs_1.WeightedGraph(false, fn);
nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (n) { return g.insert(n); });
init === null || init === void 0 ? void 0 : init.forEach(function (e) { return g.connect(e[0], e[1], e[2]); });
return g.makeReadonly();
},
unweighted: function (init, nodes) {
var g = new MutableGraphs_1.UnweightedGraph(false, true, fn);
nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (n) { return g.insert(n); });
init === null || init === void 0 ? void 0 : init.forEach(function (e) { return g.connect(e[0], e[1]); });
return g.makeReadonly();
}
},
undirected: {
weighted: function (init, nodes) {
var g = new MutableGraphs_1.WeightedGraph(true, fn);
nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (n) { return g.insert(n); });
init === null || init === void 0 ? void 0 : init.forEach(function (e) { return g.connect(e[0], e[1], e[2]); });
return g.makeReadonly();
},
unweighted: function (init, nodes) {
var g = new MutableGraphs_1.UnweightedGraph(true, true, fn);
nodes === null || nodes === void 0 ? void 0 : nodes.forEach(function (n) { return g.insert(n); });
init === null || init === void 0 ? void 0 : init.forEach(function (e) { return g.connect(e[0], e[1]); });
return g.makeReadonly();
}
}
}
};
};
/**
* A builder tool for constructing graph data structures. Returns to callback functions,
* either to build to graph with a key function or without a key function.
* @constructor
*/
exports.GraphBuilder = function () {
return {
withKeyFunction: function (fn) {
return builder(fn);
},
withoutKeyFunction: function () {
return builder();
}
var Graph = /** @class */ (function () {
function Graph() {
}
Graph.prototype.keyFn = function (fn) {
return generator(fn);
};
};
Graph.prototype.noKey = function () {
return generator();
};
return Graph;
}());
exports.Graph = Graph;
/**

@@ -79,4 +85,2 @@ * A utility object which contains graph algorithm implementations and other graph tools,

exports.GraphUtil = GraphUtility;
var GraphType_1 = require("./src/types/GraphType");
Object.defineProperty(exports, "GraphType", { enumerable: true, get: function () { return GraphType_1.GraphType; } });
//# sourceMappingURL=index.js.map

@@ -1,41 +0,19 @@

export declare const hasCycle: <V, E>(graph: import("./types/GraphSystem").ReadonlyGraph<V, E>) => boolean;
export declare const findShortestPath: <V, E>(graph: import("./types/GraphSystem").ReadonlyGraph<V, E>, start: V, end: V) => {
import * as toMatrix from './util/ToAdjacenyMatrix';
export declare const hasCycle: <V, E>(graph: import("..").ReadonlyUnweightedGraph<V, E>) => boolean;
export declare const findShortestPath: <V, E>(graph: import("..").ReadonlyGraph<V, E>, start: V, end: V) => {
path: V[];
pathSize: number;
};
export declare const json: {
stringify: <V, E>(graph: import("./types/GraphSystem").ReadonlyGraph<V, E>) => string;
parse: <V_1, E_1 = unknown>(jsonString: string, keyFunction?: ((v: V_1) => string) | undefined) => import("./types/GraphSystem").MutableGraph<V_1, E_1> | undefined;
export declare const serialize: {
stringify: <V, E>(graph: import("..").ReadonlyGraph<V, E>) => string;
parse: <V_1, E_1 = unknown>(jsonString: string, keyFunction?: ((v: V_1) => string) | undefined) => import("..").MutableGraph<V_1, E_1> | undefined;
};
export declare const castGraph: <V, E>(g: import("./types/GraphSystem").ReadonlyGraph<V, E>) => {
type: import("..").GraphType.WeightedDirected;
graph: import("./system/MutableGraphs").MutableWeightedGraph<V, E>;
} | {
type: import("..").GraphType.NonWeightedDirected;
graph: import("./system/MutableGraphs").MutableUnweightedGraph<V, E>;
} | {
type: import("..").GraphType.WeightedUndirected;
graph: import("./system/MutableGraphs").MutableWeightedGraph<V, E>;
} | {
type: import("..").GraphType.NonWeightedUndirected;
graph: import("./system/MutableGraphs").MutableUnweightedGraph<V, E>;
} | {
type: import("..").GraphType.ReadonlyWeightedUndirected;
graph: import("./system/ReadonlyGraphs").ReadonlyWeightedGraph<V, E>;
} | {
type: import("..").GraphType.ReadonlyWeightedDirected;
graph: import("./system/ReadonlyGraphs").ReadonlyWeightedGraph<V, E>;
} | {
type: import("..").GraphType.ReadonlyNonWeightedDirected;
graph: import("./system/ReadonlyGraphs").ReadonlyUnweightedGraph<V, E>;
} | {
type: import("..").GraphType.ReadonlyNonWeightedUndirected;
graph: import("./system/ReadonlyGraphs").ReadonlyUnweightedGraph<V, E>;
};
export declare const clone: <V, E>(g: import("./types/GraphSystem").ReadonlyGraph<V, E>) => import("./types/GraphSystem").MutableGraph<V, E>;
export declare const clone: <V, E>(g: import("..").ReadonlyGraph<V, E>) => import("..").MutableUnweightedGraph<V, E>;
export declare const functional: {
subset: <V, E>(g: import("./types/GraphSystem").ReadonlyGraph<V, E>, nodes: V[]) => import("./types/GraphSystem").MutableGraph<V, E>;
mapEdges: <V_1, E_1, R>(g: import("./types/GraphSystem").IReadonlyWeightedGraph<V_1, E_1>, callback: (e: E_1) => R) => import("./types/GraphSystem").IMutableWeightedGraph<V_1, R>;
mapNodes: <V_2, E_2, N>(g: import("./types/GraphSystem").ReadonlyGraph<V_2, E_2>, callback: (v: V_2) => N, newKeyFunction?: ((n: N) => string) | undefined) => import("./types/GraphSystem").MutableGraph<N, E_2>;
subset: <V, E>(g: import("..").ReadonlyGraph<V, E>, nodes: V[] | ((v: V) => boolean)) => import("..").MutableUnweightedGraph<V, E>;
mapEdges: <V_1, E_1, R>(g: import("..").ReadonlyWeightedGraph<V_1, E_1>, callback: (e: E_1) => R) => import("..").MutableWeightedGraph<V_1, R>;
mapNodes: <V_2, E_2, N>(g: import("..").ReadonlyGraph<V_2, E_2>, callback: (v: V_2) => N, newKeyFunction?: ((n: N) => string) | undefined) => import("..").MutableUnweightedGraph<N, E_2>;
};
export declare const topologicalSort: <V, E>(g: import("..").ReadonlyGraph<V, E>) => V[] | undefined;
export declare const toAdjacencyMatrix: <V, E>(g: import("..").ReadonlyGraph<V, E>) => toMatrix.AdjacencyMatrixResult<V, E>;
//# sourceMappingURL=GraphUtil.d.ts.map

@@ -33,18 +33,20 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.functional = exports.clone = exports.castGraph = exports.json = exports.findShortestPath = exports.hasCycle = void 0;
exports.toAdjacencyMatrix = exports.topologicalSort = exports.functional = exports.clone = exports.serialize = exports.findShortestPath = exports.hasCycle = void 0;
var HasCycle = __importStar(require("./util/HasCycle"));
var FindShortestPath = __importStar(require("./util/FindShortestPath"));
var j = __importStar(require("./util/json"));
var caster = __importStar(require("./util/GetExplicitGraph"));
var j = __importStar(require("./util/serialize"));
var cloner = __importStar(require("./util/GraphClone"));
var functionalFn = __importStar(require("./util/functional"));
var tpSort = __importStar(require("./util/TopologicalSort"));
var toMatrix = __importStar(require("./util/ToAdjacenyMatrix"));
exports.hasCycle = HasCycle.hasCycle;
exports.findShortestPath = FindShortestPath.findShortestPath;
exports.json = {
exports.serialize = {
stringify: j.stringify,
parse: j.parse
};
exports.castGraph = caster.castExplicitly;
exports.clone = cloner.clone;
exports.functional = __assign({}, functionalFn);
exports.topologicalSort = tpSort.topologicalSort;
exports.toAdjacencyMatrix = toMatrix.toAdjacencyMatrix;
//# sourceMappingURL=GraphUtil.js.map

@@ -1,5 +0,4 @@

import { BasicEdge, ReadonlyGraph } from '../types/GraphSystem';
import { BasicEdge, ReadonlyUnweightedGraph } from '../types/GraphSystem';
import { DefaultDictionary, Dictionary, Set } from 'typescript-collections';
import { GraphType } from '../types/GraphType';
export declare abstract class AbstractGraph<V, E> implements ReadonlyGraph<V, E> {
export declare abstract class AbstractGraph<V, E> implements ReadonlyUnweightedGraph<V, E> {
readonly toKeyFn: (v: V) => string;

@@ -11,3 +10,3 @@ protected readonly allNodes: Set<V>;

readonly isUnweighted: boolean;
protected constructor(nodes: V[], edges: ([V, V] | [V, V, E])[], isUndirected: boolean, isUnweighted: boolean, keyFn?: (v: V) => string);
protected constructor(isUndirected: boolean, isUnweighted: boolean, keyFn?: (v: V) => string);
contains(...nodes: V[]): boolean;

@@ -23,4 +22,3 @@ count(): number;

outgoingEdgesOf(source: V): BasicEdge<V, E>[];
abstract getGraphType(): GraphType;
}
//# sourceMappingURL=AbstractGraph.d.ts.map

@@ -7,3 +7,3 @@ "use strict";

var AbstractGraph = /** @class */ (function () {
function AbstractGraph(nodes, edges, isUndirected, isUnweighted, keyFn) {
function AbstractGraph(isUndirected, isUnweighted, keyFn) {
var _this = this;

@@ -14,3 +14,2 @@ this.toKeyFn = keyFn !== null && keyFn !== void 0 ? keyFn : DefaultKeyFunction_1.defaultToKeyFunction;

this.isUnweighted = isUnweighted;
nodes.forEach(function (n) { return _this.allNodes.add(n); }, this);
this.sourceToTarget = new typescript_collections_1.DefaultDictionary(function () {

@@ -22,11 +21,2 @@ return new typescript_collections_1.Dictionary(_this.toKeyFn);

}, this.toKeyFn);
edges.forEach(function (e) {
var value = e[2] !== undefined ? e[2] : null;
_this.sourceToTarget.getValue(e[0]).setValue(e[1], value);
_this.targetToSource.getValue(e[1]).setValue(e[0], value);
if (_this.isUndirected) {
_this.sourceToTarget.getValue(e[1]).setValue(e[0], value);
_this.targetToSource.getValue(e[0]).setValue(e[1], value);
}
}, this);
}

@@ -33,0 +23,0 @@ AbstractGraph.prototype.contains = function () {

@@ -1,7 +0,6 @@

import { ReadonlyGraph, ValueEdge, MutableGraph, IMutableWeightedGraph } from '../types/GraphSystem';
import { GraphType } from '../types/GraphType';
import { MutableWeightedGraph, ReadonlyWeightedGraph, MutableUnweightedGraph, ReadonlyUnweightedGraph, ValueEdge } from '../types/GraphSystem';
import { AbstractGraph } from './AbstractGraph';
export declare class MutableUnweightedGraph<V, E = null> extends AbstractGraph<V, E> implements MutableGraph<V, E>, ReadonlyGraph<V, E> {
export declare class UnweightedGraph<V, E = null> extends AbstractGraph<V, E> implements MutableUnweightedGraph<V, E> {
protected madeReadonly: boolean;
constructor(isUndirected: boolean, isUnweighted: boolean, keyFn?: (v: V) => string);
getGraphType(): GraphType;
connect(source: V, target: V, value?: any): boolean;

@@ -11,4 +10,5 @@ disconnect(source: V, target: V, value?: any): boolean;

remove(...nodes: V[]): number;
makeReadonly(): ReadonlyUnweightedGraph<V, E>;
}
export declare class MutableWeightedGraph<V, E> extends MutableUnweightedGraph<V, E> implements IMutableWeightedGraph<V, E> {
export declare class WeightedGraph<V, E> extends UnweightedGraph<V, E> implements MutableWeightedGraph<V, E> {
constructor(isUndirected: boolean, keyFn?: (v: V) => string);

@@ -23,3 +23,4 @@ weightOf(source: V, target: V): E | undefined;

getEdgeValue(source: V, target: V): E | undefined;
makeReadonly(): ReadonlyWeightedGraph<V, E>;
}
//# sourceMappingURL=MutableGraphs.d.ts.map

@@ -38,25 +38,12 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.MutableWeightedGraph = exports.MutableUnweightedGraph = void 0;
var GraphType_1 = require("../types/GraphType");
exports.WeightedGraph = exports.UnweightedGraph = void 0;
var AbstractGraph_1 = require("./AbstractGraph");
var MutableUnweightedGraph = /** @class */ (function (_super) {
__extends(MutableUnweightedGraph, _super);
function MutableUnweightedGraph(isUndirected, isUnweighted, keyFn) {
return _super.call(this, [], [], isUndirected, isUnweighted, keyFn) || this;
var UnweightedGraph = /** @class */ (function (_super) {
__extends(UnweightedGraph, _super);
function UnweightedGraph(isUndirected, isUnweighted, keyFn) {
var _this = _super.call(this, isUndirected, isUnweighted, keyFn) || this;
_this.madeReadonly = false;
return _this;
}
MutableUnweightedGraph.prototype.getGraphType = function () {
if (this.isUnweighted && this.isUndirected) {
return GraphType_1.GraphType.NonWeightedUndirected;
}
else if (this.isUndirected) {
return GraphType_1.GraphType.WeightedUndirected;
}
else if (this.isUnweighted) {
return GraphType_1.GraphType.NonWeightedDirected;
}
else {
return GraphType_1.GraphType.WeightedDirected;
}
};
MutableUnweightedGraph.prototype.connect = function (source, target, value) {
UnweightedGraph.prototype.connect = function (source, target, value) {
if (!this.contains(source, target)) {

@@ -76,3 +63,3 @@ return false;

};
MutableUnweightedGraph.prototype.disconnect = function (source, target, value) {
UnweightedGraph.prototype.disconnect = function (source, target, value) {
if (!this.contains(source, target))

@@ -92,3 +79,3 @@ return false;

};
MutableUnweightedGraph.prototype.insert = function () {
UnweightedGraph.prototype.insert = function () {
var e_1, _a;

@@ -121,3 +108,3 @@ var nodes = [];

};
MutableUnweightedGraph.prototype.remove = function () {
UnweightedGraph.prototype.remove = function () {
var e_2, _a;

@@ -160,15 +147,21 @@ var _this = this;

};
return MutableUnweightedGraph;
UnweightedGraph.prototype.makeReadonly = function () {
this.madeReadonly = true;
return this;
};
return UnweightedGraph;
}(AbstractGraph_1.AbstractGraph));
exports.MutableUnweightedGraph = MutableUnweightedGraph;
var MutableWeightedGraph = /** @class */ (function (_super) {
__extends(MutableWeightedGraph, _super);
function MutableWeightedGraph(isUndirected, keyFn) {
return _super.call(this, isUndirected, false, keyFn) || this;
exports.UnweightedGraph = UnweightedGraph;
var WeightedGraph = /** @class */ (function (_super) {
__extends(WeightedGraph, _super);
function WeightedGraph(isUndirected, keyFn) {
var _this = _super.call(this, isUndirected, false, keyFn) || this;
_this.madeReadonly = false;
return _this;
}
MutableWeightedGraph.prototype.weightOf = function (source, target) {
WeightedGraph.prototype.weightOf = function (source, target) {
var value = this.sourceToTarget.getValue(source).getValue(target);
return value !== null ? value : undefined;
};
MutableWeightedGraph.prototype.edges = function () {
WeightedGraph.prototype.edges = function () {
return _super.prototype.edges.call(this).map(function (e) {

@@ -178,3 +171,3 @@ return __assign(__assign({}, e), { value: e.value });

};
MutableWeightedGraph.prototype.incomingEdgesOf = function (target) {
WeightedGraph.prototype.incomingEdgesOf = function (target) {
return _super.prototype.incomingEdgesOf.call(this, target).map(function (e) {

@@ -184,3 +177,3 @@ return __assign(__assign({}, e), { value: e.value });

};
MutableWeightedGraph.prototype.outgoingEdgesOf = function (source) {
WeightedGraph.prototype.outgoingEdgesOf = function (source) {
return _super.prototype.outgoingEdgesOf.call(this, source).map(function (e) {

@@ -190,17 +183,21 @@ return __assign(__assign({}, e), { value: e.value });

};
MutableWeightedGraph.prototype.hasEdge = function (source, target, value) {
WeightedGraph.prototype.hasEdge = function (source, target, value) {
return _super.prototype.hasEdge.call(this, source, target, value);
};
MutableWeightedGraph.prototype.connect = function (source, target, value) {
WeightedGraph.prototype.connect = function (source, target, value) {
return _super.prototype.connect.call(this, source, target, value);
};
MutableWeightedGraph.prototype.disconnect = function (source, target, value) {
WeightedGraph.prototype.disconnect = function (source, target, value) {
return _super.prototype.disconnect.call(this, source, target, value);
};
MutableWeightedGraph.prototype.getEdgeValue = function (source, target) {
WeightedGraph.prototype.getEdgeValue = function (source, target) {
return this.weightOf(source, target);
};
return MutableWeightedGraph;
}(MutableUnweightedGraph));
exports.MutableWeightedGraph = MutableWeightedGraph;
WeightedGraph.prototype.makeReadonly = function () {
this.madeReadonly = true;
return this;
};
return WeightedGraph;
}(UnweightedGraph));
exports.WeightedGraph = WeightedGraph;
//# sourceMappingURL=MutableGraphs.js.map

@@ -1,2 +0,1 @@

import { GraphType } from './GraphType';
export interface BasicEdge<V, E = unknown> {

@@ -12,2 +11,4 @@ source: V;

export interface ReadonlyGraph<V, E> {
readonly isUnweighted: boolean;
readonly isUndirected: boolean;
readonly toKeyFn: (v: V) => string;

@@ -57,7 +58,2 @@ /**

/**
* Returns an identifier corresponding to the type of the graph, i.e. if it
* is an undirected graph or if its edges have weights.
*/
readonly getGraphType: () => GraphType;
/**
* Return true if an edge from source to the target exists in the graph,

@@ -70,3 +66,5 @@ * otherwise false.

}
export interface MutableGraph<V, E> extends ReadonlyGraph<V, E> {
export interface ReadonlyUnweightedGraph<V, E> extends ReadonlyGraph<V, E> {
}
export interface MutableGraph<V, E> extends ReadonlyUnweightedGraph<V, E> {
/**

@@ -98,4 +96,7 @@ * Add nodes to the graph.

readonly disconnect: (source: V, target: V, value?: E) => boolean;
readonly makeReadonly: () => ReadonlyUnweightedGraph<V, E>;
}
export interface IReadonlyWeightedGraph<V, E> extends ReadonlyGraph<V, E> {
export interface MutableUnweightedGraph<V, E> extends MutableGraph<V, E> {
}
export interface ReadonlyWeightedGraph<V, E> extends ReadonlyUnweightedGraph<V, E> {
readonly weightOf: (source: V, target: V) => E | undefined;

@@ -119,3 +120,3 @@ readonly getEdgeValue: (source: V, target: V) => E | undefined;

}
export interface IMutableWeightedGraph<V, E> extends MutableGraph<V, E>, IReadonlyWeightedGraph<V, E> {
export interface MutableWeightedGraph<V, E> extends MutableUnweightedGraph<V, E>, ReadonlyWeightedGraph<V, E> {
readonly connect: (source: V, target: V, value: E) => boolean;

@@ -127,3 +128,4 @@ readonly disconnect: (source: V, target: V, value?: E) => boolean;

readonly outgoingEdgesOf: (node: V) => ValueEdge<V, E>[];
readonly makeReadonly: () => ReadonlyWeightedGraph<V, E>;
}
//# sourceMappingURL=GraphSystem.d.ts.map

@@ -1,2 +0,2 @@

import { MutableGraph, ReadonlyGraph } from '../../types/GraphSystem';
import { MutableUnweightedGraph, ReadonlyGraph } from '../../types/GraphSystem';
/**

@@ -13,3 +13,3 @@ * Creates a new graph that is a subset of the given graph.

*/
export declare const subsetNode: <V, E>(g: ReadonlyGraph<V, E>, nodes: V[]) => MutableGraph<V, E>;
export declare const subsetNode: <V, E>(g: ReadonlyGraph<V, E>, nodes: V[] | ((v: V) => boolean)) => MutableUnweightedGraph<V, E>;
//# sourceMappingURL=GraphSubset.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.subsetNode = void 0;
var GraphType_1 = require("../../types/GraphType");
var typescript_collections_1 = require("typescript-collections");
var index_1 = require("../../../index");
var CreateEmptyGraphInstance_1 = require("./CreateEmptyGraphInstance");
/**

@@ -20,26 +19,14 @@ * Creates a new graph that is a subset of the given graph.

var setOfNodes = new typescript_collections_1.Set(g.toKeyFn);
nodes.forEach(function (n) { return setOfNodes.add(n); });
var clone;
var builder = index_1.GraphBuilder().withKeyFunction(g.toKeyFn);
switch (g.getGraphType()) {
case GraphType_1.GraphType.WeightedDirected:
case GraphType_1.GraphType.ReadonlyWeightedDirected:
clone = builder.directed.weighted();
break;
case GraphType_1.GraphType.NonWeightedDirected:
case GraphType_1.GraphType.ReadonlyNonWeightedDirected:
clone = builder.directed.unweighted();
break;
case GraphType_1.GraphType.WeightedUndirected:
case GraphType_1.GraphType.ReadonlyWeightedUndirected:
clone = builder.undirected.weighted();
break;
case GraphType_1.GraphType.NonWeightedUndirected:
case GraphType_1.GraphType.ReadonlyNonWeightedUndirected:
clone = builder.undirected.unweighted();
break;
if (nodes instanceof Array) {
nodes.forEach(function (n) { return setOfNodes.add(n); });
}
var clone = CreateEmptyGraphInstance_1.createEmptyGraphInstance(g, g.toKeyFn);
g.nodes().forEach(function (n) {
if (setOfNodes.contains(n))
if (nodes instanceof Array) {
if (setOfNodes.contains(n))
clone.insert(n);
}
else if (nodes(n)) {
clone.insert(n);
}
});

@@ -46,0 +33,0 @@ g.edges().forEach(function (_a) {

@@ -1,4 +0,4 @@

export declare const subset: <V, E>(g: import("../../types/GraphSystem").ReadonlyGraph<V, E>, nodes: V[]) => import("../../types/GraphSystem").MutableGraph<V, E>;
export declare const mapEdges: <V, E, R>(g: import("../../types/GraphSystem").IReadonlyWeightedGraph<V, E>, callback: (e: E) => R) => import("../../types/GraphSystem").IMutableWeightedGraph<V, R>;
export declare const mapNodes: <V, E, N>(g: import("../../types/GraphSystem").ReadonlyGraph<V, E>, callback: (v: V) => N, newKeyFunction?: ((n: N) => string) | undefined) => import("../../types/GraphSystem").MutableGraph<N, E>;
export declare const subset: <V, E>(g: import("../../..").ReadonlyGraph<V, E>, nodes: V[] | ((v: V) => boolean)) => import("../../..").MutableUnweightedGraph<V, E>;
export declare const mapEdges: <V, E, R>(g: import("../../..").ReadonlyWeightedGraph<V, E>, callback: (e: E) => R) => import("../../..").MutableWeightedGraph<V, R>;
export declare const mapNodes: <V, E, N>(g: import("../../..").ReadonlyGraph<V, E>, callback: (v: V) => N, newKeyFunction?: ((n: N) => string) | undefined) => import("../../..").MutableUnweightedGraph<N, E>;
//# sourceMappingURL=index.d.ts.map

@@ -1,2 +0,2 @@

import { IMutableWeightedGraph, IReadonlyWeightedGraph } from '../../types/GraphSystem';
import { ReadonlyWeightedGraph, MutableWeightedGraph } from '../../types/GraphSystem';
/**

@@ -9,3 +9,3 @@ * Creates a new graph that maps the edge values in a given graph to new values determined

*/
export declare const mapEdges: <V, E, R>(g: IReadonlyWeightedGraph<V, E>, callback: (e: E) => R) => IMutableWeightedGraph<V, R>;
export declare const mapEdges: <V, E, R>(g: ReadonlyWeightedGraph<V, E>, callback: (e: E) => R) => MutableWeightedGraph<V, R>;
//# sourceMappingURL=MapEdges.d.ts.map

@@ -24,4 +24,3 @@ "use strict";

exports.mapEdges = void 0;
var GraphType_1 = require("../../types/GraphType");
var index_1 = require("../../../index");
var CreateEmptyGraphInstance_1 = require("./CreateEmptyGraphInstance");
/**

@@ -37,17 +36,3 @@ * Creates a new graph that maps the edge values in a given graph to new values determined

var nodes = g.nodes();
var clone;
switch (g.getGraphType()) {
case GraphType_1.GraphType.WeightedDirected:
case GraphType_1.GraphType.ReadonlyWeightedDirected:
case GraphType_1.GraphType.NonWeightedDirected:
case GraphType_1.GraphType.ReadonlyNonWeightedDirected:
clone = index_1.GraphBuilder().withKeyFunction(g.toKeyFn).directed.weighted();
break;
case GraphType_1.GraphType.WeightedUndirected:
case GraphType_1.GraphType.ReadonlyWeightedUndirected:
case GraphType_1.GraphType.NonWeightedUndirected:
case GraphType_1.GraphType.ReadonlyNonWeightedUndirected:
clone = index_1.GraphBuilder().withKeyFunction(g.toKeyFn).undirected.weighted();
break;
}
var clone = CreateEmptyGraphInstance_1.createEmptyGraphInstance(g, g.toKeyFn, true);
clone.insert.apply(clone, __spread(nodes));

@@ -54,0 +39,0 @@ edges.forEach(function (_a) {

@@ -1,2 +0,2 @@

import { MutableGraph, ReadonlyGraph } from '../../types/GraphSystem';
import { MutableUnweightedGraph, ReadonlyGraph } from '../../types/GraphSystem';
/**

@@ -16,3 +16,3 @@ * Creates a new graph that maps the node values of the given graph to new values

*/
export declare const mapNodes: <V, E, N>(g: ReadonlyGraph<V, E>, callback: (v: V) => N, newKeyFunction?: ((n: N) => string) | undefined) => MutableGraph<N, E>;
export declare const mapNodes: <V, E, N>(g: ReadonlyGraph<V, E>, callback: (v: V) => N, newKeyFunction?: ((n: N) => string) | undefined) => MutableUnweightedGraph<N, E>;
//# sourceMappingURL=MapNodes.d.ts.map

@@ -24,4 +24,3 @@ "use strict";

exports.mapNodes = void 0;
var GraphType_1 = require("../../types/GraphType");
var index_1 = require("../../../index");
var CreateEmptyGraphInstance_1 = require("./CreateEmptyGraphInstance");
/**

@@ -44,24 +43,3 @@ * Creates a new graph that maps the node values of the given graph to new values

var nodes = g.nodes();
var clone;
var builder = newKeyFunction != null
? index_1.GraphBuilder().withKeyFunction(newKeyFunction)
: index_1.GraphBuilder().withoutKeyFunction();
switch (g.getGraphType()) {
case GraphType_1.GraphType.WeightedDirected:
case GraphType_1.GraphType.ReadonlyWeightedDirected:
clone = builder.directed.weighted();
break;
case GraphType_1.GraphType.NonWeightedDirected:
case GraphType_1.GraphType.ReadonlyNonWeightedDirected:
clone = builder.directed.unweighted();
break;
case GraphType_1.GraphType.WeightedUndirected:
case GraphType_1.GraphType.ReadonlyWeightedUndirected:
clone = builder.undirected.weighted();
break;
case GraphType_1.GraphType.NonWeightedUndirected:
case GraphType_1.GraphType.ReadonlyNonWeightedUndirected:
clone = builder.undirected.unweighted();
break;
}
var clone = CreateEmptyGraphInstance_1.createEmptyGraphInstance(g, newKeyFunction);
clone.insert.apply(clone, __spread(nodes.map(function (n) { return callback(n); })));

@@ -68,0 +46,0 @@ edges.forEach(function (_a) {

@@ -1,2 +0,2 @@

import { MutableGraph, ReadonlyGraph } from '../types/GraphSystem';
import { MutableUnweightedGraph, ReadonlyGraph } from '../types/GraphSystem';
/**

@@ -8,3 +8,3 @@ * Creates a clone of the given graph. The clone is a new graph object instance that

*/
export declare const clone: <V, E>(g: ReadonlyGraph<V, E>) => MutableGraph<V, E>;
export declare const clone: <V, E>(g: ReadonlyGraph<V, E>) => MutableUnweightedGraph<V, E>;
//# sourceMappingURL=GraphClone.d.ts.map

@@ -24,4 +24,2 @@ "use strict";

exports.clone = void 0;
var GetExplicitGraph_1 = require("./GetExplicitGraph");
var GraphType_1 = require("../types/GraphType");
var index_1 = require("../../index");

@@ -41,22 +39,15 @@ /**

var clonedGraph;
var builder = index_1.GraphBuilder().withKeyFunction(g.toKeyFn);
var type = GetExplicitGraph_1.castExplicitly(g).type;
switch (type) {
case GraphType_1.GraphType.WeightedDirected:
case GraphType_1.GraphType.ReadonlyWeightedDirected:
clonedGraph = builder.directed.weighted();
break;
case GraphType_1.GraphType.NonWeightedDirected:
case GraphType_1.GraphType.ReadonlyNonWeightedDirected:
clonedGraph = builder.directed.unweighted();
break;
case GraphType_1.GraphType.WeightedUndirected:
case GraphType_1.GraphType.ReadonlyWeightedUndirected:
clonedGraph = builder.undirected.weighted();
break;
case GraphType_1.GraphType.NonWeightedUndirected:
case GraphType_1.GraphType.ReadonlyNonWeightedUndirected:
clonedGraph = builder.undirected.unweighted();
break;
var builder = new index_1.Graph().keyFn(g.toKeyFn);
if (g.isUndirected && g.isUnweighted) {
clonedGraph = builder.undirected.unweighted();
}
else if (g.isUndirected) {
clonedGraph = builder.undirected.weighted();
}
else if (g.isUnweighted) {
clonedGraph = builder.directed.unweighted();
}
else {
clonedGraph = builder.directed.weighted();
}
var nodes = g.nodes();

@@ -63,0 +54,0 @@ var edges = g.edges();

@@ -1,2 +0,2 @@

import { ReadonlyGraph } from '../types/GraphSystem';
import { ReadonlyUnweightedGraph } from '../types/GraphSystem';
/**

@@ -10,3 +10,3 @@ * Returns true if the given graph has a cycle and false otherwise,

*/
export declare const hasCycle: <V, E>(graph: ReadonlyGraph<V, E>) => boolean;
export declare const hasCycle: <V, E>(graph: ReadonlyUnweightedGraph<V, E>) => boolean;
//# sourceMappingURL=HasCycle.d.ts.map

@@ -16,3 +16,2 @@ "use strict";

var typescript_collections_1 = require("typescript-collections");
var GraphType_1 = require("../types/GraphType");
var startSymbol = Symbol('start');

@@ -137,16 +136,9 @@ var hasCycleInUndirectedGraph = function (graph) {

exports.hasCycle = function (graph) {
var graphType = graph.getGraphType();
switch (graphType) {
case GraphType_1.GraphType.NonWeightedDirected:
case GraphType_1.GraphType.WeightedDirected:
case GraphType_1.GraphType.ReadonlyWeightedDirected:
case GraphType_1.GraphType.ReadonlyNonWeightedDirected:
return hasCycleInDirectedGraph(graph);
case GraphType_1.GraphType.NonWeightedUndirected:
case GraphType_1.GraphType.WeightedUndirected:
case GraphType_1.GraphType.ReadonlyNonWeightedUndirected:
case GraphType_1.GraphType.ReadonlyWeightedUndirected:
return hasCycleInUndirectedGraph(graph);
if (graph.isUndirected) {
return hasCycleInUndirectedGraph(graph);
}
else {
return hasCycleInDirectedGraph(graph);
}
};
//# sourceMappingURL=HasCycle.js.map
{
"name": "graphs-for-js",
"version": "0.4.2",
"version": "1.0.0-beta.0",
"description": "Some JavaScript and TypeScript implementation of a graph data structure.\nFeatures:\n\t- Insert and remove nodes. \n\t- Connect and disconnect nodes. \n\t- Algorithms for graph structures.",

@@ -9,6 +9,7 @@ "main": "./dist/index.js",

"scripts": {
"test": "mocha -r ts-node/register 'test/**/*.test.ts'",
"test": "mocha -r ts-node/register --timeout 2000 'test/**/*.test.ts'",
"coverage": "nyc mocha -r ts-node/register -r source-map-support/register --recursive 'test/**/*.test.ts'",
"prepare": "tsc",
"clean": "rm -rf ./dist"
"clean": "rm -rf ./dist ./.nyc_output ./coverage",
"send_coverage": "nyc report --reporter=text-lcov | coveralls"
},

@@ -52,3 +53,3 @@ "publishConfig": {

],
"all": true
"all": false
},

@@ -64,2 +65,3 @@ "devDependencies": {

"chai": "^4.2.0",
"coveralls": "^3.1.0",
"eslint": "^7.7.0",

@@ -72,2 +74,3 @@ "eslint-config-standard": "^14.1.1",

"mocha": "^8.1.2",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^15.1.0",

@@ -74,0 +77,0 @@ "source-map-support": "^0.5.19",

@@ -1,2 +0,2 @@

# graphs-for-js [![NPM version](https://badge.fury.io/js/graphs-for-js.svg)](https://npmjs.org/package/graphs-for-js) [![Build Status](https://travis-ci.org/ayang4114/graphs-for-js.svg?branch=master)](https://travis-ci.org/ayang4114/graphs-for-js)
# graphs-for-js [![NPM version](https://badge.fury.io/js/graphs-for-js.svg)](https://npmjs.org/package/graphs-for-js) [![Build Status](https://travis-ci.org/ayang4114/graphs-for-js.svg?branch=master)](https://travis-ci.org/ayang4114/graphs-for-js) [![Coverage Status](https://coveralls.io/repos/github/ayang4114/graphs-for-js/badge.svg?branch=master)](https://coveralls.io/github/ayang4114/graphs-for-js?branch=master)

@@ -10,19 +10,4 @@ > Implementations of graph data structures for JavaScript and TypeScript

- [Installation](#installation)
- [GraphBuilder Usage](#graphbuilder-usage)
* [Import the GraphBuilder](#import-the-graphbuilder)
* [Key Function](#key-function)
* [JavaScript initialization](#javascript-initialization)
* [TypeScript initialization](#typescript-initialization)
* [Using the Graph](#using-the-graph)
+ [Insert nodes](#insert-nodes)
+ [Removing nodes](#removing-nodes)
+ [Number of nodes](#number-of-nodes)
+ [Forming edges](#forming-edges)
+ [Removing edges](#removing-edges)
+ [Get all of the nodes and edges in the graph](#get-all-of-the-nodes-and-edges-in-the-graph)
+ [Incoming and Outgoing edges](#incoming-and-outgoing-edges)
+ [Degree of Edge](#degree-of-edge)
+ [Existence Methods](#existence-methods)
- [Graph Class Usage](#graph-class-usage)
- [GraphUtil Usage](#graphutil-usage)

@@ -42,5 +27,5 @@ * [Examples](#examples)

## GraphBuilder Usage
## Graph Class Usage
Import the `GraphBuilder` to build and initialize a graph.
Import the `Graph` class to initialize a graph.

@@ -58,2 +43,4 @@ The library supports 4 types of graphs:

For each of the above graph types, there are also readonly, immutable versions.
### Import the GraphBuilder

@@ -63,7 +50,7 @@

// With require()
const {GraphBuilder} = require('graphs-for-js')
const {Graph} = require('graphs-for-js')
// With import syntax
import {GraphBuilder} from 'graphs-for-js'
import {Graph} from 'graphs-for-js'

@@ -74,5 +61,5 @@ ```

Because JavaScript does not directly hash/map objects and their contents to unique values, the GraphBuilder accepts a function for mapping a node value to a string key.
Because JavaScript does not natively hash/map objects and their contents to unique values as object keys, the GraphBuilder accepts a function for mapping a node value to a string key.
If a Key Function is not given, then the default behaviors are the following:
If a Key Function is not given, then the default behaviors on node values are the following:
- Primitive Types

@@ -90,8 +77,7 @@ - This includes number, string, boolean, symbol, null, and undefined.

```js
const weightedGraph = GraphBuilder()
.withKeyFunction(i => `${i}`)
.directed.weighted()
const unweightedGraph = GraphBuilder()
.withoutKeyFunction()
.directed.unweighted()
const weightedGraph = new Graph()
.keyFn(i => `${i}`).directed.weighted()
const unweightedGraph = new Graph()
.noKey().directed.unweighted()
```

@@ -101,13 +87,26 @@

Use type parameters to specify the type of the nodes and of the edge values (if weighted).
Use the type parameters to specify the type of the nodes and of the edge values (if weighted).
```ts
const weightedGraph = GraphBuilder<string, number>()
.withoutKeyFunction()
.directed.weighted()
const unweightedGraph = GraphBuilder<string>()
.withoutKeyFunction()
.directed.unweighted()
const weightedGraph = new Graph<string, number>()
.noKey().directed.weighted()
const unweightedGraph = new Graph<string>()
.noKey().directed.unweighted()
```
You can also initiate a readonly graph which cannot be modified.
```ts
const weightedGraph = new Graph<number, number>()
.noKey().readonly.directed
.weighted([[1, 2, 5], [2, 3, 6]])
// Specify edges and implicitly the nodes
const unweightedGraph = new Graph<number>()
.noKey().readonly.directed
.unweighted([], [2, 3, 4, 5])
// No edges, followed by an array of extra nodes.
```
### Using the Graph

@@ -211,14 +210,76 @@

#### Edge Value
```js
weightedGraph.weightOf(1, 4)
// Returns the value of the edge from nodes 1 to 4, if it exists.
// If it does not exist, it returns undefined.
```
## GraphUtil Usage
Some helper functions are included in the `GraphUtil` import.
The `GraphUtil` module contains some helper functions/algorithms that can be used on the graphs.
- `hasCycle(graph)`
- Returns true if there is a cycle in the graph, false otherwise
- `findShortestPath(graph, start, end)`
- Finds the shortest path from the node `start` to the node `end`. Returns an object with the fields `path` and `pathLength`. If there exists a path, then `path` is an array of nodes in that path in order from `start` to `end`, and `pathLength` is the length of that path, i.e. the number of edges. If a path is not found, then `path` is an empty array, and `pathLength` is `-1`.
- `clone(graph)`
- Creates a new instance of a graph that contains all nodes and edges in the given `graph`. The type of graph returned is the same type of graph given, e.g. if an undirected, unweighted graph is given, then the cloned graph will also be undirected and unweighted.
- `topologicalSort(graph)`
- Topologically sorts the graph. Returns `undefined` if the given graph is not a DAG. Otherwise, it returns an array of nodes in topologically sorted order.
- `toAdjacencyMatrix(graph)`
- Converts the given `graph` into an adjacency matrix. Returns an object with 5 values:
- ```ts
type Result<V, E> = {
// matrix[i][j] is true if there is an edge from node i to node j. Otherwise, it is false
matrix: boolean[][]
// valueMatrix[i][j] returns the value/weight on the edge from node i to j.
// If there is no value or the edge does not exist, it is undefined.
valueMatrix: (E | undefined)[][]
// For each node n, nodeToIndex maps toKeyFn(n) to its index on the adjacency matrix
nodeToIndex: Record<string, number>
// Maps the index number on the adjacency matrix to the actual node value.
indexToNode: V[]
// An array of pairs, the node value and its index on the adjacency matrix.
nodeIndexPairs: {node: V, index: number}[]
}
```
- `functional` utility functions:
- `subset(graph, nodes)`
- Returns a new subgraph instance that contains a subset of its original nodes, where each node in that subset is in `nodes`.
- The return type of `subset` is the same as the return type for `clone`.
- `mapNodes(graph, callback, newKeyFn?)`
- Creates a new graph instance whose nodes are the results of calling the given `callback` function on each node in the given `graph`.
- The key function of the new graph is the given `newKeyFn` or the default key function if not given.
- If 2 or more nodes result in the same key value (because of the callback function or the new key function), then those nodes are merged into one node, and each edge in those nodes are connected the newly merged node. If there was edge between two of those nodes, then the merged node will have a self loop.
- `mapEdges(graph, callback)`
- Creates a new graph instance whose edge values are the results of calling the given `callback` function on each edge value in the given `graph`.
- `serialize` utility functions:
- `stringify(graph)`
- Creates a string using the nodes and edges of the graph.
- `parse(json)`
- Creates a graph using a serialized representation of the graph.
### Examples
```js
const {GraphBuilder, GraphUtil} = require('graphs-for-js')
const {Graph, GraphUtil} = require('graphs-for-js')
const graph = GraphBuilder().withoutKeyFunction().directed.unweighted()
GraphUtil.hasCycle(graph) // Returns true if there exists a cycle in `graph`. Otherwise false
const graph = new Graph().noKey().directed.unweighted()
// Returns true if there exists a cycle in `graph`. Otherwise false
GraphUtil.hasCycle(graph)
/*

@@ -225,0 +286,0 @@ Finds the shortest path from the start node to the end node.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc