Skip to content

Commit a2589bc

Browse files
committed
Added addNode and addEdge feature in adjacency list implementation
1 parent 3df40fd commit a2589bc

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

src/graph/adj-list.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { BST } from '../binary-search-tree/binary-search-tree';
2+
3+
export class AdjacencyList {
4+
constructor(isDiGraph) {
5+
this.diGraph = isDiGraph;
6+
this.edges = new Map();
7+
}
8+
9+
/**
10+
* It adds node to the map and assign an empty list
11+
* If the node is already added to the map, then nothing happens
12+
* @param {any} node
13+
*/
14+
addNode(node) {
15+
if (!this.edges.get(node)) {
16+
this.edges.set(node, new BST);
17+
}
18+
}
19+
20+
/**
21+
* It removes the node from the map
22+
* @param {any} node
23+
*/
24+
removeNode(node) {
25+
this.edges.delete(node);
26+
}
27+
28+
addEdge(fromVertex, toVertex, weight) {
29+
if (!this.edges.has(fromVertex)) {
30+
this.addNode(fromVertex);
31+
}
32+
33+
if (!this.edges.has(toVertex)) {
34+
this.addNode(toVertex);
35+
}
36+
37+
this.edges.get(fromVertex).insert(toVertex, { weight });
38+
39+
if (!this.diGraph) {
40+
this.edges.get(toVertex).insert(fromVertex, { weight });
41+
}
42+
}
43+
}

src/graph/adj-list.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { AdjacencyList } from './adj-list';
2+
3+
describe('Adjacency List', () => {
4+
let adjList;
5+
beforeEach(() => {
6+
adjList = new AdjacencyList();
7+
});
8+
9+
it('should create adjacency list object', () => {
10+
expect(adjList).toBeDefined();
11+
expect(adjList.edges).toBeDefined();
12+
});
13+
14+
it('should have empty edges when adjacency list object is created', () => {
15+
expect(adjList.edges.size).toBe(0);
16+
});
17+
18+
it('should add node through addNode method', () => {
19+
expect(adjList.edges.size).toBe(0);
20+
21+
adjList.addNode('A');
22+
expect(adjList.edges.size).toBe(1);
23+
expect(adjList.edges.get('A').len).toBe(0);
24+
25+
adjList.addNode('B');
26+
expect(adjList.edges.size).toBe(2);
27+
expect(adjList.edges.get('B').len).toBe(0);
28+
});
29+
30+
it('should delete the node from the map', () => {
31+
expect(adjList.edges.size).toBe(0);
32+
33+
adjList.addNode('A');
34+
expect(adjList.edges.size).toBe(1);
35+
36+
adjList.removeNode('A');
37+
expect(adjList.edges.size).toBe(0);
38+
expect(adjList.edges.has('A')).toBeFalsy();
39+
});
40+
41+
describe('Insert Operation', () => {
42+
let unDirectedGraph;
43+
beforeEach(() => {
44+
unDirectedGraph = new AdjacencyList(false);
45+
46+
adjList.addNode('A');
47+
adjList.addNode('B');
48+
});
49+
50+
it('should add an edge of weight 20 between A and B', () => {
51+
adjList.addEdge('A', 'B', 200);
52+
const vertexA = adjList.edges.get('A');
53+
expect(vertexA.len).toBe(1);
54+
55+
const lookupB = vertexA.lookup('B');
56+
expect(lookupB.hasVal).toBeTruthy();
57+
expect(lookupB.currentNode.details.weight).toBe(200);
58+
});
59+
it('should add edge between B and C', () => {
60+
expect(adjList.edges.size).toBe(2);
61+
62+
adjList.addEdge('B', 'C');
63+
expect(adjList.edges.size).toBe(3);
64+
const vertexB = adjList.edges.get('B');
65+
expect(vertexB.len).toBe(1);
66+
67+
const lookupC = vertexB.lookup('C');
68+
expect(lookupC.hasVal).toBeTruthy();
69+
expect(lookupC.currentNode.details.weight).toBe(undefined);
70+
});
71+
it('should add edge A and B in undirected graph', () => {
72+
expect(unDirectedGraph.edges.size).toBe(0);
73+
74+
unDirectedGraph.addEdge('A', 'B');
75+
expect(unDirectedGraph.edges.size).toBe(2);
76+
77+
const vertexA = unDirectedGraph.edges.get('A');
78+
expect(vertexA.len).toBe(1);
79+
const lookupB = vertexA.lookup('B');
80+
expect(lookupB.hasVal).toBeTruthy();
81+
82+
const vertexB = unDirectedGraph.edges.get('B');
83+
expect(vertexB.len).toBe(1);
84+
const lookupA = vertexB.lookup('A');
85+
expect(lookupA.hasVal).toBeTruthy();
86+
});
87+
});
88+
});

src/graph/graph.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { AdjacencyList } from './adj-list';
2+
3+
export class Graph {
4+
constructor(directed) {
5+
this.diGraph = directed === 'directed';
6+
}
7+
/**
8+
* Create a presentation of type adjList and returns the created object
9+
* @param {string} presentationType
10+
* @returns {AdjacencyList} graph
11+
* @example
12+
* ```
13+
* const graph = new Graph;
14+
* const adjList = graph.createGraph('adjList');
15+
* ```
16+
*/
17+
createGraph(presentationType) {
18+
let graph;
19+
if (presentationType === 'adjList') {
20+
graph = new AdjacencyList(this.diGraph);
21+
} else {
22+
return new Error('Graph Type not allowed.');
23+
}
24+
25+
graph.type = presentationType;
26+
return graph;
27+
}
28+
}

src/graph/graph.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Graph } from './graph';
2+
3+
describe('Graph Factory', () => {
4+
it('should create an object of Graph Factory', () => {
5+
const graph = new Graph;
6+
expect(graph).toBeDefined();
7+
});
8+
9+
describe('Adjacency List', () => {
10+
let graph;
11+
beforeEach(() => {
12+
graph = new Graph('directed');
13+
});
14+
it('should create adjacency list object', () => {
15+
const adjList = graph.createGraph('adjList');
16+
expect(adjList).toBeDefined();
17+
});
18+
it('should have type "adjList"', () => {
19+
const adjList = graph.createGraph('adjList');
20+
expect(adjList.type).toBeDefined();
21+
expect(adjList.type).toBe('adjList');
22+
});
23+
it('should return error when graph type does not match', () => {
24+
const adjList = graph.createGraph('some-error');
25+
expect(adjList.message).toBe('Graph Type not allowed.');
26+
});
27+
28+
it('should create a directed graph', () => {
29+
const adjList = graph.createGraph('adjList');
30+
expect(adjList.diGraph).toBeTruthy();
31+
});
32+
it('should create a un-directed graph', () => {
33+
const graph = new Graph;
34+
const adjList = graph.createGraph('adjList');
35+
expect(adjList.diGraph).toBeFalsy();
36+
});
37+
});
38+
39+
});

0 commit comments

Comments
 (0)