Skip to content

Commit 877f7e4

Browse files
authored
Merge pull request #16 from datastructures-js/development
add typescript
2 parents a7587af + e7fc143 commit 877f7e4

10 files changed

Lines changed: 85 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
## [v5.1.0] - 2021-06-20
9+
### Added
10+
- typescript.
11+
812
## [v5.0.1] - 2021-04-15
913
### Fixed
1014
- README

README.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
[![npm](https://img.shields.io/npm/v/@datastructures-js/graph.svg)](https://www.npmjs.com/package/@datastructures-js/graph)
55
[![npm](https://img.shields.io/npm/dm/@datastructures-js/graph.svg)](https://www.npmjs.com/package/@datastructures-js/graph) [![npm](https://img.shields.io/badge/node-%3E=%206.0-blue.svg)](https://www.npmjs.com/package/@datastructures-js/graph)
66

7+
Graph & Directed Graph implementation in javascript.
8+
9+
<img src="https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg">
10+
711
<table><tr><td>
812
<img alt="graph" src="https://user-images.githubusercontent.com/6517308/71645678-802cd500-2ca1-11ea-96fb-11a71fd95191.jpg">
913
</td></tr></table>
1014

11-
# Table of Contents
15+
# Contents
1216
* [Install](#install)
17+
* [require](#require)
18+
* [import](#import)
1319
* [API](#api)
14-
* [require](#require)
15-
* [import](#import)
16-
* [new](#new)
20+
* [constructor](#constructor)
1721
* [.addVertex(key, value)](#addvertexkey-value)
1822
* [.hasVertex(key)](#hasvvertex-key)
1923
* [.getVerticesCount()](#getverticescount)
@@ -35,8 +39,6 @@
3539
npm install --save @datastructures-js/graph
3640
```
3741

38-
## API
39-
4042
### require
4143

4244
```js
@@ -49,15 +51,27 @@ const { Graph, DirectedGraph } = require('@datastructures-js/graph');
4951
import { Graph, DirectedGraph } from '@datastructures-js/graph';
5052
```
5153

52-
### new
54+
## API
55+
56+
### constructor
5357
Creates a new graph
5458

59+
##### JS
5560
```js
5661
const directedGraph = new DirectedGraph();
5762

5863
const graph = new Graph();
5964
```
6065

66+
##### TS
67+
```js
68+
// DirectedGraph<T extends number|string, U = undefined>
69+
const directedGraph = new DirectedGraph<number, { id: string, someProp: any }>();
70+
71+
// Graph<T extends number|string, U = undefined>
72+
const graph = new Graph<string, number>();
73+
```
74+
6175
### .addVertex(key, value)
6276
Adds a vertex to the graph.
6377

@@ -69,11 +83,11 @@ Adds a vertex to the graph.
6983
</tr>
7084
<tr>
7185
<td>
72-
key: number | string
86+
key: T (number | string)
7387
<br />
74-
value: any
88+
value: U
7589
</td>
76-
<td align="center">Graph | DirectedGraph</td>
90+
<td align="center">Graph&lt;T, U&gt; | DirectedGraph&lt;T, U&gt;</td>
7791
<td align="center">O(1)</td>
7892
</tr>
7993
</table>
@@ -105,7 +119,7 @@ Checks if the graph has a vertex by its key.
105119
</tr>
106120
<tr>
107121
<td>
108-
key: number | string
122+
key: T (number | string)
109123
</td>
110124
<td align="center">boolean</td>
111125
<td align="center">O(1)</td>
@@ -147,13 +161,13 @@ Adds a weighted edge between two existings vertices. Default weight is 1 if not
147161
</tr>
148162
<tr>
149163
<td>
150-
srcKey: number | string
164+
srcKey: T (number | string)
151165
<br />
152-
destKey: number | string
166+
destKey: T (number | string)
153167
<br />
154168
weight: number
155169
</td>
156-
<td align="center">Graph | DirectedGraph</td>
170+
<td align="center">Graph&lt;T, U&gt; | DirectedGraph&lt;T, U&gt;</td>
157171
<td align="center">O(1)</td>
158172
</tr>
159173
</table>
@@ -189,9 +203,9 @@ Checks if the graph has an edge between two existing vertices. In directed graph
189203
</tr>
190204
<tr>
191205
<td>
192-
srcKey: number | string
206+
srcKey: T (number | string)
193207
<br />
194-
destKey: number | string
208+
destKey: T (number | string)
195209
</td>
196210
<td align="center">boolean</td>
197211
<td align="center">O(1)</td>
@@ -236,9 +250,9 @@ Gets the edge's weight between two vertices. If there is no direct edge between
236250
</tr>
237251
<tr>
238252
<td>
239-
srcKey: number | string
253+
srcKey: T (number | string)
240254
<br />
241-
destKey: number | string
255+
destKey: T (number | string)
242256
</td>
243257
<td align="center">number</td>
244258
<td align="center">O(1)</td>
@@ -267,7 +281,7 @@ Removes a vertex with all its edges from the graph.
267281
</tr>
268282
<tr>
269283
<td>
270-
key: number | string
284+
key: T (number | string)
271285
</td>
272286
<td align="center">boolean</td>
273287
<td>
@@ -299,9 +313,9 @@ Removes an edge between two existing vertices
299313
</tr>
300314
<tr>
301315
<td>
302-
srcKey: number | string
316+
srcKey: T (number | string)
303317
<br />
304-
destKey: number | string
318+
destKey: T (number | string)
305319
</td>
306320
<td align="center">boolean</td>
307321
<td align="center">O(1)</td>
@@ -327,7 +341,7 @@ Removes all connected edges to a vertex and returns the number of removed edges.
327341
</tr>
328342
<tr>
329343
<td>
330-
key: number | string
344+
key: T (number | string)
331345
</td>
332346
<td align="center">number</td>
333347
<td>
@@ -367,9 +381,9 @@ Traverses the graph using the depth-first recursive search.
367381
</tr>
368382
<tr>
369383
<td>
370-
srcKey: number | string
384+
srcKey: T (number | string)
371385
<br />
372-
cb: function
386+
cb: (key: T, value: U) => void
373387
</td>
374388
<td>
375389
O(V) : V = number of vertices in the graph
@@ -405,9 +419,9 @@ Traverses the graph using the breadth-first search with a queue.
405419
</tr>
406420
<tr>
407421
<td>
408-
srcKey: number | string
422+
srcKey: T (number | string)
409423
<br />
410-
cb: function
424+
cb: (key: T, value: U) => void
411425
</td>
412426
<td>
413427
O(V) : V = number of vertices in the graph

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const Graph = require('./src/graph');
2-
const DirectedGraph = require('./src/directedGraph');
1+
const { Graph } = require('./src/graph');
2+
const { DirectedGraph } = require('./src/directedGraph');
33

4-
module.exports = { Graph, DirectedGraph };
4+
exports.Graph = Graph;
5+
exports.DirectedGraph = DirectedGraph;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/graph",
3-
"version": "5.0.1",
3+
"version": "5.1.0",
44
"description": "graph & directed graph implementation in javascript",
55
"main": "index.js",
66
"scripts": {

src/directedGraph.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class DirectedGraph<T extends number|string, U = undefined> {
2+
addVertex(key: T, value: U): DirectedGraph<T, U>;
3+
hasVertex(key: T): boolean;
4+
removeVertex(key: T): boolean;
5+
getVerticesCount(): number;
6+
addEdge(srcKey: T, destKey: T, weight?: number): DirectedGraph<T, U>;
7+
hasEdge(srcKey: T, destKey: T): boolean;
8+
getWeight(): number;
9+
removeEdge(srcKey: T, destKey: T): boolean;
10+
removeEdges(key: T): number;
11+
getEdgesCount(): number;
12+
traverseDfs(srcKey: T, cb: (key: T, value: U) => void): void;
13+
traverseBfs(srcKey: T, cb: (key: T, value: U) => void): void;
14+
clear(): void;
15+
}

src/directedGraph.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,18 @@ class DirectedGraph {
179179
* @param {number|string} srcKey - starting node
180180
* @param {function} cb
181181
*/
182-
traverseDfs(srcKey, cb, visited = new Set()) {
183-
if (!this.hasVertex(srcKey) || visited.has(srcKey)) return;
182+
traverseDfs(srcKey, cb) {
183+
const traverseDfsRecursive = (key, visited = new Set()) => {
184+
if (!this.hasVertex(key) || visited.has(key)) return;
184185

185-
cb(srcKey, this._vertices.get(srcKey));
186-
visited.add(srcKey);
186+
cb(key, this._vertices.get(key));
187+
visited.add(key);
187188

188-
this._edges.get(srcKey).forEach((weight, destKey) => {
189-
this.traverseDfs(destKey, cb, visited);
190-
});
189+
this._edges.get(key).forEach((weight, destKey) => {
190+
traverseDfsRecursive(destKey, visited);
191+
});
192+
};
193+
traverseDfsRecursive(srcKey);
191194
}
192195

193196
/**
@@ -225,4 +228,4 @@ class DirectedGraph {
225228
}
226229
}
227230

228-
module.exports = DirectedGraph;
231+
exports.DirectedGraph = DirectedGraph;

src/graph.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { DirectedGraph } from '../src/directedGraph';
2+
3+
export class Graph<T extends number|string, U = undefined> extends DirectedGraph<T, U> {
4+
addVertex(key: T, value: U): Graph<T, U>;
5+
addEdge(srcKey: T, destKey: T, weight?: number): Graph<T, U>;
6+
}

src/graph.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @license MIT
55
*/
66

7-
const DirectedGraph = require('./directedGraph');
7+
const { DirectedGraph } = require('./directedGraph');
88

99
/**
1010
* @class Graph
@@ -71,4 +71,4 @@ class Graph extends DirectedGraph {
7171
}
7272
}
7373

74-
module.exports = Graph;
74+
exports.Graph = Graph;

test/directedGraph.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { expect } = require('chai');
22
const sinon = require('sinon');
3-
const DirectedGraph = require('../src/directedGraph');
3+
const { DirectedGraph } = require('../src/directedGraph');
44

55
describe('DirectedGraph unit tests', () => {
66
const directedGraph = new DirectedGraph();

test/graph.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { expect } = require('chai');
22

3-
const Graph = require('../src/graph');
3+
const { Graph } = require('../src/graph');
44

55
describe('Graph unit tests', () => {
66
const graph = new Graph();

0 commit comments

Comments
 (0)