1234567891011121314151617181920212223242526272829303132333435363738 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const dfs_1 = require("./dfs");
- const assert_1 = require("./assert");
- const _ = require("lodash");
- const graph = new Map([
- [0, new Map([['a', 1], ['b', 2]])],
- [1, new Map([['c', 0]])],
- [2, new Map([['d', 3]])],
- ]);
- function getNeighbors(node) {
- const outgoing = graph.get(node);
- if (outgoing === undefined) {
- return [];
- }
- return [...outgoing.entries()];
- }
- describe("DFS", () => {
- it("Find node one hop", () => {
- const path = (0, dfs_1.findDFS)(0, 1, getNeighbors);
- (0, assert_1.assert)(_.isEqual(path, ['a']), "Expected node to be found.");
- });
- it("Find node two hops", () => {
- const path = (0, dfs_1.findDFS)(0, 3, getNeighbors);
- // There is more than one path from 0 to 3 (in fact, there are infinitely many, if we allow loops),
- // but we don't allow visiting the same element more than once, so only one path is possible:
- (0, assert_1.assert)(_.isEqual(path, ['b', 'd']), "Expected node to be found.");
- });
- it("Find node zero hops", () => {
- const path = (0, dfs_1.findDFS)(0, 0, getNeighbors);
- (0, assert_1.assert)(_.isEqual(path, []), "Expected node to be found.");
- });
- it("Find unreachable node", () => {
- const path = (0, dfs_1.findDFS)(3, 0, getNeighbors);
- (0, assert_1.assert)(path === undefined, "Expected node to be unreachable.");
- });
- });
- //# sourceMappingURL=dfs.test.js.map
|