mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
api docs deploied, graph basic test completed
This commit is contained in:
parent
5d25e6f05b
commit
d5e64f8127
|
@ -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:
|
||||
|
|
|
@ -11,7 +11,7 @@ export class AbstractVertex {
|
|||
this._id = id;
|
||||
}
|
||||
|
||||
private _id: VertexId;
|
||||
protected _id: VertexId;
|
||||
|
||||
public get id(): VertexId {
|
||||
return this._id;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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', () => {
|
0
tests/unit/data-structures/graph/abstract-graph.ts
Normal file
0
tests/unit/data-structures/graph/abstract-graph.ts
Normal file
71
tests/unit/data-structures/graph/directed-graph.test.ts
Normal file
71
tests/unit/data-structures/graph/directed-graph.test.ts
Normal 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...
|
||||
});
|
3
tests/unit/data-structures/graph/index.ts
Normal file
3
tests/unit/data-structures/graph/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export * from './abstract-graph';
|
||||
export * from './directed-graph.test';
|
||||
export * from './undirected-graph';
|
Loading…
Reference in a new issue