@@ -178,6 +178,97 @@ describe('Graph', () => {
178
178
});
179
179
});
180
180
181
+ describe('Graph Search: UNDIRECTED', () => {
182
+ let first;
183
+
184
+ beforeEach(() => {
185
+ graph = new Graph(Graph.UNDIRECTED);
186
+ [first] = graph.addEdge(1, 2);
187
+ graph.addEdge(1, 3);
188
+ graph.addEdge(1, 4);
189
+
190
+ graph.addEdge(5, 2);
191
+ graph.addEdge(6, 3);
192
+ graph.addEdge(7, 3);
193
+ graph.addEdge(8, 4);
194
+ graph.addEdge(9, 5);
195
+ graph.addEdge(10, 6);
196
+ });
197
+
198
+ describe('#dfs', () => {
199
+
200
+ it('should visit nodes using depth-first search', () => {
201
+ const visitedOrder = Array.from(graph.dfs(first)).map(getValue);
202
+ expect(visitedOrder).toEqual([1, 4, 8, 3, 7, 6, 10, 2, 5, 9]);
203
+ });
204
+
205
+ it('should use iterator', () => {
206
+ const dfs = graph.dfs(first);
207
+
208
+ expect(dfs.next().value.value).toBe(1);
209
+ expect(dfs.next().value.value).toBe(4);
210
+ expect(dfs.next().value.value).toBe(8);
211
+ expect(dfs.next().value.value).toBe(3);
212
+ expect(dfs.next().value.value).toBe(7);
213
+ expect(dfs.next().value.value).toBe(6);
214
+ expect(dfs.next().value.value).toBe(10);
215
+ expect(dfs.next().value.value).toBe(2);
216
+ expect(dfs.next().value.value).toBe(5);
217
+ expect(dfs.next().value.value).toBe(9);
218
+
219
+ expect(dfs.next().value).toBe(undefined);
220
+ expect(dfs.next().done).toBe(true);
221
+ });
222
+ });
223
+
224
+ describe('#bfs', () => {
225
+
226
+ it('should visit nodes using breadth-first search', () => {
227
+ const visitedOrder = Array.from(graph.bfs(first)).map(getValue);
228
+ expect(visitedOrder).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
229
+ });
230
+ });
231
+ });
232
+
233
+ describe('with (people) edges on undigraph', () => {
234
+ let you, mary, barbara;
235
+
236
+ beforeEach(() => {
237
+ graph = new Graph(Graph.UNDIRECTED);
238
+ [you] = graph.addEdge('You', 'Mary');
239
+ [mary, barbara] = graph.addEdge('Mary', 'Barbara');
240
+ });
241
+
242
+ describe('#areConnected', () => {
243
+
244
+ it('should return true if two nodes are connected', () => {
245
+ expect(graph.areConnected('You', 'Barbara')).toBe(true);
246
+ });
247
+
248
+ it('should return true if two nodes are connected', () => {
249
+ expect(graph.areConnected('You', 'You')).toBe(true);
250
+ });
251
+
252
+ it('should return true if two nodes are connected', () => {
253
+ expect(graph.areConnected('You', 'John')).toBe(false);
254
+ });
255
+ });
256
+
257
+ describe('#findPath', () => {
258
+ it('should get connecting path', () => {
259
+ expect(graph.findPath('You', 'Barbara').map(getValue)).toEqual(['You', 'Mary', 'Barbara']);
260
+ });
261
+
262
+ it('should get adjacent connecting path', () => {
263
+ expect(graph.findPath('Mary', 'Barbara').map(getValue)).toEqual(['Mary', 'You', 'Barbara']);
264
+ });
265
+
266
+ it('should return empty if there is no connection', () => {
267
+ expect(graph.findPath('You', 'Obama').map(getValue)).toEqual([]);
268
+ });
269
+ });
270
+ });
271
+
181
272
182
273
function getValue(node) {
183
274
return node.value;
0 commit comments