
What
Brief
This is a standalone Undirected 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
How
install
npm
npm i undirected-graph-typed --save
yarn
yarn add undirected-graph-typed
snippet
basic UndirectedGraph vertex and edge creation
const graph = new UndirectedGraph<string>();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
console.log(graph.hasVertex('A'));
console.log(graph.hasVertex('B'));
console.log(graph.hasVertex('E'));
console.log(graph.size);
UndirectedGraph edge operations (bidirectional)
const graph = new UndirectedGraph<string>();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addEdge('A', 'B', 1);
graph.addEdge('B', 'C', 2);
graph.addEdge('A', 'C', 3);
console.log(graph.hasEdge('A', 'B'));
console.log(graph.hasEdge('B', 'A'));
console.log(graph.hasEdge('C', 'B'));
console.log(graph.hasEdge('B', 'C'));
const neighborsA = graph.getNeighbors('A');
console.log(neighborsA[0].key);
console.log(neighborsA[1].key);
UndirectedGraph deleteEdge and vertex operations
const graph = new UndirectedGraph<string>();
graph.addVertex('X');
graph.addVertex('Y');
graph.addVertex('Z');
graph.addEdge('X', 'Y', 1);
graph.addEdge('Y', 'Z', 2);
graph.addEdge('X', 'Z', 3);
graph.deleteEdge('X', 'Y');
console.log(graph.hasEdge('X', 'Y'));
console.log(graph.hasEdge('Y', 'X'));
console.log(graph.hasEdge('Y', 'Z'));
console.log(graph.hasEdge('Z', 'Y'));
graph.deleteVertex('Y');
console.log(graph.hasVertex('Y'));
console.log(graph.size);
UndirectedGraph connectivity and neighbors
const graph = new UndirectedGraph<string>();
const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
for (const person of people) {
graph.addVertex(person);
}
graph.addEdge('Alice', 'Bob', 1);
graph.addEdge('Alice', 'Charlie', 1);
graph.addEdge('Bob', 'Diana', 1);
graph.addEdge('Charlie', 'Eve', 1);
graph.addEdge('Diana', 'Eve', 1);
const aliceFriends = graph.getNeighbors('Alice');
console.log(aliceFriends[0].key);
console.log(aliceFriends[1].key);
console.log(aliceFriends.length);
const dianaFriends = graph.getNeighbors('Diana');
console.log(dianaFriends[0].key);
console.log(dianaFriends[1].key);
console.log(dianaFriends.length);
const bobFriends = graph.getNeighbors('Bob');
console.log(bobFriends[0].key);
console.log(bobFriends[1].key);
UndirectedGraph for social network connectivity analysis
interface Person {
id: number;
name: string;
location: string;
}
const socialNetwork = new UndirectedGraph<number, Person>();
const people: [number, Person][] = [
[1, { id: 1, name: 'Alice', location: 'New York' }],
[2, { id: 2, name: 'Bob', location: 'San Francisco' }],
[3, { id: 3, name: 'Charlie', location: 'Boston' }],
[4, { id: 4, name: 'Diana', location: 'New York' }],
[5, { id: 5, name: 'Eve', location: 'Seattle' }]
];
for (const [id] of people) {
socialNetwork.addVertex(id);
}
socialNetwork.addEdge(1, 2, 1);
socialNetwork.addEdge(1, 3, 1);
socialNetwork.addEdge(2, 4, 1);
socialNetwork.addEdge(3, 5, 1);
socialNetwork.addEdge(4, 5, 1);
console.log(socialNetwork.size);
const aliceConnections = socialNetwork.getNeighbors(1);
console.log(aliceConnections[0].key);
console.log(aliceConnections[1].key);
console.log(aliceConnections.length);
console.log(socialNetwork.hasEdge(1, 2));
console.log(socialNetwork.hasEdge(2, 1));
socialNetwork.deleteVertex(2);
console.log(socialNetwork.hasVertex(2));
console.log(socialNetwork.size);
const updatedAliceConnections = socialNetwork.getNeighbors(1);
console.log(updatedAliceConnections[0].key);
console.log(updatedAliceConnections[1]);
const dianaConnections = socialNetwork.getNeighbors(4);
console.log(dianaConnections[0].key);
console.log(dianaConnections[1]);
API docs & Examples
API Docs
Live Examples
Examples Repository
Data Structures
| Data Structure |
Unit Test |
Performance Test |
API Docs |
| Undirected Graph |
 |
 |
UndirectedGraph |
Standard library data structure comparison
| Data Structure Typed |
C++ STL |
java.util |
Python collections |
| UndirectedGraph<V, E> |
- |
- |
- |
Benchmark
Built-in classic algorithms
| Algorithm |
Function Description |
Iteration Type |
| Graph DFS |
Traverse a graph in a depth-first manner, starting from a given node, exploring along one path as deeply as
possible, and backtracking to explore other paths. Used for finding connected components, paths, etc.
|
Recursion + Iteration |
| Graph BFS |
Traverse a graph in a breadth-first manner, starting from a given node, first visiting nodes directly connected
to the starting node, and then expanding level by level. Used for finding shortest paths, etc.
|
Recursion + Iteration |
| Graph Tarjan's Algorithm |
Find strongly connected components in a graph, typically implemented using depth-first search. |
Recursion |
| Graph Bellman-Ford Algorithm |
Finding the shortest paths from a single source, can handle negative weight edges |
Iteration |
| Graph Dijkstra's Algorithm |
Finding the shortest paths from a single source, cannot handle negative weight edges |
Iteration |
| Graph Floyd-Warshall Algorithm |
Finding the shortest paths between all pairs of nodes |
Iteration |
| Graph getCycles |
Find all cycles in a graph or detect the presence of cycles. |
Recursion |
| Graph getCutVertexes |
Find cut vertices in a graph, which are nodes that, when removed, increase the number of connected components in
the graph.
|
Recursion |
| Graph getSCCs |
Find strongly connected components in a graph, which are subgraphs where any two nodes can reach each other.
|
Recursion |
| Graph getBridges |
Find bridges in a graph, which are edges that, when removed, increase the number of connected components in the
graph.
|
Recursion |
| Graph topologicalSort |
Perform topological sorting on a directed acyclic graph (DAG) to find a linear order of nodes such that all
directed edges go from earlier nodes to later nodes.
|
Recursion |
Software Engineering Design Standards
| 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. |