Skip to content

Commit

Permalink
Merge branch 'master' into lru-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshyadav committed Dec 3, 2020
2 parents 16d17b2 + c11db57 commit 0132d92
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 43 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,7 @@ Light weight javascript data structures library
- Graph
- Disjoint-set
- LRU Cache (New!)
- HashSet

## Installation and Usage

Expand Down Expand Up @@ -143,6 +144,22 @@ lru.get(4) // 4

```

### Hash-set

```js
const { HashSet } = require("data-structures-again");

const set = new HashSet()
set.add(1)
set.add(2)

set.has(1) // true
set.has(2) // true
set.has(3)) // false

```


## License

MIT.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -5,6 +5,8 @@ const Heap = require('./src/heap')
const Graph = require('./src/graph')
const DisjointSet = require('./src/disjoint-set')
const LRUCache = require('./src/lru-cache')
const HashSet = require('./src/hash-set')


module.exports = {
BST,
Expand All @@ -13,5 +15,6 @@ module.exports = {
Heap,
Graph,
DisjointSet,
LRUCache
LRUCache,
HashSet
}
14 changes: 14 additions & 0 deletions index.test.js
Expand Up @@ -72,6 +72,7 @@ test('disjoint-set example', () => {
expect(ds.isConnected('a', 'd')).toBeTruthy()
})


test('LRU Cache example', () => {
const { LRUCache } = require('./index')

Expand All @@ -87,3 +88,16 @@ test('LRU Cache example', () => {
lru.get(3) // 3
lru.get(4) // 4
})

test('hash-set example', () => {
const { HashSet } = require('./index')

const set = new HashSet()
set.add(1)
set.add(2)

expect(set.has(1)).toBeTruthy() // true
expect(set.has(2)).toBeTruthy()
expect(set.has(3)).toBeFalsy()

})
57 changes: 24 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
@@ -1,14 +1,14 @@
{
"name": "data-structures-again",
"version": "2.1.2",
"version": "2.1.10",
"description": "A Javascript library of simple data structures",
"homepage": "https://github.com/divyanshyadav/data-structures-again",
"main": "index.js",
"scripts": {
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"test": "jest",
"test:watch": "jest --watchAll",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:coveralls": "jest --coverage && cat coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls.js",
"build": "webpack",
Expand Down
3 changes: 2 additions & 1 deletion src/disjoint-set/index.js
Expand Up @@ -32,7 +32,8 @@ class DisjointSet {

if (this.rank[parentOfB] > this.rank[parentOfA]) {
this.container[parentOfA] = parentOfB
this.rank[parentOfB] += 1
} else if (this.rank[parentOfA] > this.rank[parentOfB]) {
this.container[parentOfB] = parentOfA
} else {
this.container[parentOfB] = parentOfA
this.rank[parentOfA] += 1
Expand Down
37 changes: 33 additions & 4 deletions src/graph/index.js
Expand Up @@ -25,7 +25,7 @@ class Vertex {
}

getAdjVertices () {
return this.adjVertices.toArray().map(n => n.name)
return this.adjVertices.toArray()
}

hasAdjVertex (name) {
Expand Down Expand Up @@ -70,8 +70,13 @@ class Graph {
.addNeighbor(this.getVertex(v2).name, weight)
}

addUndirectedEdge (v1, v2, weight) {
this.addEdge(v1, v2, weight)
this.addEdge(v2, v1, weight)
}

getAdjVertices (vertex) {
return this.getVertex(vertex).adjVertices
return this.getVertex(vertex).getAdjVertices()
}

isAdjacent (v1, v2) {
Expand Down Expand Up @@ -132,7 +137,7 @@ class Graph {
const current = queue.dequeue()

current.getAdjVertices().forEach(v => {
const neighbor = this.getVertex(v)
const neighbor = this.getVertex(v.name)

if (neighbor.color === COLORS.white) {
neighbor.color = COLORS.grey
Expand All @@ -157,7 +162,7 @@ class Graph {
fn(vertex)

vertex.getAdjVertices().forEach(v => {
const neighbor = this.getVertex(v)
const neighbor = this.getVertex(v.name)
if (neighbor.color === COLORS.white) {
helper(neighbor)
}
Expand All @@ -168,6 +173,30 @@ class Graph {

helper(startVertex)
}
/**
* @returns {[[String, String, Number]]}
*/
getEdges () {
const edges = []
for (const [vertex, value] of this.adjList) {
const neighbors = value.getAdjVertices()
neighbors.forEach(n => {
edges.push([vertex, n.name, this.getEdgeValue(vertex, n.name)])
})
};

return edges
}

getVertices () {
const vertices = []
// eslint-disable-next-line no-unused-vars
for (const [_, value] of this.adjList) {
vertices.push(value)
}

return vertices
}
}

module.exports = Graph
67 changes: 65 additions & 2 deletions src/graph/index.test.js
Expand Up @@ -24,8 +24,8 @@ describe('test graph DS', () => {
graph.addEdge('c', 'a')
graph.addEdge('c', 'b')

expect(graph.getAdjVertices('a').toArray().map(e => e.name)).toEqual(['b'])
expect(graph.getAdjVertices('c').toArray().map(e => e.name)).toEqual(['a', 'b'])
expect(graph.getAdjVertices('a').map(e => e.name)).toEqual(['b'])
expect(graph.getAdjVertices('c').map(e => e.name)).toEqual(['a', 'b'])
})

it('should do bfs', () => {
Expand Down Expand Up @@ -220,4 +220,67 @@ describe('test graph DS', () => {
const graph = new Graph()
expect(() => graph.dfs('a')).toThrow()
})

it('should return all edges', () => {
const graph = new Graph()

/*
a---> b
^ /
| /
c
*/

graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')

graph.addEdge('a', 'b')
graph.addEdge('c', 'a')
graph.addEdge('c', 'b')

expect(graph.getEdges()).toEqual(
[['a', 'b', 0], ['c', 'a', 0], ['c', 'b', 0]]
)
})

it('should return all vertices', () => {
const graph = new Graph()

/*
a---> b
^ /
| /
c
*/

graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')

expect(graph.getVertices().map(v => v.name)).toEqual(['a', 'b', 'c'])
})

it('should construct a undirected graph', () => {
const graph = new Graph()

/*
a--- b
| /
| /
c /
*/

graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')

graph.addUndirectedEdge('a', 'b')
graph.addUndirectedEdge('c', 'a')
graph.addUndirectedEdge('c', 'b')

expect(graph.getAdjVertices('a').map(e => e.name)).toEqual(['b', 'c'])
expect(graph.getAdjVertices('b').map(e => e.name)).toEqual(['a', 'c'])
expect(graph.getAdjVertices('c').map(e => e.name)).toEqual(['a', 'b'])
})
})

0 comments on commit 0132d92

Please sign in to comment.