api docs deploied, graph basic test completed

This commit is contained in:
Revone 2023-08-14 08:47:26 +08:00
parent 5d25e6f05b
commit d5e64f8127
8 changed files with 85 additions and 7 deletions

View file

@ -16,6 +16,10 @@ yarn add data-structure-typed
npm install data-structure-typed
```
## api docs
[api docs](https://data-structure-typed-docs.vercel.app/)
## data structures
Meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures:

View file

@ -11,7 +11,7 @@ export class AbstractVertex {
this._id = id;
}
private _id: VertexId;
protected _id: VertexId;
public get id(): VertexId {
return this._id;

View file

@ -158,7 +158,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
* 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.
*/
removeEdge(edge: E): E | null {
removeEdge(edge: E ): E | null {
let removed: E | null = null;
const src = this.getVertex(edge.src);
const dest = this.getVertex(edge.dest);
@ -304,7 +304,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
* @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
* the graph. If there is a cycle in the graph, it returns `null`.
*/
topologicalSort(): (V | VertexId)[] | null {
topologicalSort(): V[] | null {
// vector<vector<int>> g;
// vector<int> color;
// int last;
@ -336,14 +336,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
// }
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V, TopologicalStatus>();
const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
for (const entry of this._vertices) {
statusMap.set(entry[1], 0);
}
const sorted: (V | VertexId)[] = [];
const sorted: (V)[] = [];
let hasCycle = false;
const dfs = (cur: V | VertexId) => {
const dfs = (cur: V) => {
statusMap.set(cur, 1);
const children = this.getDestinations(cur);
for (const child of children) {

View file

@ -1,4 +1,4 @@
import {BST, BSTNode} from '../../../src';
import {BST, BSTNode} from '../../../../src';
describe('bst-case6', () => {
it('should perform various operations on a Binary Search Tree', () => {

View file

@ -0,0 +1,71 @@
import { DirectedGraph, DirectedVertex, DirectedEdge } from '../../../../src';
describe('DirectedGraph', () => {
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;
beforeEach(() => {
graph = new DirectedGraph();
});
it('should add vertices', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
expect(graph.containsVertex(vertex1)).toBe(true);
expect(graph.containsVertex(vertex2)).toBe(true);
});
it('should add edges', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
const edge = new DirectedEdge('A', 'B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.containsEdge('A', 'B')).toBe(true);
expect(graph.containsEdge('B', 'A')).toBe(false);
});
it('should remove edges', () => {
const vertex1 = new DirectedVertex('A');
const vertex2 = new DirectedVertex('B');
const edge = new DirectedEdge('A', 'B');
graph.addVertex(vertex1);
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.removeEdge(edge)).toBe(edge);
expect(graph.containsEdge('A', 'B')).toBe(false);
});
// Add more test cases for other methods...
it('should perform topological sort', () => {
// Create a graph with vertices and edges
const vertexA = new DirectedVertex('A');
const vertexB = new DirectedVertex('B');
const vertexC = new DirectedVertex('C');
const edgeAB = new DirectedEdge('A', 'B');
const edgeBC = new DirectedEdge('B', 'C');
graph.addVertex(vertexA);
graph.addVertex(vertexB);
graph.addVertex(vertexC);
graph.addEdge(edgeAB);
graph.addEdge(edgeBC);
// Perform topological sort
const topologicalOrder = graph.topologicalSort();
if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']);
});
// Add more test cases for other methods...
});

View file

@ -0,0 +1,3 @@
export * from './abstract-graph';
export * from './directed-graph.test';
export * from './undirected-graph';