1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.dfs = void 0;
|
4 | const each = require("lodash.foreach");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function dfs(g, vs, order) {
|
14 | if (!Array.isArray(vs)) {
|
15 | vs = [vs];
|
16 | }
|
17 | const navigation = (g.isDirected() ? g.successors : g.neighbors).bind(g);
|
18 | const acc = [];
|
19 | const visited = {};
|
20 | each(vs, (v) => {
|
21 | if (!g.hasNode(v)) {
|
22 | throw new Error('Graph does not have node: ' + v);
|
23 | }
|
24 | doDfs(g, v, order === 'post', visited, navigation, acc);
|
25 | });
|
26 | return acc;
|
27 | }
|
28 | exports.dfs = dfs;
|
29 | function doDfs(g, v, postorder, visited, navigation, acc) {
|
30 | if (!(v in visited)) {
|
31 | visited[v] = true;
|
32 | if (!postorder) {
|
33 | acc.push(v);
|
34 | }
|
35 | each(navigation(v), function (w) {
|
36 | doDfs(g, w, postorder, visited, navigation, acc);
|
37 | });
|
38 | if (postorder) {
|
39 | acc.push(v);
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
\ | No newline at end of file |