diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index 6ff0bf9..d695009 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -2,5 +2,6 @@
+
\ No newline at end of file
diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts
index 1c96c97..3ecd76b 100644
--- a/src/data-structures/graph/abstract-graph.ts
+++ b/src/data-structures/graph/abstract-graph.ts
@@ -10,12 +10,14 @@ import {PriorityQueue} from '../priority-queue';
import type {DijkstraResult, VertexId} from '../types';
import {IGraph} from '../interfaces';
-export class AbstractVertex {
- constructor(id: VertexId) {
+export abstract class AbstractVertex {
+
+ protected constructor(id: VertexId, val?: V) {
this._id = id;
+ this._val = val;
}
- protected _id: VertexId;
+ private _id: VertexId;
get id(): VertexId {
return this._id;
@@ -24,22 +26,45 @@ export class AbstractVertex {
set id(v: VertexId) {
this._id = v;
}
+
+ private _val: V | undefined;
+
+ get val(): V | undefined {
+ return this._val;
+ }
+
+ set val(value: V | undefined) {
+ this._val = value;
+ }
+
+ // /**
+ // * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ // * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ // * @param id
+ // * @param val
+ // */
+ // abstract _createVertex(id: VertexId, val?: V): AbstractVertex;
}
-export abstract class AbstractEdge {
+export abstract class AbstractEdge {
-
- /**
- * The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
- * no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
- */
- protected constructor(weight?: number) {
+ protected constructor(weight?: number, val?: E) {
this._weight = weight !== undefined ? weight : 1;
+ this._val = val;
this._hashCode = uuidV4();
}
- protected _weight: number;
+ private _val: E | undefined;
+
+ get val(): E | undefined {
+ return this._val;
+ }
+
+ set val(value: E | undefined) {
+ this._val = value;
+ }
+
+ private _weight: number;
get weight(): number {
return this._weight;
@@ -55,40 +80,61 @@ export abstract class AbstractEdge {
return this._hashCode;
}
+ // /**
+ // * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ // * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ // * @param srcOrV1
+ // * @param destOrV2
+ // * @param weight
+ // * @param val
+ // */
+ // abstract _createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): AbstractEdge;
+
protected _setHashCode(v: string) {
this._hashCode = v;
}
}
// Connected Component === Largest Connected Sub-Graph
-export abstract class AbstractGraph implements IGraph {
+export abstract class AbstractGraph implements IGraph {
+ private _vertices: Map> = new Map>();
- protected _vertices: Map = new Map();
-
- abstract removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
-
- abstract removeEdge(edge: E): E | null;
-
- /**
- * The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it
- * does not exist.
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
- * @returns The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter.
- * If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.
- */
- getVertex(vertexOrId: VertexId | V): V | null {
- const vertexId = this.getVertexId(vertexOrId);
- return this._vertices.get(vertexId) || null;
+ get vertices(): Map> {
+ return this._vertices;
}
/**
- * The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as
- * a `VertexId`.
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
- * (`VertexId`).
- * @returns the id of the vertex.
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ * @param id
+ * @param val
*/
- getVertexId(vertexOrId: V | VertexId): VertexId {
+ abstract _createVertex(id: VertexId, val?: V): AbstractVertex;
+
+ /**
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ * @param srcOrV1
+ * @param destOrV2
+ * @param weight
+ * @param val
+ */
+ abstract _createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): AbstractEdge;
+
+ abstract removeEdgeBetween(srcOrId: AbstractVertex | VertexId, destOrId: AbstractVertex | VertexId): AbstractEdge | null;
+
+ abstract removeEdge(edge: AbstractEdge): AbstractEdge | null;
+
+ _getVertex(vertexOrId: VertexId | AbstractVertex): AbstractVertex | null {
+ const vertexId = this._getVertexId(vertexOrId);
+ return this._vertices.get(vertexId) || null;
+ }
+
+ getVertex(vertexId: VertexId): AbstractVertex | null {
+ return this._vertices.get(vertexId) || null;
+ }
+
+ _getVertexId(vertexOrId: AbstractVertex | VertexId): VertexId {
return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
}
@@ -98,29 +144,20 @@ export abstract class AbstractGraph | VertexId): boolean {
+ return this._vertices.has(this._getVertexId(vertexOrId));
}
- /**
- * The function `vertexSet()` returns a map of vertices.
- * @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
- */
- vertexSet(): Map {
- return this._vertices;
+ abstract getEdge(srcOrId: AbstractVertex | VertexId, destOrId: AbstractVertex | VertexId): AbstractEdge | null;
+
+ createAddVertex(id: VertexId, val?: V): boolean {
+ const newVertex = this._createVertex(id, val);
+ return this.addVertex(newVertex);
}
- abstract getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
-
- /**
- * The addVertex function adds a new vertex to a graph if it does not already exist.
- * @param {V} newVertex - The parameter "newVertex" is of type V, which represents a vertex in a graph.
- * @returns The method is returning a boolean value. If the newVertex is already contained in the graph, it will return
- * false. Otherwise, it will add the newVertex to the graph and return true.
- */
- addVertex(newVertex: V): boolean {
+ addVertex(newVertex: AbstractVertex): boolean {
if (this.hasVertex(newVertex)) {
- return false;
+ throw (new Error('Duplicated vertex id is not allowed'));
}
this._vertices.set(newVertex.id, newVertex);
return true;
@@ -132,8 +169,8 @@ export abstract class AbstractGraph | VertexId): boolean {
+ const vertexId = this._getVertexId(vertexOrId);
return this._vertices.delete(vertexId);
}
@@ -144,7 +181,7 @@ export abstract class AbstractGraph[] | VertexId[]): boolean {
const removed: boolean[] = [];
for (const v of vertices) {
removed.push(this.removeVertex(v));
@@ -152,11 +189,11 @@ export abstract class AbstractGraph 0;
}
- abstract degreeOf(vertexOrId: V | VertexId): number;
+ abstract degreeOf(vertexOrId: AbstractVertex | VertexId): number;
- abstract edgeSet(): E[];
+ abstract edgeSet(): AbstractEdge[];
- abstract edgesOf(vertexOrId: V | VertexId): E[];
+ abstract edgesOf(vertexOrId: AbstractVertex | VertexId): AbstractEdge[];
/**
* The function checks if there is an edge between two vertices in a graph.
@@ -167,12 +204,19 @@ export abstract class AbstractGraph, v2: VertexId | AbstractVertex): boolean {
const edge = this.getEdge(v1, v2);
return !!edge;
}
- abstract addEdge(edge: E): boolean;
+ createAddEdge(src: AbstractVertex | VertexId, dest: AbstractVertex | VertexId, weight: number, val: E): boolean {
+ if (src instanceof AbstractVertex) src = src.id;
+ if (dest instanceof AbstractVertex) dest = dest.id;
+ const newEdge = this._createEdge(src, dest, weight, val);
+ return this.addEdge(newEdge);
+ }
+
+ abstract addEdge(edge: AbstractEdge): boolean;
/**
* The function sets the weight of an edge between two vertices in a graph.
@@ -185,7 +229,7 @@ export abstract class AbstractGraph, destOrId: VertexId | AbstractVertex, weight: number): boolean {
const edge = this.getEdge(srcOrId, destOrId);
if (edge) {
edge.weight = weight;
@@ -195,7 +239,7 @@ export abstract class AbstractGraph | VertexId): AbstractVertex[];
/**
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
@@ -206,15 +250,15 @@ export abstract class AbstractGraph | VertexId, v2: AbstractVertex | VertexId): AbstractVertex[][] {
+ const paths: AbstractVertex[][] = [];
+ const vertex1 = this._getVertex(v1);
+ const vertex2 = this._getVertex(v2);
if (!(vertex1 && vertex2)) {
return [];
}
- const dfs = (cur: V, dest: V, visiting: Map, path: V[]) => {
+ const dfs = (cur: AbstractVertex, dest: AbstractVertex, visiting: Map, boolean>, path: AbstractVertex[]) => {
visiting.set(cur, true);
if (cur === dest) {
@@ -226,14 +270,14 @@ export abstract class AbstractGraph vertex === neighbor);
+ arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
}
}
visiting.set(cur, false);
};
- dfs(vertex1, vertex2, new Map(), []);
+ dfs(vertex1, vertex2, new Map, boolean>(), []);
return paths;
}
@@ -242,7 +286,7 @@ export abstract class AbstractGraph[]): number {
let sum = 0;
for (let i = 0; i < path.length; i++) {
sum += this.getEdge(path[i], path[i + 1])?.weight || 0;
@@ -264,7 +308,7 @@ export abstract class AbstractGraph | VertexId, v2: AbstractVertex | VertexId, isWeight?: boolean): number | null {
if (isWeight === undefined) isWeight = false;
if (isWeight) {
@@ -276,14 +320,14 @@ export abstract class AbstractGraph = new Map();
- const queue: V[] = [vertex1];
+ const visited: Map, boolean> = new Map();
+ const queue: AbstractVertex[] = [vertex1];
visited.set(vertex1, true);
let cost = 0;
while (queue.length > 0) {
@@ -321,7 +365,7 @@ export abstract class AbstractGraph | VertexId, v2: AbstractVertex | VertexId, isWeight?: boolean): AbstractVertex[] | null {
if (isWeight === undefined) isWeight = false;
if (isWeight) {
@@ -340,14 +384,14 @@ export abstract class AbstractGraph[] = [];
+ const vertex1 = this._getVertex(v1);
+ const vertex2 = this._getVertex(v2);
if (!(vertex1 && vertex2)) {
return [];
}
- const dfs = (cur: V, dest: V, visiting: Map, path: V[]) => {
+ const dfs = (cur: AbstractVertex, dest: AbstractVertex, visiting: Map, boolean>, path: AbstractVertex[]) => {
visiting.set(cur, true);
if (cur === dest) {
@@ -360,14 +404,14 @@ export abstract class AbstractGraph vertex === neighbor);
+ arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
}
}
visiting.set(cur, false);
};
- dfs(vertex1, vertex2, new Map(), []);
+ dfs(vertex1, vertex2, new Map, boolean>(), []);
return minPath;
}
}
@@ -389,23 +433,23 @@ export abstract class AbstractGraph`.
*/
- dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult {
+ dijkstraWithoutHeap(src: AbstractVertex | VertexId, dest?: AbstractVertex | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult> {
if (getMinDist === undefined) getMinDist = false;
if (genPaths === undefined) genPaths = false;
if (dest === undefined) dest = null;
let minDist = Infinity;
- let minDest: V | null = null;
- let minPath: V[] = [];
- const paths: V[][] = [];
+ let minDest: AbstractVertex | null = null;
+ let minPath: AbstractVertex[] = [];
+ const paths: AbstractVertex[][] = [];
const vertices = this._vertices;
- const distMap: Map = new Map();
- const seen: Set = new Set();
- const preMap: Map = new Map(); // predecessor
- const srcVertex = this.getVertex(src);
+ const distMap: Map, number> = new Map();
+ const seen: Set> = new Set();
+ const preMap: Map, AbstractVertex | null> = new Map(); // predecessor
+ const srcVertex = this._getVertex(src);
- const destVertex = dest ? this.getVertex(dest) : null;
+ const destVertex = dest ? this._getVertex(dest) : null;
if (!srcVertex) {
return null;
@@ -420,7 +464,7 @@ export abstract class AbstractGraph {
let min = Infinity;
- let minV: V | null = null;
+ let minV: AbstractVertex | null = null;
for (const [key, val] of distMap) {
if (!seen.has(key)) {
if (val < min) {
@@ -432,12 +476,12 @@ export abstract class AbstractGraph {
+ const getPaths = (minV: AbstractVertex | null) => {
for (const vertex of vertices) {
const vertexOrId = vertex[1];
if (vertexOrId instanceof AbstractVertex) {
- const path: V[] = [vertexOrId];
+ const path: AbstractVertex[] = [vertexOrId];
let parent = preMap.get(vertexOrId);
while (parent) {
path.push(parent);
@@ -498,17 +542,6 @@ export abstract class AbstractGraph`.
*/
- dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult {
+ dijkstra(src: AbstractVertex | VertexId, dest?: AbstractVertex | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult> {
if (getMinDist === undefined) getMinDist = false;
if (genPaths === undefined) genPaths = false;
if (dest === undefined) dest = null;
let minDist = Infinity;
- let minDest: V | null = null;
- let minPath: V[] = [];
- const paths: V[][] = [];
+ let minDest: AbstractVertex | null = null;
+ let minPath: AbstractVertex[] = [];
+ const paths: AbstractVertex[][] = [];
const vertices = this._vertices;
- const distMap: Map = new Map();
- const seen: Set = new Set();
- const preMap: Map = new Map(); // predecessor
+ const distMap: Map, number> = new Map();
+ const seen: Set> = new Set();
+ const preMap: Map, AbstractVertex | null> = new Map(); // predecessor
- const srcVertex = this.getVertex(src);
- const destVertex = dest ? this.getVertex(dest) : null;
+ const srcVertex = this._getVertex(src);
+ const destVertex = dest ? this._getVertex(dest) : null;
if (!srcVertex) {
return null;
@@ -553,17 +586,17 @@ export abstract class AbstractGraph({comparator: (a, b) => a.id - b.id});
+ const heap = new PriorityQueue<{ id: number, val: AbstractVertex }>({comparator: (a, b) => a.id - b.id});
heap.add({id: 0, val: srcVertex});
distMap.set(srcVertex, 0);
preMap.set(srcVertex, null);
- const getPaths = (minV: V | null) => {
+ const getPaths = (minV: AbstractVertex | null) => {
for (const vertex of vertices) {
const vertexOrId = vertex[1];
if (vertexOrId instanceof AbstractVertex) {
- const path: V[] = [vertexOrId];
+ const path: AbstractVertex[] = [vertexOrId];
let parent = preMap.get(vertexOrId);
while (parent) {
path.push(parent);
@@ -634,15 +667,19 @@ export abstract class AbstractGraph): [AbstractVertex, AbstractVertex] | null;
+
/**
* BellmanFord time:O(VE) space:O(V)
* one to rest pairs
@@ -659,16 +696,16 @@ export abstract class AbstractGraph | VertexId, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean) {
if (getMin === undefined) getMin = false;
if (genPath === undefined) genPath = false;
- const srcVertex = this.getVertex(src);
- const paths: V[][] = [];
- const distMap: Map = new Map();
- const preMap: Map = new Map(); // predecessor
+ const srcVertex = this._getVertex(src);
+ const paths: AbstractVertex[][] = [];
+ const distMap: Map, number> = new Map();
+ const preMap: Map, AbstractVertex> = new Map(); // predecessor
let min = Infinity;
- let minPath: V[] = [];
+ let minPath: AbstractVertex[] = [];
// TODO
let hasNegativeCycle: boolean | undefined;
if (scanNegativeCycle) hasNegativeCycle = false;
@@ -703,7 +740,7 @@ export abstract class AbstractGraph | null = null;
if (getMin) {
distMap.forEach((d, v) => {
if (v !== srcVertex) {
@@ -719,7 +756,7 @@ export abstract class AbstractGraph[] = [vertexOrId];
let parent = preMap.get(vertexOrId);
while (parent !== undefined) {
path.push(parent);
@@ -748,9 +785,10 @@ export abstract class AbstractGraph | null)[][] } {
const idAndVertices = [...this._vertices];
const n = idAndVertices.length;
const costs: number[][] = [];
- const predecessor: (V | null)[][] = [];
+ const predecessor: (AbstractVertex | null)[][] = [];
// successors
for (let i = 0; i < n; i++) {
@@ -800,8 +838,11 @@ export abstract class AbstractGraph = new Map();
- const lowMap: Map = new Map();
+ const dfnMap: Map, number> = new Map();
+ const lowMap: Map, number> = new Map();
const vertices = this._vertices;
vertices.forEach(v => {
dfnMap.set(v, -1);
@@ -845,10 +886,10 @@ export abstract class AbstractGraph[] = [];
+ const bridges: AbstractEdge[] = [];
let dfn = 0;
- const dfs = (cur: V, parent: V | null) => {
+ const dfs = (cur: AbstractVertex, parent: AbstractVertex | null) => {
dfn++;
dfnMap.set(cur, dfn);
lowMap.set(cur, dfn);
@@ -892,10 +933,10 @@ export abstract class AbstractGraph = new Map();
+ let SCCs: Map[]> = new Map();
const getSCCs = () => {
- const SCCs: Map = new Map();
+ const SCCs: Map[]> = new Map();
lowMap.forEach((low, vertex) => {
if (!SCCs.has(low)) {
SCCs.set(low, [vertex]);
@@ -910,9 +951,9 @@ export abstract class AbstractGraph = new Map();
+ const cycles: Map[]> = new Map();
if (needCycles) {
- let SCCs: Map = new Map();
+ let SCCs: Map[]> = new Map();
if (SCCs.size < 1) {
SCCs = getSCCs();
}
@@ -928,6 +969,13 @@ export abstract class AbstractGraph>) {
+ this._vertices = value;
+ }
+
+
// unionFind() {}
/**--- end find cycles --- */
diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts
index abd7fca..29e651a 100644
--- a/src/data-structures/graph/directed-graph.ts
+++ b/src/data-structures/graph/directed-graph.ts
@@ -10,34 +10,45 @@ import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
import type {TopologicalStatus, VertexId} from '../types';
import {IDirectedGraph} from '../interfaces';
-export class DirectedVertex extends AbstractVertex {
+export class DirectedVertex extends AbstractVertex {
/**
- * The constructor function initializes an object with a given id.
- * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
- * vertex within a graph or network.
+ * The constructor function initializes a vertex with an optional value.
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is of type `VertexId`, which is
+ * typically a unique identifier for each vertex in a graph.
+ * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to specify the value
+ * associated with the vertex.
*/
- constructor(id: VertexId) {
- super(id);
+ constructor(id: VertexId, val?: V) {
+ super(id, val);
}
+
+ // _createVertex(id: VertexId, val?: V): DirectedVertex {
+ // return new DirectedVertex(id, val);
+ // }
}
-export class DirectedEdge extends AbstractEdge {
+export class DirectedEdge extends AbstractEdge {
+
/**
- * The constructor function initializes the source and destination vertices of an edge, with an optional weight.
+ * The constructor function initializes the source and destination vertices of an edge, along with an optional weight
+ * and value.
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
* a graph.
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
- * to which an edge is directed.
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
- * between two vertices.
+ * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
+ * is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
+ * If the weight is not provided, it will default to `undefined`.
+ * @param {E} [val] - The "val" parameter is an optional parameter of type E. It represents the value associated with
+ * the edge.
*/
- constructor(src: VertexId, dest: VertexId, weight?: number) {
- super(weight);
+ constructor(src: VertexId, dest: VertexId, weight?: number, val?: E) {
+ super(weight, val);
this._src = src;
this._dest = dest;
}
private _src: VertexId;
+
get src(): VertexId {
return this._src;
}
@@ -55,33 +66,69 @@ export class DirectedEdge extends AbstractEdge {
set dest(v: VertexId) {
this._dest = v;
}
+
+ // _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E): DirectedEdge {
+ // if (weight === undefined || weight === null) weight = 1;
+ // return new DirectedEdge(src, dest, weight, val);
+ // }
}
// Strongly connected, One direction connected, Weakly connected
-export class DirectedGraph extends AbstractGraph implements IDirectedGraph {
-
- protected _outEdgeMap: Map = new Map();
-
- protected _inEdgeMap: Map = new Map();
+export class DirectedGraph extends AbstractGraph implements IDirectedGraph {
constructor() {
super();
}
+ private _outEdgeMap: Map, DirectedEdge[]> = new Map, DirectedEdge[]>();
+
+ get outEdgeMap(): Map, DirectedEdge[]> {
+ return this._outEdgeMap;
+ }
+
+ private _inEdgeMap: Map, DirectedEdge[]> = new Map, DirectedEdge[]>();
+
+ get inEdgeMap(): Map, DirectedEdge[]> {
+ return this._inEdgeMap;
+ }
+
/**
- * The function `getEdge` returns the first edge between two vertices, given their source and destination.
- * @param {V | null | VertexId} srcOrId - The `srcOrId` parameter can be either a vertex object (`V`), a vertex ID
- * (`VertexId`), or `null`. It represents the source vertex of the edge.
- * @param {V | null | VertexId} destOrId - The `destOrId` parameter is either a vertex object (`V`), a vertex ID
- * (`VertexId`), or `null`. It represents the destination vertex of the edge.
- * @returns an edge (E) or null.
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ * @param id
+ * @param val
*/
- getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
- let edges: E[] = [];
+ _createVertex(id: VertexId, val?: V): DirectedVertex {
+ return new DirectedVertex(id, val);
+ }
+
+ /**
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
+ * @param src
+ * @param dest
+ * @param weight
+ * @param val
+ */
+ _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E): DirectedEdge {
+ if (weight === undefined || weight === null) weight = 1;
+ return new DirectedEdge(src, dest, weight, val);
+ }
+
+ /**
+ * The function `getEdge` returns the directed edge between two vertices, given their source and destination.
+ * @param {DirectedVertex | null | VertexId} srcOrId - The source vertex or its ID. It can be either a
+ * DirectedVertex object or a VertexId.
+ * @param {DirectedVertex | null | VertexId} destOrId - The `destOrId` parameter is the destination vertex or its
+ * ID. It can be either a `DirectedVertex` object or a `VertexId` value.
+ * @returns a DirectedEdge object or null.
+ */
+ getEdge(srcOrId: DirectedVertex | null | VertexId, destOrId: DirectedVertex | null | VertexId): DirectedEdge | null {
+ let edges: DirectedEdge[] = [];
if (srcOrId !== null && destOrId !== null) {
- const src: V | null = this.getVertex(srcOrId);
- const dest: V | null = this.getVertex(destOrId);
+ const src: DirectedVertex | null = this._getVertex(srcOrId);
+ const dest: DirectedVertex | null = this._getVertex(destOrId);
if (src && dest) {
const srcOutEdges = this._outEdgeMap.get(src);
@@ -95,19 +142,19 @@ export class DirectedGraph ext
}
/**
- * The `addEdge` function adds an edge to a graph if the source and destination vertices exist.
- * @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in the graph. It contains
- * information about the source vertex (`src`) and the destination vertex (`dest`) of the edge.
- * @returns The `addEdge` function returns a boolean value. It returns `true` if the edge was successfully added to the
- * graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
+ * The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
+ * @param edge - The parameter `edge` is of type `DirectedEdge`, which represents a directed edge in a graph. It
+ * contains two properties:
+ * @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
+ * graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
*/
- addEdge(edge: E): boolean {
+ addEdge(edge: DirectedEdge): boolean {
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
return false;
}
- const srcVertex = this.getVertex(edge.src);
- const destVertex = this.getVertex(edge.dest);
+ const srcVertex = this._getVertex(edge.src);
+ const destVertex = this._getVertex(edge.dest);
// TODO after no-non-null-assertion not ensure the logic
if (srcVertex && destVertex) {
@@ -131,20 +178,20 @@ export class DirectedGraph ext
}
/**
- * The function removes an edge between two vertices in a directed graph and returns the removed edge.
- * @param {V | VertexId} srcOrId - The source vertex or its ID.
- * @param {V | VertexId} destOrId - The `destOrId` parameter in the `removeEdgeBetween` function represents the
- * destination vertex of the edge that needs to be removed. It can be either a vertex object (`V`) or a vertex ID
- * (`VertexId`).
- * @returns The function `removeEdgeBetween` returns the removed edge (`E`) if the edge between the source and
- * destination vertices is successfully removed. If either the source or destination vertex is not found, or if the
- * edge does not exist, it returns `null`.
+ * The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
+ * edge, or null if the edge was not found.
+ * @param {DirectedVertex | VertexId} srcOrId - The `srcOrId` parameter represents either a `DirectedVertex`
+ * object or a `VertexId` value. It is used to specify the source vertex of the edge that you want to remove.
+ * @param {DirectedVertex | VertexId} destOrId - The `destOrId` parameter represents the destination vertex of the
+ * edge that you want to remove. It can be either a `DirectedVertex` object or a `VertexId` value.
+ * @returns The function `removeEdgeBetween` returns the removed edge (`DirectedEdge`) if it exists, or `null` if
+ * the edge does not exist.
*/
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
+ removeEdgeBetween(srcOrId: DirectedVertex | VertexId, destOrId: DirectedVertex | VertexId): DirectedEdge | null {
- const src: V | null = this.getVertex(srcOrId);
- const dest: V | null = this.getVertex(destOrId);
- let removed: E | null = null;
+ const src: DirectedVertex | null = this._getVertex(srcOrId);
+ const dest: DirectedVertex | null = this._getVertex(destOrId);
+ let removed: DirectedEdge | null = null;
if (!src || !dest) {
return null;
}
@@ -154,41 +201,42 @@ export class DirectedGraph ext
/**
* The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
* found.
- * @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
+ * @param {DirectedEdge} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
* object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
* respectively.
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
+ * @returns The method `removeEdge` returns the removed edge (`DirectedEdge`) if it exists, or `null` if the edge does not exist.
*/
- arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
+ arrayRemove>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
}
const destInEdges = this._inEdgeMap.get(dest);
if (destInEdges) {
- removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
+ removed = arrayRemove>(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
}
return removed;
}
/**
- * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
- * found.
- * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
- * and `dest`, which represent the source and destination vertices of the edge, respectively.
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
+ * The `removeEdge` function removes a directed edge from a graph and returns the removed edge, or null if the edge was
+ * not found.
+ * @param edge - The `edge` parameter is an object of type `DirectedEdge`, which represents a directed edge in a
+ * graph. It has two properties:
+ * @returns The function `removeEdge` returns a `DirectedEdge` object if an edge is successfully removed, or `null`
+ * if no edge is removed.
*/
- removeEdge(edge: E): E | null {
- let removed: E | null = null;
- const src = this.getVertex(edge.src);
- const dest = this.getVertex(edge.dest);
+ removeEdge(edge: DirectedEdge): DirectedEdge | null {
+ let removed: DirectedEdge | null = null;
+ const src = this._getVertex(edge.src);
+ const dest = this._getVertex(edge.dest);
if (src && dest) {
const srcOutEdges = this._outEdgeMap.get(src);
if (srcOutEdges && srcOutEdges.length > 0) {
- arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
+ arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
}
const destInEdges = this._inEdgeMap.get(dest);
if (destInEdges && destInEdges.length > 0) {
- removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.id)[0];
+ removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0];
}
}
@@ -198,38 +246,37 @@ export class DirectedGraph ext
/**
* The function removeAllEdges removes all edges between two vertices.
- * @param {VertexId | V} src - The `src` parameter represents the source vertex from which the edges will be removed.
- * It can be either a `VertexId` or a `V` type, which represents the identifier or object of the vertex.
- * @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It can be either a
- * `VertexId` or a vertex object `V`.
- * @returns An empty array is being returned.
+ * @param {VertexId | DirectedVertex} src - The `src` parameter can be either a `VertexId` or a `DirectedVertex`.
+ * @param {VertexId | DirectedVertex} dest - The `dest` parameter represents the destination vertex of an edge. It
+ * can be either a `VertexId` or a `DirectedVertex`.
+ * @returns An empty array of DirectedEdge objects is being returned.
*/
- removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
+ removeAllEdges(src: VertexId | DirectedVertex, dest: VertexId | DirectedVertex): DirectedEdge[] {
return [];
}
/**
- * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
- * (`VertexId`).
- * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
+ * The function returns an array of incoming edges of a given vertex or vertex ID.
+ * @param {DirectedVertex | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `DirectedVertex`
+ * object or a `VertexId`.
+ * @returns The method `incomingEdgesOf` returns an array of `DirectedEdge` objects.
*/
- incomingEdgesOf(vertexOrId: V | VertexId): E[] {
- const target = this.getVertex(vertexOrId);
+ incomingEdgesOf(vertexOrId: DirectedVertex | VertexId): DirectedEdge[] {
+ const target = this._getVertex(vertexOrId);
if (target) {
- return this._inEdgeMap.get(target) || [];
+ return this.inEdgeMap.get(target) || []
}
return [];
}
/**
- * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
- * (`VertexId`).
- * @returns The method `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
+ * The function `outgoingEdgesOf` returns an array of outgoing directed edges from a given vertex or vertex ID.
+ * @param {DirectedVertex | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `DirectedVertex`
+ * object or a `VertexId`.
+ * @returns The method `outgoingEdgesOf` returns an array of `DirectedEdge` objects.
*/
- outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
- const target = this.getVertex(vertexOrId);
+ outgoingEdgesOf(vertexOrId: DirectedVertex | VertexId): DirectedEdge[] {
+ const target = this._getVertex(vertexOrId);
if (target) {
return this._outEdgeMap.get(target) || [];
}
@@ -237,71 +284,76 @@ export class DirectedGraph ext
}
/**
- * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
- * @returns the sum of the out-degree and in-degree of the specified vertex or vertex ID.
+ * The function "degreeOf" returns the total degree of a vertex in a directed graph, which is the sum of its out-degree
+ * and in-degree.
+ * @param {VertexId | DirectedVertex} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a
+ * `DirectedVertex`.
+ * @returns The sum of the out-degree and in-degree of the given vertex or vertex ID.
*/
- degreeOf(vertexOrId: VertexId | V): number {
+ degreeOf(vertexOrId: VertexId | DirectedVertex): number {
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
}
/**
- * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
+ * The function "inDegreeOf" returns the number of incoming edges for a given vertex or vertex ID in a directed graph.
+ * @param {VertexId | DirectedVertex} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a
+ * `DirectedVertex`.
* @returns The number of incoming edges of the specified vertex or vertex ID.
*/
- inDegreeOf(vertexOrId: VertexId | V): number {
+ inDegreeOf(vertexOrId: VertexId | DirectedVertex): number {
return this.incomingEdgesOf(vertexOrId).length;
}
/**
- * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
+ * The function "outDegreeOf" returns the number of outgoing edges from a given vertex.
+ * @param {VertexId | DirectedVertex} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a
+ * `DirectedVertex`.
* @returns The number of outgoing edges from the specified vertex or vertex ID.
*/
- outDegreeOf(vertexOrId: VertexId | V): number {
+ outDegreeOf(vertexOrId: VertexId | DirectedVertex): number {
return this.outgoingEdgesOf(vertexOrId).length;
}
/**
- * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
- * @returns The function `edgesOf` returns an array of edges.
+ * The function "edgesOf" returns an array of both outgoing and incoming directed edges of a given vertex or vertex ID.
+ * @param {VertexId | DirectedVertex} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a
+ * `DirectedVertex`.
+ * @returns an array of directed edges.
*/
- edgesOf(vertexOrId: VertexId | V): E[] {
+ edgesOf(vertexOrId: VertexId | DirectedVertex): DirectedEdge[] {
return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
}
/**
- * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
- * @param {E} e - E - an edge object
- * @returns the source vertex of the given edge, or null if the edge does not exist.
+ * The function "getEdgeSrc" returns the source vertex of a directed edge, or null if the edge does not exist.
+ * @param e - A directed edge object of type E.
+ * @returns either a DirectedVertex object or null.
*/
- getEdgeSrc(e: E): V | null {
- return this.getVertex(e.src);
+ getEdgeSrc(e: DirectedEdge): DirectedVertex