graph-typed 1.19.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +449 -0
- package/README.md +700 -0
- package/dist/directed-graph.d.ts +200 -0
- package/dist/directed-graph.js +552 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/undirected-graph.d.ts +127 -0
- package/dist/undirected-graph.js +341 -0
- package/jest.config.js +5 -0
- package/package.json +68 -0
- package/tsconfig.json +37 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from 'data-structure-typed';
|
|
2
|
+
import type { VertexId } from 'data-structure-typed';
|
|
3
|
+
import { IUNDirectedGraph } from 'data-structure-typed';
|
|
4
|
+
export declare class UndirectedVertex<T = number> extends AbstractVertex<T> {
|
|
5
|
+
/**
|
|
6
|
+
* The constructor function initializes a vertex with an optional value.
|
|
7
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
8
|
+
* used to uniquely identify the vertex within a graph or network.
|
|
9
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
|
|
10
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
11
|
+
*/
|
|
12
|
+
constructor(id: VertexId, val?: T);
|
|
13
|
+
}
|
|
14
|
+
export declare class UndirectedEdge<T = number> extends AbstractEdge<T> {
|
|
15
|
+
/**
|
|
16
|
+
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
17
|
+
* value.
|
|
18
|
+
* @param {VertexId} v1 - The first vertex ID of the edge.
|
|
19
|
+
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
20
|
+
* graph edge.
|
|
21
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
22
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to store a value associated
|
|
23
|
+
* with the edge.
|
|
24
|
+
*/
|
|
25
|
+
constructor(v1: VertexId, v2: VertexId, weight?: number, val?: T);
|
|
26
|
+
private _vertices;
|
|
27
|
+
get vertices(): [VertexId, VertexId];
|
|
28
|
+
set vertices(v: [VertexId, VertexId]);
|
|
29
|
+
}
|
|
30
|
+
export declare class UndirectedGraph<V extends UndirectedVertex<any> = UndirectedVertex, E extends UndirectedEdge<any> = UndirectedEdge> extends AbstractGraph<V, E> implements IUNDirectedGraph<V, E> {
|
|
31
|
+
/**
|
|
32
|
+
* The constructor initializes a new Map object to store edges.
|
|
33
|
+
*/
|
|
34
|
+
constructor();
|
|
35
|
+
protected _edges: Map<V, E[]>;
|
|
36
|
+
get edges(): Map<V, E[]>;
|
|
37
|
+
/**
|
|
38
|
+
* The function creates a new vertex with an optional value and returns it.
|
|
39
|
+
* @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
40
|
+
* vertex from another in the graph.
|
|
41
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
42
|
+
* it will be used as the value of the vertex. If no value is provided, the `id` parameter will be used as the value of
|
|
43
|
+
* the vertex.
|
|
44
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `V`.
|
|
45
|
+
*/
|
|
46
|
+
createVertex(id: VertexId, val?: V['val']): V;
|
|
47
|
+
/**
|
|
48
|
+
* The function creates an undirected edge between two vertices with an optional weight and value.
|
|
49
|
+
* @param {VertexId} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
50
|
+
* @param {VertexId} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
51
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
52
|
+
* no weight is provided, it defaults to 1.
|
|
53
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
54
|
+
* is used to store additional information or data associated with the edge.
|
|
55
|
+
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `E`.
|
|
56
|
+
*/
|
|
57
|
+
createEdge(v1: VertexId, v2: VertexId, weight?: number, val?: E['val']): E;
|
|
58
|
+
/**
|
|
59
|
+
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
60
|
+
* @param {V | null | VertexId} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
61
|
+
* object), `null`, or `VertexId` (a string or number representing the ID of a vertex).
|
|
62
|
+
* @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
63
|
+
* object), `null`, or `VertexId` (vertex ID).
|
|
64
|
+
* @returns an edge (E) or null.
|
|
65
|
+
*/
|
|
66
|
+
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null;
|
|
67
|
+
/**
|
|
68
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
69
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
70
|
+
* @param {V | VertexId} v2 - V | VertexId - This parameter can be either a vertex object (V) or a vertex ID
|
|
71
|
+
* (VertexId). It represents the second vertex of the edge that needs to be removed.
|
|
72
|
+
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
73
|
+
*/
|
|
74
|
+
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null;
|
|
75
|
+
/**
|
|
76
|
+
* The removeEdge function removes an edge between two vertices in a graph.
|
|
77
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
78
|
+
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
79
|
+
*/
|
|
80
|
+
removeEdge(edge: E): E | null;
|
|
81
|
+
/**
|
|
82
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
83
|
+
* vertex.
|
|
84
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
85
|
+
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
86
|
+
* edges connected to that vertex.
|
|
87
|
+
*/
|
|
88
|
+
degreeOf(vertexOrId: VertexId | V): number;
|
|
89
|
+
/**
|
|
90
|
+
* The function returns the edges of a given vertex or vertex ID.
|
|
91
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`. A `VertexId` is a
|
|
92
|
+
* unique identifier for a vertex in a graph, while `V` represents the type of the vertex.
|
|
93
|
+
* @returns an array of edges.
|
|
94
|
+
*/
|
|
95
|
+
edgesOf(vertexOrId: VertexId | V): E[];
|
|
96
|
+
/**
|
|
97
|
+
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
98
|
+
* @returns The method `edgeSet()` returns an array of type `E[]`.
|
|
99
|
+
*/
|
|
100
|
+
edgeSet(): E[];
|
|
101
|
+
/**
|
|
102
|
+
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
103
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
104
|
+
* (`VertexId`).
|
|
105
|
+
* @returns an array of vertices (V[]).
|
|
106
|
+
*/
|
|
107
|
+
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
108
|
+
/**
|
|
109
|
+
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
110
|
+
* it returns null.
|
|
111
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
112
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
113
|
+
* graph. If the edge does not exist, it returns `null`.
|
|
114
|
+
*/
|
|
115
|
+
getEndsOfEdge(edge: E): [V, V] | null;
|
|
116
|
+
/**
|
|
117
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
|
|
118
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
119
|
+
* @returns a boolean value.
|
|
120
|
+
*/
|
|
121
|
+
protected _addEdgeOnly(edge: E): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* The function sets the edges of a graph.
|
|
124
|
+
* @param v - A map where the keys are of type V and the values are arrays of type E.
|
|
125
|
+
*/
|
|
126
|
+
protected _setEdges(v: Map<V, E[]>): void;
|
|
127
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
+
if (ar || !(i in from)) {
|
|
36
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
+
ar[i] = from[i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
+
};
|
|
42
|
+
var __values = (this && this.__values) || function(o) {
|
|
43
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
44
|
+
if (m) return m.call(o);
|
|
45
|
+
if (o && typeof o.length === "number") return {
|
|
46
|
+
next: function () {
|
|
47
|
+
if (o && i >= o.length) o = void 0;
|
|
48
|
+
return { value: o && o[i++], done: !o };
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
|
|
55
|
+
/**
|
|
56
|
+
* data-structure-typed
|
|
57
|
+
*
|
|
58
|
+
* @author Tyler Zeng
|
|
59
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
60
|
+
* @license MIT License
|
|
61
|
+
*/
|
|
62
|
+
var data_structure_typed_1 = require("data-structure-typed");
|
|
63
|
+
var data_structure_typed_2 = require("data-structure-typed");
|
|
64
|
+
var UndirectedVertex = /** @class */ (function (_super) {
|
|
65
|
+
__extends(UndirectedVertex, _super);
|
|
66
|
+
/**
|
|
67
|
+
* The constructor function initializes a vertex with an optional value.
|
|
68
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
69
|
+
* used to uniquely identify the vertex within a graph or network.
|
|
70
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
|
|
71
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
72
|
+
*/
|
|
73
|
+
function UndirectedVertex(id, val) {
|
|
74
|
+
return _super.call(this, id, val) || this;
|
|
75
|
+
}
|
|
76
|
+
return UndirectedVertex;
|
|
77
|
+
}(data_structure_typed_2.AbstractVertex));
|
|
78
|
+
exports.UndirectedVertex = UndirectedVertex;
|
|
79
|
+
var UndirectedEdge = /** @class */ (function (_super) {
|
|
80
|
+
__extends(UndirectedEdge, _super);
|
|
81
|
+
/**
|
|
82
|
+
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
83
|
+
* value.
|
|
84
|
+
* @param {VertexId} v1 - The first vertex ID of the edge.
|
|
85
|
+
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
86
|
+
* graph edge.
|
|
87
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
88
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to store a value associated
|
|
89
|
+
* with the edge.
|
|
90
|
+
*/
|
|
91
|
+
function UndirectedEdge(v1, v2, weight, val) {
|
|
92
|
+
var _this = _super.call(this, weight, val) || this;
|
|
93
|
+
_this._vertices = [v1, v2];
|
|
94
|
+
return _this;
|
|
95
|
+
}
|
|
96
|
+
Object.defineProperty(UndirectedEdge.prototype, "vertices", {
|
|
97
|
+
get: function () {
|
|
98
|
+
return this._vertices;
|
|
99
|
+
},
|
|
100
|
+
set: function (v) {
|
|
101
|
+
this._vertices = v;
|
|
102
|
+
},
|
|
103
|
+
enumerable: false,
|
|
104
|
+
configurable: true
|
|
105
|
+
});
|
|
106
|
+
return UndirectedEdge;
|
|
107
|
+
}(data_structure_typed_2.AbstractEdge));
|
|
108
|
+
exports.UndirectedEdge = UndirectedEdge;
|
|
109
|
+
var UndirectedGraph = /** @class */ (function (_super) {
|
|
110
|
+
__extends(UndirectedGraph, _super);
|
|
111
|
+
/**
|
|
112
|
+
* The constructor initializes a new Map object to store edges.
|
|
113
|
+
*/
|
|
114
|
+
function UndirectedGraph() {
|
|
115
|
+
var _this = _super.call(this) || this;
|
|
116
|
+
_this._edges = new Map();
|
|
117
|
+
return _this;
|
|
118
|
+
}
|
|
119
|
+
Object.defineProperty(UndirectedGraph.prototype, "edges", {
|
|
120
|
+
get: function () {
|
|
121
|
+
return this._edges;
|
|
122
|
+
},
|
|
123
|
+
enumerable: false,
|
|
124
|
+
configurable: true
|
|
125
|
+
});
|
|
126
|
+
/**
|
|
127
|
+
* The function creates a new vertex with an optional value and returns it.
|
|
128
|
+
* @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
129
|
+
* vertex from another in the graph.
|
|
130
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
131
|
+
* it will be used as the value of the vertex. If no value is provided, the `id` parameter will be used as the value of
|
|
132
|
+
* the vertex.
|
|
133
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `V`.
|
|
134
|
+
*/
|
|
135
|
+
UndirectedGraph.prototype.createVertex = function (id, val) {
|
|
136
|
+
return new UndirectedVertex(id, val !== null && val !== void 0 ? val : id);
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* The function creates an undirected edge between two vertices with an optional weight and value.
|
|
140
|
+
* @param {VertexId} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
141
|
+
* @param {VertexId} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
142
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
143
|
+
* no weight is provided, it defaults to 1.
|
|
144
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
145
|
+
* is used to store additional information or data associated with the edge.
|
|
146
|
+
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `E`.
|
|
147
|
+
*/
|
|
148
|
+
UndirectedGraph.prototype.createEdge = function (v1, v2, weight, val) {
|
|
149
|
+
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, val);
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
153
|
+
* @param {V | null | VertexId} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
154
|
+
* object), `null`, or `VertexId` (a string or number representing the ID of a vertex).
|
|
155
|
+
* @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
156
|
+
* object), `null`, or `VertexId` (vertex ID).
|
|
157
|
+
* @returns an edge (E) or null.
|
|
158
|
+
*/
|
|
159
|
+
UndirectedGraph.prototype.getEdge = function (v1, v2) {
|
|
160
|
+
var _a;
|
|
161
|
+
var edges = [];
|
|
162
|
+
if (v1 !== null && v2 !== null) {
|
|
163
|
+
var vertex1 = this._getVertex(v1);
|
|
164
|
+
var vertex2_1 = this._getVertex(v2);
|
|
165
|
+
if (vertex1 && vertex2_1) {
|
|
166
|
+
edges = (_a = this._edges.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(function (e) { return e.vertices.includes(vertex2_1.id); });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return edges ? edges[0] || null : null;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
173
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
174
|
+
* @param {V | VertexId} v2 - V | VertexId - This parameter can be either a vertex object (V) or a vertex ID
|
|
175
|
+
* (VertexId). It represents the second vertex of the edge that needs to be removed.
|
|
176
|
+
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
|
|
177
|
+
*/
|
|
178
|
+
UndirectedGraph.prototype.removeEdgeBetween = function (v1, v2) {
|
|
179
|
+
var vertex1 = this._getVertex(v1);
|
|
180
|
+
var vertex2 = this._getVertex(v2);
|
|
181
|
+
if (!vertex1 || !vertex2) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
var v1Edges = this._edges.get(vertex1);
|
|
185
|
+
var removed = null;
|
|
186
|
+
if (v1Edges) {
|
|
187
|
+
removed = (0, data_structure_typed_1.arrayRemove)(v1Edges, function (e) { return e.vertices.includes(vertex2.id); })[0] || null;
|
|
188
|
+
}
|
|
189
|
+
var v2Edges = this._edges.get(vertex2);
|
|
190
|
+
if (v2Edges) {
|
|
191
|
+
(0, data_structure_typed_1.arrayRemove)(v2Edges, function (e) { return e.vertices.includes(vertex1.id); });
|
|
192
|
+
}
|
|
193
|
+
return removed;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* The removeEdge function removes an edge between two vertices in a graph.
|
|
197
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
198
|
+
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
199
|
+
*/
|
|
200
|
+
UndirectedGraph.prototype.removeEdge = function (edge) {
|
|
201
|
+
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
205
|
+
* vertex.
|
|
206
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
207
|
+
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
208
|
+
* edges connected to that vertex.
|
|
209
|
+
*/
|
|
210
|
+
UndirectedGraph.prototype.degreeOf = function (vertexOrId) {
|
|
211
|
+
var _a;
|
|
212
|
+
var vertex = this._getVertex(vertexOrId);
|
|
213
|
+
if (vertex) {
|
|
214
|
+
return ((_a = this._edges.get(vertex)) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* The function returns the edges of a given vertex or vertex ID.
|
|
222
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`. A `VertexId` is a
|
|
223
|
+
* unique identifier for a vertex in a graph, while `V` represents the type of the vertex.
|
|
224
|
+
* @returns an array of edges.
|
|
225
|
+
*/
|
|
226
|
+
UndirectedGraph.prototype.edgesOf = function (vertexOrId) {
|
|
227
|
+
var vertex = this._getVertex(vertexOrId);
|
|
228
|
+
if (vertex) {
|
|
229
|
+
return this._edges.get(vertex) || [];
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
237
|
+
* @returns The method `edgeSet()` returns an array of type `E[]`.
|
|
238
|
+
*/
|
|
239
|
+
UndirectedGraph.prototype.edgeSet = function () {
|
|
240
|
+
var edgeSet = new Set();
|
|
241
|
+
this._edges.forEach(function (edges) {
|
|
242
|
+
edges.forEach(function (edge) {
|
|
243
|
+
edgeSet.add(edge);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
return __spreadArray([], __read(edgeSet), false);
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
250
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
251
|
+
* (`VertexId`).
|
|
252
|
+
* @returns an array of vertices (V[]).
|
|
253
|
+
*/
|
|
254
|
+
UndirectedGraph.prototype.getNeighbors = function (vertexOrId) {
|
|
255
|
+
var e_1, _a;
|
|
256
|
+
var neighbors = [];
|
|
257
|
+
var vertex = this._getVertex(vertexOrId);
|
|
258
|
+
if (vertex) {
|
|
259
|
+
var neighborEdges = this.edgesOf(vertex);
|
|
260
|
+
try {
|
|
261
|
+
for (var neighborEdges_1 = __values(neighborEdges), neighborEdges_1_1 = neighborEdges_1.next(); !neighborEdges_1_1.done; neighborEdges_1_1 = neighborEdges_1.next()) {
|
|
262
|
+
var edge = neighborEdges_1_1.value;
|
|
263
|
+
var neighbor = this._getVertex(edge.vertices.filter(function (e) { return e !== vertex.id; })[0]);
|
|
264
|
+
if (neighbor) {
|
|
265
|
+
neighbors.push(neighbor);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
270
|
+
finally {
|
|
271
|
+
try {
|
|
272
|
+
if (neighborEdges_1_1 && !neighborEdges_1_1.done && (_a = neighborEdges_1.return)) _a.call(neighborEdges_1);
|
|
273
|
+
}
|
|
274
|
+
finally { if (e_1) throw e_1.error; }
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return neighbors;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
281
|
+
* it returns null.
|
|
282
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
283
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
284
|
+
* graph. If the edge does not exist, it returns `null`.
|
|
285
|
+
*/
|
|
286
|
+
UndirectedGraph.prototype.getEndsOfEdge = function (edge) {
|
|
287
|
+
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
var v1 = this._getVertex(edge.vertices[0]);
|
|
291
|
+
var v2 = this._getVertex(edge.vertices[1]);
|
|
292
|
+
if (v1 && v2) {
|
|
293
|
+
return [v1, v2];
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
|
|
301
|
+
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
302
|
+
* @returns a boolean value.
|
|
303
|
+
*/
|
|
304
|
+
UndirectedGraph.prototype._addEdgeOnly = function (edge) {
|
|
305
|
+
var e_2, _a;
|
|
306
|
+
try {
|
|
307
|
+
for (var _b = __values(edge.vertices), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
308
|
+
var end = _c.value;
|
|
309
|
+
var endVertex = this._getVertex(end);
|
|
310
|
+
if (endVertex === null)
|
|
311
|
+
return false;
|
|
312
|
+
if (endVertex) {
|
|
313
|
+
var edges = this._edges.get(endVertex);
|
|
314
|
+
if (edges) {
|
|
315
|
+
edges.push(edge);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
this._edges.set(endVertex, [edge]);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
324
|
+
finally {
|
|
325
|
+
try {
|
|
326
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
327
|
+
}
|
|
328
|
+
finally { if (e_2) throw e_2.error; }
|
|
329
|
+
}
|
|
330
|
+
return true;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* The function sets the edges of a graph.
|
|
334
|
+
* @param v - A map where the keys are of type V and the values are arrays of type E.
|
|
335
|
+
*/
|
|
336
|
+
UndirectedGraph.prototype._setEdges = function (v) {
|
|
337
|
+
this._edges = v;
|
|
338
|
+
};
|
|
339
|
+
return UndirectedGraph;
|
|
340
|
+
}(data_structure_typed_2.AbstractGraph));
|
|
341
|
+
exports.UndirectedGraph = UndirectedGraph;
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graph-typed",
|
|
3
|
+
"version": "1.19.3",
|
|
4
|
+
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rm -rf dist && npx tsc",
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"build:docs": "typedoc --out docs ./src",
|
|
10
|
+
"deps:check": "dependency-cruiser src",
|
|
11
|
+
"build:publish": "npm run build && npm run build:docs && npm publish"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/zrwusa/data-structure-typed"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"Graph",
|
|
19
|
+
"Vertex",
|
|
20
|
+
"Node",
|
|
21
|
+
"Edge",
|
|
22
|
+
"Graph theory",
|
|
23
|
+
"Graph structure",
|
|
24
|
+
"Undirected",
|
|
25
|
+
"Directed",
|
|
26
|
+
"Weighted",
|
|
27
|
+
"Unweighted",
|
|
28
|
+
"Adjacency",
|
|
29
|
+
"Connectivity",
|
|
30
|
+
"Path",
|
|
31
|
+
"Cycle",
|
|
32
|
+
"Traversal",
|
|
33
|
+
"Depth-first search (DFS)",
|
|
34
|
+
"Breadth-first search (BFS)",
|
|
35
|
+
"Graph algorithms",
|
|
36
|
+
"Graph representation",
|
|
37
|
+
"Connectivity matrix",
|
|
38
|
+
"Adjacency list",
|
|
39
|
+
"Adjacency matrix",
|
|
40
|
+
"Graph theory",
|
|
41
|
+
"Network",
|
|
42
|
+
"Node degree",
|
|
43
|
+
"Eulerian path",
|
|
44
|
+
"Hamiltonian cycle",
|
|
45
|
+
"Sparse graph",
|
|
46
|
+
"Dense graph"
|
|
47
|
+
],
|
|
48
|
+
"author": "Tyler Zeng zrwusa@gmail.com",
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/zrwusa/data-structure-typed/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/zrwusa/data-structure-typed#readme",
|
|
54
|
+
"types": "dist/index.d.ts",
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/jest": "^29.5.3",
|
|
57
|
+
"@types/node": "^20.4.9",
|
|
58
|
+
"dependency-cruiser": "^13.1.2",
|
|
59
|
+
"jest": "^29.6.2",
|
|
60
|
+
"ts-jest": "^29.1.1",
|
|
61
|
+
"typedoc": "^0.24.8",
|
|
62
|
+
"typescript": "^4.9.5"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"data-structure-typed": "^1.19.3",
|
|
66
|
+
"zod": "^3.22.2"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true,
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"target": "es5",
|
|
7
|
+
"lib": [
|
|
8
|
+
"esnext",
|
|
9
|
+
],
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"declarationDir": "./dist",
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"downlevelIteration": true,
|
|
16
|
+
"experimentalDecorators": true,
|
|
17
|
+
// "allowJs": true,
|
|
18
|
+
// "allowSyntheticDefaultImports": true,
|
|
19
|
+
// "forceConsistentCasingInFileNames": true,
|
|
20
|
+
// "noFallthroughCasesInSwitch": true,
|
|
21
|
+
// "resolveJsonModule": true,
|
|
22
|
+
// "isolatedModules": true,
|
|
23
|
+
// "noEmit": true,
|
|
24
|
+
"typeRoots": [
|
|
25
|
+
"node_modules/@types"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
"include": [
|
|
30
|
+
"src",
|
|
31
|
+
],
|
|
32
|
+
"exclude": [
|
|
33
|
+
// "node_modules/data-structure-typed",
|
|
34
|
+
"node_modules",
|
|
35
|
+
"dist"
|
|
36
|
+
]
|
|
37
|
+
}
|