This is a standalone Directed Graph data structure from the data-structure-typed collection. If you wish to access more
data structures or advanced features, you can transition to directly installing the
complete data-structure-typed package
typescript
// Create a simple directed graph
const graph = new DirectedGraph(); // Add vertices
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
// Verify vertices exist
console.log(graph.hasVertex('A')); // true;
console.log(graph.hasVertex('B')); // true;
console.log(graph.hasVertex('C')); // true;
console.log(graph.hasVertex('D')); // false;
// Check vertex count
console.log(graph.size); // 3;
typescript
const graph = new DirectedGraph(); // Add vertices
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
// Add directed edges
graph.addEdge('A', 'B', 1);
graph.addEdge('B', 'C', 2);
graph.addEdge('A', 'C', 3);
// Verify edges exist
console.log(graph.hasEdge('A', 'B')); // true;
console.log(graph.hasEdge('B', 'C')); // true;
console.log(graph.hasEdge('C', 'B')); // false; // Graph is directed
// Get neighbors of A
const neighborsA = graph.getNeighbors('A');
console.log(neighborsA[0].key); // 'B';
console.log(neighborsA[1].key); // 'C';
typescript
const graph = new DirectedGraph(); // Build a small graph
graph.addVertex('X');
graph.addVertex('Y');
graph.addVertex('Z');
graph.addEdge('X', 'Y', 1);
graph.addEdge('Y', 'Z', 2);
// Delete an edge
graph.deleteEdgeSrcToDest('X', 'Y');
console.log(graph.hasEdge('X', 'Y')); // false;
// Edge in other direction should not exist
console.log(graph.hasEdge('Y', 'X')); // false;
// Other edges should remain
console.log(graph.hasEdge('Y', 'Z')); // true;
// Delete a vertex
graph.deleteVertex('Y');
console.log(graph.hasVertex('Y')); // false;
console.log(graph.size); // 2;
typescript
const graph = new DirectedGraph(); // Build a DAG (Directed Acyclic Graph) for task dependencies
graph.addVertex('Design');
graph.addVertex('Implement');
graph.addVertex('Test');
graph.addVertex('Deploy');
// Add dependency edges
graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
// Topological sort gives valid execution order
const executionOrder = graph.topologicalSort();
console.log(executionOrder); // defined;
console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
// All vertices should be included
console.log(executionOrder?.length); // 4;
typescript
// Build a weighted directed graph representing network nodes and costs
const network = new DirectedGraph(); // Add network nodes
network.addVertex('Router-A');
network.addVertex('Router-B');
network.addVertex('Router-C');
network.addVertex('Router-D');
network.addVertex('Router-E');
// Add weighted edges (network latency costs)
network.addEdge('Router-A', 'Router-B', 5);
network.addEdge('Router-A', 'Router-C', 10);
network.addEdge('Router-B', 'Router-D', 3);
network.addEdge('Router-C', 'Router-D', 2);
network.addEdge('Router-D', 'Router-E', 4);
network.addEdge('Router-B', 'Router-E', 12);
// Find shortest path from Router-A to Router-E
const { minDist, minPath } = network.dijkstra('Router-A', 'Router-E', true, true) || {
minDist: undefined,
minPath: undefined
};
// Verify shortest path is found
console.log(minDist); // defined;
console.log(minPath); // defined;
// Shortest path should be A -> B -> D -> E with cost 5+3+4=12
// Or A -> C -> D -> E with cost 10+2+4=16
// So the minimum is 12
console.log(minDist); // <= 16;
// Verify path is valid (includes start and end)
console.log(minPath?.[0].key); // 'Router-A';
console.log(minPath?.[minPath.length - 1].key); // 'Router-E';
Principle |
Description |
|---|
Practicality |
Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names. |
Extensibility |
Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures. |
Modularization |
Includes data structure modularization and independent NPM packages. |
Efficiency |
All methods provide time and space complexity, comparable to native JS performance. |
Maintainability |
Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns. |
Testability |
Automated and customized unit testing, performance testing, and integration testing. |
Portability |
Plans for porting to Java, Python, and C++, currently achieved to 80%. |
Reusability |
Fully decoupled, minimized side effects, and adheres to OOP. |
Security |
Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects. |
Scalability |
Data structure software does not involve load issues. |