/
index.test.js
286 lines (220 loc) · 6.67 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const Graph = require('.')
describe('test graph DS', () => {
it('should construct an empty graph', () => {
const graph = new Graph()
expect(graph).toBeDefined()
})
it('should construct a directed graph', () => {
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.getAdjVertices('a').map(e => e.name)).toEqual(['b'])
expect(graph.getAdjVertices('c').map(e => e.name)).toEqual(['a', 'b'])
})
it('should do bfs', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'b')
graph.addEdge('a', 'c')
graph.addEdge('c', 'b')
const output = []
graph.bfs('a', vertex => output.push(vertex.name))
const expectOutput = ['a', 'b', 'c']
expect(output).toEqual(expectOutput)
})
it('should do dfs', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'c')
graph.addEdge('c', 'b')
graph.addEdge('a', 'b')
const output = []
graph.dfs('a', vertex => output.push(vertex.name))
const expectOutput = ['a', 'c', 'b']
expect(output).toEqual(expectOutput)
})
it('should tell if v1 is adjacent to v2', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'c')
graph.addEdge('c', 'b')
graph.addEdge('a', 'b')
const output = graph.isAdjacent('a', 'b')
const expectOutput = true
expect(output).toEqual(expectOutput)
})
it('should remove the given edge', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'c')
graph.addEdge('c', 'b')
graph.addEdge('a', 'b')
graph.removeEdge('a', 'b')
const output = graph.isAdjacent('a', 'b')
const expectOutput = false
expect(output).toEqual(expectOutput)
})
it('should remove the given vertex', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'c')
graph.addEdge('c', 'b')
graph.addEdge('a', 'b')
graph.removeVertex('a')
const output = graph.isAdjacent('a', 'b') || graph.isAdjacent('a', 'c')
const expectOutput = false
expect(output).toEqual(expectOutput)
})
it('should able to add weights to edges', () => {
const graph = new Graph()
/*
a ---> b
| ^
| /
> c
*/
graph.addVertex('a')
graph.addVertex('b')
graph.addVertex('c')
graph.addEdge('a', 'c', 5)
graph.addEdge('c', 'b', 7)
graph.addEdge('a', 'b', 9)
graph.setEdgeValue('a', 'b', 10)
expect(graph.getEdgeValue('a', 'c')).toBe(5)
expect(graph.getEdgeValue('a', 'b')).toBe(10)
})
it('should do on adding existing vertex', () => {
const graph = new Graph()
graph.addVertex('a')
graph.addVertex('a')
})
it('should throw error on adding invalid edge', () => {
const graph = new Graph()
graph.addVertex('a')
expect(() => graph.addEdge('a', 'b')).toThrow()
expect(() => graph.addEdge('b', 'c')).toThrow()
})
it('should throw error on accessing neighbors of invalid vertex', () => {
const graph = new Graph()
expect(() => graph.getAdjVertices('a')).toThrow()
})
it('should throw error on setting value of invalid vertex', () => {
const graph = new Graph()
expect(() => graph.setVertexValue('a', 1)).toThrow()
})
it('should throw error on accessing value of invalid vertex', () => {
const graph = new Graph()
expect(() => graph.getVertexValue('a')).toThrow()
})
it('should throw error on accessing invalid vertex', () => {
const graph = new Graph()
expect(() => graph.getVertex('a')).toThrow()
})
it('bfs should throw error on invalid vertex start node', () => {
const graph = new Graph()
expect(() => graph.bfs('a')).toThrow()
})
it('dfs should throw error on invalid vertex start node', () => {
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'])
})
})