https://github.com/mzusin/mz-graph
Typescript implementation of graphs.
https://github.com/mzusin/mz-graph
adjacency-list adjacency-matrix bfs cycle-detection dfs dijkstra dijkstra-algorithm disjoint-set-union dsu graph matrix union-find
Last synced: 2 months ago
JSON representation
Typescript implementation of graphs.
- Host: GitHub
- URL: https://github.com/mzusin/mz-graph
- Owner: mzusin
- License: mit
- Created: 2023-11-11T22:22:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-09T14:32:27.000Z (12 months ago)
- Last Synced: 2025-01-30T01:18:20.727Z (4 months ago)
- Topics: adjacency-list, adjacency-matrix, bfs, cycle-detection, dfs, dijkstra, dijkstra-algorithm, disjoint-set-union, dsu, graph, matrix, union-find
- Language: TypeScript
- Homepage:
- Size: 187 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Graphs
Typescript implementation of graphs.
**Adjacency List Representation**
```ts
export type Label = string|number;export interface IVertex {
label: Label;
edgeWeight: T;
}export type AdjacencyList = Map[]>;
export interface IGraph {
addVertex: (label: Label) => void;
getVertex: (label: Label) => IVertex[]|null;
hasVertex: (label: Label) => boolean;
addEdge: (source: Label, destination: Label, edgeWeight?: T) => void;
printGraph: () => void;bfs: (callback: (label: Label) => void, startLabel?: Label) => void;
dfs: (callback: (label: Label) => void, startLabel?: Label) => void;
dfsRecursive: (callback: (label: Label, startLabel?: Label) => void) => void;hasCycle: () => boolean;
findShortestPathDijkstra: (startLabel: Label) => Map;
}export interface IAdjacencyListOptions {
isDirected?: boolean;
initial?: { [key: Label]: IVertex[] };
}export const graph: (options: IAdjacencyListOptions) => IGraph;
```Usage example:
```ts
const myGraph: IGraph = graph({
isDirected: true,
initial: {
A: [{ label: 'B', edgeWeight: 10 }],
B: [{ label: 'C', edgeWeight: 20 }],
C: [],
}
});// or
const myGraph: IGraph = graph({
isDirected: false
});// add/get a vertex
myGraph.addVertex('A'); // or use a number myGraph.addVertex(10);
console.log(myGraph.getVertex('A'));// add an edge
myGraph.addVertex('B');
myGraph.addVertex('C');
myGraph.addEdge('A', 'B', 10);
myGraph.addEdge('B', 'C');// print the graph
myGraph.printGraph();myGraph.bfs((label: Label) => {
console.log(label);
});myGraph.dfs((label: Label) => {
console.log(label);
});myGraph.dfsRecursive((label: Label) => {
console.log(label);
});console.log(myGraph.hasCycle()); // true or false
const distances = myGraph.findShortestPathDijkstra('A');
```**Adjacency Matrix Representation**
```ts
export type AdjacencyMatrix = T[][];export interface IMatrix {
getMatrix: () => AdjacencyMatrix;
addEdge: (source: number, destination: number, weight: T) => void;
printGraph: () => void;
bfs: (callback: (row: number, col: number, value: T) => void) => void;
dfs: (callback: (row: number, col: number, value: T) => void) => void;
}export interface IAdjacencyMatrixOptions {
isDirected?: boolean;
rowsCount?: number;
columnsCount?: number;
defaultValue?: T;
initial?: T[][];
}export const matrix: (options: IAdjacencyMatrixOptions) => IMatrix;
```Usage example:
```ts
const myGraph: IMatrix = matrix({
initial: [
[2, 1],
[1, 2],
]
});// or
// create a matrix 2x2 with default value 0
const myGraph: IMatrix = matrix({
isDirected: false,
rowsCount: 2,
columnsCount: 2,
defaultValue: 0,
});// add edge, row = 0, col = 1, weight = 5
myGraph.addEdge(0, 1, 5);// get matrix
const res = myGraph.getMatrix();// print the matrix
myGraph.printGraph();myGraph.bfs((row: number, col: number, value: number) => {
console.log(row, col, value);
});myGraph.dfs((row: number, col: number, value: number) => {
console.log(row, col, value);
});
```**Union-Find**
```ts
const uf: IUnionFind = unionFind(5);expect(uf.union(0, 1)).toBe(true);
expect(uf.find(0)).toBe(0);
expect(uf.find(1)).toBe(0);expect(uf.union(1, 2)).toBe(true);
expect(uf.find(2)).toBe(0);expect(uf.union(3, 4)).toBe(true);
expect(uf.find(3)).toBe(3);
expect(uf.find(4)).toBe(3);expect(uf.union(2, 4)).toBe(true);
expect(uf.find(0)).toBe(0);
expect(uf.find(1)).toBe(0);
expect(uf.find(2)).toBe(0);
expect(uf.find(3)).toBe(0);
expect(uf.find(4)).toBe(0);
```