Skip to content

Commit 04f146e

Browse files
committed
changed edged to vertices
1 parent f9ef9b4 commit 04f146e

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

src/graph/adj-list.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BST } from '../binary-search-tree/binary-search-tree';
33
export class AdjacencyList {
44
constructor(isDiGraph) {
55
this.diGraph = isDiGraph;
6-
this.edges = new Map();
6+
this.vertices = new Map();
77
}
88

99
/**
@@ -12,8 +12,8 @@ export class AdjacencyList {
1212
* @param {string|number} node
1313
*/
1414
addNode(node) {
15-
if (!this.edges.get(node)) {
16-
this.edges.set(node, new BST);
15+
if (!this.vertices.get(node)) {
16+
this.vertices.set(node, new BST);
1717
}
1818
}
1919

@@ -22,13 +22,13 @@ export class AdjacencyList {
2222
* @param {string|number} node
2323
*/
2424
removeNode(node) {
25-
// Remove all the edges formed by this node
26-
this.edges.forEach((val, key) => {
25+
// Remove all the vertices formed by this node
26+
this.vertices.forEach((val, key) => {
2727
this.removeEdge(key, node);
2828
});
2929

3030
// finally remove the node
31-
this.edges.delete(node);
31+
this.vertices.delete(node);
3232
}
3333

3434
/**
@@ -41,18 +41,18 @@ export class AdjacencyList {
4141
* @param {number} weight
4242
*/
4343
addEdge(fromVertex, toVertex, weight) {
44-
if (!this.edges.has(fromVertex)) {
44+
if (!this.vertices.has(fromVertex)) {
4545
this.addNode(fromVertex);
4646
}
4747

48-
if (!this.edges.has(toVertex)) {
48+
if (!this.vertices.has(toVertex)) {
4949
this.addNode(toVertex);
5050
}
5151

52-
this.edges.get(fromVertex).insert(toVertex, { weight });
52+
this.vertices.get(fromVertex).insert(toVertex, { weight });
5353

5454
if (!this.diGraph) {
55-
this.edges.get(toVertex).insert(fromVertex, { weight });
55+
this.vertices.get(toVertex).insert(fromVertex, { weight });
5656
}
5757
}
5858

@@ -62,8 +62,8 @@ export class AdjacencyList {
6262
* @param {string|number} toVertex
6363
*/
6464
removeEdge(fromVertex, toVertex) {
65-
if (this.edges.has(fromVertex)) {
66-
const deleteEdge = this.edges.get(fromVertex).delete(toVertex);
65+
if (this.vertices.has(fromVertex)) {
66+
const deleteEdge = this.vertices.get(fromVertex).delete(toVertex);
6767
if (deleteEdge && deleteEdge.constructor === Error) {
6868
return new Error(`No edge present between ${fromVertex} and ${toVertex}`);
6969
}
@@ -91,8 +91,8 @@ export class AdjacencyList {
9191
*/
9292
getEdgeWeight(fromVertex, toVertex) {
9393
let weight;
94-
if (this.edges.has(fromVertex)) {
95-
const lookup = this.edges.get(fromVertex).lookup(toVertex);
94+
if (this.vertices.has(fromVertex)) {
95+
const lookup = this.vertices.get(fromVertex).lookup(toVertex);
9696
weight = lookup.hasVal ? lookup.currentNode.details.weight : void 0;
9797
}
9898
return weight ? weight : new Error(`Edge not found between ${fromVertex} and ${toVertex}`);

src/graph/adj-list.spec.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@ describe('Adjacency List', () => {
88

99
it('should create adjacency list object', () => {
1010
expect(adjList).toBeDefined();
11-
expect(adjList.edges).toBeDefined();
11+
expect(adjList.vertices).toBeDefined();
1212
});
1313

14-
it('should have empty edges when adjacency list object is created', () => {
15-
expect(adjList.edges.size).toBe(0);
14+
it('should have empty vertices when adjacency list object is created', () => {
15+
expect(adjList.vertices.size).toBe(0);
1616
});
1717

1818
it('should add node through addNode method', () => {
19-
expect(adjList.edges.size).toBe(0);
19+
expect(adjList.vertices.size).toBe(0);
2020

2121
adjList.addNode('A');
22-
expect(adjList.edges.size).toBe(1);
23-
expect(adjList.edges.get('A').len).toBe(0);
22+
expect(adjList.vertices.size).toBe(1);
23+
expect(adjList.vertices.get('A').len).toBe(0);
2424

2525
adjList.addNode('B');
26-
expect(adjList.edges.size).toBe(2);
27-
expect(adjList.edges.get('B').len).toBe(0);
26+
expect(adjList.vertices.size).toBe(2);
27+
expect(adjList.vertices.get('B').len).toBe(0);
2828
});
2929

3030
it('should delete the node from the map', () => {
31-
expect(adjList.edges.size).toBe(0);
31+
expect(adjList.vertices.size).toBe(0);
3232

3333
adjList.addNode('A');
34-
expect(adjList.edges.size).toBe(1);
34+
expect(adjList.vertices.size).toBe(1);
3535

3636
adjList.removeNode('A');
37-
expect(adjList.edges.size).toBe(0);
38-
expect(adjList.edges.has('A')).toBeFalsy();
37+
expect(adjList.vertices.size).toBe(0);
38+
expect(adjList.vertices.has('A')).toBeFalsy();
3939
});
4040

4141
describe('Insert Operation', () => {
@@ -49,37 +49,37 @@ describe('Adjacency List', () => {
4949

5050
it('should add an edge of weight 20 between A and B', () => {
5151
adjList.addEdge('A', 'B', 200);
52-
const vertexA = adjList.edges.get('A');
52+
const vertexA = adjList.vertices.get('A');
5353
expect(vertexA.len).toBe(1);
5454

5555
const lookupB = vertexA.lookup('B');
5656
expect(lookupB.hasVal).toBeTruthy();
5757
expect(lookupB.currentNode.details.weight).toBe(200);
5858
});
5959
it('should add edge between B and C', () => {
60-
expect(adjList.edges.size).toBe(2);
60+
expect(adjList.vertices.size).toBe(2);
6161

6262
adjList.addEdge('B', 'C');
63-
expect(adjList.edges.size).toBe(3);
64-
const vertexB = adjList.edges.get('B');
63+
expect(adjList.vertices.size).toBe(3);
64+
const vertexB = adjList.vertices.get('B');
6565
expect(vertexB.len).toBe(1);
6666

6767
const lookupC = vertexB.lookup('C');
6868
expect(lookupC.hasVal).toBeTruthy();
6969
expect(lookupC.currentNode.details.weight).toBe(undefined);
7070
});
7171
it('should add edge A and B in undirected graph', () => {
72-
expect(unDirectedGraph.edges.size).toBe(0);
72+
expect(unDirectedGraph.vertices.size).toBe(0);
7373

7474
unDirectedGraph.addEdge('A', 'B');
75-
expect(unDirectedGraph.edges.size).toBe(2);
75+
expect(unDirectedGraph.vertices.size).toBe(2);
7676

77-
const vertexA = unDirectedGraph.edges.get('A');
77+
const vertexA = unDirectedGraph.vertices.get('A');
7878
expect(vertexA.len).toBe(1);
7979
const lookupB = vertexA.lookup('B');
8080
expect(lookupB.hasVal).toBeTruthy();
8181

82-
const vertexB = unDirectedGraph.edges.get('B');
82+
const vertexB = unDirectedGraph.vertices.get('B');
8383
expect(vertexB.len).toBe(1);
8484
const lookupA = vertexB.lookup('A');
8585
expect(lookupA.hasVal).toBeTruthy();
@@ -93,8 +93,8 @@ describe('Adjacency List', () => {
9393
});
9494

9595
it('should delete edge A -> C', () => {
96-
expect(adjList.edges.size).toBe(3);
97-
const vertexA = adjList.edges.get('A');
96+
expect(adjList.vertices.size).toBe(3);
97+
const vertexA = adjList.vertices.get('A');
9898
expect(vertexA.len).toBe(2);
9999
expect(vertexA.lookup('C').hasVal).toBeTruthy();
100100
adjList.removeEdge('A', 'C');
@@ -114,10 +114,10 @@ describe('Adjacency List', () => {
114114
});
115115

116116
it('should delete edge A -> C and edge C -> A', () => {
117-
expect(undirectedList.edges.size).toBe(3);
117+
expect(undirectedList.vertices.size).toBe(3);
118118
const msg = undirectedList.removeEdge('A', 'C');
119-
expect(undirectedList.edges.get('A').lookup('C').hasVal).toBeFalsy();
120-
expect(undirectedList.edges.get('C').lookup('A').hasVal).toBeFalsy();
119+
expect(undirectedList.vertices.get('A').lookup('C').hasVal).toBeFalsy();
120+
expect(undirectedList.vertices.get('C').lookup('A').hasVal).toBeFalsy();
121121
expect(msg).toBe(void 0);
122122
});
123123
});
@@ -130,14 +130,14 @@ describe('Adjacency List', () => {
130130
});
131131

132132
it('should delete vertex C and finally edge A -> C', () => {
133-
expect(adjList.edges.size).toBe(3);
134-
expect(adjList.edges.get('A').len).toBe(2);
135-
expect(adjList.edges.get('A').lookup('C').hasVal).toBeTruthy();
133+
expect(adjList.vertices.size).toBe(3);
134+
expect(adjList.vertices.get('A').len).toBe(2);
135+
expect(adjList.vertices.get('A').lookup('C').hasVal).toBeTruthy();
136136

137137
adjList.removeNode('C');
138-
expect(adjList.edges.size).toBe(2);
139-
expect(adjList.edges.get('A').len).toBe(1);
140-
expect(adjList.edges.get('A').lookup('C').hasVal).toBeFalsy();
138+
expect(adjList.vertices.size).toBe(2);
139+
expect(adjList.vertices.get('A').len).toBe(1);
140+
expect(adjList.vertices.get('A').lookup('C').hasVal).toBeFalsy();
141141
});
142142

143143
describe('For undirectional Graph', () => {
@@ -148,19 +148,19 @@ describe('Adjacency List', () => {
148148
undirectedList.addEdge('A', 'C');
149149
});
150150

151-
it('should delete node C and also edges A -> C and C -> A', () => {
152-
expect(undirectedList.edges.size).toBe(3);
153-
expect(undirectedList.edges.get('A').len).toBe(2);
154-
expect(undirectedList.edges.get('A').lookup('C').hasVal).toBeTruthy();
155-
expect(undirectedList.edges.get('C').len).toBe(1);
156-
expect(undirectedList.edges.get('C').lookup('A').hasVal).toBeTruthy();
151+
it('should delete node C and also vertices A -> C and C -> A', () => {
152+
expect(undirectedList.vertices.size).toBe(3);
153+
expect(undirectedList.vertices.get('A').len).toBe(2);
154+
expect(undirectedList.vertices.get('A').lookup('C').hasVal).toBeTruthy();
155+
expect(undirectedList.vertices.get('C').len).toBe(1);
156+
expect(undirectedList.vertices.get('C').lookup('A').hasVal).toBeTruthy();
157157

158158
undirectedList.removeNode('C');
159159

160-
expect(undirectedList.edges.size).toBe(2);
161-
expect(undirectedList.edges.get('A').len).toBe(1);
162-
expect(undirectedList.edges.get('A').lookup('C').hasVal).toBeFalsy();
163-
expect(undirectedList.edges.has('C')).toBeFalsy();
160+
expect(undirectedList.vertices.size).toBe(2);
161+
expect(undirectedList.vertices.get('A').len).toBe(1);
162+
expect(undirectedList.vertices.get('A').lookup('C').hasVal).toBeFalsy();
163+
expect(undirectedList.vertices.has('C')).toBeFalsy();
164164
});
165165
});
166166
});

0 commit comments

Comments
 (0)