Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [v5.1.0] - 2021-06-20
### Added
- typescript.

## [v5.0.1] - 2021-04-15
### Fixed
- README
Expand Down
66 changes: 40 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
[![npm](https://img.shields.io/npm/v/@datastructures-js/graph.svg)](https://www.npmjs.com/package/@datastructures-js/graph)
[![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)

Graph & Directed Graph implementation in javascript.

<img src="https://user-images.githubusercontent.com/6517308/121813242-859a9700-cc6b-11eb-99c0-49e5bb63005b.jpg">

<table><tr><td>
<img alt="graph" src="https://user-images.githubusercontent.com/6517308/71645678-802cd500-2ca1-11ea-96fb-11a71fd95191.jpg">
</td></tr></table>

# Table of Contents
# Contents
* [Install](#install)
* [require](#require)
* [import](#import)
* [API](#api)
* [require](#require)
* [import](#import)
* [new](#new)
* [constructor](#constructor)
* [.addVertex(key, value)](#addvertexkey-value)
* [.hasVertex(key)](#hasvvertex-key)
* [.getVerticesCount()](#getverticescount)
Expand All @@ -35,8 +39,6 @@
npm install --save @datastructures-js/graph
```

## API

### require

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

### new
## API

### constructor
Creates a new graph

##### JS
```js
const directedGraph = new DirectedGraph();

const graph = new Graph();
```

##### TS
```js
// DirectedGraph<T extends number|string, U = undefined>
const directedGraph = new DirectedGraph<number, { id: string, someProp: any }>();

// Graph<T extends number|string, U = undefined>
const graph = new Graph<string, number>();
```

### .addVertex(key, value)
Adds a vertex to the graph.

Expand All @@ -69,11 +83,11 @@ Adds a vertex to the graph.
</tr>
<tr>
<td>
key: number | string
key: T (number | string)
<br />
value: any
value: U
</td>
<td align="center">Graph | DirectedGraph</td>
<td align="center">Graph&lt;T, U&gt; | DirectedGraph&lt;T, U&gt;</td>
<td align="center">O(1)</td>
</tr>
</table>
Expand Down Expand Up @@ -105,7 +119,7 @@ Checks if the graph has a vertex by its key.
</tr>
<tr>
<td>
key: number | string
key: T (number | string)
</td>
<td align="center">boolean</td>
<td align="center">O(1)</td>
Expand Down Expand Up @@ -147,13 +161,13 @@ Adds a weighted edge between two existings vertices. Default weight is 1 if not
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
destKey: number | string
destKey: T (number | string)
<br />
weight: number
</td>
<td align="center">Graph | DirectedGraph</td>
<td align="center">Graph&lt;T, U&gt; | DirectedGraph&lt;T, U&gt;</td>
<td align="center">O(1)</td>
</tr>
</table>
Expand Down Expand Up @@ -189,9 +203,9 @@ Checks if the graph has an edge between two existing vertices. In directed graph
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
destKey: number | string
destKey: T (number | string)
</td>
<td align="center">boolean</td>
<td align="center">O(1)</td>
Expand Down Expand Up @@ -236,9 +250,9 @@ Gets the edge's weight between two vertices. If there is no direct edge between
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
destKey: number | string
destKey: T (number | string)
</td>
<td align="center">number</td>
<td align="center">O(1)</td>
Expand Down Expand Up @@ -267,7 +281,7 @@ Removes a vertex with all its edges from the graph.
</tr>
<tr>
<td>
key: number | string
key: T (number | string)
</td>
<td align="center">boolean</td>
<td>
Expand Down Expand Up @@ -299,9 +313,9 @@ Removes an edge between two existing vertices
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
destKey: number | string
destKey: T (number | string)
</td>
<td align="center">boolean</td>
<td align="center">O(1)</td>
Expand All @@ -327,7 +341,7 @@ Removes all connected edges to a vertex and returns the number of removed edges.
</tr>
<tr>
<td>
key: number | string
key: T (number | string)
</td>
<td align="center">number</td>
<td>
Expand Down Expand Up @@ -367,9 +381,9 @@ Traverses the graph using the depth-first recursive search.
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
cb: function
cb: (key: T, value: U) => void
</td>
<td>
O(V) : V = number of vertices in the graph
Expand Down Expand Up @@ -405,9 +419,9 @@ Traverses the graph using the breadth-first search with a queue.
</tr>
<tr>
<td>
srcKey: number | string
srcKey: T (number | string)
<br />
cb: function
cb: (key: T, value: U) => void
</td>
<td>
O(V) : V = number of vertices in the graph
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Graph = require('./src/graph');
const DirectedGraph = require('./src/directedGraph');
const { Graph } = require('./src/graph');
const { DirectedGraph } = require('./src/directedGraph');

module.exports = { Graph, DirectedGraph };
exports.Graph = Graph;
exports.DirectedGraph = DirectedGraph;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/graph",
"version": "5.0.1",
"version": "5.1.0",
"description": "graph & directed graph implementation in javascript",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/directedGraph.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class DirectedGraph<T extends number|string, U = undefined> {
addVertex(key: T, value: U): DirectedGraph<T, U>;
hasVertex(key: T): boolean;
removeVertex(key: T): boolean;
getVerticesCount(): number;
addEdge(srcKey: T, destKey: T, weight?: number): DirectedGraph<T, U>;
hasEdge(srcKey: T, destKey: T): boolean;
getWeight(): number;
removeEdge(srcKey: T, destKey: T): boolean;
removeEdges(key: T): number;
getEdgesCount(): number;
traverseDfs(srcKey: T, cb: (key: T, value: U) => void): void;
traverseBfs(srcKey: T, cb: (key: T, value: U) => void): void;
clear(): void;
}
19 changes: 11 additions & 8 deletions src/directedGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,18 @@ class DirectedGraph {
* @param {number|string} srcKey - starting node
* @param {function} cb
*/
traverseDfs(srcKey, cb, visited = new Set()) {
if (!this.hasVertex(srcKey) || visited.has(srcKey)) return;
traverseDfs(srcKey, cb) {
const traverseDfsRecursive = (key, visited = new Set()) => {
if (!this.hasVertex(key) || visited.has(key)) return;

cb(srcKey, this._vertices.get(srcKey));
visited.add(srcKey);
cb(key, this._vertices.get(key));
visited.add(key);

this._edges.get(srcKey).forEach((weight, destKey) => {
this.traverseDfs(destKey, cb, visited);
});
this._edges.get(key).forEach((weight, destKey) => {
traverseDfsRecursive(destKey, visited);
});
};
traverseDfsRecursive(srcKey);
}

/**
Expand Down Expand Up @@ -225,4 +228,4 @@ class DirectedGraph {
}
}

module.exports = DirectedGraph;
exports.DirectedGraph = DirectedGraph;
6 changes: 6 additions & 0 deletions src/graph.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DirectedGraph } from '../src/directedGraph';

export class Graph<T extends number|string, U = undefined> extends DirectedGraph<T, U> {
addVertex(key: T, value: U): Graph<T, U>;
addEdge(srcKey: T, destKey: T, weight?: number): Graph<T, U>;
}
4 changes: 2 additions & 2 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license MIT
*/

const DirectedGraph = require('./directedGraph');
const { DirectedGraph } = require('./directedGraph');

/**
* @class Graph
Expand Down Expand Up @@ -71,4 +71,4 @@ class Graph extends DirectedGraph {
}
}

module.exports = Graph;
exports.Graph = Graph;
2 changes: 1 addition & 1 deletion test/directedGraph.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai');
const sinon = require('sinon');
const DirectedGraph = require('../src/directedGraph');
const { DirectedGraph } = require('../src/directedGraph');

describe('DirectedGraph unit tests', () => {
const directedGraph = new DirectedGraph();
Expand Down
2 changes: 1 addition & 1 deletion test/graph.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai');

const Graph = require('../src/graph');
const { Graph } = require('../src/graph');

describe('Graph unit tests', () => {
const graph = new Graph();
Expand Down